o
    Zh                     @   s   d Z ddlmZmZ ddlmZ ddlmZ G dd deZ	G dd deZ
G d	d
 d
eZG dd dZdd ZG dd dZdS )a  
The ``Parser`` tries to convert the available Python code in an easy to read
format, something like an abstract syntax tree. The classes who represent this
tree, are sitting in the :mod:`parso.tree` module.

The Python module ``tokenize`` is a very important part in the ``Parser``,
because it splits the code into different words (tokens).  Sometimes it looks a
bit messy. Sorry for that! You might ask now: "Why didn't you use the ``ast``
module for this? Well, ``ast`` does a very good job understanding proper Python
code, but fails to work as soon as there's a single line of broken code.

There's one important optimization that needs to be known: Statements are not
being parsed completely. ``Statement`` is just a representation of the tokens
within the statement. This lowers memory usage and cpu time and reduces the
complexity of the ``Parser`` (there's another parser sitting inside
``Statement``, which produces ``Array`` and ``Call``).
    )DictType)tree)ReservedStringc                   @      e Zd ZdZdd ZdS )ParserSyntaxErrorz_
    Contains error information about the parser tree.

    May be raised as an exception.
    c                 C   s   || _ || _d S N)message
error_leaf)selfr	   r
    r   C/var/www/html/lang_env/lib/python3.10/site-packages/parso/parser.py__init__&      
zParserSyntaxError.__init__N__name__
__module____qualname____doc__r   r   r   r   r   r       s    r   c                   @   r   )InternalParseErrorz
    Exception to signal the parser is stuck and error recovery didn't help.
    Basically this shouldn't happen. It's a sign that something is really
    wrong.
    c                 C   s6   t | d||j||f  || _t| _|| _|| _d S )Nz#%s: type=%r, value=%r, start_pos=%r)	Exceptionr   namemsgtypevalue	start_pos)r   r   type_r   r   r   r   r   r   2   s   
zInternalParseError.__init__Nr   r   r   r   r   r   +   s    r   c                   @   s   e Zd Zdd ZdS )Stackc                    s    fdd}t | S )Nc                  3   sJ    t  D ]} | jjD ]}t|tr|jV  q|V  q| jjs" d S qd S r   )reverseddfatransitions
isinstancer   r   is_final)Z
stack_node
transitionr   r   r   iterate=   s   

z@Stack._allowed_transition_names_and_token_types.<locals>.iterate)list)r   r%   r   r$   r   )_allowed_transition_names_and_token_types<   s   
z/Stack._allowed_transition_names_and_token_typesN)r   r   r   r'   r   r   r   r   r   ;   s    r   c                   @   s(   e Zd Zdd Zedd Zdd ZdS )	StackNodec                 C   s   || _ g | _d S r   )r   nodes)r   r   r   r   r   r   M   r   zStackNode.__init__c                 C   s   | j jS r   )r   	from_ruler$   r   r   r   nonterminalQ   s   zStackNode.nonterminalc                 C   s   d| j j| j| jf S )Nz
%s(%s, %s))	__class__r   r   r)   r$   r   r   r   __repr__U   s   zStackNode.__repr__N)r   r   r   r   propertyr+   r-   r   r   r   r   r(   L   s
    
r(   c                 C   s.   |j jrz| j| W S  ty   Y |S w |S r   )r   Zcontains_syntaxZreserved_syntax_stringsKeyError)grammarr   r   r   r   r   _token_to_transitionY   s   r1   c                   @   s   e Zd ZU dZi Zeeeej	 f e
d< ejZi Zeeeej f e
d< ejZdddZdd	 Zd
d Zdd Zdd Zdd Zdd ZdS )
BaseParseraI  Parser engine.

    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 python/tokenize.py for how to get input tokens by a string.

    When a syntax error occurs, error_recovery() is called.
    node_mapleaf_map
file_inputFc                 C   s   || _ || _|| _d S r   )_pgen_grammar_start_nonterminal_error_recovery)r   Zpgen_grammarZstart_nonterminalerror_recoveryr   r   r   r   w   s   
zBaseParser.__init__c                 C   s   | j j| j d }tt|g| _|D ]}| | q	 | jd }|jjs/t	d|j
|j|jt| jdkr;|   n| |j|jS q)Nr   Tzincomplete input   )r6   Znonterminal_to_dfasr7   r   r(   stack
_add_tokenr   r"   r   r   stringr   len_popconvert_noder+   r)   )r   tokensZ	first_dfatokentosr   r   r   parse|   s   

zBaseParser.parsec                 C   s4   | j rtd|\}}}}t||||}td|)Nz!Error Recovery is not implementedzSyntaxError: invalid syntax)r8   NotImplementedErrorr   Z	ErrorLeafr   )r   rC   r   r   r   prefixr
   r   r   r   r9      s
   
zBaseParser.error_recoveryc                 C   s6   z
| j | |}W |S  ty   | ||}Y |S w r   )r3   r/   default_node)r   r+   childrennoder   r   r   rA      s   zBaseParser.convert_nodec                 C   s6   z
| j | |||W S  ty   | ||| Y S w r   )r4   r/   default_leaf)r   r   r   rG   r   r   r   r   convert_leaf   s
   zBaseParser.convert_leafc                 C   s   | j }| j}|\}}}}t|||}	 z
|d jj| }	W n* ty9   |d jjr/|   n| | Y dS Y n t	yF   t
d|||w q|	j|d _|	jD ]	}
|t|
 qQ| ||||}|d j| dS )z
        This is the only core function for parsing. Here happens basically
        everything. Everything is well prepared by the parser generator and we
        only apply the necessary steps here.
        Tr:   Nztoo much input)r6   r<   r1   r   r    r/   r"   r@   r9   
IndexErrorr   Znext_dfaZ
dfa_pushesappendr(   rL   r)   )r   rC   r0   r<   r   r   r   rG   r#   Zplanpushleafr   r   r   r=      s.   


zBaseParser._add_tokenc                 C   sL   | j  }t|jdkr|jd }n	| |jj|j}| j d j| d S )Nr;   r   r:   )r<   popr?   r)   rA   r   r*   rN   )r   rD   new_noder   r   r   r@      s
   
zBaseParser._popN)r5   F)r   r   r   r   r3   r   strr   r   ZBaseNode__annotations__NoderH   r4   ZLeafrK   r   rE   r9   rA   rL   r=   r@   r   r   r   r   r2   e   s   
 
 r2   N)r   typingr   r   Zparsor   Zparso.pgen2.generatorr   r   r   r   r&   r   r(   r1   r2   r   r   r   r   <module>   s   