
    ;e6                         d dl mZmZmZ  G d dej                  Z G d de          Z G d de          Z G d d	e          Z	ddZ
dS )   )grammartokentokenizec                       e Zd ZdS )PgenGrammarN)__name__
__module____qualname__     )/usr/lib/python3.11/lib2to3/pgen2/pgen.pyr   r      s        Dr   r   c                   ~    e Zd ZddZd Zd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zd Zd Zd ZddZd Zd ZdS )ParserGeneratorNc                 N   d }|t          |d          }|j        }|| _        || _        t	          j        |j                  | _        |                                  | 	                                \  | _
        | _        |
 |             i | _        |                                  d S )Nzutf-8)encoding)openclosefilenamestreamr   generate_tokensreadline	generatorgettokenparsedfasstartsymbolfirstaddfirstsets)selfr   r   close_streams       r   __init__zParserGenerator.__init__   s    >(W555F!<L !1&/BB&*jjll#	4##LNNN
r   c           	         t                      }t          | j                                                  }|                                 |                    | j                   |                    d| j                   |D ]-}dt          |j	                  z   }||j	        |<   ||j
        |<   .|D ]}| j        |         }g }|D ]}g }t          |j                                                  D ]C\  }	}
|                    |                     ||	          |                    |
          f           D|j        r*|                    d|                    |          f           |                    |           |j                            |           ||                     ||          f|j        |j	        |         <   |j	        | j                 |_        |S )N       )r   listr   keyssortremover   insertlensymbol2numbernumber2symbolsortedarcsitemsappend
make_labelindexisfinalstates
make_firststart)r   cnamesnameidfar4   stater.   labelnexts              r   make_grammarzParserGenerator.make_grammar   s   MMTY^^%%&&

T%&&&Q())) 	& 	&Dc!/***A$%AOD!!%AOA 	O 	OD)D/CF $ $#)%**:*:*<*<#=#= N NKE4KKE!:!:CIIdOO LMMMM= 7KKCIIe$4$4 5666d####HOOF###-3T__Q5M5M,NAF1?4())/$"23r   c                 ~    | j         |         }i }t          |          D ]}|                     ||          }d||<   |S Nr   )r   r-   r1   )r   r7   r9   rawfirstr   r=   ilabels          r   r5   zParserGenerator.make_first4   sM    :d#H%% 	 	E__Q..FE&MMr   c                 ,   t          |j                  }|d                                         r||j        v rI||j        v r|j        |         S |j                            |j        |         d f           ||j        |<   |S t          t          |d           }||j        v r|j        |         S |j                            |d f           ||j        |<   |S t          |          }|d                                         rH||j
        v r|j
        |         S |j                            t          j        |f           ||j
        |<   |S t          j        |         }||j        v r|j        |         S |j                            |d f           ||j        |<   |S Nr#   )r*   labelsisalphar+   symbol2labelr0   getattrr   tokensevalkeywordsNAMEr   opmap)r   r7   r=   rC   itokenvalues         r   r1   zParserGenerator.make_label=   s   QX8 )	"''AN**>%00HOOQ_U%;T$BCCC,2AN5)!M !t44 QX%%8F++HOOVTN333'-AHV$!M KKEQx!! "AJ&&:e,,HOOUZ$7888(.AJu%!M !u-QX%%8F++HOOVTN333'-AHV$!Mr   c                     t          | j                                                  }|                                 |D ] }|| j        vr|                     |           !d S N)r%   r   r&   r'   r   	calcfirst)r   r8   r9   s      r   r   zParserGenerator.addfirstsetsk   sa    TY^^%%&&

 	% 	%D4:%%t$$$	% 	%r   c                 .   | j         |         }d | j        |<   |d         }i }i }|j                                        D ]\  }}|| j         v rh|| j        v r"| j        |         }|t	          d|z            n"|                     |           | j        |         }|                    |           |||<   vd||<   |di||<   i }	|                                D ]4\  }}
|
D ],}||	v r!t	          d|d|d|d|	|                   ||	|<   -5|| j        |<   d S )Nr#   zrecursion for rule %rr   zrule z is ambiguous; z is in the first sets of z as well as )r   r   r.   r/   
ValueErrorrS   update)r   r9   r;   r<   totalsetoverlapcheckr=   r>   fsetinverseitsfirstsymbols               r   rS   zParserGenerator.calcfirsts   s   io
4A :++-- 	1 	1KE4	!!DJ&&:e,D|()@4)GHHH $ NN5))):e,D%%%&*U##"#',ajU##+1133 	( 	(OE8" ( (W$$$*&*ddFFFEEE76??&L M M M #(( $
4r   c                 t   i }d }| j         t          j        k    r| j         t          j        k    r)|                                  | j         t          j        k    )|                     t          j                  }|                     t          j        d           |                                 \  }}|                     t          j                   | 	                    ||          }t          |          }|                     |           t          |          }|||<   ||}| j         t          j        k    ||fS )N:)typer   	ENDMARKERNEWLINEr   expectrM   OP	parse_rhsmake_dfar*   simplify_dfa)	r   r   r   r9   azr;   oldlennewlens	            r   r   zParserGenerator.parse   s	   i5?**)u},, )u},, ;;uz**DKK#&&&>>##DAqKK&&&--1%%CXXFc"""XXFDJ""# i5?**$ [  r   c           
         fd}fdt           ||          |          g}|D ]}i }|j        D ]1}|j        D ]'\  }}	|  |	|                    |i                      (2t	          |                                          D ]R\  }}
|D ]}|j        |
k    r n&t          |
|          }|                    |           |                    ||           S|S )Nc                 $    i } | |           |S rR   r   )r<   base
addclosures     r   closurez)ParserGenerator.make_dfa.<locals>.closure   s    DJud###Kr   c                 T    | |v rd S d|| <   | j         D ]\  }}| ||           d S rA   r.   )r<   rm   r=   r>   rn   s       r   rn   z,ParserGenerator.make_dfa.<locals>.addclosure   sQ    }}DK$z + +t=JtT***+ +r   )DFAStatenfasetr.   
setdefaultr-   r/   r0   addarc)r   r6   finishro   r4   r<   r.   nfastater=   r>   rs   strn   s               @r   re   zParserGenerator.make_dfa   sM   	 	 	 	 		+ 	+ 	+ 	+ 	+ 775>>6223 	( 	(ED!L E E#+= E EKE4("
4)C)CDDDE "(

!5!5 ( (v  & &ByF** + "&&11BMM"%%%R''''( r   c                 l   t          d|           |g}t          |          D ]\  }}t          d|||u rdpd           |j        D ]l\  }}||v r|                    |          }	n$t	          |          }	|                    |           |t          d|	z             Xt          d||	fz             md S )NzDump of NFA for  State(final) z	    -> %d    %s -> %d)print	enumerater.   r2   r*   r0   )
r   r9   r6   rv   todor:   r<   r=   r>   js
             r   dump_nfazParserGenerator.dump_nfa   s    &&&w!$ 	7 	7HAu)Q =I CDDD$z 	7 	7t4<<

4((AAD		AKK%%%=+/****.E1:56666	7	7 	7r   c           	      *   t          d|           t          |          D ]r\  }}t          d||j        rdpd           t          |j                                                  D ],\  }}t          d||                    |          fz             -sd S )NzDump of DFA forrz   r{   r|   r}   )r~   r   r3   r-   r.   r/   r2   )r   r9   r;   r:   r<   r=   r>   s          r   dump_dfazParserGenerator.dump_dfa   s    &&&!# 	A 	AHAu)Q ;) ArBBB%ej&6&6&8&899 A Atnsyy'??@@@@A	A 	Ar   c                     d}|rnd}t          |          D ]X\  }}t          |dz   t          |                    D ]2}||         }||k    r"||= |D ]}|                    ||           d} n3Y|ld S d S )NTFr   )r   ranger*   
unifystate)r   r;   changesr:   state_ir   state_jr<   s           r   rf   zParserGenerator.simplify_dfa   s      	G'nn 	 	
7qsCHH--  A!!fG'))F%( ? ?E!,,Wg>>>>"& *  	 	 	 	 	r   c                    |                                  \  }}| j        dk    r||fS t                      }t                      }|                    |           |                    |           | j        dk    r`|                                  |                                  \  }}|                    |           |                    |           | j        dk    `||fS )N|)	parse_altrP   NFAStateru   r   )r   rg   rh   aazzs        r   rd   zParserGenerator.parse_rhs   s    ~~1:a4KBBIIaLLLHHRLLL*##~~''1		!	 *##
 r6Mr   c                 4   |                                  \  }}| j        dv s| j        t          j        t          j        fv rV|                                  \  }}|                    |           |}| j        dv 7| j        t          j        t          j        fv V||fS )N)([)
parse_itemrP   r_   r   rM   STRINGru   )r   rg   br7   ds        r   r   zParserGenerator.parse_alt
  s      1zZ''yUZ666??$$DAqHHQKKKA	 zZ''yUZ666 !tr   c                    | j         dk    rd|                                  |                                 \  }}|                     t          j        d           |                    |           ||fS |                                 \  }}| j         }|dvr||fS |                                  |                    |           |dk    r||fS ||fS )Nr   ])+*r   )rP   r   rd   rb   r   rc   ru   
parse_atom)r   rg   rh   rP   s       r   r   zParserGenerator.parse_item  s    :MMOOO>>##DAqKK#&&&HHQKKKa4K??$$DAqJEJ&&!tMMOOOHHQKKK||!t!tr   c                    | j         dk    rO|                                  |                                 \  }}|                     t          j        d           ||fS | j        t          j        t          j        fv rOt                      }t                      }|
                    || j                    |                                  ||fS |                     d| j        | j                    d S )Nr   )z+expected (...) or NAME or STRING, got %s/%s)rP   r   rd   rb   r   rc   r_   rM   r   r   ru   raise_error)r   rg   rh   s      r   r   zParserGenerator.parse_atom(  s    :MMOOO>>##DAqKK#&&&a4KY5:u|444

A

AHHQ
###MMOOOa4KJ!Y
4 4 4 4 4r   c                     | j         |k    s|.| j        |k    r#|                     d||| j         | j                   | j        }|                                  |S )Nzexpected %s/%s, got %s/%s)r_   rP   r   r   )r   r_   rP   s      r   rb   zParserGenerator.expect9  sd    9!2tzU7J7J8!5$)TZA A A
r   c                    t          | j                  }|d         t          j        t          j        fv r4t          | j                  }|d         t          j        t          j        fv 4|\  | _        | _        | _        | _        | _	        d S rE   )
r>   r   r   COMMENTNLr_   rP   beginendline)r   tups     r   r   zParserGenerator.gettokenA  sr    4>""!f)8;777t~&&C !f)8;777AD>	4:tz48TYYYr   c           
          |rG	 ||z  }n@#  d                     |gt          t          t          |                    z             }Y nxY wt	          || j        | j        d         | j        d         | j        f          )N r#   r   )joinr%   mapstrSyntaxErrorr   r   r   )r   msgargss      r   r   zParserGenerator.raise_errorH  s     	==Dj=hhutCTNN';';;<<#tx{ $TY 8 9 9 	9s	   
 ;ArR   )r   r	   r
   r!   r?   r5   r1   r   rS   r   re   r   r   rf   rd   r   r   r   rb   r   r   r   r   r   r   r   
   s4            2  ," ," ,"\% % %$ $ $<! ! !0" " "H7 7 7 A A A  *  "    (4 4 4"   E E E9 9 9 9 9r   r   c                       e Zd Zd ZddZdS )r   c                     g | _         d S rR   rq   )r   s    r   r!   zNFAState.__init__S  s    			r   Nc                 >    | j                             ||f           d S rR   )r.   r0   r   r>   r=   s      r   ru   zNFAState.addarcV  s$     		%'''''r   rR   )r   r	   r
   r!   ru   r   r   r   r   r   Q  s7          ( ( ( ( ( (r   r   c                   *    e Zd Zd Zd Zd Zd ZdZdS )rr   c                 4    || _         ||v | _        i | _        d S rR   )rs   r3   r.   )r   rs   finals      r   r!   zDFAState.__init__]  s!     			r   c                     || j         |<   d S rR   rq   r   s      r   ru   zDFAState.addarce  s      	%r   c                 `    | j                                         D ]\  }}||u r
|| j         |<   d S rR   )r.   r/   )r   oldnewr=   r>   s        r   r   zDFAState.unifystatek  sA    9??,, 	' 	'KE4s{{#&	% 	' 	'r   c                     | j         |j         k    rdS t          | j                  t          |j                  k    rdS | j                                        D ]$\  }}||j                            |          ur dS %dS )NFT)r3   r*   r.   r/   get)r   otherr=   r>   s       r   __eq__zDFAState.__eq__p  s     <5=((5 ty>>S__,,59??,, 	 	KE45:>>%0000uu 1tr   N)r   r	   r
   r!   ru   r   r   __hash__r   r   r   rr   rr   [  sQ               ' ' '
   HHHr   rr   Grammar.txtc                 H    t          |           }|                                S rR   )r   r?   )r   ps     r   generate_grammarr     s    !!A>>r   N)r   )r|   r   r   r   Grammarr   objectr   r   rr   r   r   r   r   <module>r      s   
 ' & & & & & & & & &	 	 	 	 	'/ 	 	 	E9 E9 E9 E9 E9f E9 E9 E9N
( ( ( ( (v ( ( (# # # # #v # # #J     r   