U
e5d� � @ sl d Z dZddlZddlZddlmZ ddlmZ ddlm Z G dd � d e
�ZG d
d� de
�Zi a
dd
� ZdS )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 � N)�defaultdict� )�pytree)�reduce_treec @ s e Zd ZdZe�� Zdd� ZdS )�BMNodez?Class for a node of the Aho-Corasick automaton used in matchingc C s" i | _ g | _ttj�| _d| _d S )N� )�transition_table�fixers�nextr �count�id�content��self� r �+/usr/lib64/python3.8/lib2to3/btm_matcher.py�__init__ s zBMNode.__init__N)�__name__�
__module__�__qualname__�__doc__� itertoolsr r r r r r r s r c @ s8 e Zd ZdZdd� Zdd� Zdd� Zdd � Zd
d� ZdS )
�
BottomMatcherzgThe main matcher class. After instantiating the patterns should
be added using the add_fixer methodc C s0 t � | _t� | _| jg| _g | _t�d�| _d S )NZRefactoringTool) �set�matchr �rootZnodesr �loggingZ getLoggerZloggerr r r r r s
zBottomMatcher.__init__c C sH | j �|� t|j�}|�� }| j|| jd�}|D ]}|j �|� q2dS )z�Reduces 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 Zpattern_treeZget_linear_subpattern�addr )r �fixerZtreeZlinear�match_nodesZ
match_noder r r � add_fixer% s
zBottomMatcher.add_fixerc C s� |s
|gS t |d t�r`g }|d D ]6}| j||d�}|D ]}|�| �|dd� |�� q:q$|S |d |jkr�t� }||j|d <