a
    ze                     @   sh   d Z ddlZddlZddlmZ ddlmZ ddlmZ G dd de	Z
G d	d
 d
e	Zi add ZdS )z+George Boutsioukis <gboutsioukis@gmail.com>    N)defaultdict   )pytree)reduce_treec                   @   s   e Zd Ze Zdd ZdS )BMNodec                 C   s"   i | _ g | _ttj| _d| _d S )N )transition_tablefixersnextr   countidcontentself r   )/usr/lib/python3.9/lib2to3/btm_matcher.py__init__   s    zBMNode.__init__N)__name__
__module____qualname__	itertoolsr   r   r   r   r   r   r      s   r   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )BottomMatcherc                 C   s0   t  | _t | _| jg| _g | _td| _d S )NZRefactoringTool)	setmatchr   rootZnodesr	   logging	getLoggerloggerr   r   r   r   r      s
    
zBottomMatcher.__init__c                 C   sH   | j | t|j}| }| j|| jd}|D ]}|j | q2d S )Nstart)r	   appendr   Zpattern_treeZget_linear_subpatternaddr   )r   fixerZtreeZlinearmatch_nodesZ
match_noder   r   r   	add_fixer%   s    
zBottomMatcher.add_fixerc              	   C   s   |s
|gS t |d tr`g }|d D ]6}| j||d}|D ]}|| |dd  | q:q$|S |d |jvrt }||j|d < n|j|d  }|dd  r| j|dd  |d}n|g}|S d S )Nr   r   r   )
isinstancetupler!   extendr   r   )r   patternr   r#   ZalternativeZ	end_nodesendZ	next_noder   r   r   r!   1   s"    zBottomMatcher.addc           	      C   s   | j }tt}|D ]}|}|rd|_|jD ]$}t|tjr*|jdkr*d|_ qPq*|j	dkrb|j}n|j	}||j
v r|j
| }|jD ]}|| | qnH| j }|jd ur|jjrq||j
v r|j
| }|jD ]}|| | q|j}qq|S )NT;Fr   )r   r   listZwas_checkedZchildrenr%   r   ZLeafvaluetyper   r	   r    parent)	r   ZleavesZcurrent_ac_noderesultsZleafZcurrent_ast_nodechildZ
node_tokenr"   r   r   r   runS   s8    









zBottomMatcher.runc                    s*   t d  fdd  | j t d d S )Nz
digraph g{c                    sZ   | j  D ]J}| j | }td| j|jt|t|jf  |dkrLt|j  | q
d S )Nz%d -> %d [label=%s] //%sr   )r   keysprintr   	type_reprstrr	   r   )ZnodeZsubnode_keyZsubnode
print_noder   r   r7      s    

z*BottomMatcher.print_ac.<locals>.print_node})r3   r   r   r   r6   r   print_ac   s    
zBottomMatcher.print_acN)r   r   r   r   r$   r!   r1   r9   r   r   r   r   r      s
   "8r   c                 C   sD   t s8ddlm} |j D ]\}}t|tkr|t |< qt | | S )Nr   )python_symbols)_type_reprsZpygramr:   __dict__itemsr-   int
setdefault)Ztype_numr:   namevalr   r   r   r4      s
    r4   )
__author__r   r   collectionsr   r   r   Z	btm_utilsr   objectr   r   r;   r4   r   r   r   r   <module>   s   	 