o bc@sldZdZddlZddlZddlmZddlmZddlm 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 eZdZdZeZddZdS)BMNodez?Class for a node of the Aho-Corasick automaton used in matchingcCs"i|_g|_ttj|_d|_dS)N)transition_tablefixersnextrcountidcontentselfr*/usr/lib/python3.10/lib2to3/btm_matcher.py__init__s  zBMNode.__init__N)__name__ __module__ __qualname____doc__ itertoolsr rrrrrrs rc@s8eZdZdZddZddZddZdd Zd d Zd S) BottomMatcherzgThe main matcher class. After instantiating the patterns should be added using the add_fixer methodcCs0t|_t|_|jg|_g|_td|_dS)NRefactoringTool) setmatchrrootnodesr logging getLoggerloggerrrrrrs  zBottomMatcher.__init__cCsH|j|t|j}|}|j||jd}|D]}|j|qdS)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 appendr pattern_treeget_linear_subpatternaddr)rfixertreelinear match_nodes match_noderrr add_fixer%s  zBottomMatcher.add_fixerc Cs|s|gSt|dtr0g}|dD]}|j||d}|D]}|||dd|qq|S|d|jvrBt}||j|d<n|j|d}|ddr\|j|dd|d}|S|g}|S)z5Recursively adds a linear pattern to the AC automatonrr!rN) isinstancetupler&extendrr)rpatternr"r* alternative end_nodesend next_noderrrr&1s&  zBottomMatcher.addc Cs|j}tt}|D]l}|}|rud|_|jD]}t|tjr'|jdkr'd|_nq|j dkr1|j}n|j }||j vrL|j |}|j D] }|| |qAn$|j}|j durY|j jrYn||j vrp|j |}|j D] }|| |qf|j }|sq |S)auThe main interface with the bottom matcher. The tree is traversed from the bottom using the constructed automaton. Nodes are only checked once as the tree is retraversed. When the automaton fails, we give it one more shot(in case the above tree matches as a whole with the rejected leaf), then we break for the next leaf. There is the special case of multiple arguments(see code comments) where we recheck the nodes Args: The leaves of the AST tree to be matched Returns: A dictionary of node matches with fixers as the keys T;FrN)rrlist was_checkedchildrenr-rLeafvaluetyperr r#parent) rleavescurrent_ac_noderesultsleafcurrent_ast_nodechild node_tokenr'rrrrunSs@         "zBottomMatcher.runcs*tdfdd|jtddS)z %d [label=%s] //%sr)rkeysprintr type_reprstrr r )node subnode_keysubnode print_noderrrMs   z*BottomMatcher.print_ac..print_node}N)rFrrrrLrprint_acs   zBottomMatcher.print_acN) rrrrrr,r&rDrOrrrrrs " 8rcCsDtsddlm}|jD]\}}t|tkr|t|<q t||S)Nr)python_symbols) _type_reprspygramrP__dict__itemsr;int setdefault)type_numrPnamevalrrrrGs   rG)r __author__rr collectionsrrr btm_utilsrobjectrrrQrGrrrrs