Back to index

python3.2  3.2.2
Classes | Functions | Variables
lib2to3.btm_utils Namespace Reference

Classes

class  MinNode

Functions

def reduce_tree
def get_characteristic_subpattern
def rec_test

Variables

 syms = pattern_symbols
 pysyms = python_symbols
 tokens = grammar.opmap
 token_labels = token
int TYPE_ANY = 1
int TYPE_ALTERNATIVES = 2
int TYPE_GROUP = 3

Function Documentation

Picks the most characteristic from a list of linear patterns
Current order used is:
names > common_names > common_chars

Definition at line 238 of file btm_utils.py.

00238 
00239 def get_characteristic_subpattern(subpatterns):
00240     """Picks the most characteristic from a list of linear patterns
00241     Current order used is:
00242     names > common_names > common_chars
00243     """
00244     if not isinstance(subpatterns, list):
00245         return subpatterns
00246     if len(subpatterns)==1:
00247         return subpatterns[0]
00248 
00249     # first pick out the ones containing variable names
00250     subpatterns_with_names = []
00251     subpatterns_with_common_names = []
00252     common_names = ['in', 'for', 'if' , 'not', 'None']
00253     subpatterns_with_common_chars = []
00254     common_chars = "[]().,:"
00255     for subpattern in subpatterns:
00256         if any(rec_test(subpattern, lambda x: type(x) is str)):
00257             if any(rec_test(subpattern,
00258                             lambda x: isinstance(x, str) and x in common_chars)):
00259                 subpatterns_with_common_chars.append(subpattern)
00260             elif any(rec_test(subpattern,
00261                               lambda x: isinstance(x, str) and x in common_names)):
00262                 subpatterns_with_common_names.append(subpattern)
00263 
00264             else:
00265                 subpatterns_with_names.append(subpattern)
00266 
00267     if subpatterns_with_names:
00268         subpatterns = subpatterns_with_names
00269     elif subpatterns_with_common_names:
00270         subpatterns = subpatterns_with_common_names
00271     elif subpatterns_with_common_chars:
00272         subpatterns = subpatterns_with_common_chars
00273     # of the remaining subpatterns pick out the longest one
00274     return max(subpatterns, key=len)

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.btm_utils.rec_test (   sequence,
  test_func 
)
Tests test_func on all items of sequence and items of included
sub-iterables

Definition at line 275 of file btm_utils.py.

00275 
00276 def rec_test(sequence, test_func):
00277     """Tests test_func on all items of sequence and items of included
00278     sub-iterables"""
00279     for x in sequence:
00280         if isinstance(x, (list, tuple)):
00281             for y in rec_test(x, test_func):
00282                 yield y
00283         else:
00284             yield test_func(x)

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.btm_utils.reduce_tree (   node,
  parent = None 
)
Internal function. Reduces a compiled pattern tree to an
intermediate representation suitable for feeding the
automaton. This also trims off any optional pattern elements(like
[a], a*).

Definition at line 104 of file btm_utils.py.

00104 
00105 def reduce_tree(node, parent=None):
00106     """
00107     Internal function. Reduces a compiled pattern tree to an
00108     intermediate representation suitable for feeding the
00109     automaton. This also trims off any optional pattern elements(like
00110     [a], a*).
00111     """
00112 
00113     new_node = None
00114     #switch on the node type
00115     if node.type == syms.Matcher:
00116         #skip
00117         node = node.children[0]
00118 
00119     if node.type == syms.Alternatives  :
00120         #2 cases
00121         if len(node.children) <= 2:
00122             #just a single 'Alternative', skip this node
00123             new_node = reduce_tree(node.children[0], parent)
00124         else:
00125             #real alternatives
00126             new_node = MinNode(type=TYPE_ALTERNATIVES)
00127             #skip odd children('|' tokens)
00128             for child in node.children:
00129                 if node.children.index(child)%2:
00130                     continue
00131                 reduced = reduce_tree(child, new_node)
00132                 if reduced is not None:
00133                     new_node.children.append(reduced)
00134     elif node.type == syms.Alternative:
00135         if len(node.children) > 1:
00136 
00137             new_node = MinNode(type=TYPE_GROUP)
00138             for child in node.children:
00139                 reduced = reduce_tree(child, new_node)
00140                 if reduced:
00141                     new_node.children.append(reduced)
00142             if not new_node.children:
00143                 # delete the group if all of the children were reduced to None
00144                 new_node = None
00145 
00146         else:
00147             new_node = reduce_tree(node.children[0], parent)
00148 
00149     elif node.type == syms.Unit:
00150         if (isinstance(node.children[0], pytree.Leaf) and
00151             node.children[0].value == '('):
00152             #skip parentheses
00153             return reduce_tree(node.children[1], parent)
00154         if ((isinstance(node.children[0], pytree.Leaf) and
00155                node.children[0].value == '[')
00156                or
00157                (len(node.children)>1 and
00158                hasattr(node.children[1], "value") and
00159                node.children[1].value == '[')):
00160             #skip whole unit if its optional
00161             return None
00162 
00163         leaf = True
00164         details_node = None
00165         alternatives_node = None
00166         has_repeater = False
00167         repeater_node = None
00168         has_variable_name = False
00169 
00170         for child in node.children:
00171             if child.type == syms.Details:
00172                 leaf = False
00173                 details_node = child
00174             elif child.type == syms.Repeater:
00175                 has_repeater = True
00176                 repeater_node = child
00177             elif child.type == syms.Alternatives:
00178                 alternatives_node = child
00179             if hasattr(child, 'value') and child.value == '=': # variable name
00180                 has_variable_name = True
00181 
00182         #skip variable name
00183         if has_variable_name:
00184             #skip variable name, '='
00185             name_leaf = node.children[2]
00186             if hasattr(name_leaf, 'value') and name_leaf.value == '(':
00187                 # skip parenthesis
00188                 name_leaf = node.children[3]
00189         else:
00190             name_leaf = node.children[0]
00191 
00192         #set node type
00193         if name_leaf.type == token_labels.NAME:
00194             #(python) non-name or wildcard
00195             if name_leaf.value == 'any':
00196                 new_node = MinNode(type=TYPE_ANY)
00197             else:
00198                 if hasattr(token_labels, name_leaf.value):
00199                     new_node = MinNode(type=getattr(token_labels, name_leaf.value))
00200                 else:
00201                     new_node = MinNode(type=getattr(pysyms, name_leaf.value))
00202 
00203         elif name_leaf.type == token_labels.STRING:
00204             #(python) name or character; remove the apostrophes from
00205             #the string value
00206             name = name_leaf.value.strip("'")
00207             if name in tokens:
00208                 new_node = MinNode(type=tokens[name])
00209             else:
00210                 new_node = MinNode(type=token_labels.NAME, name=name)
00211         elif name_leaf.type == syms.Alternatives:
00212             new_node = reduce_tree(alternatives_node, parent)
00213 
00214         #handle repeaters
00215         if has_repeater:
00216             if repeater_node.children[0].value == '*':
00217                 #reduce to None
00218                 new_node = None
00219             elif repeater_node.children[0].value == '+':
00220                 #reduce to a single occurence i.e. do nothing
00221                 pass
00222             else:
00223                 #TODO: handle {min, max} repeaters
00224                 raise NotImplementedError
00225                 pass
00226 
00227         #add children
00228         if details_node and new_node is not None:
00229             for child in details_node.children[1:-1]:
00230                 #skip '<', '>' markers
00231                 reduced = reduce_tree(child, new_node)
00232                 if reduced is not None:
00233                     new_node.children.append(reduced)
00234     if new_node:
00235         new_node.parent = parent
00236     return new_node
00237 

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

lib2to3.btm_utils.pysyms = python_symbols

Definition at line 8 of file btm_utils.py.

lib2to3.btm_utils.syms = pattern_symbols

Definition at line 7 of file btm_utils.py.

Definition at line 10 of file btm_utils.py.

lib2to3.btm_utils.tokens = grammar.opmap

Definition at line 9 of file btm_utils.py.

Definition at line 13 of file btm_utils.py.

Definition at line 12 of file btm_utils.py.

Definition at line 14 of file btm_utils.py.