a
    ö£Be¬  ã                   @   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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„Z0dd„ Z1dd„ Z2‡  Z3S )Ú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_inputÚ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sync_comp_forÚ	decoratorZlambdefZlambdef_nocondZnamedexpr_testTr   c                    s(   t ƒ j|||d� g | _g | _d| _d S )N)Úerror_recoveryr   )ÚsuperÚ__init__Zsyntax_errorsÚ_omit_dedent_listÚ_indent_counter)ÚselfZpgen_grammarr	   Zstart_nonterminal©Ú	__class__© úV/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/parso/python/parser.pyr   @   s    
ÿzParser.__init__c                    s*   | j r| jdkrt‚|  |¡}tƒ  |¡S )Nr   )Ú_error_recoveryÚ_start_nonterminalÚNotImplementedErrorÚ_recovery_tokenizer
   Úparse)r   Útokensr   r   r   r   H   s
    

zParser.parsec                 C   sT   z| j | |ƒ}W n< tyN   |dkr>|d g|dd…  }|  ||¡}Y n0 |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   é   éÿÿÿÿ)Únode_mapÚKeyErrorÚdefault_node)r   ÚnonterminalÚchildrenÚnoder   r   r   Úconvert_nodeQ   s    zParser.convert_nodec                 C   sH   |t kr0|| jjv r"t |||¡S t |||¡S | j |tj¡|||ƒS )N)	ÚNAMEZ_pgen_grammarZreserved_syntax_stringsr   ÚKeywordÚNameÚ	_leaf_mapÚgetÚOperator)r   ÚtypeÚvalueÚprefixÚ	start_posr   r   r   Úconvert_leafe   s
    zParser.convert_leafc                    sŒ  | j d j}|r|d  ¡ }nd }| jdkrÂ|jtjksZ|jtkrÂ|j 	d¡sÂ|j 	d¡sÂ| j d j
jdkrÂz| j d j
jtj }W n ty–   Y n,0 |jjrÂ|jsÂ|j| j d _
|  |¡ d S | jsÔtƒ  |¡S dd„ }|| j ƒ}|  |d ¡�r|  |¡ nH|\}}}	}
|tk�r&| j | j¡ t |j||	|
¡}| j d j |¡ | j d }|jd	k�rˆz|j
jd
 |_
W n t�y†   Y n0 d S )Nr   r   Ú
úZsimple_stmtc                 S   sJ   t tt| ƒƒƒD ]4\}}|jdkr( qFq|jdkrt|jƒdkr qFq|S )Nr   r   é   )ÚreversedÚlistÚ	enumerater   ÚlenÚnodes)ÚstackÚuntil_indexÚ
stack_noder   r   r   Úcurrent_suiteŽ   s    

z,Parser.error_recovery.<locals>.current_suiter0   r   Ústmt) r6   r5   Zget_last_leafr   r)   r   Ú	ENDMARKERÚDEDENTr*   ÚendswithZdfaZ	from_ruleZtransitionsÚNEWLINEr   Znext_dfaÚis_finalZ
dfa_pushesZ
_add_tokenr   r
   r	   Ú_stack_removalÚINDENTr   Úappendr   r   ZPythonErrorLeafÚnamer   Zarcs)r   ÚtokenZ	tos_nodesZ	last_leafZplanr9   r7   Útypr*   r,   r+   Z
error_leafZtosr   r   r   r	   o   sN    

ÿþ
þ
ý



zParser.error_recoveryc                 C   sR   dd„ | j |d … D ƒ}|r<t |¡}| j |d  j |¡ g | j |d …< t|ƒS )Nc                 S   s   g | ]}|j D ]}|‘qqS r   )r5   )Ú.0r8   r!   r   r   r   Ú
<listcomp>¶   ó    z)Parser._stack_removal.<locals>.<listcomp>r0   )r6   r   ZPythonErrorNoder5   rB   Úbool)r   Ústart_indexZ	all_nodesr!   r   r   r   r@   µ   s    
zParser._stack_removalc                 c   sz   |D ]p}|d }|t krX| j}|rH|d | jkrH| ¡  |  jd8  _q|  jd8  _n|tkrn|  jd7  _|V  qd S )Nr   r   r0   )r<   r   r   ÚpoprA   )r   r   rD   rE   Úor   r   r   r   ¿   s    zParser._recovery_tokenize)Tr   )4Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ZExprStmtÚClassÚFunctionÚModuleZ
ImportNameÚ
ImportFromZKeywordStatementZ
ReturnStmtZ	YieldExprZ
GlobalStmtZ
AssertStmtZIfStmtZWithStmtZForStmtZ	WhileStmtZTryStmtZSyncCompForÚ	DecoratorÚLambdaÚ	NamedExprr   Z
PythonNoder   r   ÚSTRINGÚStringÚNUMBERÚNumberr>   ZNewliner;   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#   rA   r<   r   r   r   r   r   Ú<module>   s   