LeJdZddlZed\ZZZZZejdejejzjZ ejdejjZejdejejzjZejdejjZejdejjZejd ejjZGd d eZej-ed d Zej1ddDej1ddDej1ddDGddZedk(rddlmZeddyy)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 [^[\](){}#'"\\]+ ceZdZdZdZy)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' cy)Nx)selfkeys 6/opt/alt/python312/lib64/python3.12/idlelib/pyparse.py __missing__zParseMap.__missing__rsN)__name__ __module__ __qualname____doc__r rr r rrfs  r rrc#HK|]}t|tdfyw)(Nord.0cs r rx /Ac!fc#h  "z({[c#HK|]}t|tdfyw))Nrrs r rryrrz)}]c#HK|]}t|t|fywNrrs r rrzs 2z!c!fc!f zrz"'\ #c`eZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZy)Parserc ||_||_yr ) indentwidthtabwidth)r r$r%s r __init__zParser.__init__s&  r cPt|dk(s |ddk(sJ||_d|_y)Nr )lencode study_level)r ss r set_codezParser.set_codes,1v{aetm++ r c|jd}}t|}tdD]k}|jdd|}|dkrnQ|jdd|dz}t |||}|r(||j s|j }n|}m|5t |}|r&||j s|j }|S|dz}t ||x}r,|j \}}||s|}t ||x}r,|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) r is_char_in_stringr+poslimittriesimr-s r find_good_parse_startzParser.find_good_parse_startsIItc D 1XE 5!U+A1u 4A&*Aq%(A*17795ggiE ;A*17795ggiJ !GD!$$a$668DAq$Q'D!$$a$ r cp|dk(s|j|dz dk(sJ|dkDr|j|d|_yy)zx Throw away the start of the string. Intended to be called with the result of find_good_parse_start(). rr0r)N)r+)r los r set_loz Parser.set_losA Qw$))BqD/T111 6 "#DI r c|jdk\ryd|_|j}|jt}|j dd}|j dd}|j dd}|j dd}|j dd}t }d x}}d gx|_}|j}d t|}}||krN||} |dz}| dk(r| dk(r|dz}|d k(r||.| d k(r|dz}9| d k(r|r|dz }F| d k(s| d k(r| } ||dz |dz| dzk(r| dz} |} t| dz } || z}||krg||} |dz}| dk(r||dz || z| k(r|| z}nX| dk(r|dz}| d k(r|d k(r||n;G| dk(r||ksJ||dk(r|dz}|dz}f||krg|dz | k(rt}nt}| dk(r|jd|}|d k\sJ| dk(sJ||ksJ||dk(r|dz}|dz|k(rt}|dz}||krN|tk7r|tk7r |d kDrt}||_|t k(|d|k(k(sJ|d|k7r ||yy)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}. r0Nxxxxxxxxxxxxxxxz xr)rrr"'\#r()r,r+ translatetransreplaceC_NONE goodlinesappendr*C_STRING_FIRST_LINEC_STRING_NEXT_LINESfind C_BACKSLASH C_BRACKET continuation) r r+rVlevellnorO push_goodr:nchquotefirstlnows r _study1zParser._study1s   q  yy~~e$||J,||FC(||D#&||D#&||E4( &'S($$ #d)1!eaB!ASyTzAgA:cNSy Sy!AIESyB#I!AaC=EAI-!AIEJNaC!eaB!ASy AaC!}-aCTz!Ag6$z )#! Tz 1u u7d?"%'CaC 3!e@aH,(; ': SyIIdA&Av v: :q5L5Aw$AgQ3!8#.L!Ak!et / / 33 $L(&IbMS,@AAA R=C  cN r c:|j|jSr )r_rVr s r get_continuation_typezParser.get_continuation_typeKs    r cb|jdk\ry|jd|_|j|j}}t |dz }t |}|rO|sJ|}t ||dz ||D]}|j dd|dz dz}t||r|dz }nn|rO|dk(r |dk(sJ|}|c|_|_ d}g}|j} |dfg} ||krt|||} | rF| j} | dz }||k\r||dvr|dz }||k\r||dvr||k\r||}| }||k\rnT||} | dvr,| || j|t |f| }|dz}| d vr)|r|d =| }|dz}| j|t |f| d k(s| d k(rZ| j|t |dzf| }t|||j}| j|t |f!| d k(rZ| j|t |dzf|jd||dz}|dkDsJ| j|t |f| dk(sJ|dz}||ksJ||dk7r| ||z}|dz}||kr||_|r|d nd|_t#| |_y)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 rGNr0r)r ([{)]}r(rErFrJrI)r,r_r+rOr*r1r2_junkre stmt_startstmt_endrP_chew_ordinaryreend_match_stringrerSlastchlastopenbracketpostuplestmt_bracketing)r r+rOr:pqnothingrnstack push_stack bracketingr;newpr[s r _study2zParser._study2Os&   q  ))T^^i  NQ  IH1A 1Q31>JJtQ!,q0? a aC 66M6A)*A&\\ !fX !e q!,Auuw1H1faG!3!A1faG!36!!WF6aBU{1 !!1c%j/2aCU{b aC!!1c%j/2SyB#I!!1c%jl"34#D!Q/335!!1c%j/2Sy!!1c%jl"34IIdAq)A-1u u!!1c%j/2: :!Aq5L5Aw$d1g!A{!eB /4%)$$Z0r c|j|jtk(sJ|j}|j}t |}|j dd|dzx}}|dz}||krAt||}|r|jdz }d}nA|jd|dzx}}||krA|x}}||dvr |dz}||dvr |j}t |||j|j|zS)zpReturn number of spaces the next line should be indented. Line continuation must be C_BRACKET. r)rr0 ) ryrVrUror+r*r2_itemrerlrSr$ expandtabsr%)r jr+rZorigir:r;extras r compute_bracket_indentzParser.compute_bracket_indents   I---  # #yy IJJtQ*Q.. aC!ea AEEGaK $*Q..A!eMAq'U"aCq'U"$$E4!9'' 67%??r cP|j|j}|d|dz S)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()r_rO)r rOs r get_num_lines_in_stmtzParser.get_num_lines_in_stmts) NN }y},,r c|j|jtk(sJ|j}|j}||dvr |dz}||dvr |}|j d|dz}dx}}||kr||}|dvr |dz}|dz}ni|dvr |r|dz }|dz}nX|dk(s|dk(rt |||j}n2|d k(rn2|dk(r"|d k(r|dk(s ||dz d vr||dzd k7rd}n |dz}||kr|r |dz}tjd |||d u}|s|}||dvr |dz}||dvr t||j|j|jdzS)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{r0r)rrfrgrErFrJ=z=<>!z\s*\\Nre) ryrVrTr+rirSrmrlrematchr*r}r%)r r+r:startposendposfoundrWr[s r compute_backslash_indentzParser.compute_backslash_indents   K///yy OO1g!A1g4*Q.&jaBU{ aCu!AIEaCsbCi#D!V488:s!c Fd1Q3iv5!9#aC+&j. !AHHXtAf~6$>EAq'(aCq'(4*55%)]]45789 9r c|j|j|j}}|}|j}||kr||dvr|dz}||kr||dvr|||S)z`Return the leading whitespace on the initial line of the last interesting stmt. r{r0)ryrirjr+)r r:rZr~r+s r get_base_indent_stringzParser.get_base_indent_string,sg  1 yy!eQ5(AA!eQ5(Aayr c@|j|jdk(S)z-79r " @ $r r"__main__)mainzidlelib.idle_test.test_pyparserG) verbosity)rrr1rNrTrQrRrUcompileVERBOSE MULTILINEsearchr3rrhDOTALLrmr|rrkdictrfromkeysrLupdater"runittestrrr r rs  $)8!)i 2::&ZZ",,' &!'' . "**ZZ   "** ZZ"))! #U!* "**ZZ  2:: ZZ  2::ZZ t " %*c* / // / // 2z 22K$K$\ z )Q7r