Repository URL to install this package:
|
Version:
2.2.5 ▾
|
ó
AögWc @ s d Z d Z d d l Z d d l Z d d l m Z d d l m Z d d l m Z d e
f d YZ d
e
f d YZ i a
d Z d S(
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 <gboutsioukis@gmail.com>iÿÿÿÿN( t defaultdicti ( t pytree( t reduce_treet BMNodec B s# e Z d Z e j Z d Z RS( s? Class for a node of the Aho-Corasick automaton used in matchingc C s1 i | _ g | _ t t j | _ d | _ d S( Nt ( t transition_tablet fixerst nextR t countt idt content( t self( ( s ./lib2to3/btm_matcher.pyt __init__ s ( t __name__t
__module__t __doc__t itertoolsR R ( ( ( s ./lib2to3/btm_matcher.pyR s t
BottomMatcherc B s; e Z d Z d Z d Z d Z d Z d Z RS( sg The main matcher class. After instantiating the patterns should
be added using the add_fixer methodc C sF t | _ t | _ | j g | _ g | _ t j d | _ d S( Nt RefactoringTool( t sett matchR t roott nodesR t loggingt getLoggert logger( R ( ( s ./lib2to3/btm_matcher.pyR s
c C sh | j j | t | j } | j } | j | d | j } x | D] } | j j | qJ Wd S( 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
reachedt startN( R t appendR t pattern_treet get_linear_subpatternt addR ( R t fixert treet lineart match_nodest
match_node( ( s ./lib2to3/btm_matcher.pyt add_fixer% s
c C sú | s
| g St | d t r g } xU | d D]I } | j | d | } x+ | D]# } | j | j | d | qS Wq1 W| S| d | j k r² t } | | j | d <n | j | d } | d ré | j | d d | } n | g } | Sd S( s5 Recursively adds a linear pattern to the AC automatoni R i N( t
isinstancet tupleR t extendR R ( R t patternR R" t alternativet end_nodest endt next_node( ( s ./lib2to3/btm_matcher.pyR 1 s"
%
c C s | j } t t } x| D]y} | } xj| rt | _ x? | j D]4 } t | t j rD | j d k rD t
| _ PqD qD W| j d k r | j } n | j } | | j k rý | j | } xÉ | j
D]0 } | | k rå g | | <n | | j | qÆ Wn | j } | j d k r%| j j r%Pn | | j k r| j | } xD | j
D]6 } | | j k rpg | | <n | | j | qKWn | j } q+ Wq W| S( su The 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
u ;i N( R R t listt Truet was_checkedt childrenR% R t Leaft valuet Falset typeR R R t parentt Nonet keys( R t leavest current_ac_nodet resultst leaft current_ast_nodet childt
node_tokenR ( ( s ./lib2to3/btm_matcher.pyt runS s>