Back to index

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

List of all members.

Public Member Functions

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

Public Attributes

 content
 min
 max
 name
_PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt
struct _typeobjectob_type

Static Public Attributes

 type = None

Private Member Functions

def _iterative_matches
def _bare_name_matches
def _recursive_matches

Detailed Description

A wildcard pattern can match zero or more nodes.

This has all the flexibility needed to implement patterns like:

.*      .+      .?      .{m,n}
(a b c | d e | f)
(...)*  (...)+  (...)?  (...){m,n}

except it always uses non-greedy matching.

Definition at line 639 of file pytree.py.


Constructor & Destructor Documentation

def lib2to3.pytree.WildcardPattern.__init__ (   self,
  content = None,
  min = 0,
  max = HUGE,
  name = None 
)
Initializer.

Args:
    content: optional sequence of subsequences of patterns;
     if absent, matches one node;
     if present, each subsequence is an alternative [*]
    min: optional minimum number of times to match, default 0
    max: optional maximum number of times to match, default HUGE
    name: optional name assigned to this match

[*] Thus, if content is [[a, b, c], [d, e], [f, g, h]] this is
    equivalent to (a b c | d e | f g h); if content is None,
    this is equivalent to '.' in regular expression terms.
    The min and max parameters work as follows:
min=0, max=maxint: .*
min=1, max=maxint: .+
min=0, max=1: .?
min=1, max=1: .
    If content is not None, replace the dot with the parenthesized
    list of alternatives, e.g. (a b c | d e | f g h)*

Definition at line 653 of file pytree.py.

00653 
00654     def __init__(self, content=None, min=0, max=HUGE, name=None):
00655         """
00656         Initializer.
00657 
00658         Args:
00659             content: optional sequence of subsequences of patterns;
00660                      if absent, matches one node;
00661                      if present, each subsequence is an alternative [*]
00662             min: optional minimum number of times to match, default 0
00663             max: optional maximum number of times to match, default HUGE
00664             name: optional name assigned to this match
00665 
00666         [*] Thus, if content is [[a, b, c], [d, e], [f, g, h]] this is
00667             equivalent to (a b c | d e | f g h); if content is None,
00668             this is equivalent to '.' in regular expression terms.
00669             The min and max parameters work as follows:
00670                 min=0, max=maxint: .*
00671                 min=1, max=maxint: .+
00672                 min=0, max=1: .?
00673                 min=1, max=1: .
00674             If content is not None, replace the dot with the parenthesized
00675             list of alternatives, e.g. (a b c | d e | f g h)*
00676         """
00677         assert 0 <= min <= max <= HUGE, (min, max)
00678         if content is not None:
00679             content = tuple(map(tuple, content))  # Protect against alterations
00680             # Check sanity of alternatives
00681             assert len(content), repr(content)  # Can't have zero alternatives
00682             for alt in content:
00683                 assert len(alt), repr(alt) # Can have empty alternatives
00684         self.content = content
00685         self.min = min
00686         self.max = max
00687         self.name = name

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.WildcardPattern._bare_name_matches (   self,
  nodes 
) [private]
Special optimized matcher for bare_name.

Definition at line 796 of file pytree.py.

00796 
00797     def _bare_name_matches(self, nodes):
00798         """Special optimized matcher for bare_name."""
00799         count = 0
00800         r = {}
00801         done = False
00802         max = len(nodes)
00803         while not done and count < max:
00804             done = True
00805             for leaf in self.content:
00806                 if leaf[0].match(nodes[count], r):
00807                     count += 1
00808                     done = False
00809                     break
00810         r[self.name] = nodes[:count]
00811         return count, r

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.pytree.WildcardPattern._iterative_matches (   self,
  nodes 
) [private]
Helper to iteratively yield the matches.

Definition at line 767 of file pytree.py.

00767 
00768     def _iterative_matches(self, nodes):
00769         """Helper to iteratively yield the matches."""
00770         nodelen = len(nodes)
00771         if 0 >= self.min:
00772             yield 0, {}
00773 
00774         results = []
00775         # generate matches that use just one alt from self.content
00776         for alt in self.content:
00777             for c, r in generate_matches(alt, nodes):
00778                 yield c, r
00779                 results.append((c, r))
00780 
00781         # for each match, iterate down the nodes
00782         while results:
00783             new_results = []
00784             for c0, r0 in results:
00785                 # stop if the entire set of nodes has been matched
00786                 if c0 < nodelen and c0 <= self.max:
00787                     for alt in self.content:
00788                         for c1, r1 in generate_matches(alt, nodes[c0:]):
00789                             if c1 > 0:
00790                                 r = {}
00791                                 r.update(r0)
00792                                 r.update(r1)
00793                                 yield c0 + c1, r
00794                                 new_results.append((c0 + c1, r))
00795             results = new_results

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.pytree.WildcardPattern._recursive_matches (   self,
  nodes,
  count 
) [private]
Helper to recursively yield the matches.

Definition at line 812 of file pytree.py.

00812 
00813     def _recursive_matches(self, nodes, count):
00814         """Helper to recursively yield the matches."""
00815         assert self.content is not None
00816         if count >= self.min:
00817             yield 0, {}
00818         if count < self.max:
00819             for alt in self.content:
00820                 for c0, r0 in generate_matches(alt, nodes):
00821                     for c1, r1 in self._recursive_matches(nodes[c0:], count+1):
00822                         r = {}
00823                         r.update(r0)
00824                         r.update(r1)
00825                         yield c0 + c1, r
00826 

Here is the call graph for this function:

Here is the caller graph for this function:

Generator yielding matches for a sequence of nodes.

Args:
    nodes: sequence of nodes

Yields:
    (count, results) tuples where:
    count: the match comprises nodes[:count];
    results: dict containing named submatches.

Reimplemented from lib2to3.pytree.BasePattern.

Definition at line 722 of file pytree.py.

00722 
00723     def generate_matches(self, nodes):
00724         """
00725         Generator yielding matches for a sequence of nodes.
00726 
00727         Args:
00728             nodes: sequence of nodes
00729 
00730         Yields:
00731             (count, results) tuples where:
00732             count: the match comprises nodes[:count];
00733             results: dict containing named submatches.
00734         """
00735         if self.content is None:
00736             # Shortcut for special case (see __init__.__doc__)
00737             for count in range(self.min, 1 + min(len(nodes), self.max)):
00738                 r = {}
00739                 if self.name:
00740                     r[self.name] = nodes[:count]
00741                 yield count, r
00742         elif self.name == "bare_name":
00743             yield self._bare_name_matches(nodes)
00744         else:
00745             # The reason for this is that hitting the recursion limit usually
00746             # results in some ugly messages about how RuntimeErrors are being
00747             # ignored. We only have to do this on CPython, though, because other
00748             # implementations don't have this nasty bug in the first place.
00749             if hasattr(sys, "getrefcount"):
00750                 save_stderr = sys.stderr
00751                 sys.stderr = StringIO()
00752             try:
00753                 for count, r in self._recursive_matches(nodes, 0):
00754                     if self.name:
00755                         r[self.name] = nodes[:count]
00756                     yield count, r
00757             except RuntimeError:
00758                 # We fall back to the iterative pattern matching scheme if the recursive
00759                 # scheme hits the recursion limit.
00760                 for count, r in self._iterative_matches(nodes):
00761                     if self.name:
00762                         r[self.name] = nodes[:count]
00763                     yield count, r
00764             finally:
00765                 if hasattr(sys, "getrefcount"):
00766                     sys.stderr = save_stderr

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.pytree.WildcardPattern.match (   self,
  node,
  results = None 
)
Does this pattern exactly match a node?

Reimplemented from lib2to3.pytree.BasePattern.

Definition at line 707 of file pytree.py.

00707 
00708     def match(self, node, results=None):
00709         """Does this pattern exactly match a node?"""
00710         return self.match_seq([node], results)

Here is the call graph for this function:

Here is the caller graph for this function:

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

Reimplemented from lib2to3.pytree.BasePattern.

Definition at line 711 of file pytree.py.

00711 
00712     def match_seq(self, nodes, results=None):
00713         """Does this pattern exactly match a sequence of nodes?"""
00714         for c, r in self.generate_matches(nodes):
00715             if c == len(nodes):
00716                 if results is not None:
00717                     results.update(r)
00718                     if self.name:
00719                         results[self.name] = list(nodes)
00720                 return True
00721         return False

Here is the call graph for this function:

Optimize certain stacked wildcard patterns.

Reimplemented from lib2to3.pytree.BasePattern.

Definition at line 688 of file pytree.py.

00688 
00689     def optimize(self):
00690         """Optimize certain stacked wildcard patterns."""
00691         subpattern = None
00692         if (self.content is not None and
00693             len(self.content) == 1 and len(self.content[0]) == 1):
00694             subpattern = self.content[0][0]
00695         if self.min == 1 and self.max == 1:
00696             if self.content is None:
00697                 return NodePattern(name=self.name)
00698             if subpattern is not None and  self.name == subpattern.name:
00699                 return subpattern.optimize()
00700         if (self.min <= 1 and isinstance(subpattern, WildcardPattern) and
00701             subpattern.min <= 1 and self.name == subpattern.name):
00702             return WildcardPattern(subpattern.content,
00703                                    self.min*subpattern.min,
00704                                    self.max*subpattern.max,
00705                                    subpattern.name)
00706         return self


Member Data Documentation

Reimplemented from lib2to3.pytree.BasePattern.

Definition at line 683 of file pytree.py.

Definition at line 685 of file pytree.py.

Definition at line 684 of file pytree.py.

Reimplemented from lib2to3.pytree.BasePattern.

Definition at line 686 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.

lib2to3.pytree.BasePattern.type = None [static, inherited]

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

Definition at line 465 of file pytree.py.


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