B
    t\!                 @   sJ   d dl mZ d dlmZ d dlmZ ejZejZejZG dd deZ	dS )    )tree)PythonTokenTypes)
BaseParserc                   s  e Zd ZdZejejejejej	ej
ejejejejejejejejejejejejejejejejejejejejejejejdZejZejejejejej ej!ej"ej#ej$ej%ej&ej'ej(ej)iZ*d fdd	Z+ fddZ,d	d
 Z-dd Z. fddZ/dd Z0dd Z1  Z2S )Parserz
    This class is used to parse a Python file, it then divides them into a
    class structure of different scopes.

    :param pgen_grammar: The grammar object of pgen2. Loaded by load_grammar.
    )Z	expr_stmtZclassdefZfuncdef
file_inputZimport_nameZimport_fromZ
break_stmtZcontinue_stmtZreturn_stmtZ
raise_stmtZ
yield_exprZdel_stmtZ	pass_stmtZglobal_stmtZnonlocal_stmtZ
print_stmtZassert_stmtZif_stmtZ	with_stmtZfor_stmtZ
while_stmtZtry_stmtZcomp_forZlist_forZgen_forZ	decoratorZlambdefZold_lambdefZlambdef_nocondTr   c                s,   t t| j|||d g | _g | _d| _d S )N)error_recoveryr   )superr   __init__Zsyntax_errors_omit_dedent_list_indent_counter)selfZpgen_grammarr   Zstart_nonterminal)	__class__ 2lib/python3.7/site-packages/parso/python/parser.pyr	   C   s
    zParser.__init__c                s.   | j r| jdkrt| |}tt| |S )Nr   )_error_recovery_start_nonterminalNotImplementedError_recovery_tokenizer   r   parse)r   tokens)r   r   r   r   K   s
    

zParser.parsec             C   s   y| j | |}W nZ tk
rl   |dkrB|d g|dd  }n|dkrPd}n|dkr\d}| ||}Y nX x|D ]
}||_qtW |S )	a  
        Convert raw node information to a PythonBaseNode instance.

        This is passed to the parser driver which calls it whenever a reduction of a
        grammar rule produces a new complete node, so that the tree is build
        strictly bottom-up.
        suiter      Zlist_ifZcomp_ifZ	listmakerZtestlist_comp)node_mapKeyErrordefault_nodeparent)r   nonterminalZchildrennodecr   r   r   convert_nodeT   s    

zParser.convert_nodec             C   sH   |t kr0|| jjkr"t|||S t|||S | j|tj|||S )N)	NAMEZ_pgen_grammarZreserved_syntax_stringsr   ZKeywordName	_leaf_mapgetOperator)r   typevalueprefix	start_posr   r   r   convert_leafp   s
    zParser.convert_leafc                s  | j d j}|r|d  }nd }| jdkr|jtjksV|jtkrd|jkrd|jkr| j d j	j
dkry| j d j	jtj }W n tk
r   Y n,X |jjr|js|j| j d _	| | d S | jstt| |S dd }|| j }| |d r| | nH|\}}}	}
|tkr(| j| j t|j||	|
}| j d j| | j d }|jd	kry|j	jd
 |_	W n tk
r   Y nX d S )Nr   r   
Zsimple_stmtc             S   sJ   xDt tt| D ]0\}}|jdkr(P q|jdkrt|jdkrP qW |S )Nr   r      )reversedlist	enumerater   lennodes)stackuntil_index
stack_noder   r   r   current_suite   s    

z,Parser.error_recovery.<locals>.current_suiter-   r   Zstmt) r3   r2   Zget_last_leafr   r&   r   	ENDMARKERDEDENTr'   ZdfaZ	from_ruleZtransitionsNEWLINEr   Znext_dfaZis_finalZ
dfa_pushesZ
_add_tokenr   r   r   r   _stack_removalINDENTr
   appendr   r   ZPythonErrorLeafnamer   Zarcs)r   tokenZ	tos_nodesZ	last_leafZplanr6   r4   typr'   r)   r(   Z
error_leafZtos)r   r   r   r   z   sD    





zParser.error_recoveryc             C   sf   dd | j |d  D }|rPt|}x|D ]
}||_q,W | j |d  j| g | j |d < t|S )Nc             S   s   g | ]}|j D ]}|qqS r   )r2   ).0r5   r   r   r   r   
<listcomp>   s    z)Parser._stack_removal.<locals>.<listcomp>r-   )r3   r   ZPythonErrorNoder   r2   r<   bool)r   Zstart_indexZ	all_nodesr   nr   r   r   r:      s    


zParser._stack_removalc             c   sp   xj|D ]b}|d }|t krL| j}|r<|d | jkr<|  q|  jd8  _n|tkrb|  jd7  _|V  qW d S )Nr   r   r-   )r8   r
   r   popr;   )r   r   r>   r?   or   r   r   r      s    
zParser._recovery_tokenize)Tr   )3__name__
__module____qualname____doc__r   ZExprStmtZClassZFunctionZModuleZ
ImportNameZ
ImportFromZKeywordStatementZ
ReturnStmtZ	YieldExprZ
GlobalStmtZ
AssertStmtZIfStmtZWithStmtZForStmtZ	WhileStmtZTryStmtZCompForZ	DecoratorZLambdar   Z
PythonNoder   r   STRINGStringNUMBERNumberr9   ZNewliner7   Z	EndMarkerZFSTRING_STRINGZFStringStringZFSTRING_STARTZFStringStartZFSTRING_ENDZ
FStringEndr#   r	   r   r    r*   r   r:   r   __classcell__r   r   )r   r   r      sZ   
	
Fr   N)
Zparso.pythonr   Zparso.python.tokenr   Zparso.parserr   r!   r;   r8   r   r   r   r   r   <module>   s   