B
    v9a                 @   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.7/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	   loggingZ	getLoggerZlogger)r   r   r   r   r      s
    
zBottomMatcher.__init__c             C   sL   | j | t|j}| }| j|| jd}x|D ]}|j | q4W 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rhg }xF|d D ]:}| j||d}x&|D ]}|| |dd  | q>W q&W |S |d |jk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}x|D ]}|}x|rd|_x,|jD ]"}t|tjr0|jdkr0d|_P q0W |j	dkrh|j}n|j	}||j
kr|j
| }xj|jD ]}|| | qW nL| j }|jd k	r|jjrP ||j
kr|j
| }x|jD ]}|| | qW |j}qW qW |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_nodeZchildZ
node_tokenr   r   r   r   runS   s6    






zBottomMatcher.runc                s*   t d  fdd  | j t d d S )Nz
digraph g{c                s^   xX| j  D ]J}| j | }td| j|jt|t|jf  |dkrNt|j  | qW d S )Nz%d -> %d [label=%s] //%sr   )r   keysprintr   	type_reprstrr	   r   )ZnodeZsubnode_keyZsubnode)
print_noder   r   r1      s    

z*BottomMatcher.print_ac.<locals>.print_node})r.   r   )r   r   )r1   r   print_ac   s    
zBottomMatcher.print_acN)r   r   r   r   r    r   r,   r3   r   r   r   r   r      s
   "8r   c             C   sH   t s<ddlm} x*|j D ]\}}t|tkr|t |< qW t | | S )Nr   )python_symbols)_type_reprsZpygramr4   __dict__itemsr)   int
setdefault)Ztype_numr4   namevalr   r   r   r/      s     r/   )
__author__r   r   collectionsr   r   r   Z	btm_utilsr   objectr   r   r5   r/   r   r   r   r   <module>   s   	 