<s> """<STR_LIT>""" <EOL> import string <EOL> import keyword <EOL> from idlelib import PyParse <EOL> class HyperParser : <EOL> def __init__ ( self , editwin , index ) : <EOL> """<STR_LIT>""" <EOL> self . editwin = editwin <EOL> self . text = text = editwin . text <EOL> parser = PyParse . Parser ( editwin . indentwidth , editwin . tabwidth ) <EOL> def index2line ( index ) : <EOL> return int ( float ( index ) ) <EOL> lno = index2line ( text . index ( index ) ) <EOL> if not editwin . context_use_ps1 : <EOL> for context in editwin . num_context_lines : <EOL> startat = max ( lno - context , <NUM_LIT:1> ) <EOL> startatindex = repr ( startat ) + "<STR_LIT>" <EOL> stopatindex = "<STR_LIT>" % lno <EOL> parser . set_str ( text . get ( startatindex , stopatindex ) + '<STR_LIT>' ) <EOL> bod = parser . find_good_parse_start ( <EOL> editwin . _build_char_in_string_func ( startatindex ) ) <EOL> if bod is not None or startat == <NUM_LIT:1> : <EOL> break <EOL> parser . set_lo ( bod or <NUM_LIT:0> ) <EOL> else : <EOL> r = text . tag_prevrange ( "<STR_LIT>" , index ) <EOL> if r : <EOL> startatindex = r [ <NUM_LIT:1> ] <EOL> else : <EOL> startatindex = "<STR_LIT:1.0>" <EOL> stopatindex = "<STR_LIT>" % lno <EOL> parser . set_str ( text . get ( startatindex , stopatindex ) + '<STR_LIT>' ) <EOL> parser . set_lo ( <NUM_LIT:0> ) <EOL> self . rawtext = parser . str [ : - <NUM_LIT:2> ] <EOL> self . stopatindex = stopatindex <EOL> self . bracketing = parser . get_last_stmt_bracketing ( ) <EOL> self . isopener = [ i > <NUM_LIT:0> and self . bracketing [ i ] [ <NUM_LIT:1> ] > self . bracketing [ i - <NUM_LIT:1> ] [ <NUM_LIT:1> ] <EOL> for i in range ( len ( self . bracketing ) ) ] <EOL> self . set_index ( index ) <EOL> def set_index ( self , index ) : <EOL> """<STR_LIT>""" <EOL> indexinrawtext = len ( self . rawtext ) - len ( self . text . get ( index , self . stopatindex ) ) <EOL> if indexinrawtext < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . indexinrawtext = indexinrawtext <EOL> self . indexbracket = <NUM_LIT:0> <EOL> while self . indexbracket < len ( self . bracketing ) - <NUM_LIT:1> and self . bracketing [ self . indexbracket + <NUM_LIT:1> ] [ <NUM_LIT:0> ] < self . indexinrawtext : <EOL> self . indexbracket += <NUM_LIT:1> <EOL> if self . indexbracket < len ( self . bracketing ) - <NUM_LIT:1> and self . bracketing [ self . indexbracket + <NUM_LIT:1> ] [ <NUM_LIT:0> ] == self . indexinrawtext and not self . isopener [ self . indexbracket + <NUM_LIT:1> ] : <EOL> self . indexbracket += <NUM_LIT:1> <EOL> def is_in_string ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . isopener [ self . indexbracket ] and self . rawtext [ self . bracketing [ self . indexbracket ] [ <NUM_LIT:0> ] ] in ( '<STR_LIT:">' , "<STR_LIT:'>" ) <EOL> def is_in_code ( self ) : <EOL> """<STR_LIT>""" <EOL> return not self . isopener [ self . indexbracket ] or self . rawtext [ self . bracketing [ self . indexbracket ] [ <NUM_LIT:0> ] ] not in ( '<STR_LIT:#>' , '<STR_LIT:">' , "<STR_LIT:'>" ) <EOL> def get_surrounding_brackets ( self , openers = '<STR_LIT>' , mustclose = False ) : <EOL> """<STR_LIT>""" <EOL> bracketinglevel = self . bracketing [ self . indexbracket ] [ <NUM_LIT:1> ] <EOL> before = self . indexbracket <EOL> while not self . isopener [ before ] or self . rawtext [ self . bracketing [ before ] [ <NUM_LIT:0> ] ] not in openers or self . bracketing [ before ] [ <NUM_LIT:1> ] > bracketinglevel : <EOL> before -= <NUM_LIT:1> <EOL> if before < <NUM_LIT:0> : <EOL> return None <EOL> bracketinglevel = min ( bracketinglevel , self . bracketing [ before ] [ <NUM_LIT:1> ] ) <EOL> after = self . indexbracket + <NUM_LIT:1> <EOL> while after < len ( self . bracketing ) and self . bracketing [ after ] [ <NUM_LIT:1> ] >= bracketinglevel : <EOL> after += <NUM_LIT:1> <EOL> beforeindex = self . text . index ( "<STR_LIT>" % <EOL> ( self . stopatindex , len ( self . rawtext ) - self . bracketing [ before ] [ <NUM_LIT:0> ] ) ) <EOL> if after >= len ( self . bracketing ) or self . bracketing [ after ] [ <NUM_LIT:0> ] > len ( self . rawtext ) : <EOL> if mustclose : <EOL> return None <EOL> afterindex = self . stopatindex <EOL> else : <EOL> afterindex = self . text . index ( "<STR_LIT>" % <EOL> ( self . stopatindex , <EOL> len ( self . rawtext ) - ( self . bracketing [ after ] [ <NUM_LIT:0> ] - <NUM_LIT:1> ) ) ) <EOL> return beforeindex , afterindex <EOL> _whitespace_chars = "<STR_LIT>" <EOL> _id_chars = string . ascii_letters + string . digits + "<STR_LIT:_>" <EOL> _id_first_chars = string . ascii_letters + "<STR_LIT:_>" <EOL> def _eat_identifier ( self , str , limit , pos ) : <EOL> i = pos <EOL> while i > limit and str [ i - <NUM_LIT:1> ] in self . _id_chars : <EOL> i -= <NUM_LIT:1> <EOL> if i < pos and ( str [ i ] not in self . _id_first_chars or keyword . iskeyword ( str [ i : pos ] ) ) : <EOL> i = pos <EOL> return pos - i <EOL> def get_expression ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . is_in_code ( ) : <EOL> raise ValueError ( "<STR_LIT>" "<STR_LIT>" ) <EOL> rawtext = self . rawtext <EOL> bracketing = self . bracketing <EOL> brck_index = self . indexbracket <EOL> brck_limit = bracketing [ brck_index ] [ <NUM_LIT:0> ] <EOL> pos = self . indexinrawtext <EOL> last_identifier_pos = pos <EOL> postdot_phase = True <EOL> while <NUM_LIT:1> : <EOL> while <NUM_LIT:1> : <EOL> if pos > brck_limit and rawtext [ pos - <NUM_LIT:1> ] in self . _whitespace_chars : <EOL> pos -= <NUM_LIT:1> <EOL> elif not postdot_phase and pos > brck_limit and rawtext [ pos - <NUM_LIT:1> ] == '<STR_LIT:.>' : <EOL> pos -= <NUM_LIT:1> <EOL> postdot_phase = True <EOL> elif pos == brck_limit and brck_index > <NUM_LIT:0> and rawtext [ bracketing [ brck_index - <NUM_LIT:1> ] [ <NUM_LIT:0> ] ] == '<STR_LIT:#>' : <EOL> brck_index -= <NUM_LIT:2> <EOL> brck_limit = bracketing [ brck_index ] [ <NUM_LIT:0> ] <EOL> pos = bracketing [ brck_index + <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> else : <EOL> break <EOL> if not postdot_phase : <EOL> break <EOL> ret = self . _eat_identifier ( rawtext , brck_limit , pos ) <EOL> if ret : <EOL> pos = pos - ret <EOL> last_identifier_pos = pos <EOL> postdot_phase = False <EOL> elif pos == brck_limit : <EOL> level = bracketing [ brck_index ] [ <NUM_LIT:1> ] <EOL> while brck_index > <NUM_LIT:0> and bracketing [ brck_index - <NUM_LIT:1> ] [ <NUM_LIT:1> ] > level : <EOL> brck_index -= <NUM_LIT:1> <EOL> if bracketing [ brck_index ] [ <NUM_LIT:0> ] == brck_limit : <EOL> break <EOL> pos = bracketing [ brck_index ] [ <NUM_LIT:0> ] <EOL> brck_index -= <NUM_LIT:1> <EOL> brck_limit = bracketing [ brck_index ] [ <NUM_LIT:0> ] <EOL> last_identifier_pos = pos <EOL> if rawtext [ pos ] in "<STR_LIT>" : <EOL> pass <EOL> else : <EOL> if rawtext [ pos ] in "<STR_LIT>" : <EOL> while pos > <NUM_LIT:0> and rawtext [ pos - <NUM_LIT:1> ] in "<STR_LIT>" : <EOL> pos -= <NUM_LIT:1> <EOL> last_identifier_pos = pos <EOL> break <EOL> else : <EOL> break <EOL> return rawtext [ last_identifier_pos : self . indexinrawtext ] </s>
