Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
lib2to3.patcomp.PatternCompiler Class Reference
Inheritance diagram for lib2to3.patcomp.PatternCompiler:
Inheritance graph
[legend]
Collaboration diagram for lib2to3.patcomp.PatternCompiler:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def compile_pattern
def compile_node
def compile_basic
def get_int

Public Attributes

 grammar
 syms
 pygrammar
 pysyms
 driver
_PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt
struct _typeobjectob_type

Detailed Description

Definition at line 42 of file patcomp.py.


Constructor & Destructor Documentation

Initializer.

Takes an optional alternative filename for the pattern grammar.

Definition at line 44 of file patcomp.py.

00044 
00045     def __init__(self, grammar_file=_PATTERN_GRAMMAR_FILE):
00046         """Initializer.
00047 
00048         Takes an optional alternative filename for the pattern grammar.
00049         """
00050         self.grammar = driver.load_grammar(grammar_file)
00051         self.syms = pygram.Symbols(self.grammar)
00052         self.pygrammar = pygram.python_grammar
00053         self.pysyms = pygram.python_symbols
00054         self.driver = driver.Driver(self.grammar, convert=pattern_convert)

Here is the caller graph for this function:


Member Function Documentation

def lib2to3.patcomp.PatternCompiler.compile_basic (   self,
  nodes,
  repeat = None 
)

Definition at line 138 of file patcomp.py.

00138 
00139     def compile_basic(self, nodes, repeat=None):
00140         # Compile STRING | NAME [Details] | (...) | [...]
00141         assert len(nodes) >= 1
00142         node = nodes[0]
00143         if node.type == token.STRING:
00144             value = str(literals.evalString(node.value))
00145             return pytree.LeafPattern(_type_of_literal(value), value)
00146         elif node.type == token.NAME:
00147             value = node.value
00148             if value.isupper():
00149                 if value not in TOKEN_MAP:
00150                     raise PatternSyntaxError("Invalid token: %r" % value)
00151                 if nodes[1:]:
00152                     raise PatternSyntaxError("Can't have details for token")
00153                 return pytree.LeafPattern(TOKEN_MAP[value])
00154             else:
00155                 if value == "any":
00156                     type = None
00157                 elif not value.startswith("_"):
00158                     type = getattr(self.pysyms, value, None)
00159                     if type is None:
00160                         raise PatternSyntaxError("Invalid symbol: %r" % value)
00161                 if nodes[1:]: # Details present
00162                     content = [self.compile_node(nodes[1].children[1])]
00163                 else:
00164                     content = None
00165                 return pytree.NodePattern(type, content)
00166         elif node.value == "(":
00167             return self.compile_node(nodes[1])
00168         elif node.value == "[":
00169             assert repeat is None
00170             subpattern = self.compile_node(nodes[1])
00171             return pytree.WildcardPattern([[subpattern]], min=0, max=1)
00172         assert False, node

Here is the call graph for this function:

Here is the caller graph for this function:

Compiles a node, recursively.

This is one big switch on the node type.

Definition at line 67 of file patcomp.py.

00067 
00068     def compile_node(self, node):
00069         """Compiles a node, recursively.
00070 
00071         This is one big switch on the node type.
00072         """
00073         # XXX Optimize certain Wildcard-containing-Wildcard patterns
00074         # that can be merged
00075         if node.type == self.syms.Matcher:
00076             node = node.children[0] # Avoid unneeded recursion
00077 
00078         if node.type == self.syms.Alternatives:
00079             # Skip the odd children since they are just '|' tokens
00080             alts = [self.compile_node(ch) for ch in node.children[::2]]
00081             if len(alts) == 1:
00082                 return alts[0]
00083             p = pytree.WildcardPattern([[a] for a in alts], min=1, max=1)
00084             return p.optimize()
00085 
00086         if node.type == self.syms.Alternative:
00087             units = [self.compile_node(ch) for ch in node.children]
00088             if len(units) == 1:
00089                 return units[0]
00090             p = pytree.WildcardPattern([units], min=1, max=1)
00091             return p.optimize()
00092 
00093         if node.type == self.syms.NegatedUnit:
00094             pattern = self.compile_basic(node.children[1:])
00095             p = pytree.NegatedPattern(pattern)
00096             return p.optimize()
00097 
00098         assert node.type == self.syms.Unit
00099 
00100         name = None
00101         nodes = node.children
00102         if len(nodes) >= 3 and nodes[1].type == token.EQUAL:
00103             name = nodes[0].value
00104             nodes = nodes[2:]
00105         repeat = None
00106         if len(nodes) >= 2 and nodes[-1].type == self.syms.Repeater:
00107             repeat = nodes[-1]
00108             nodes = nodes[:-1]
00109 
00110         # Now we've reduced it to: STRING | NAME [Details] | (...) | [...]
00111         pattern = self.compile_basic(nodes, repeat)
00112 
00113         if repeat is not None:
00114             assert repeat.type == self.syms.Repeater
00115             children = repeat.children
00116             child = children[0]
00117             if child.type == token.STAR:
00118                 min = 0
00119                 max = pytree.HUGE
00120             elif child.type == token.PLUS:
00121                 min = 1
00122                 max = pytree.HUGE
00123             elif child.type == token.LBRACE:
00124                 assert children[-1].type == token.RBRACE
00125                 assert  len(children) in (3, 5)
00126                 min = max = self.get_int(children[1])
00127                 if len(children) == 5:
00128                     max = self.get_int(children[3])
00129             else:
00130                 assert False
00131             if min != 1 or max != 1:
00132                 pattern = pattern.optimize()
00133                 pattern = pytree.WildcardPattern([[pattern]], min=min, max=max)
00134 
00135         if name is not None:
00136             pattern.name = name
00137         return pattern.optimize()

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.patcomp.PatternCompiler.compile_pattern (   self,
  input,
  debug = False,
  with_tree = False 
)
Compiles a pattern string to a nested pytree.*Pattern object.

Definition at line 55 of file patcomp.py.

00055 
00056     def compile_pattern(self, input, debug=False, with_tree=False):
00057         """Compiles a pattern string to a nested pytree.*Pattern object."""
00058         tokens = tokenize_wrapper(input)
00059         try:
00060             root = self.driver.parse_tokens(tokens, debug=debug)
00061         except parse.ParseError as e:
00062             raise PatternSyntaxError(str(e))
00063         if with_tree:
00064             return self.compile_node(root), root
00065         else:
00066             return self.compile_node(root)

Here is the call graph for this function:

def lib2to3.patcomp.PatternCompiler.get_int (   self,
  node 
)

Definition at line 173 of file patcomp.py.

00173 
00174     def get_int(self, node):
00175         assert node.type == token.NUMBER
00176         return int(node.value)
00177 
00178 
# Map named tokens to the type value for a LeafPattern

Here is the caller graph for this function:


Member Data Documentation

Definition at line 53 of file patcomp.py.

Definition at line 49 of file patcomp.py.

Definition at line 107 of file object.h.

struct _typeobject* _object::ob_type [inherited]

Definition at line 108 of file object.h.

Definition at line 51 of file patcomp.py.

Definition at line 52 of file patcomp.py.

Definition at line 50 of file patcomp.py.


The documentation for this class was generated from the following file: