B
    M§a  ã               @   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               @   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   2   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   ;   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   M   s    zStackNode.__init__c             C   s   | j jS )N)r   Ú	from_rule)r	   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(y
| j| S  tk
r&   Y nX |S )N)r   Zcontains_syntaxZreserved_syntax_stringsÚKeyError)Úgrammarr   r   r
   r
   r   Ú_token_to_transitionY   s    
r,   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 )N)Ú_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ƒ| _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é   )r1   Znonterminal_to_dfasr2   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   s:   | j rtdƒ‚n&|\}}}}t ||||¡}td|ƒ‚d S )Nz!Error Recovery is not implementedzSyntaxError: invalid syntax)r3   ÚNotImplementedErrorr   Z	ErrorLeafr   )r	   r>   r   r   r   Úprefixr   r
   r
   r   r4   ’   s
    
zBaseParser.error_recoveryc             C   s8   y| j | |ƒ}W n  tk
r2   |  ||¡}Y nX |S )N)r.   r*   Údefault_node)r	   r&   ZchildrenZnoder
   r
   r   r<   š   s
    zBaseParser.convert_nodec             C   s6   y| j | |||ƒS  tk
r0   |  |||¡S X d S )N)r/   r*   Údefault_leaf)r	   r   r   rB   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|
ƒ¡ q®W |  ||||¡}|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.
        r5   Nztoo much input)r1   r7   r,   r   r   r*   r   r;   r4   Ú
IndexErrorr   Znext_dfaZ
dfa_pushesÚappendr#   rE   r$   )r	   r>   r+   r7   r   r   r   rB   r   ZplanÚpushZleafr
   r
   r   r8   §   s(    

zBaseParser._add_tokenc             C   sL   | j  ¡ }t|jƒdkr$|jd }n|  |jj|j¡}| j d j |¡ d S )Nr6   r   r5   )r7   Úpopr:   r$   r<   r   r%   rG   )r	   r?   Znew_noder
   r
   r   r;   Ç   s
    
zBaseParser._popN)r0   F)r   r   r   r   r.   r   Ústrr   r   ZBaseNodeÚ__annotations__ZNoderC   r/   ZLeafrD   r   r@   r4   r<   rE   r8   r;   r
   r
   r
   r   r-   e   s   


 r-   N)r   Útypingr   r   Zparsor   Zparso.pgen2.generatorr   r   r   r   r!   r   r#   r,   r-   r
   r
   r
   r   Ú<module>   s   