
    ^h<                        S r SSKJrJr  SSKJr  SSKJrJrJ	r	J
r
Jr  SSKJr  SSKJrJrJrJrJrJr  SSKJrJrJr  \(       a  SS	KJr  \\\4   r\\\/\
\\4   4   r\\\ \!\!4         r"\ \"\\!\!4   4   r#S
\S\S\4S jr$Sr%S\\ \#\!\4      S\\ \#\!\4      4S jr& " S S5      r' " S S\(5      r) " S S5      r*g)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.

    )CallableIterator)contextmanager)TYPE_CHECKINGAnyOptionalUnioncast)Grammar)NLContextLeafNodeRawNodeconvert   )grammartokentokenize)
TokenProxyr   nodereturnc                 :    US   c   e[        US   US   US   S9$ )N   r      )typechildrencontext)r   )r   r   s     N/home/james-whalen/.local/lib/python3.13/site-packages/blib2to3/pgen2/parse.pylam_subr        s-    7T!WtAwQ@@    )NNNstackc                 P    U  VVVs/ s H  u  po1U[         4PM     snnn$ s  snnnf )zNodeless stack copy.)
DUMMY_NODE)r#   dfalabel_s       r   
stack_copyr)   )   s&     ;@@%Q%$%@@@s   !c            	           \ rS rSrSSS\\   S\SS4S jr\S\	\   4S	 j5       r
\S
\S\S   4S j5       r\S\S   4S j5       rSS\S\S\SS4S jjr SS\\   S\S\\   4S jjrSrg)Recorder0   parserParserilabelsr   r   Nc                     Xl         X l        X0l        [        5       U l        U R                   R
                  U l        U Vs0 s H  oD[        U R                  5      _M     snU l        g s  snf N)	r-   _ilabelsr   set_dead_ilabelsr#   _start_pointr)   _points)selfr-   r/   r   ilabels        r   __init__Recorder.__init__1   sT    '*u KK--LSTG&
4+<+< ==GTTs   A)c                 L    U R                   R                  U R                  5      $ r1   )r4   symmetric_differencer2   )r7   s    r   r/   Recorder.ilabels:   s    !!66t}}EEr!   r8   c              #   t  #    U R                  5          U R                  U   U R                  l         S v   U R                  U R                  l         S S S 5        g ! [         a    U R
                  R                  U5         NLf = f! U R                  U R                  l        f = f! , (       d  f       g = f7fr1   )	backtrackr6   r-   r#   
ParseErrorr4   addr5   )r7   r8   s     r   	switch_toRecorder.switch_to>   s     ^^ $V 4DKK6 %)$5$5!   /""&&v./ %)$5$5! sJ   B8B'AB'	B8%BBBBB$$B''
B51B8c              #      #    U R                   R                  n SU R                   l        Sv   XR                   l        g! XR                   l        f = f7f)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)r7   rE   s     r   r?   Recorder.backtrackI   s>      ++55	:*.DKK'*9KK'/KK's   AA  A AAtok_typetok_valrawc                 *   U R                    Hq  nU R                  U5         U(       a'  U R                  R                  XAX R                  5        O&U R                  R                  XU R                  5        S S S 5        Ms     g ! , (       d  f       M  = fr1   )r/   rB   r-   	_addtokenr   addtoken)r7   rG   rH   rI   r8   s        r   	add_tokenRecorder.add_token[   s^    llF'KK))&G\\RKK((DLLI	 (' #''s   AB
B	valueforcec                     U R                   n[        U5      S:X  a%  U R                  Gt pE[        SXQU R                  5      eUtpgU(       d  U(       d  U$ g )Nr   	bad input)r/   lenr4   r@   r   )r7   rO   rP   alive_ilabelsr(   most_successful_ilabelr8   rests           r   determine_routeRecorder.determine_routec   sP     }")-););&Q[*@VV%Mr!   )r4   r2   r6   r5   r   r-   )F)NF)__name__
__module____qualname____firstlineno__listintr   r9   propertyr3   r/   r   r   rB   r?   strboolrM   r   rW   __static_attributes__ r!   r   r+   r+   0   s    Ux U$s) Ug URV U FS F F 6 6 6 6 :8D> : :"J# J J$ J4 J :?c]26	# r!   r+   c            
       B    \ rS rSrSrS\S\\   S\\   S\SS4
S	 jr	S
r
g)r@   r   z(Exception to signal the parser is stuck.msgr   rO   r   r   Nc           
      z    [         R                  X SU< SU< SU< 35        Xl        X l        X0l        X@l        g )Nz: type=z, value=z
, context=)	Exceptionr9   rf   r   rO   r   )r7   rf   r   rO   r   s        r   r9   ParseError.__init__u   sB     	E	G;O	
 	
r!   )r   rf   r   rO   )rY   rZ   r[   r\   __doc__r`   r   r^   r   r9   rb   rc   r!   r   r@   r@   r   s:    2		&sm	4<SM	LS			r!   r@   c            
           \ rS rSrSrSS\S\\   SS4S jjrSSS	S
\\	   SS4S jjr
S\	S\S\S\4S jrS\	S\	S\S\S\4
S jrS\	S\S\S\\	   4S jrS\	S\S\	S\SS4
S jrS\	S\S\	S\SS4
S jrSS jrSrg)r.      a  Parser engine.

The proper usage sequence is:

p = Parser(grammar, [converter])  # create instance
p.setup([start])                  # prepare for parsing
<for each input token>:
    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()).

Nr   r   r   c                 T    Xl         U=(       d    [        U l        SU l        SU l        g)a-  Constructor.

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)r   r    r   rE   
last_token)r7   r   r   s      r   r9   Parser.__init__   s%    D )'$)-r!   proxyr   startc                     Uc  U R                   R                  nUSS/ 4nU R                   R                  U   SU4nU/U l        SU l        [        5       U l        Xl        SU l        g)ac  Prepare 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   )	r   rq   dfasr#   rootnoder3   
used_namesrp   rn   )r7   rp   rq   newnode
stackentrys        r   setupParser.setup   sh     =LL&&E "4r2ll''.7;
7Al
&*$'E
r!   r   rO   r   c           	         U R                  XU5      n[        U5      S:  d   e[        U5      S:X  a  Uu  nU R                  XQX#5      $ U R                  R	                  5        nSu  px[        XU5      n	U	R                  XSS9  Un
U	R                  U
5      c  UR                  U5      (       d  SnOUR                  U5      tpnU[        R                  [        R                  4;   a  US-  nMi  U[        R                  :X  a  [        R                  U
   nU	R                  X5        US-  nU	R                  U
5      c  M  [!        ["        U	R                  XS95      nUc   e SSS5        U R                  WXU5      $ ! , (       d  f       N!= f)z<Add a token; return True iff this is the end of the program.r   )r   FT)rI   N)rP   )classifyrS   rK   rp   releaser+   rM   rW   can_advanceeatr   COMMENTr   OPr   opmapr
   r^   )r7   r   rO   r   r/   r8   rp   counterrP   recordernext_token_valuenext_token_typer(   s                r   rL   Parser.addtoken   s    --W57|q    w<1HV>>&?? ZZ!U%NGw7Ht5$**+;<D((11 E8=		'8J5A"x'7'7&EEqLG"hkk1&-mm4D&EO""?E1 **+;<D  #x778H7VWF%%%/ "2 ~~fd7;;3 "!s   #CE> "E>>
Fr8   c                 x    U R                   S   u  pVnUu  pX   n
U
 H  u  pU R                  R                  U   S   nUS:  a:  U R                  R                  U   nUu  nnUU;   a  U R	                  XX5          OMa  X:X  d  Mh  U R                  X#X5        UnX   SU4/:X  aF  U R                  5         U R                   (       d    gU R                   S   u  pVnUu  pX   SU4/:X  a  MF  X l          g   SU4U
;   a/  U R                  5         U R                   (       d  [        SX#U5      eO[        SX#U5      eGM:  )NTr"   r      Fztoo much inputrR   )	r#   r   labelsrs   pushshiftpoprn   r@   )r7   r8   r   rO   r   r&   stater   statesfirstarcsinewstatetitsdfa	itsstatesitsfirsts                    r   rK   Parser._addtoken  sK   #zz"~CMF=D#LL''*1-8!\\..q1F*0'Ix)		!X? *
 [ JJtH>$E -QJ<7
#zz#'+/::b>(D(+ !-QJ<7 '+O 5  $: u:%HHJ::()94PP &
 %[$wGGU r!   c                    U[         R                  :X  Ga*  U R                  R                  U5        X R                  R
                  ;   a  U R                  R
                  U   /$ X R                  R                  ;   a  XR                  R                  ;   d   eU R                  S[         R                  [         R                  [         R                  [         R                  [         R                  4;  a  U R                  R                  U   /$ U R                  R                  U   U R                  R                  U   /$ U R                  R                  R                  U5      nUc  [        SXU5      eU/$ )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)r   NAMEru   rA   r   keywordssoft_keywordstokensrn   INDENTDEDENTNEWLINESEMICOLONgetr@   )r7   r   rO   r   r8   s        r   r{   Parser.classify?  s(   
 5::OO&-----e455,,444||22222 ??LLLLMMJJKK+  !LL//566LL''-LL..u5 
 $$((.>[$w??xr!   r   c                 *   U R                   (       a(  U R                  S   u  pVnXS[        4U R                  S'   gU R                  S   u  pVnXUS4n	[        U R                  U	5      n
US   c   eUS   R                  U
5        XSU4U R                  S'   g)zShift a token.  (Internal)r"   N)rE   r#   r%   r   r   append)r7   r   rO   r   r   r&   r   r(   r   rawnoderv   s              r   r   Parser.shiftd  s     JJrNMC!Z8DJJrN#zz"~C $Wd;GdllG4G8'''HOOG$!T2DJJrNr!   newdfac                 H   U R                   (       aJ  U R                  S   u  pVnXS[        4U R                  S'   U R                  R                  US[        45        gU R                  S   u  pVnUSU/ 4n	XSU4U R                  S'   U R                  R                  USU	45        g)zPush a nonterminal.  (Internal)r"   r   N)rE   r#   r%   r   )
r7   r   r   r   r   r&   r   r(   r   rv   s
             r   r   Parser.pushq  s     JJrNMC!Z8DJJrNJJvq*56#zz"~C $dGR8G!T2DJJrNJJvq'23r!   c                    U R                   (       a  U R                  R                  5         gU R                  R                  5       u  pn[        U R                  U5      nU R                  (       a/  U R                  S   u  pVnUS   c   eUS   R                  U5        gX@l        U R                  U R                  l        g)zPop a nonterminal.  (Internal)r"   N)rE   r#   r   r   r   r   rt   ru   )r7   popdfapopstatepopnoderv   r&   r   r   s           r   r   
Parser.pop}  s    JJNN(,

(8%FgdllG4Gzz#'::b> DBx+++R( '+/??(r!   )r   r   rE   rn   rp   rt   r#   ru   r1   )r   N)rY   rZ   r[   r\   rj   r   r   Convertr9   r^   rx   r`   r   ra   rL   rK   r]   r{   r   DFASr   r   rb   rc   r!   r   r.   r.      s   :&. &.(72C &.t &.P<   4.<S .< .<w .<4 .<`,H ,H3 ,Hs ,HW ,HQU ,H\#S # #w #49 #J3# 3c 3S 37 3t 3
4 
4d 
4c 
4G 
4PT 
4;r!   r.   N)+rj   collections.abcr   r   
contextlibr   typingr   r   r   r	   r
   blib2to3.pgen2.grammarr   blib2to3.pytreer   r   r   r   r   r    r   r   r   blib2to3.pgen2.driverr   dictr`   Resultsr   r]   tupler^   DFAr   r    r%   r)   r+   rh   r@   r.   rc   r!   r   <module>r      s   / % < < * E E ' &0 sBw-
GW%uT4Z'88
9
4c3h !S$sCx. !AW AG A A $
AdC()*A	%c7"
#$A? ?D I; I;r!   