o =?hM@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)selfkeyrr6/opt/alt/python310/lib64/python3.10/idlelib/pyparse.py __missing__rszParseMap.__missing__N)__name__ __module__ __qualname____doc__r rrrrrfs rrcc |] }t|tdfVqdS)(Nord.0crrr 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|}|dkrn |dd|d}t|||}|r:||s:|}n|}q|durSt|}|rQ||sQ|}|S|d}t||}rq|\}}||sj|}t||}s^|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)r!r rangerfind_synchrestartspan) rZis_char_in_stringr!poslimitZtriesimr#rrrfind_good_parse_starts0     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(). rr%rN)r!)rlorrrset_losz Parser.set_loc Cs|jdkrdSd|_|j}|t}|dd}|dd}|dd}|dd}|dd}t}d }}d g|_}|j}d t|}}||krE||} |d}| dkrXqF| dkri|d}|d krh||qF| d krr|d}qF| d kr}|r||d}qF| d ks| d kr| } ||d|d| dkr| d} |} t| d} || }||kr||} |d}| dkrq||d|| | kr|| }n?| dkr|d}| d kr|d kr||n*q| dkr||ksJ||dkr|d}|d}q||ks|d| krt }nt }qF| dkr| d|}|d ksJqF| dks"J||ks)J||dkr=|d}|d|kr=t }|d}||ksK|t krV|t krV|d krVt }||_|tk|d|kksfJ|d|krs||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}. r%NZxxxxxxxxxZxxxxZxxz xrrrr"'\#r)r"r! translatetransreplaceC_NONE goodlinesappendr C_STRING_FIRST_LINEC_STRING_NEXT_LINESfind C_BACKSLASH C_BRACKET continuation) rr!rDlevellnor=Z push_goodr-nchZquoteZfirstlnowrrr_study1s                Z zParser._study1cC||jSr)rJrDrrrrget_continuation_typeKszParser.get_continuation_typecCs|jdkrdS|d|_|j|j}}t|d}t|}|rM|s%J|}t||d||D] }|dd|dd}q2t||rJ|d}nn|s!|dkrY|dksWJ|}|||_|_ d}g}|j } |dfg} ||krVt |||} | r| } | d}||kr||dvr|d}||kr||dvs||kr||}| }||krn||} | dvr| || |t|f| }|d}ql| d vr|r|d =| }|d}| |t|fql| d ks| d kr| |t|df| }t ||| }| |t|fql| d kr/| |t|df|d||d}|dks%J| |t|fql| dks6J|d}||ksAJ||dkrN| ||}|d}||ksq||_|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 r5Nr%rr ([{)]}rr3r4r8r7)r"rJr!r=r r&r'_junkre stmt_startstmt_endr>_chew_ordinaryreend_match_stringrerAlastchlastopenbracketpostuplestmt_bracketing)rr!r=r-pqZnothingrXstackZ push_stackZ bracketingr.ZnewprHrrr_study2Os          AzParser._study2cCs||jtks J|j}|j}t|}|dd|d}}|d}||krFt||}|r8|d}d}n%| d|d}}||ks(|}}||dvrZ|d}||dvsP|j }t||| |j |S)zpReturn number of spaces the next line should be indented. Line continuation must be C_BRACKET. rrr% ) r_rDrCrYr!r r'_itemrerVrAr expandtabsr)rjr!rGZorigir-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. r)rJr=)rr=rrrget_num_lines_in_stmtszParser.get_num_lines_in_stmtcCs||jtks J|j}|j}||dvr!|d}||dvs|}|d|d}d}}||kr||}|dvrD|d}|d}nH|dvrS|rN|d}|d}n9|dks[|dkrdt|||}n(|d krin'|dkr|d kr|dks}||dd vr||dd krd}n|d}||ks3|r|d}t d |||d u}|s|}||dvr|d}||dvst ||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. r`r%rrrPrQr3r4r8=z=<>!z\s*\\NrO) r_rDrBr!rSrArWrVrematchr rbr)rr!r-ZstartposendposfoundrErHrrrcompute_backslash_indentsV      zParser.compute_backslash_indentcCs\||j|j}}|}|j}||kr(||dvr(|d}||kr(||dvs|||S)z`Return the leading whitespace on the initial line of the last interesting stmt. r`r%)r_rSrTr!)rr-rGrcr!rrrget_base_indent_string,s zParser.get_base_indent_stringcCs||jdkS)zs`     Q