o
    bc                     @   sl   d Z 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 )a  A bottom-up tree matching algorithm implementation meant to speed
up 2to3's matching process. After the tree patterns are reduced to
their rarest linear path, a linear Aho-Corasick automaton is
created. The linear automaton traverses the linear paths from the
leaves to the root of the AST and returns a set of nodes for further
matching. This reduces significantly the number of candidate nodes.z+George Boutsioukis <gboutsioukis@gmail.com>    N)defaultdict   )pytree)reduce_treec                   @   s    e Zd ZdZe Zdd ZdS )BMNodez?Class for a node of the Aho-Corasick automaton used in matchingc                 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____doc__	itertoolsr   r   r   r   r   r   r      s    r   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )BottomMatcherzgThe main matcher class. After instantiating the patterns should
    be added using the add_fixer methodc                 C   s0   t  | _t | _| jg| _g | _td| _d S )NRefactoringTool)	setmatchr   rootnodesr	   logging	getLoggerloggerr   r   r   r   r      s
   
zBottomMatcher.__init__c                 C   sH   | j | t|j}| }| j|| jd}|D ]}|j | qdS )zReduces a fixer's pattern tree to a linear path and adds it
        to the matcher(a common Aho-Corasick automaton). The fixer is
        appended on the matching states and called when they are
        reachedstartN)r	   appendr   pattern_treeget_linear_subpatternaddr   )r   fixertreelinearmatch_nodes
match_noder   r   r   	add_fixer%   s   
zBottomMatcher.add_fixerc              	   C   s   |s|gS t |d tr0g }|d D ]}| j||d}|D ]}|| |dd | qq|S |d |jvrBt }||j|d < n|j|d  }|dd r\| j|dd |d}|S |g}|S )z5Recursively adds a linear pattern to the AC automatonr   r!   r   N)
isinstancetupler&   extendr   r   )r   patternr"   r*   alternative	end_nodesend	next_noder   r   r   r&   1   s&   zBottomMatcher.addc           	      C   s   | j }tt}|D ]l}|}|rud|_|jD ]}t|tjr'|jdkr'd|_ nq|j	dkr1|j}n|j	}||j
v rL|j
| }|jD ]	}|| | qAn$| j }|jdurY|jjrYn||j
v rp|j
| }|jD ]	}|| | qf|j}|sq	|S )au  The main interface with the bottom matcher. The tree is
        traversed from the bottom using the constructed
        automaton. Nodes are only checked once as the tree is
        retraversed. When the automaton fails, we give it one more
        shot(in case the above tree matches as a whole with the
        rejected leaf), then we break for the next leaf. There is the
        special case of multiple arguments(see code comments) where we
        recheck the nodes

        Args:
           The leaves of the AST tree to be matched

        Returns:
           A dictionary of node matches with fixers as the keys
        T;Fr   N)r   r   listwas_checkedchildrenr-   r   Leafvaluetyper   r	   r#   parent)	r   leavescurrent_ac_noderesultsleafcurrent_ast_nodechild
node_tokenr'   r   r   r   runS   s@   








"zBottomMatcher.runc                    s*   t d  fdd  | j t d dS )z<Prints a graphviz diagram of the BM automaton(for debugging)z
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   )nodesubnode_keysubnode
print_noder   r   rM      s   


z*BottomMatcher.print_ac.<locals>.print_node}N)rF   r   r   r   rL   r   print_ac   s   
zBottomMatcher.print_acN)	r   r   r   r   r   r,   r&   rD   rO   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_reprspygramrP   __dict__itemsr;   int
setdefault)type_numrP   namevalr   r   r   rG      s   rG   )r   
__author__r   r   collectionsr   r   r   	btm_utilsr   objectr   r   rQ   rG   r   r   r   r   <module>   s    	 