h _Mc@sdZddlZed\ZZZZZedej ej Bj Z edej j Zedej ejBj Zedej j Zedej j Zed ej j ZGd d d eZeed d ZedddDedddDedddDGdddZedkrddlmZeddddSdS)aDefine partial Python code Parser used by editor and hyperparser. Instances of ParseMap are used with str.translate. The following bound search and match functions are defined: _synchre - start of popular statement; _junkre - whitespace or comment line; _match_stringre: string, possibly without closer; _itemre - line that may have bracket structure start; _closere - line that must be followed by dedent. _chew_ordinaryre - non-special characters. Nz ^ [ \t]* (?: while | else | def | return | assert | break | class | continue | elif | try | except | raise | import | yield ) \b z' [ \t]* (?: \# \S .* )? \n aK \""" [^"\\]* (?: (?: \\. | "(?!"") ) [^"\\]* )* (?: \""" )? | " [^"\\\n]* (?: \\. [^"\\\n]* )* "? | ''' [^'\\]* (?: (?: \\. | '(?!'') ) [^'\\]* )* (?: ''' )? | ' [^'\\\n]* (?: \\. [^'\\\n]* )* '? zM [ \t]* [^\s#\\] # if we match, m.end()-1 is the interesting char z_ \s* (?: return | break | continue | raise | pass ) \b z [^[\](){}#'"\\]+ c@seZdZdZddZdS)ParseMapapDict subclass that maps anything not in dict to 'x'. This is designed to be used with str.translate in study1. Anything not specifically mapped otherwise becomes 'x'. Example: replace everything except whitespace with 'x'. >>> keepwhite = ParseMap((ord(c), ord(c)) for c in ' \t\n\r') >>> "a + b\tc\nd".translate(keepwhite) 'x x x\tx\nx' cCsdS)Nx)selfZkeyrr(/usr/lib64/python3.10/idlelib/pyparse.py __missing__rszParseMap.__missing__N)__name__ __module__ __qualname____doc__rrrrrrfs rircc|]}t|tdfVqdS)(NZordZ.0Zcrrr xrz({[ccr ))Nrrrrrryrz)}]ccs|]}t|t|fVqdSNrrrrrrzrz"'\ #c@s|eZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZdS)ParsercCs||_||_dSr) indentwidthtabwidth)rrrrrr__init__s zParser.__init__cCs,t|dks|ddksJ||_d|_dS)Nr )lencode study_level)rsrrrset_codes zParser.set_codec Cs|jd}}t|}tdD]\}|dd|}|dkr:qz|dd|d}t|||}|rt||st|}qz|}q|durt|}|r||s|}|S|d}t||}|r|\}}||s|}q|S)a^ Return index of a good place to begin parsing, as close to the end of the string as possible. This will be the start of some popular stmt like "if" or "def". Return None if none found: the caller should pass more prior context then, if possible, or if not (the entire program text up until the point of interest has already been tried) pass 0 to set_lo(). This will be reliable iff given a reliable is_char_in_string() function, meaning that when it says "no", it's absolutely guaranteed that the char is not in a string. Nrz: rr)rrrangerfind_synchreZstartZspan) rZis_char_in_stringrZposZlimitZtriesimrrrrfind_good_parse_starts2      zParser.find_good_parse_startcCs>|dks|j|ddksJ|dkr:|j|d|_dSdS)zx Throw away the start of the string. Intended to be called with the result of find_good_parse_start(). rrrN)r)rZlorrrset_los z Parser.set_loc Cs|jdkrdSd|_|j}|t}|dd}|dd}|dd}|dd}|dd}t}d }}d g|_}|j}d t|}}||kr||} |d}| dkrq| dkr|d}|d kr||q| d kr|d}q| d kr|r|d}q| d ks| d kr| } ||d|d| dkr8| d} |} t| d} || }||kr||} |d}| dkrxqP||d|| | kr|| }q| dkr|d}| d krP|d kr||q| dkrP||ksJ||dkr|d}|d}qP|d| krt }qt }q| dkr@| d|}|d ksJ| dksNJ||ks\J||dkr|d}|d|krt }|d}q|t kr|t kr|d krt }||_|tk|d|kksJ|d|kr||dSdS)zFind the line numbers of non-continuation lines. As quickly as humanly possible , find the line numbers (0- based) of the non-continuation lines. Creates self.{goodlines, continuation}. rNZxxxxxxxxZxZxxxxZxxz xrrr r"'i\#r)rrZ translatetransZreplaceC_NONE goodlinesappendrC_STRING_FIRST_LINEC_STRING_NEXT_LINESfind C_BACKSLASH C_BRACKET continuation) rrr5levelZlnor.Z push_goodr#nchZquoteZfirstlnoZwrrr_study1s                     zParser._study1cC||jSr)r9r5rrrrget_continuation_typeOszParser.get_continuation_typecCs|jdkrdS|d|_|j|j}}t|d}t|}|r|sJJ|}t||d||D]}|dd|dd}qdt||r|d}q>|dkr|dksJ|}|||_|_ d}g}|j } |dfg} ||krt |||} | rD| } | d}||kr ||dvr |d}q||kr2||}| }||krDq||} | dvr~| || |t|f| }|d}q| d vr|r|d =| }|d}| |t|fq| d ks| d kr| |t|df| }t ||| }| |t|fq| d krZ| |t|df|d||d}|dksFJ| |t|fq| dkshJ|d}||ks~J||dkr| ||}|d}q||_|r|d nd|_t| |_dS)am study1 was sufficient to determine the continuation status, but doing more requires looking at every character. study2 does this for the last interesting statement in the block. Creates: self.stmt_start, stmt_end slice indices of last interesting stmt self.stmt_bracketing the bracketing structure of the last interesting stmt; for example, for the statement "say(boo) or die", stmt_bracketing will be ((0, 0), (0, 1), (2, 0), (2, 1), (4, 0)). Strings and comments are treated as brackets, for the matter. self.lastch last interesting character before optional trailing comment self.lastopenbracketpos if continuation is C_BRACKET, index of last open bracket r)NrrrZ ([{)]}rr'r(r+r*)rr9rr.rr r!_junkre stmt_startstmt_endr/_chew_ordinaryreend_match_stringrer2lastchlastopenbracketposZtuplestmt_bracketing)rrr.r#ZpZqZnothingrFZstackZ push_stackZ bracketingr$Znewpr8rrr_study2Ss                zParser._study2cCs||jtksJ|j}|j}t|}|dd|d}}|d}||krt||}|rp|d}d}q| d|d}}qH|}}||dvr|d}q|j }t||| |j |S)zpReturn number of spaces the next line should be indented. Line continuation must be C_BRACKET. rrr N) rIr5r4rGrrr!_itemrerDr2r expandtabsr)rjrr7Zorigir#r$Zextrarrrcompute_bracket_indents(    zParser.compute_bracket_indentcCs||j}|d|dS)zReturn number of physical lines in last stmt. The statement doesn't have to be an interesting statement. This is intended to be called when continuation is C_BACKSLASH. riN)r9r.)rr.rrrget_num_lines_in_stmtszParser.get_num_lines_in_stmtcCs||jtksJ|j}|j}||dvr8|d}q"|}|d|d}d}}||kr||}|dvr|d}|d}qT|dvr|r|d}|d}qT|dks|dkrt|||}qT|d kr̐q|dkr|d kr|dks||dd vr||dd krd}q|d}qT|rD|d}t d |||d u}|sh|}||dvrh|d}qNt ||j| |j dS)zReturn number of spaces the next line should be indented. Line continuation must be C_BACKSLASH. Also assume that the new line is the first one following the initial line of the stmt. rJrrrr>r?r'r(r+z=z=<>!z\s*\\Nr=) rIr5r3rrAr2rErDrematchrrLr)rrr#ZstartposZendposZfoundr6r8rrrcompute_backslash_indentsZ       zParser.compute_backslash_indentcCsJ||j|j}}|}|j}||kr>||dvr>|d}q |||S)z`Return the leading whitespace on the initial line of the last interesting stmt. rJrN)rIrArBr)rr#r7rMrrrrget_base_indent_string0s  zParser.get_base_indent_stringcCs||jdkS)zsJ     U