B
    t\                 @   sx   d 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e
Zdd ZG dd de
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``).
    )tree)ReservedStringc               @   s   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   +lib/python3.7/site-packages/parso/parser.py__init__$   s    zParserSyntaxError.__init__N)__name__
__module____qualname____doc__r
   r   r   r   r	   r      s   r   c               @   s   e Zd ZdZdd ZdS )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
   0   s    zInternalParseError.__init__N)r   r   r   r   r
   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   sL   xFt  D ]:} x*| jjD ]}t|tr0|jV  q|V  qW | jjs
P q
W d S )N)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   9   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 )N)r   nodes)r   r   r   r   r	   r
   K   s    zStackNode.__init__c             C   s   | j jS )N)r   	from_rule)r   r   r   r	   nonterminalO   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__S   s    zStackNode.__repr__N)r   r   r   r
   propertyr$   r&   r   r   r   r	   r!   J   s   r!   c             C   s*   |j r&y
| j| S  tk
r$   Y nX |S )N)Zcontains_syntaxZreserved_syntax_stringsKeyError)grammarr   r   r   r   r	   _token_to_transitionW   s    
r*   c               @   s^   e Zd ZdZi ZejZi Z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.
    
file_inputFc             C   s   || _ || _|| _d S )N)_pgen_grammar_start_nonterminal_error_recovery)r   Zpgen_grammarZstart_nonterminalerror_recoveryr   r   r	   r
   v   s    zBaseParser.__init__c             C   s   | j j| j d }tt|g| _x|D ]}| | q(W xR| jd }|jjsbt	d|j
|j|jt| jdkrz|   q<| |j|jS q<W d S )Nr   zincomplete input   )r-   Znonterminal_to_dfasr.   r   r!   stack
_add_tokenr   r   r   r   r   r   len_popconvert_noder$   r"   )r   tokensZ	first_dfatokentosr   r   r	   parse{   s    


zBaseParser.parsec             C   s:   | j rtdn&|\}}}}t||||}td|d S )Nz!Error Recovery is not implementedzSyntaxError: invalid syntax)r/   NotImplementedErrorr   Z	ErrorLeafr   )r   r9   r   r   r   prefixr   r   r   r	   r0      s
    
zBaseParser.error_recoveryc             C   sL   y| j | |}W n  tk
r2   | ||}Y nX x|D ]
}||_q:W |S )N)node_mapr(   default_nodeparent)r   r$   ZchildrenZnodecr   r   r	   r7      s    

zBaseParser.convert_nodec             C   s6   y| j | |||S  tk
r0   | |||S X d S )N)leaf_mapr(   default_leaf)r   r   r   r=   r   r   r   r	   convert_leaf   s    zBaseParser.convert_leafc             C   s   | j }| j}|\}}}}t|||}xty|d jj| }	P W q& tk
rt   |d jjrb|   n| | dS Y q& t	k
r   t
d|||Y q&X q&W |	j|d _x|	jD ]}
|t|
 qW | ||||}|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.
        r1   Nztoo much input)r-   r3   r*   r   r   r(   r   r6   r0   
IndexErrorr   Znext_dfaZ
dfa_pushesappendr!   rD   r"   )r   r9   r)   r3   r   r   r   r=   r   ZplanpushZleafr   r   r	   r4      s(    

zBaseParser._add_tokenc             C   sL   | j  }t|jdkr$|jd }n| |jj|j}| j d j| d S )Nr2   r   r1   )r3   popr5   r"   r7   r   r#   rF   )r   r:   Znew_noder   r   r	   r6      s
    
zBaseParser._popN)r,   F)r   r   r   r   r>   r   ZNoder?   rB   ZLeafrC   r
   r;   r0   r7   rD   r4   r6   r   r   r   r	   r+   c   s   

	 r+   N)r   Zparsor   Zparso.pgen2.generatorr   r   r   r   r   r   objectr!   r*   r+   r   r   r   r	   <module>   s   