ZL i<vdZddlmZmZddlmZddlmZmZm Z m Z m Z ddl m Z ddlmZmZmZmZmZmZddlmZmZmZerdd lmZeeefZee ege eeffZeee e!e!fZ"e e"ee!e!ffZ#d e d ed efd Z$dZ%dee e#e!efd ee e#e!effdZ&GddZ'Gdde(Z)GddZ*y)zParser engine for the grammar tables generated by pgen. The grammar table must be loaded first. See Parser/parser.c in the Python distribution for additional info on how this parsing engine works. )CallableIterator)contextmanager) TYPE_CHECKINGAnyOptionalUnioncast)Grammar)NLContextLeafNodeRawNodeconvert)grammartokentokenize) TokenProxyrnodereturnc>|dJt|d|d|dS)Nr)typechildrencontext)r)rrs Z/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/blib2to3/pgen2/parse.pylam_subr !s- 7   T!WtAwQ @@)NNNstackcN|Dcgc]\}}}||tfc}}}Scc}}}w)zNodeless stack copy.) DUMMY_NODE)r#dfalabel_s r stack_copyr)*s);@ @ @eQS% $ @@ @s c eZdZdddeededdfdZedeefdZ e d ede dfd Z e de dfd Z dd ed ededdfdZ ddeededeefdZy)RecorderparserParserilabelsrrNc||_||_||_t|_|jj |_|Dcic]}|t|j c}|_ycc}wN) r,_ilabelsrset _dead_ilabelsr# _start_pointr)_points)selfr,r.rilabels r__init__zRecorder.__init__2sX   '*u KK--LST& 4+<+< ==T TsA*cL|jj|jSr0)r3symmetric_differencer1)r6s rr.zRecorder.ilabels;s!!66t}}EEr!r7c#hK|j5|j||j_ d|j|j_ dddy#t$r|j j |YKwxYw#|j|j_wxYw#1swYyxYwwr0) backtrackr5r,r# ParseErrorr3addr4)r6r7s r switch_tozRecorder.switch_to?s ^^  6 $ V 4DKK  6%)$5$5 ! 6 6 /""&&v. /%)$5$5 ! 6 6sJB2B&AB& B2$BBBBB##B&&B/+B2c#K|jj} d|j_d||j_y#||j_wxYww)a Use the node-level invariant ones for basic parsing operations (push/pop/shift). These still will operate on the stack; but they won't create any new nodes, or modify the contents of any other existing nodes. This saves us a ton of time when we are backtracking, since we want to restore to the initial state as quick as possible, which can only be done by having as little mutatations as possible. TN)r,is_backtracking)r6rAs rr<zRecorder.backtrackJsC++55 :*.DKK ' *9DKK '/DKK 'sAAAAAtok_typetok_valrawc|jD]n}|j|5|r)|jj||||jn'|jj |||jdddpy#1swY{xYwr0)r.r?r, _addtokenraddtoken)r6rBrCrDr7s r add_tokenzRecorder.add_token\s{ll JF' JKK))&(GT\\RKK((7DLLI  J J J J Js AA??B valueforcec|j}t|dk(r'|j^}}td|||j|^}}|s|s|Sy)Nr bad input)r.lenr3r=r)r6rIrJ alive_ilabelsr(most_successful_ilabelr7rests rdetermine_routezRecorder.determine_routedsW }  ")-);); &Q&[*@%V V%  Mr!)F)NF)__name__ __module__ __qualname__listintr r8propertyr2r.rrr?r<strboolrHrrQr!rr+r+1sUxU$s)UgURVUFSFF6666:8D>::"J#JJ$J4J:? c] 26  # r!r+c 8eZdZdZdedeedeededdf dZy) r=z(Exception to signal the parser is stuck.msgrrIrrNc ~tj||d|d|d|||_||_||_||_y)Nz: type=z, value=z , context=) Exceptionr8r\rrIr)r6r\rrIrs rr8zParseError.__init__vsL  SE G;O    r!) rRrSrT__doc__rXrrVr r8rZr!rr=r=ss:2  &sm 4: if p.addtoken(...): # parse a token; may raise ParseError break root = p.rootnode # root of abstract syntax tree A Parser instance may be reused by calling setup() repeatedly. A Parser instance contains state pertaining to the current token sequence, and should not be used concurrently by different threads to parse separate token sequences. See driver.py for how to get input tokens by tokenizing a file or string. Parsing is complete when addtoken() returns True; the root of the abstract syntax tree can then be retrieved from the rootnode instance variable. When a syntax error occurs, addtoken() raises the ParseError exception. There is no error recovery; the parser cannot be used after a syntax error was reported (but it can be reinitialized by calling setup()). NrrrcL||_|xst|_d|_d|_y)aConstructor. The grammar argument is a grammar.Grammar instance; see the grammar module for more information. The parser is not ready yet for parsing; you must call the setup() method to get it started. The optional convert argument is a function mapping concrete syntax tree nodes to abstract syntax tree nodes. If not given, no conversion is done and the syntax tree produced is the concrete syntax tree. If given, it must be a function of two arguments, the first being the grammar (a grammar.Grammar instance), and the second being the concrete syntax tree node to be converted. The syntax tree is converted from the bottom up. **post-note: the convert argument is ignored since for Black's usage, convert will always be blib2to3.pytree.convert. Allowing this to be dynamic hurts mypyc's ability to use early binding. These docs are left for historical and informational value. A concrete syntax tree node is a (type, value, context, nodes) tuple, where type is the node type (a token or symbol number), value is None for symbols and a string for tokens, context is None or an opaque value used for error reporting (typically a (lineno, offset) pair), and nodes is a list of children for symbols, and None for tokens. An abstract syntax tree node may be anything; this is entirely up to the converter function. FN)rr rrA last_token)r6rrs rr8zParser.__init__s'D )' $)-r!proxyrstartc||jj}|ddgf}|jj|d|f}|g|_d|_t |_||_d|_y)aPrepare for parsing. This *must* be called before starting to parse. The optional argument is an alternative start symbol; it defaults to the grammar's start symbol. You can use a Parser instance to parse any number of programs; each time you call setup() the parser is reset to an initial state determined by the (implicit or explicit) start symbol. Nr) rrddfasr#rootnoder2 used_namesrcrb)r6rcrdnewnode stackentrys rsetupz Parser.setupsj =LL&&E"4r2ll''.7; 7Al &* $'E r!rrIrc|j|||}t|dk\sJt|dk(r|\}|j||||S|jj 5}d\}}t |||} | j ||d|} | j| |j|sd}n|j|^} } } | tjtjfvr|dz }a| tjk(rtj| } | j | | |dz }| j| t!t"| j| |}|J ddd|j|||S#1swYxYw)z>&$w? ?ZZ   ! &U%NGUgw7H   tU  5$ **+;<D((1 E8= '8J5!1A"x'7'7&EEqLG"hkk1&-mm4D&EO""?4DE1 **+;<D #x778HPU7VWF% %%/ &2~~fdE7;;3 & &s$CE?=%E??Fr7ch |jd\}}}|\}} ||} | D]\} } |jj| d} | dk\r9|jj| }|\}}||vsJ|j | || |n|| k(sf|j ||| || }||d|fgk(rB|j |jsy|jd\}}}|\}} ||d|fgk(rB||_yd|f| vr*|j |jstd|||td|||2)NTr"rFztoo much inputrL) r#rlabelsrfpushshiftpoprbr=)r6r7rrIrr&staterstatesfirstarcsinewstatetitsdfa itsstatesitsfirsts rrFzParser._addtokensf#zz"~ CMFE%=D#% H 8LL''*1-8!\\..q1F*0'Ix) !VXw?q[JJtUHg>$E -QJ<7 #zz#'+/::b>(UD(+  !-QJ<7'+DO 5% H:u:%HHJ::()94PP%[$wGGUr!c|tjk(r%|jj|||jj vr|jj |gS||jj vr||jjvsJ|jdtjtjtjtjtjfvr|jj|gS|jj||jj |gS|jjj|}|td||||gS)zTurn a token into a label. (Internal) Depending on whether the value is a soft-keyword or not, this function may return multiple labels to choose from.Nz bad token)rNAMErhr>rkeywords soft_keywordstokensrbINDENTDEDENTNEWLINESEMICOLONgetr=)r6rrIrr7s rrmzParser.classify@s0 5::  OO   & --- --e455$,,444t||22222 ??LLLLMMJJKK +!LL//566LL''-LL..u5 $$((. >[$w? ?xr!rc(|jr*|jd\}}}||tf|jd<y|jd\}}}|||df} t|j| } |dJ|dj | |||f|jd<y)zShift a token. (Internal)r"N)rAr#r%rrappend) r6rrIrrr&r~r(rrawnoderis rr|z Parser.shiftes    JJrNMC!8Z8DJJrN#zz"~ C $eWd;GdllG4G8' '' HOOG $!8T2DJJrNr!newdfacF|jrL|jd\}}}||tf|jd<|jj|dtfy|jd\}}}|d|gf} |||f|jd<|jj|d| fy)zPush a nonterminal. (Internal)r"rN)rAr#r%r) r6rrrrr&r~r(rris rr{z Parser.pushrs    JJrNMC!8Z8DJJrN JJ  vq*5 6#zz"~ C $dGR8G!8T2DJJrN JJ  vq'2 3r!ct|jr|jjy|jj\}}}t|j|}|jr/|jd\}}}|dJ|dj |y||_|j|j _y)zPop a nonterminal. (Internal)r"N)rAr#r}rrrrgrh)r6popdfapopstatepopnoderir&r~rs rr}z Parser.pop~s    JJNN (, (8 %FHgdllG4Gzz#'::b> UDBx+++R( ' +/?? (r!r0)rN)rRrSrTr_r rConvertr8rVrkrXr rYrGrFrUrmr|DFASr{r}rZr!rr-r-s:&.&.(72C&.t&.P< 4.rs /%<<*EE'&0 sBw- GW%uT4Z'88 9 4c3h ! S$sCx. !AWAGAA $ A dC() *A %c7" #$A??D  I;I;r!