Warning: session_start(): Session cannot be started after headers have already been sent in /home/tvrreohg/public_html/manga.php on line 13
3 \±ã@sldZdZddlZddlZddlmZddlmZddlm Z Gdd „d e ƒZ Gd d „d e ƒZ ia d d „ZdS)aŒ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.z+George Boutsioukis éN)Ú defaultdicté)Úpytree)Ú reduce_treec@s eZdZdZejƒZdd„ZdS)ÚBMNodez?Class for a node of the Aho-Corasick automaton used in matchingcCs"i|_g|_ttjƒ|_d|_dS)NÚ)Útransition_tableÚfixersÚnextrÚcountÚidÚcontent)Úself©rú+/usr/lib64/python3.6/lib2to3/btm_matcher.pyÚ__init__s zBMNode.__init__N)Ú__name__Ú __module__Ú __qualname__Ú__doc__Ú itertoolsr rrrrrrsrc@s8eZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd S) Ú BottomMatcherzgThe main matcher class. After instantiating the patterns should be added using the add_fixer methodcCs0tƒ|_tƒ|_|jg|_g|_tjdƒ|_dS)NZRefactoringTool) ÚsetÚmatchrÚrootZnodesr ÚloggingZ getLoggerZlogger)rrrrrs  zBottomMatcher.__init__cCsL|jj|ƒt|jƒ}|jƒ}|j||jd�}x|D]}|jj|ƒq4WdS)zÒ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 reached)ÚstartN)r ÚappendrZ pattern_treeZget_linear_subpatternÚaddr)rÚfixerZtreeZlinearÚ match_nodesZ match_noderrrÚ add_fixer%s    zBottomMatcher.add_fixercCsÌ|s |gSt|dtƒrhg}xF|dD]:}|j||d�}x&|D]}|j|j|dd…|ƒƒq>Wq&W|S|d|jkrŒtƒ}||j|d<n|j|d}|dd…r¾|j|dd…|d�}n|g}|SdS)z5Recursively adds a linear pattern to the AC automatonr)rrN)Ú isinstanceÚtuplerÚextendrr)rÚpatternrr Ú alternativeZ end_nodesÚendZ next_noderrrr1s" " zBottomMatcher.addc Cs0|j}ttƒ}�x|D�]}|}�x|�r&d|_x,|jD]"}t|tjƒr8|jdkr8d|_Pq8W|j dkrp|j}n|j }||j krº|j |}x’|j D]"}||kr¦g||<||j |ƒq’Wnd|j}|j dk rÔ|j jrÔP||j k�r|j |}x2|j D](}||jƒk�r g||<||j |ƒqòW|j }q$WqW|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)rrÚlistZ was_checkedZchildrenr"rZLeafÚvalueÚtyperr rÚparentÚkeys) rZleavesZcurrent_ac_nodeZresultsZleafZcurrent_ast_nodeZchildZ node_tokenrrrrÚrunSs>          zBottomMatcher.runcs*tdƒ‡fdd„‰ˆ|jƒtdƒdS)z %d [label=%s] //%sr)rr-Úprintr Ú type_reprÚstrr r )ZnodeZ subnode_keyZsubnode)Ú print_noderrr2“s  z*BottomMatcher.print_ac..print_nodeú}N)r/r)rr)r2rÚprint_ac�s  zBottomMatcher.print_acN) rrrrrr!rr.r4rrrrrs  "=rcCsHtss