3 § f–Nã@sdZddlZedƒ\ZZZZZejdej ej Bƒj Z ejdej ƒj Zejdej ejBƒj Zejdej ƒj Zejdej ƒj Zejd ej ƒj ZGd d „d eƒZejed ƒd ƒZejdd„dDƒƒejdd„dDƒƒejdd„dDƒƒGdd„dƒZedk�rddlmZeddd�dS)aÄDefine 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. éNézä ^ [ \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@seZdZdZdd„ZdS)Ú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)Néx©)ÚselfÚkeyrrú4/opt/alt/python36/lib64/python3.6/idlelib/pyparse.pyÚ __missing__rszParseMap.__missing__N)Ú__name__Ú __module__Ú __qualname__Ú__doc__r rrrrrfs ré€rccs|]}t|ƒtdƒfVqdS)ú(N)Úord)Ú.0Úcrrrú xsrz({[ccs|]}t|ƒtdƒfVqdS)ú)N)r)rrrrrrysz)}]ccs|]}t|ƒt|ƒfVqdS)N)r)rrrrrrzsz"'\ #c@s‚eZdZdd„Zdd„Zdefdd„Zdd „Zd d „Zd d „Z dd„Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zdd„ZdS)ÚParsercCs||_||_dS)N)Ú indentwidthÚtabwidth)rrrrrrÚ__init__szParser.__init__cCs||_d|_dS)Nr)ÚcodeÚ study_level)rÚsrrrÚset_codeƒszParser.set_codeNc Csî|jd}}|sdSt|ƒ}xftdƒD]Z}|jdd|ƒ}|dkrBP|jdd|ƒd}||||ƒ}|r|||jƒƒ r||jƒ}P|}q&W|dkr²||ƒ}|r®||jƒƒ r®|jƒ}|S|d}x.|||ƒ}|rä|jƒ\} }|| ƒsæ| }q¼Pq¼W|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: rÚ é)rÚlenÚrangeÚrfindÚstartÚspan) rZis_char_in_stringÚ_synchrerÚposÚlimitZtriesÚiÚmrrrrÚfind_good_parse_startˆs8     zParser.find_good_parse_startcCs|dkr|j|d…|_dS)zx Throw away the start of the string. Intended to be called with the result of find_good_parse_start(). rN)r)rÚlorrrÚset_loÅsz Parser.set_loc Cs²|jdkrdSd|_|j}|jtƒ}|jddƒ}|jddƒ}|jddƒ}|jddƒ}|jddƒ}t}d }}d g|_}|j}d t|ƒ}}�xà||k�rn||} |d}| dkr´q�| dkrÖ|d}|d kr�||ƒq�| d krè|d}q�| d k�r|r�|d}q�| d k�s| d k�r$| } ||d|d…| dk�r>| d} |} t| ƒd} || }xÊ||k�r||} |d}| dk�r€�qX||d|| …| k�r¤|| }P| dk�rØ|d}| d k�rX|d k�rÒ||ƒP�qX| dk�rX||dk�rø|d}|d}�qX�qXW|d| k�rt }q�t }q�| dk�r<|j d|ƒ}q�||dk�rd|d}|d|k�rdt }|d}q�W|t k�r’|t k�r’|d k�r’t }||_|d|k�r®||ƒdS)záFind 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}. rNZxxxxxxxxÚxZxxxxZxxz xrrrrú"ú'ééú\ú#éÿÿÿÿ)rrÚ translateÚtransÚreplaceÚC_NONEÚ goodlinesÚappendrÚC_STRING_FIRST_LINEÚC_STRING_NEXT_LINESÚfindÚ C_BACKSLASHÚ C_BRACKETÚ continuation) rrr?ÚlevelZlnor8Z push_goodr'ÚnÚchZquoteZfirstlnoÚwrrrÚ_study1Îsš                     zParser._study1cCs|jƒ|jS)N)rDr?)rrrrÚget_continuation_typeTszParser.get_continuation_typecCs¢|jdkrdS|jƒd|_|j|j}}t|ƒd}t|ƒ}xX|r–|}x4t||d||ƒD]}|jdd|dƒd}q`Wt||ƒr’|d}q@Pq@W|dkr¤|}|||_|_ d}g}|j } |dfg} �x¬||k�rxt |||ƒ} | �rD| j ƒ} | d}x&||k�r ||dk�r |d}�qüW||k�r4||}| }||k�rDP||} | dk�r~| |ƒ| j |t|ƒfƒ| }|d}qÎ| d k�r´|�r”|d =| }|d}| j |t|ƒfƒqÎ| d k�sÈ| d k�r| j |t|ƒdfƒ| }t |||ƒj ƒ}| j |t|ƒfƒqÎ| d k�rL| j |t|ƒdfƒ|jd||ƒd}| j |t|ƒfƒqÎ|d}||dk�rn| ||}|d}qÎW||_|�rŽ|dnd|_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/NrrrÚz z([{z)]}r-r.r2r3r3)rrDrr8rr r!Ú_junkreÚ stmt_startÚstmt_endr9Ú_chew_ordinaryreÚendÚ_match_stringrer<ÚlastchÚlastopenbracketposÚtupleÚstmt_bracketing)rrr8r'ÚpÚqZnothingrMÚstackZ push_stackZ bracketingr(ZnewprBrrrÚ_study2Xs‚             zParser._study2cCsÂ|jƒ|j}|j}t|ƒ}|jdd|ƒd}}|d}xj||kr|t||ƒ}|rf|jƒd}d}Pq<|jd|ƒd}}q!z\s*\\Nz ) rTrrHr<rLrKÚreÚmatchrrVr)rrr'ÚstartposÚendposÚfoundr@rBrrrÚcompute_backslash_indentüsH     zParser.compute_backslash_indentcCsN|jƒ|j|j}}|}|j}x ||kr@||dkr@|d}q"W|||…S)z`Return the leading whitespace on the initial line of the last interesting stmt. z r)rTrHrIr)rr'rArWrrrrÚget_base_indent_string5s zParser.get_base_indent_stringcCs|jƒ|jdkS)z=Return True if the last interesting statemtent opens a block.ú:)rTrM)rrrrÚis_block_openerAszParser.is_block_openercCs|jƒt|j|jƒdk S)z=Return True if the last interesting statement closes a block.N)rTÚ_closererrH)rrrrÚis_block_closerFszParser.is_block_closercCs|jƒ|jS)zŠReturn bracketing structure of the last interesting statement. The returned tuple is in the format defined in _study2(). )rTrP)rrrrÚget_last_stmt_bracketingKszParser.get_last_stmt_bracketing)r r r rrr$r)r+rDrErTrXrZrarbrdrfrgrrrrr}s  < { 9 rÚ__main__)Úmainzidlelib.idle_test.test_pyparser/)Ú verbosity)r r\r r7r=r:r;r>ÚcompileÚVERBOSEÚ MULTILINEÚsearchr$r]rGÚDOTALLrLrUrerJÚdictrÚfromkeysr5Úupdaterr ZunittestrirrrrÚ s4    Z