ó o”þfc@s“dZdZddlZddlZddlmZddlmZddlm Z de fd „ƒYZ d e fd „ƒYZ ia d „ZdS( sŒ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.s+George Boutsioukis iÿÿÿÿN(t defaultdicti(tpytree(t reduce_treetBMNodecBs#eZdZejƒZd„ZRS(s?Class for a node of the Aho-Corasick automaton used in matchingcCs1i|_g|_ttjƒ|_d|_dS(Nt(ttransition_tabletfixerstnextRtcounttidtcontent(tself((s+/usr/lib64/python2.7/lib2to3/btm_matcher.pyt__init__s  (t__name__t __module__t__doc__t itertoolsRR (((s+/usr/lib64/python2.7/lib2to3/btm_matcher.pyRs t BottomMatchercBs;eZdZd„Zd„Zd„Zd„Zd„ZRS(sgThe main matcher class. After instantiating the patterns should be added using the add_fixer methodcCsFtƒ|_tƒ|_|jg|_g|_tjdƒ|_dS(NtRefactoringTool( tsettmatchRtroottnodesRtloggingt getLoggertlogger(R ((s+/usr/lib64/python2.7/lib2to3/btm_matcher.pyR s    cCsh|jj|ƒt|jƒ}|jƒ}|j|d|jƒ}x|D]}|jj|ƒqJWdS(sÒ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 reachedtstartN(RtappendRt pattern_treetget_linear_subpatterntaddR(R tfixerttreetlineart match_nodest match_node((s+/usr/lib64/python2.7/lib2to3/btm_matcher.pyt add_fixer%s   cCsú|s |gSt|dtƒr‚g}xU|dD]I}|j|d|ƒ}x+|D]#}|j|j|d|ƒƒqSWq1W|S|d|jkr²tƒ}||j|d     !          cs*dGH‡fd†‰ˆ|jƒdGHdS(s<Prints a graphviz diagram of the BM automaton(for debugging)s digraph g{csvxo|jjƒD]^}|j|}d|j|jt|ƒt|jƒfGH|dkrd|jGHnˆ|ƒqWdS(Ns%d -> %d [label=%s] //%si(RR7R t type_reprtstrRR (tnodet subnode_keytsubnode(t print_node(s+/usr/lib64/python2.7/lib2to3/btm_matcher.pyRE“s '  t}N(R(R ((REs+/usr/lib64/python2.7/lib2to3/btm_matcher.pytprint_acs (R RRR R$RR?RG(((s+/usr/lib64/python2.7/lib2to3/btm_matcher.pyRs   " =cCshtsXddlm}x?|jjƒD]+\}}t|ƒtkr&|t|s   …