o
     JAf                     @   sj   	 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.10/lib2to3/btm_matcher.py__init__   s   
zBMNode.__init__N)__name__
__module____qualname__	itertoolsr   r   r   r   r   r   r      s    r   c                   @   s6   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   sJ   	 | j | t|j}| }| j|| jd}|D ]}|j | qd 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r1g }|d D ]}| j||d}|D ]}|| |dd  | qq|S |d |jvrCt }||j|d < n|j|d  }|dd  r]| j|dd  |d}|S |g}|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 ]l}|}|rvd|_|jD ]}t|tjr(|jdkr(d|_ nq|j	dkr2|j}n|j	}||j
v rM|j
| }|jD ]	}|| | qBn$| j }|jd urZ|jjrZn||j
v rq|j
| }|jD ]	}|| | qg|j}|sq
|S )NT;Fr   )r   r   listZwas_checked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   sB   








"zBottomMatcher.runc                    s,   	 t d  fdd  | j t d d S )Nz
digraph g{c                    sZ   | j  D ]%}| j | }td| j|jt|t|jf  |dkr&t|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   r8      s   


z*BottomMatcher.print_ac.<locals>.print_node})r4   r   r   r   r7   r   print_ac   s
   
zBottomMatcher.print_acN)r   r   r   r   r$   r!   r2   r:   r   r   r   r   r      s    "8r   c                 C   sD   t sd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   r5      s   r5   )
__author__r   r   collectionsr   r   r   Z	btm_utilsr   objectr   r   r<   r5   r   r   r   r   <module>   s    	 