ofc@sdZdZddlZddlZddlmZddlmZddlm Z de fd YZ d e fd YZ ia d ZdS( sA 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 iN(t defaultdicti(tpytree(t reduce_treetBMNodecBs#eZdZejZdZRS(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;eZdZdZdZdZdZdZRS(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(sReduces 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|dtrg}xU|dD]I}|j|d|}x+|D]#}|j|j|d|qSWq1W|S|d|jkrt}||j|d     !          cs*dGHfd|jdGHdS(s<Prints a graphviz diagram of the BM automaton(for debugging)s digraph g{csvxo|jjD]^}|j|}d|j|jt|t|jfGH|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.pyREs '  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?|jjD]+\}}t|tkr&|t|s