j f@sdZdZddlZddlZddlmZddlmZddlm Z Gdd d e Z Gd d d e Z ia d d ZdS)aA 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.eZdZdZejZddZdS)BMNodez?Class for a node of the Aho-Corasick automaton used in matchingcCs1i|_g|_ttj|_d|_dS)N)transition_tablefixersnextrcountidcontent)selfr8/opt/alt/python34/lib64/python3.4/lib2to3/btm_matcher.py__init__s  zBMNode.__init__N)__name__ __module__ __qualname____doc__ itertoolsr rrrrrrs  rc@sReZdZdZddZddZddZdd Zd d Zd S) BottomMatcherzgThe main matcher class. After instantiating the patterns should be added using the add_fixer methodcCsFt|_t|_|jg|_g|_tjd|_dS)NZRefactoringTool) setmatchrrootZnodesr loggingZ getLoggerZlogger)rrrrrs    zBottomMatcher.__init__cCsh|jj|t|j}|j}|j|d|j}x|D]}|jj|qJWdS)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 reachedstartN)r appendrZ pattern_treeZget_linear_subpatternaddr)rfixerZtreeZlinear match_nodesZ match_noderrr add_fixer%s   zBottomMatcher.add_fixercCs |s |gSt|dtrg}x[|dD]O}|j|d|}x1|D])}|j|j|dd|qSWq1W|S|d|jkrt}||j|d     !          zBottomMatcher.runcs7tdfdd|jtddS)z %d [label=%s] //%sr)rr,printr type_reprstrr r )ZnodeZ subnode_keyZsubnode) print_noderrr1s ) z*BottomMatcher.print_ac..print_node}N)r.r)rr)r1rprint_acs  zBottomMatcher.print_acN) rrrrrr!rr-r3rrrrrs   " =rcCshtsXddlm}x?|jjD]+\}}t|tkr&|t|s