Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions
lib2to3.pytree.NodePattern Class Reference
Inheritance diagram for lib2to3.pytree.NodePattern:
Inheritance graph
[legend]
Collaboration diagram for lib2to3.pytree.NodePattern:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def __new__
def __repr__
def optimize
def match
def match_seq
def generate_matches

Public Attributes

 type
 content
 name
_PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt
struct _typeobjectob_type

Static Public Attributes

 wildcards = False

Private Member Functions

def _submatch

Detailed Description

Definition at line 578 of file pytree.py.


Constructor & Destructor Documentation

def lib2to3.pytree.NodePattern.__init__ (   self,
  type = None,
  content = None,
  name = None 
)
Initializer.  Takes optional type, content, and name.

The type, if given, must be a symbol type (>= 256).  If the
type is None this matches *any* single node (leaf or not),
except if content is not None, in which it only matches
non-leaf nodes that also match the content pattern.

The content, if not None, must be a sequence of Patterns that
must match the node's children exactly.  If the content is
given, the type must not be None.

If a name is given, the matching node is stored in the results
dict under that key.

Definition at line 582 of file pytree.py.

00582 
00583     def __init__(self, type=None, content=None, name=None):
00584         """
00585         Initializer.  Takes optional type, content, and name.
00586 
00587         The type, if given, must be a symbol type (>= 256).  If the
00588         type is None this matches *any* single node (leaf or not),
00589         except if content is not None, in which it only matches
00590         non-leaf nodes that also match the content pattern.
00591 
00592         The content, if not None, must be a sequence of Patterns that
00593         must match the node's children exactly.  If the content is
00594         given, the type must not be None.
00595 
00596         If a name is given, the matching node is stored in the results
00597         dict under that key.
00598         """
00599         if type is not None:
00600             assert type >= 256, type
00601         if content is not None:
00602             assert not isinstance(content, str), repr(content)
00603             content = list(content)
00604             for i, item in enumerate(content):
00605                 assert isinstance(item, BasePattern), (i, item)
00606                 if isinstance(item, WildcardPattern):
00607                     self.wildcards = True
00608         self.type = type
00609         self.content = content
00610         self.name = name

Here is the call graph for this function:

Here is the caller graph for this function:


Member Function Documentation

def lib2to3.pytree.BasePattern.__new__ (   cls,
  args,
  kwds 
) [inherited]
Constructor that prevents BasePattern from being instantiated.

Definition at line 469 of file pytree.py.

00469 
00470     def __new__(cls, *args, **kwds):
00471         """Constructor that prevents BasePattern from being instantiated."""
00472         assert cls is not BasePattern, "Cannot instantiate BasePattern"
00473         return object.__new__(cls)

def lib2to3.pytree.BasePattern.__repr__ (   self) [inherited]

Definition at line 474 of file pytree.py.

00474 
00475     def __repr__(self):
00476         args = [type_repr(self.type), self.content, self.name]
00477         while args and args[-1] is None:
00478             del args[-1]
00479         return "%s(%s)" % (self.__class__.__name__, ", ".join(map(repr, args)))

Here is the call graph for this function:

def lib2to3.pytree.NodePattern._submatch (   self,
  node,
  results = None 
) [private]
Match the pattern's content to the node's children.

This assumes the node type matches and self.content is not None.

Returns True if it matches, False if not.

If results is not None, it must be a dict which will be
updated with the nodes matching named subpatterns.

When returning False, the results dict may still be updated.

Definition at line 611 of file pytree.py.

00611 
00612     def _submatch(self, node, results=None):
00613         """
00614         Match the pattern's content to the node's children.
00615 
00616         This assumes the node type matches and self.content is not None.
00617 
00618         Returns True if it matches, False if not.
00619 
00620         If results is not None, it must be a dict which will be
00621         updated with the nodes matching named subpatterns.
00622 
00623         When returning False, the results dict may still be updated.
00624         """
00625         if self.wildcards:
00626             for c, r in generate_matches(self.content, node.children):
00627                 if c == len(node.children):
00628                     if results is not None:
00629                         results.update(r)
00630                     return True
00631             return False
00632         if len(self.content) != len(node.children):
00633             return False
00634         for subpattern, child in zip(self.content, node.children):
00635             if not subpattern.match(child, results):
00636                 return False
00637         return True
00638 

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.pytree.BasePattern.generate_matches (   self,
  nodes 
) [inherited]
Generator yielding all matches for this pattern.

Default implementation for non-wildcard patterns.

Reimplemented in lib2to3.pytree.NegatedPattern, and lib2to3.pytree.WildcardPattern.

Definition at line 523 of file pytree.py.

00523 
00524     def generate_matches(self, nodes):
00525         """
00526         Generator yielding all matches for this pattern.
00527 
00528         Default implementation for non-wildcard patterns.
00529         """
00530         r = {}
00531         if nodes and self.match(nodes[0], r):
00532             yield 1, r
00533 

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.pytree.BasePattern.match (   self,
  node,
  results = None 
) [inherited]
Does this pattern exactly match a node?

Returns True if it matches, False if not.

If results is not None, it must be a dict which will be
updated with the nodes matching named subpatterns.

Default implementation for non-wildcard patterns.

Reimplemented in lib2to3.pytree.WildcardPattern, and lib2to3.pytree.LeafPattern.

Definition at line 488 of file pytree.py.

00488 
00489     def match(self, node, results=None):
00490         """
00491         Does this pattern exactly match a node?
00492 
00493         Returns True if it matches, False if not.
00494 
00495         If results is not None, it must be a dict which will be
00496         updated with the nodes matching named subpatterns.
00497 
00498         Default implementation for non-wildcard patterns.
00499         """
00500         if self.type is not None and node.type != self.type:
00501             return False
00502         if self.content is not None:
00503             r = None
00504             if results is not None:
00505                 r = {}
00506             if not self._submatch(node, r):
00507                 return False
00508             if r:
00509                 results.update(r)
00510         if results is not None and self.name:
00511             results[self.name] = node
00512         return True

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.pytree.BasePattern.match_seq (   self,
  nodes,
  results = None 
) [inherited]
Does this pattern exactly match a sequence of nodes?

Default implementation for non-wildcard patterns.

Reimplemented in lib2to3.pytree.WildcardPattern.

Definition at line 513 of file pytree.py.

00513 
00514     def match_seq(self, nodes, results=None):
00515         """
00516         Does this pattern exactly match a sequence of nodes?
00517 
00518         Default implementation for non-wildcard patterns.
00519         """
00520         if len(nodes) != 1:
00521             return False
00522         return self.match(nodes[0], results)

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.pytree.BasePattern.optimize (   self) [inherited]
A subclass can define this as a hook for optimizations.

Returns either self or another node with the same effect.

Reimplemented in lib2to3.pytree.WildcardPattern.

Definition at line 480 of file pytree.py.

00480 
00481     def optimize(self):
00482         """
00483         A subclass can define this as a hook for optimizations.
00484 
00485         Returns either self or another node with the same effect.
00486         """
00487         return self


Member Data Documentation

Reimplemented from lib2to3.pytree.BasePattern.

Definition at line 608 of file pytree.py.

Reimplemented from lib2to3.pytree.BasePattern.

Definition at line 609 of file pytree.py.

Definition at line 107 of file object.h.

struct _typeobject* _object::ob_type [inherited]

Definition at line 108 of file object.h.

Reimplemented from lib2to3.pytree.BasePattern.

Definition at line 607 of file pytree.py.

Definition at line 580 of file pytree.py.


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