
    ^h8                         S 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  \" S5      r " S S\\   5      r " S S	5      r " S
 S\\   5      r " S S5      rS rS rS rS rS\S\4S jrS rS rS rg)a  
This module defines the data structures used to represent a grammar.

Specifying grammars in pgen is possible with this grammar::

    grammar: (NEWLINE | rule)* ENDMARKER
    rule: NAME ':' rhs NEWLINE
    rhs: items ('|' items)*
    items: item+
    item: '[' rhs ']' | atom ['+' | '*']
    atom: '(' rhs ')' | NAME | STRING

This grammar is self-referencing.

This parser generator (pgen2) was created by Guido Rossum and used for lib2to3.
Most of the code has been refactored to make it more Pythonic. Since this was a
"copy" of the CPython Parser parser "pgen", there was some work needed to make
it more readable. It should also be slightly faster than the original pgen2,
because we made some optimizations.
    )literal_eval)TypeVarGenericMappingSequenceSetUnion)GrammarParserNFAState_TokenTypeTc                   H    \ rS rSrSrS\S\\\S   4   S\\S4   4S jrS	r	g
)Grammar%   z
Once initialized, this class supplies the grammar tables for the
parsing engine implemented by parse.py.  The parsing engine
accesses the instance variables directly.

The only important part in this parsers are dfas and transitions between
dfas.
start_nonterminalrule_to_dfaszDFAState[_TokenTypeT]reserved_syntax_stringsReservedStringc                 (    X l         X0l        Xl        g Nnonterminal_to_dfasr   r   )selfr   r   r   s       O/home/james-whalen/.local/lib/python3.13/site-packages/parso/pgen2/generator.py__init__Grammar.__init__/   s     $0 '>$!2    r   N)
__name__
__module____qualname____firstlineno____doc__strr   r   r   __static_attributes__ r   r   r   r   %   sA    3$'3&sH5L,M'MN3 +2#7G2G*H3r   r   c                   <    \ rS rSrSr/ 4SSS\S   4S jjrS rSrg	)
DFAPlan8   z^
Plans are used for the parser to create stack nodes and do the proper
DFA state transitions.
next_dfaDFAState
dfa_pushesc                     Xl         X l        g r   )r(   r*   )r   r(   r*   s      r   r   DFAPlan.__init__=   s     $r   c                 n    U R                   R                  < SU R                  < SU R                  < S3$ )N(z, ))	__class__r   r(   r*   r   s    r   __repr__DFAPlan.__repr__A   s!    #~~66tWWr   )r*   r(   N)	r   r   r   r    r!   r   r   r2   r#   r$   r   r   r&   r&   8   s,     QS % %*9M %Xr   r&   c                   L    \ rS rSrSrS\S\\   S\4S jrS r	S r
S	 rS
 rSrg)r)   E   aE  
The DFAState object is the core class for pretty much anything. DFAState
are the vertices of an ordered graph while arcs and transitions are the
edges.

Arcs are the initial edges, where most DFAStates are not connected and
transitions are then calculated to connect the DFA state machines that have
different nonterminals.
	from_rulenfa_setfinalc                    [        U[        5      (       d   e[        [        [        U5      5      [        5      (       d   e[        U[        5      (       d   eXl        X l        0 U l        0 U l        0 U l	        X2;   U l
        g r   )
isinstancesetnextiterr   r6   r7   arcsnonterminal_arcstransitionsis_final)r   r6   r7   r8   s       r   r   DFAState.__init__O   su    '3''''$tG}-x8888%****",.	 9; RT(r   c                     [        U[        5      (       d   eX R                  ;  d   e[        U[        5      (       d   eXR                  U'   g r   )r:   r"   r>   r)   )r   next_labels      r   add_arcDFAState.add_arc`   sD    %%%%%II%%%%**** 		%r   c                 r    U R                   R                  5        H  u  p4XAL d  M  X R                   U'   M     g r   )r>   items)r   oldnewrE   rD   s        r   
unifystateDFAState.unifystatef   s*     IIOO-LE|#&		%  .r   c                 F   [        U[        5      (       d   eU R                  UR                  :w  a  g[        U R                  5      [        UR                  5      :w  a  gU R                  R                  5        H$  u  p#X1R                  R                  U5      Ld  M$    g   g)NFT)r:   r)   rA   lenr>   rI   get)r   otherrE   rD   s       r   __eq__DFAState.__eq__k   sw    %****==ENN* tyy>S_, IIOO-LEJJNN511 . r   c                 p    SU R                   R                  < SU R                  < SU R                  < S3$ )N<z: z
 is_final=>)r0   r   r6   rA   r1   s    r   r2   DFAState.__repr__y   s%    NN##T^^T]]
 	
r   )r>   r6   rA   r7   r?   r@   N)r   r   r   r    r!   r"   r   r   r   rF   rL   rR   r2   r#   r$   r   r   r)   r)   E   s9    )# )H )h )"!'

r   r)   c                   ,    \ rS rSrSrS\4S jrS rSrg)r      z
Most grammars will have certain keywords and operators that are mentioned
in the grammar as strings (e.g. "if") and not token types (e.g. NUMBER).
This class basically is the former.
valuec                     Xl         g r   rZ   )r   rZ   s     r   r   ReservedString.__init__   s    
r   c                 R    U R                   R                  < SU R                  < S3$ )Nr.   r/   )r0   r   rZ   r1   s    r   r2   ReservedString.__repr__   s    >>22DJJ??r   r\   N)	r   r   r   r    r!   r"   r   r2   r#   r$   r   r   r   r      s    c @r   r   c                     SnU(       ai  Sn[        U 5       HN  u  p#[        US-   [        U 5      5       H-  nX   nX5:X  d  M  X	 U  H  nUR                  XS5        M     Sn  ML     MP     U(       a  Mh  gg)z
This is not theoretically optimal, but works well enough.
Algorithm: repeatedly look for two states that have the same
set of arcs (same labels pointing to the same nodes) and
unify them, until things stop changing.

dfas is a list of DFAState instances
TF   N)	enumeraterangerO   rL   )dfaschangesistate_ijstate_jstates          r   _simplify_dfasrk      ss     G
#D/JA1q5#d),'%!%((: "&"G - * 'r   c                   ^ [        U [        5      (       d   e[        U[        5      (       d   eU4S jm[        5       nT" X5        [        U R                  X!5      /nU H  n0 nUR
                   H\  nUR                   HI  nUR                  c  M  UR                  UR                  [        5       5      nT" UR                  U5        MK     M^     UR                  5        HX  u  pU H  n
U
R
                  U:X  d  M    O)   [        U R                  X5      n
UR                  U
5        UR                  X5        MZ     M     U$ )z
Uses the powerset construction algorithm to create DFA states from sets of
NFA states.

Also does state reduction if some states are not needed.
c                    > [        U [        5      (       d   eX;   a  g UR                  U 5        U R                   H%  nUR                  b  M  T" UR
                  U5        M'     g r   )r:   r   addr>   nonterminal_or_stringr<   )	nfa_statebase_nfa_setnfa_arc
addclosures      r   rs   _make_dfas.<locals>.addclosure   sU    )X....$# ~~G,,47<<6 &r   )r:   r   r;   r)   r6   r7   r>   ro   
setdefaultr<   rI   appendrF   )startfinishrq   statesrj   r>   rp   rr   r7   ro   nested_staters   s              @r   
_make_dfasr{      s    eX&&&&fh''''7 5Lu#u=>FI$>>00<"oog.K.KSUSGw||W5 * ' /3jjl*! &''72 !'
  (Il+MM,> /; , Mr   c                    [        SU R                  5        U /n[        U5       H  u  p4[        SX4UL =(       a    S=(       d    S5        UR                   Ho  nUR                  UR
                  pvXr;   a  UR                  U5      nO[        U5      nUR                  U5        Uc  [        SU-  5        M`  [        SXh4-  5        Mq     M     g )NzDump of NFA for  State(final) z	    -> %d    %s -> %d)	printr6   rb   r>   ro   r<   indexrO   rv   )	rw   rx   todorf   rj   arcrE   rD   rh   s	            r   	_dump_nfar      s    	
U__-7DdOiVO9	?R@::C44chh5}JJu%IE"}kAo&nz12  $r   c           	      0   [        SU S   R                  5        [        U 5       Hn  u  p[        SXR                  =(       a    S=(       d    S5        UR                  R                  5        H#  u  p4[        SX0R                  U5      4-  5        M%     Mp     g )NzDump of DFA forr   r}   r~   r   r   )r   r6   rb   rA   r>   rI   r   )rd   rf   rj   nonterminalrD   s        r   
_dump_dfasr      sr    	
T!W../dOiNN8y>B?"'**"2"2"4K.KE1B#CCD #5 $r   bnf_grammarreturnc                    0 nSn[        U 5      R                  5        H:  u  pE[        XE5      n[        U5        XbUR                  '   Ub  M.  UR                  nM<     0 nUR                  5        Hk  u  pU H`  n	U	R                  R                  5        H?  u  pX;   a  XR                  U
'   M  [        UUU
5      n[        U5      U	R                  U'   MA     Mb     Mm     [        U5        [        X2U5      $ )z
``bnf_text`` is a grammar in extended BNF (using * for repetition, + for
at-least-once repetition, [] for optional parts, | for alternatives and ()
for grouping).

It's not EBNF according to ISO/IEC 14977. It's a dialect Python uses in its
own parser.
N)r
   parser{   rk   r6   rI   r>   r?   _make_transitionr&   r@   _calculate_tree_traversalr   )r   token_namespacer   r   nfa_anfa_zrd   reserved_stringsr   	dfa_stateterminal_or_nonterminalr(   
transitions                r   generate_grammarr      s     L%k288:%' 	t(,U__% $ % ; 68)//1I5>^^5I5I5K1'*:JR../FG!1'(/"J
 9@8II))*5 6L  2 l+$4DEEr   c                 &   US   R                  5       (       a  [        X5      $ US   S;   d   U5       eUR                  S5      (       d  UR                  S5      (       a   e[        U5      n X   $ ! [         a    [        U5      =oAU'   Us $ f = f)z
Creates a reserved string ("if", "for", "*", ...) or returns the token type
(NUMBER, STRING, ...) for a given grammar terminal.
r   )"'z"""z''')isalphagetattr
startswithr   KeyErrorr   )r   r   rE   rZ   rs        r   r   r     s    
 Qx.. Qx:%,u,%##E**53C3CE3J3JJJU#	*11 	1?1FFA.H	s   .A2 2BBc                    0 n[        U R                  5       5      nUR                  5         U H  nX1;  d  M
  [        XU5        M     U R	                  5        H  nU H  nUR
                  nUR                  R                  5        H  u  p7X   R                  5        H  u  pX;   a  Xh   n
[        U
R                  (       a  U
R                  S   R                  OU
R                  R                  U	(       a  U	S   R                  OUR                  /5      n[        SUR                  U4[        U5      -   -  5      e[        Xy5      Xh'   M     M     M     M     g)z
By this point we know how dfas can move around within a stack node, but we
don't know how we can add a new stack node (nonterminal transitions).
r   zZRule %s is ambiguous; given a %s token, we can't determine if we should evaluate %s or %s.N)listkeyssort_calculate_first_plansvaluesr@   r?   rI   sortedr*   r6   r(   
ValueErrortupler&   )r   first_plansnonterminalsr   rd   r   r@   r(   r   pushes	prev_planchoicess               r   r   r   .  s[    K+0023L#)"#6[Q $ $**,I#//K)2)C)C)I)I)K%*5*B*H*H*J&J!0$/$;	 #) $-#7#7 !* 4 4Q 7 A A%.%7%7%A%A $* !'q	 3 3/7/A/A
* 
# )N %.$7$7$.!" %*'N!3	 	 /6h.GK+5 +K *L  -r   c                 ~   X   n0 nSX'   US   nUR                   R                  5        H  u  pgUR                  /XF'   M     UR                  R                  5        H=  u  p X   n	U	c  [	        SU-  5      e U	R                  5        H  u  pU/U-   XJ'   M     M?     XAU'   U$ ! [
         a    [        XU5      n	 NCf = f)z
Calculates the first plan in the first_plans dictionary for every given
nonterminal. This is going to be used to know when to create stack nodes.
Nr   zleft recursion for rule %r)r@   rI   r(   r?   r   r   r   )r   r   r   rd   new_first_plansrj   r   rD   nonterminal2first_plans2tr   s               r   r   r   `  s    
 +DO#K GE"..446
',~~&6# 7  %55;;=	M&4L # !=!KLL $ &++-IA"'6!1O .  >  /  	b12ET`aL	bs   "B##B<;B<N)r!   astr   typingr   r   r   r   r   r	   parso.pgen2.grammar_parserr
   r   r   r   r&   r)   r   rk   r{   r   r   r"   r   r   r   r   r$   r   r   <module>r      s   *  B B >m$3gk" 3&
X 
X7
w{# 7
t@ @./d3$E'F# 'F7 'FT(/Hdr   