gvdZdZddlZddlZddlmZddlmZddlm Z Gdd e Z Gd d e Z ia d Zy) 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_treec8eZdZdZej ZdZy)BMNodez?Class for a node of the Aho-Corasick automaton used in matchingcji|_g|_ttj|_d|_y)N)transition_tablefixersnextrcountidcontentselfs :/opt/alt/python312/lib64/python3.12/lib2to3/btm_matcher.py__init__zBMNode.__init__s( " v||$ N)__name__ __module__ __qualname____doc__ itertoolsrrrrrrsI IOO Errc.eZdZdZdZdZdZdZdZy) BottomMatcherzgThe main matcher class. After instantiating the patterns should be added using the add_fixer methodct|_t|_|jg|_g|_t jd|_y)NRefactoringTool) setmatchrrootnodesr logging getLoggerloggerrs rrzBottomMatcher.__init__s;U H ii[  ''(9: rc|jj|t|j}|j }|j ||j }|D]}|jj|y)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_nodes r add_fixerzBottomMatcher.add_fixer%sh 5!5--.++-hhvTYYh7 %J    $ $U +&rc |s|gSt|dtrLg}|dD]@}|j||}|D]&}|j|j|dd|(B|S|d|jvrt }||j|d<n|j|d}|ddr|j|dd|}|S|g}|S)z5Recursively adds a linear pattern to the AC automatonrr(rN) isinstancetupler-extendr r)rpatternr)r1 alternative end_nodesend next_nodes rr-zBottomMatcher.add1s7N gaj% (K&qz !HH[H> $C&&txx S'AB% *  qz!7!77"H 5>&&wqz2"2271:> qr{ HHWQR[ HB  'K  rc|j}tt}|D]?}|}|s d|_|jD]5}t |t js|jdk(s.d|_n|jdk(r |j}n |j}||jvr5|j|}|jD]}||j|nq|j}|j|jjr||jvr4|j|}|jD]}||j||j}|r7B|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;Fr)r"rlist was_checkedchildrenr5rLeafvaluetyper r r*parent) rleavescurrent_ac_noderesultsleafcurrent_ast_nodechild node_tokenr.s rrunzBottomMatcher.runSsW ))d#D# "/3 ,-66E!%5%++:L7<(4 7 $((A-!1!7!7J!1!6!6J!A!AA&5&F&Fz&RO!0!7!7--.>?"8'+iiO(//;,33??"_%E%EE*9*J*J:*V%4%;%;E#EN112BC&<$4#:#: C#Hrc`tdfd|jtdy)z %d [label=%s] //%sr)r keysprintr type_reprstrr r)node subnode_keysubnode print_nodes rrWz*BottomMatcher.print_ac..print_nodes|#4499; // <0ww Ik,BCDWXYZ!#'//*7# risHG; #"V}F}@ 6r