Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 1 | # Copyright 2006 Google, Inc. All Rights Reserved. |
| 2 | # Licensed to PSF under a Contributor Agreement. |
| 3 | |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 4 | """ |
| 5 | Python parse tree definitions. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 6 | |
| 7 | This is a very concrete parse tree; we need to keep every token and |
| 8 | even the comments and whitespace between tokens. |
| 9 | |
| 10 | There's also a pattern matching implementation here. |
| 11 | """ |
| 12 | |
| 13 | __author__ = "Guido van Rossum <guido@python.org>" |
| 14 | |
Benjamin Peterson | 5cff931 | 2008-11-28 23:01:28 +0000 | [diff] [blame] | 15 | import sys |
Benjamin Peterson | 2c3ac6b | 2009-06-11 23:47:38 +0000 | [diff] [blame] | 16 | import warnings |
Benjamin Peterson | 5cff931 | 2008-11-28 23:01:28 +0000 | [diff] [blame] | 17 | from io import StringIO |
| 18 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 19 | HUGE = 0x7FFFFFFF # maximum repeat count, default max |
| 20 | |
Martin v. Löwis | 3faa84f | 2008-03-22 00:07:09 +0000 | [diff] [blame] | 21 | _type_reprs = {} |
| 22 | def type_repr(type_num): |
| 23 | global _type_reprs |
| 24 | if not _type_reprs: |
| 25 | from .pygram import python_symbols |
| 26 | # printing tokens is possible but not as useful |
| 27 | # from .pgen2 import token // token.__dict__.items(): |
| 28 | for name, val in python_symbols.__dict__.items(): |
| 29 | if type(val) == int: _type_reprs[val] = name |
| 30 | return _type_reprs.setdefault(type_num, type_num) |
| 31 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 32 | class Base(object): |
| 33 | |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 34 | """ |
| 35 | Abstract base class for Node and Leaf. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 36 | |
| 37 | This provides some default functionality and boilerplate using the |
| 38 | template pattern. |
| 39 | |
| 40 | A node may be a subnode of at most one parent. |
| 41 | """ |
| 42 | |
| 43 | # Default values for instance variables |
| 44 | type = None # int: token number (< 256) or symbol number (>= 256) |
| 45 | parent = None # Parent node pointer, or None |
| 46 | children = () # Tuple of subnodes |
| 47 | was_changed = False |
Benjamin Peterson | b0871ca | 2010-10-14 23:03:32 +0000 | [diff] [blame] | 48 | was_checked = False |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 49 | |
| 50 | def __new__(cls, *args, **kwds): |
| 51 | """Constructor that prevents Base from being instantiated.""" |
| 52 | assert cls is not Base, "Cannot instantiate Base" |
| 53 | return object.__new__(cls) |
| 54 | |
| 55 | def __eq__(self, other): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 56 | """ |
| 57 | Compare two nodes for equality. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 58 | |
| 59 | This calls the method _eq(). |
| 60 | """ |
| 61 | if self.__class__ is not other.__class__: |
| 62 | return NotImplemented |
| 63 | return self._eq(other) |
| 64 | |
Benjamin Peterson | e80b51f | 2009-11-02 18:33:36 +0000 | [diff] [blame] | 65 | __hash__ = None # For Py3 compatibility. |
| 66 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 67 | def __ne__(self, other): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 68 | """ |
| 69 | Compare two nodes for inequality. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 70 | |
| 71 | This calls the method _eq(). |
| 72 | """ |
| 73 | if self.__class__ is not other.__class__: |
| 74 | return NotImplemented |
| 75 | return not self._eq(other) |
| 76 | |
| 77 | def _eq(self, other): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 78 | """ |
| 79 | Compare two nodes for equality. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 80 | |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 81 | This is called by __eq__ and __ne__. It is only called if the two nodes |
| 82 | have the same type. This must be implemented by the concrete subclass. |
| 83 | Nodes should be considered equal if they have the same structure, |
| 84 | ignoring the prefix string and other context information. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 85 | """ |
| 86 | raise NotImplementedError |
| 87 | |
| 88 | def clone(self): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 89 | """ |
| 90 | Return a cloned (deep) copy of self. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 91 | |
| 92 | This must be implemented by the concrete subclass. |
| 93 | """ |
| 94 | raise NotImplementedError |
| 95 | |
| 96 | def post_order(self): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 97 | """ |
| 98 | Return a post-order iterator for the tree. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 99 | |
| 100 | This must be implemented by the concrete subclass. |
| 101 | """ |
| 102 | raise NotImplementedError |
| 103 | |
| 104 | def pre_order(self): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 105 | """ |
| 106 | Return a pre-order iterator for the tree. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 107 | |
| 108 | This must be implemented by the concrete subclass. |
| 109 | """ |
| 110 | raise NotImplementedError |
| 111 | |
| 112 | def set_prefix(self, prefix): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 113 | """ |
| 114 | Set the prefix for the node (see Leaf class). |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 115 | |
Benjamin Peterson | 2c3ac6b | 2009-06-11 23:47:38 +0000 | [diff] [blame] | 116 | DEPRECATED; use the prefix property directly. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 117 | """ |
Benjamin Peterson | 2c3ac6b | 2009-06-11 23:47:38 +0000 | [diff] [blame] | 118 | warnings.warn("set_prefix() is deprecated; use the prefix property", |
| 119 | DeprecationWarning, stacklevel=2) |
| 120 | self.prefix = prefix |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 121 | |
| 122 | def get_prefix(self): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 123 | """ |
| 124 | Return the prefix for the node (see Leaf class). |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 125 | |
Benjamin Peterson | 2c3ac6b | 2009-06-11 23:47:38 +0000 | [diff] [blame] | 126 | DEPRECATED; use the prefix property directly. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 127 | """ |
Benjamin Peterson | 2c3ac6b | 2009-06-11 23:47:38 +0000 | [diff] [blame] | 128 | warnings.warn("get_prefix() is deprecated; use the prefix property", |
| 129 | DeprecationWarning, stacklevel=2) |
| 130 | return self.prefix |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 131 | |
| 132 | def replace(self, new): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 133 | """Replace this node with a new one in the parent.""" |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 134 | assert self.parent is not None, str(self) |
| 135 | assert new is not None |
| 136 | if not isinstance(new, list): |
| 137 | new = [new] |
| 138 | l_children = [] |
| 139 | found = False |
| 140 | for ch in self.parent.children: |
| 141 | if ch is self: |
| 142 | assert not found, (self.parent.children, self, new) |
| 143 | if new is not None: |
| 144 | l_children.extend(new) |
| 145 | found = True |
| 146 | else: |
| 147 | l_children.append(ch) |
| 148 | assert found, (self.children, self, new) |
| 149 | self.parent.changed() |
| 150 | self.parent.children = l_children |
| 151 | for x in new: |
| 152 | x.parent = self.parent |
| 153 | self.parent = None |
| 154 | |
| 155 | def get_lineno(self): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 156 | """Return the line number which generated the invocant node.""" |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 157 | node = self |
| 158 | while not isinstance(node, Leaf): |
| 159 | if not node.children: |
| 160 | return |
| 161 | node = node.children[0] |
| 162 | return node.lineno |
| 163 | |
| 164 | def changed(self): |
| 165 | if self.parent: |
| 166 | self.parent.changed() |
| 167 | self.was_changed = True |
| 168 | |
| 169 | def remove(self): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 170 | """ |
| 171 | Remove the node from the tree. Returns the position of the node in its |
| 172 | parent's children before it was removed. |
| 173 | """ |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 174 | if self.parent: |
| 175 | for i, node in enumerate(self.parent.children): |
| 176 | if node is self: |
| 177 | self.parent.changed() |
| 178 | del self.parent.children[i] |
| 179 | self.parent = None |
| 180 | return i |
| 181 | |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 182 | @property |
| 183 | def next_sibling(self): |
| 184 | """ |
| 185 | The node immediately following the invocant in their parent's children |
| 186 | list. If the invocant does not have a next sibling, it is None |
| 187 | """ |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 188 | if self.parent is None: |
| 189 | return None |
| 190 | |
| 191 | # Can't use index(); we need to test by identity |
Martin v. Löwis | 3de92bf | 2008-04-10 02:50:50 +0000 | [diff] [blame] | 192 | for i, child in enumerate(self.parent.children): |
| 193 | if child is self: |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 194 | try: |
| 195 | return self.parent.children[i+1] |
| 196 | except IndexError: |
| 197 | return None |
| 198 | |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 199 | @property |
| 200 | def prev_sibling(self): |
| 201 | """ |
| 202 | The node immediately preceding the invocant in their parent's children |
| 203 | list. If the invocant does not have a previous sibling, it is None. |
| 204 | """ |
Martin v. Löwis | 3de92bf | 2008-04-10 02:50:50 +0000 | [diff] [blame] | 205 | if self.parent is None: |
| 206 | return None |
| 207 | |
| 208 | # Can't use index(); we need to test by identity |
| 209 | for i, child in enumerate(self.parent.children): |
| 210 | if child is self: |
| 211 | if i == 0: |
| 212 | return None |
| 213 | return self.parent.children[i-1] |
| 214 | |
Benjamin Peterson | b0871ca | 2010-10-14 23:03:32 +0000 | [diff] [blame] | 215 | def leaves(self): |
| 216 | for child in self.children: |
| 217 | for x in child.leaves(): |
| 218 | yield x |
| 219 | |
| 220 | def depth(self): |
| 221 | if self.parent is None: |
| 222 | return 0 |
| 223 | return 1 + self.parent.depth() |
| 224 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 225 | def get_suffix(self): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 226 | """ |
| 227 | Return the string immediately following the invocant node. This is |
Benjamin Peterson | 2c3ac6b | 2009-06-11 23:47:38 +0000 | [diff] [blame] | 228 | effectively equivalent to node.next_sibling.prefix |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 229 | """ |
| 230 | next_sib = self.next_sibling |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 231 | if next_sib is None: |
| 232 | return "" |
Benjamin Peterson | 2c3ac6b | 2009-06-11 23:47:38 +0000 | [diff] [blame] | 233 | return next_sib.prefix |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 234 | |
Benjamin Peterson | d481e3d | 2009-05-09 19:42:23 +0000 | [diff] [blame] | 235 | if sys.version_info < (3, 0): |
| 236 | def __str__(self): |
| 237 | return str(self).encode("ascii") |
| 238 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 239 | class Node(Base): |
| 240 | |
| 241 | """Concrete implementation for interior nodes.""" |
| 242 | |
Benjamin Peterson | b0871ca | 2010-10-14 23:03:32 +0000 | [diff] [blame] | 243 | def __init__(self,type, children, |
| 244 | context=None, |
| 245 | prefix=None, |
| 246 | fixers_applied=None): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 247 | """ |
| 248 | Initializer. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 249 | |
| 250 | Takes a type constant (a symbol number >= 256), a sequence of |
| 251 | child nodes, and an optional context keyword argument. |
| 252 | |
| 253 | As a side effect, the parent pointers of the children are updated. |
| 254 | """ |
| 255 | assert type >= 256, type |
| 256 | self.type = type |
| 257 | self.children = list(children) |
| 258 | for ch in self.children: |
| 259 | assert ch.parent is None, repr(ch) |
| 260 | ch.parent = self |
| 261 | if prefix is not None: |
Benjamin Peterson | 2c3ac6b | 2009-06-11 23:47:38 +0000 | [diff] [blame] | 262 | self.prefix = prefix |
Benjamin Peterson | b0871ca | 2010-10-14 23:03:32 +0000 | [diff] [blame] | 263 | if fixers_applied: |
| 264 | self.fixers_applied = fixers_applied[:] |
| 265 | else: |
| 266 | self.fixers_applied = None |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 267 | |
| 268 | def __repr__(self): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 269 | """Return a canonical string representation.""" |
Martin v. Löwis | 3faa84f | 2008-03-22 00:07:09 +0000 | [diff] [blame] | 270 | return "%s(%s, %r)" % (self.__class__.__name__, |
| 271 | type_repr(self.type), |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 272 | self.children) |
| 273 | |
Benjamin Peterson | d481e3d | 2009-05-09 19:42:23 +0000 | [diff] [blame] | 274 | def __unicode__(self): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 275 | """ |
| 276 | Return a pretty string representation. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 277 | |
| 278 | This reproduces the input source exactly. |
| 279 | """ |
| 280 | return "".join(map(str, self.children)) |
| 281 | |
Benjamin Peterson | d481e3d | 2009-05-09 19:42:23 +0000 | [diff] [blame] | 282 | if sys.version_info > (3, 0): |
| 283 | __str__ = __unicode__ |
| 284 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 285 | def _eq(self, other): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 286 | """Compare two nodes for equality.""" |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 287 | return (self.type, self.children) == (other.type, other.children) |
| 288 | |
| 289 | def clone(self): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 290 | """Return a cloned (deep) copy of self.""" |
Benjamin Peterson | b0871ca | 2010-10-14 23:03:32 +0000 | [diff] [blame] | 291 | return Node(self.type, [ch.clone() for ch in self.children], |
| 292 | fixers_applied=self.fixers_applied) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 293 | |
| 294 | def post_order(self): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 295 | """Return a post-order iterator for the tree.""" |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 296 | for child in self.children: |
| 297 | for node in child.post_order(): |
| 298 | yield node |
| 299 | yield self |
| 300 | |
| 301 | def pre_order(self): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 302 | """Return a pre-order iterator for the tree.""" |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 303 | yield self |
| 304 | for child in self.children: |
Benjamin Peterson | 68c80ed | 2010-08-08 19:23:25 +0000 | [diff] [blame] | 305 | for node in child.pre_order(): |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 306 | yield node |
| 307 | |
Benjamin Peterson | c9e833f | 2010-05-08 15:46:00 +0000 | [diff] [blame] | 308 | def _prefix_getter(self): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 309 | """ |
Benjamin Peterson | 2c3ac6b | 2009-06-11 23:47:38 +0000 | [diff] [blame] | 310 | The whitespace and comments preceding this node in the input. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 311 | """ |
| 312 | if not self.children: |
| 313 | return "" |
Benjamin Peterson | 2c3ac6b | 2009-06-11 23:47:38 +0000 | [diff] [blame] | 314 | return self.children[0].prefix |
| 315 | |
Benjamin Peterson | c9e833f | 2010-05-08 15:46:00 +0000 | [diff] [blame] | 316 | def _prefix_setter(self, prefix): |
Benjamin Peterson | 2c3ac6b | 2009-06-11 23:47:38 +0000 | [diff] [blame] | 317 | if self.children: |
| 318 | self.children[0].prefix = prefix |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 319 | |
Benjamin Peterson | c9e833f | 2010-05-08 15:46:00 +0000 | [diff] [blame] | 320 | prefix = property(_prefix_getter, _prefix_setter) |
| 321 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 322 | def set_child(self, i, child): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 323 | """ |
| 324 | Equivalent to 'node.children[i] = child'. This method also sets the |
| 325 | child's parent attribute appropriately. |
| 326 | """ |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 327 | child.parent = self |
| 328 | self.children[i].parent = None |
| 329 | self.children[i] = child |
Benjamin Peterson | 0b24b3d | 2008-12-16 03:57:54 +0000 | [diff] [blame] | 330 | self.changed() |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 331 | |
| 332 | def insert_child(self, i, child): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 333 | """ |
| 334 | Equivalent to 'node.children.insert(i, child)'. This method also sets |
| 335 | the child's parent attribute appropriately. |
| 336 | """ |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 337 | child.parent = self |
| 338 | self.children.insert(i, child) |
Benjamin Peterson | 0b24b3d | 2008-12-16 03:57:54 +0000 | [diff] [blame] | 339 | self.changed() |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 340 | |
| 341 | def append_child(self, child): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 342 | """ |
| 343 | Equivalent to 'node.children.append(child)'. This method also sets the |
| 344 | child's parent attribute appropriately. |
| 345 | """ |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 346 | child.parent = self |
| 347 | self.children.append(child) |
Benjamin Peterson | 0b24b3d | 2008-12-16 03:57:54 +0000 | [diff] [blame] | 348 | self.changed() |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 349 | |
| 350 | |
| 351 | class Leaf(Base): |
| 352 | |
| 353 | """Concrete implementation for leaf nodes.""" |
| 354 | |
| 355 | # Default values for instance variables |
Benjamin Peterson | 2c3ac6b | 2009-06-11 23:47:38 +0000 | [diff] [blame] | 356 | _prefix = "" # Whitespace and comments preceding this token in the input |
| 357 | lineno = 0 # Line where this token starts in the input |
| 358 | column = 0 # Column where this token tarts in the input |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 359 | |
Benjamin Peterson | b0871ca | 2010-10-14 23:03:32 +0000 | [diff] [blame] | 360 | def __init__(self, type, value, |
| 361 | context=None, |
| 362 | prefix=None, |
| 363 | fixers_applied=[]): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 364 | """ |
| 365 | Initializer. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 366 | |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 367 | Takes a type constant (a token number < 256), a string value, and an |
| 368 | optional context keyword argument. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 369 | """ |
| 370 | assert 0 <= type < 256, type |
| 371 | if context is not None: |
Benjamin Peterson | 2c3ac6b | 2009-06-11 23:47:38 +0000 | [diff] [blame] | 372 | self._prefix, (self.lineno, self.column) = context |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 373 | self.type = type |
| 374 | self.value = value |
| 375 | if prefix is not None: |
Benjamin Peterson | 2c3ac6b | 2009-06-11 23:47:38 +0000 | [diff] [blame] | 376 | self._prefix = prefix |
Benjamin Peterson | b0871ca | 2010-10-14 23:03:32 +0000 | [diff] [blame] | 377 | self.fixers_applied = fixers_applied[:] |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 378 | |
| 379 | def __repr__(self): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 380 | """Return a canonical string representation.""" |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 381 | return "%s(%r, %r)" % (self.__class__.__name__, |
| 382 | self.type, |
| 383 | self.value) |
| 384 | |
Benjamin Peterson | d481e3d | 2009-05-09 19:42:23 +0000 | [diff] [blame] | 385 | def __unicode__(self): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 386 | """ |
| 387 | Return a pretty string representation. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 388 | |
| 389 | This reproduces the input source exactly. |
| 390 | """ |
| 391 | return self.prefix + str(self.value) |
| 392 | |
Benjamin Peterson | d481e3d | 2009-05-09 19:42:23 +0000 | [diff] [blame] | 393 | if sys.version_info > (3, 0): |
| 394 | __str__ = __unicode__ |
| 395 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 396 | def _eq(self, other): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 397 | """Compare two nodes for equality.""" |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 398 | return (self.type, self.value) == (other.type, other.value) |
| 399 | |
| 400 | def clone(self): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 401 | """Return a cloned (deep) copy of self.""" |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 402 | return Leaf(self.type, self.value, |
Benjamin Peterson | b0871ca | 2010-10-14 23:03:32 +0000 | [diff] [blame] | 403 | (self.prefix, (self.lineno, self.column)), |
| 404 | fixers_applied=self.fixers_applied) |
| 405 | |
| 406 | def leaves(self): |
| 407 | yield self |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 408 | |
| 409 | def post_order(self): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 410 | """Return a post-order iterator for the tree.""" |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 411 | yield self |
| 412 | |
| 413 | def pre_order(self): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 414 | """Return a pre-order iterator for the tree.""" |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 415 | yield self |
| 416 | |
Benjamin Peterson | c9e833f | 2010-05-08 15:46:00 +0000 | [diff] [blame] | 417 | def _prefix_getter(self): |
Benjamin Peterson | 2c3ac6b | 2009-06-11 23:47:38 +0000 | [diff] [blame] | 418 | """ |
| 419 | The whitespace and comments preceding this token in the input. |
| 420 | """ |
| 421 | return self._prefix |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 422 | |
Benjamin Peterson | c9e833f | 2010-05-08 15:46:00 +0000 | [diff] [blame] | 423 | def _prefix_setter(self, prefix): |
Benjamin Peterson | 2c3ac6b | 2009-06-11 23:47:38 +0000 | [diff] [blame] | 424 | self.changed() |
| 425 | self._prefix = prefix |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 426 | |
Benjamin Peterson | c9e833f | 2010-05-08 15:46:00 +0000 | [diff] [blame] | 427 | prefix = property(_prefix_getter, _prefix_setter) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 428 | |
| 429 | def convert(gr, raw_node): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 430 | """ |
| 431 | Convert raw node information to a Node or Leaf instance. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 432 | |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 433 | This is passed to the parser driver which calls it whenever a reduction of a |
| 434 | grammar rule produces a new complete node, so that the tree is build |
| 435 | strictly bottom-up. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 436 | """ |
| 437 | type, value, context, children = raw_node |
| 438 | if children or type in gr.number2symbol: |
| 439 | # If there's exactly one child, return that child instead of |
| 440 | # creating a new node. |
| 441 | if len(children) == 1: |
| 442 | return children[0] |
| 443 | return Node(type, children, context=context) |
| 444 | else: |
| 445 | return Leaf(type, value, context=context) |
| 446 | |
| 447 | |
| 448 | class BasePattern(object): |
| 449 | |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 450 | """ |
| 451 | A pattern is a tree matching pattern. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 452 | |
| 453 | It looks for a specific node type (token or symbol), and |
| 454 | optionally for a specific content. |
| 455 | |
| 456 | This is an abstract base class. There are three concrete |
| 457 | subclasses: |
| 458 | |
| 459 | - LeafPattern matches a single leaf node; |
| 460 | - NodePattern matches a single node (usually non-leaf); |
| 461 | - WildcardPattern matches a sequence of nodes of variable length. |
| 462 | """ |
| 463 | |
| 464 | # Defaults for instance variables |
| 465 | type = None # Node type (token if < 256, symbol if >= 256) |
| 466 | content = None # Optional content matching pattern |
| 467 | name = None # Optional name used to store match in results dict |
| 468 | |
| 469 | def __new__(cls, *args, **kwds): |
| 470 | """Constructor that prevents BasePattern from being instantiated.""" |
| 471 | assert cls is not BasePattern, "Cannot instantiate BasePattern" |
| 472 | return object.__new__(cls) |
| 473 | |
| 474 | def __repr__(self): |
Martin v. Löwis | 3faa84f | 2008-03-22 00:07:09 +0000 | [diff] [blame] | 475 | args = [type_repr(self.type), self.content, self.name] |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 476 | while args and args[-1] is None: |
| 477 | del args[-1] |
| 478 | return "%s(%s)" % (self.__class__.__name__, ", ".join(map(repr, args))) |
| 479 | |
| 480 | def optimize(self): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 481 | """ |
| 482 | A subclass can define this as a hook for optimizations. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 483 | |
| 484 | Returns either self or another node with the same effect. |
| 485 | """ |
| 486 | return self |
| 487 | |
| 488 | def match(self, node, results=None): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 489 | """ |
| 490 | Does this pattern exactly match a node? |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 491 | |
| 492 | Returns True if it matches, False if not. |
| 493 | |
| 494 | If results is not None, it must be a dict which will be |
| 495 | updated with the nodes matching named subpatterns. |
| 496 | |
| 497 | Default implementation for non-wildcard patterns. |
| 498 | """ |
| 499 | if self.type is not None and node.type != self.type: |
| 500 | return False |
| 501 | if self.content is not None: |
| 502 | r = None |
| 503 | if results is not None: |
| 504 | r = {} |
| 505 | if not self._submatch(node, r): |
| 506 | return False |
| 507 | if r: |
| 508 | results.update(r) |
| 509 | if results is not None and self.name: |
| 510 | results[self.name] = node |
| 511 | return True |
| 512 | |
| 513 | def match_seq(self, nodes, results=None): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 514 | """ |
| 515 | Does this pattern exactly match a sequence of nodes? |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 516 | |
| 517 | Default implementation for non-wildcard patterns. |
| 518 | """ |
| 519 | if len(nodes) != 1: |
| 520 | return False |
| 521 | return self.match(nodes[0], results) |
| 522 | |
| 523 | def generate_matches(self, nodes): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 524 | """ |
| 525 | Generator yielding all matches for this pattern. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 526 | |
| 527 | Default implementation for non-wildcard patterns. |
| 528 | """ |
| 529 | r = {} |
| 530 | if nodes and self.match(nodes[0], r): |
| 531 | yield 1, r |
| 532 | |
| 533 | |
| 534 | class LeafPattern(BasePattern): |
| 535 | |
| 536 | def __init__(self, type=None, content=None, name=None): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 537 | """ |
| 538 | Initializer. Takes optional type, content, and name. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 539 | |
| 540 | The type, if given must be a token type (< 256). If not given, |
| 541 | this matches any *leaf* node; the content may still be required. |
| 542 | |
| 543 | The content, if given, must be a string. |
| 544 | |
| 545 | If a name is given, the matching node is stored in the results |
| 546 | dict under that key. |
| 547 | """ |
| 548 | if type is not None: |
| 549 | assert 0 <= type < 256, type |
| 550 | if content is not None: |
Martin v. Löwis | 8a5f8ca | 2008-03-19 05:33:36 +0000 | [diff] [blame] | 551 | assert isinstance(content, str), repr(content) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 552 | self.type = type |
| 553 | self.content = content |
| 554 | self.name = name |
| 555 | |
| 556 | def match(self, node, results=None): |
| 557 | """Override match() to insist on a leaf node.""" |
| 558 | if not isinstance(node, Leaf): |
| 559 | return False |
| 560 | return BasePattern.match(self, node, results) |
| 561 | |
| 562 | def _submatch(self, node, results=None): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 563 | """ |
| 564 | Match the pattern's content to the node's children. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 565 | |
| 566 | This assumes the node type matches and self.content is not None. |
| 567 | |
| 568 | Returns True if it matches, False if not. |
| 569 | |
| 570 | If results is not None, it must be a dict which will be |
| 571 | updated with the nodes matching named subpatterns. |
| 572 | |
| 573 | When returning False, the results dict may still be updated. |
| 574 | """ |
| 575 | return self.content == node.value |
| 576 | |
| 577 | |
| 578 | class NodePattern(BasePattern): |
| 579 | |
| 580 | wildcards = False |
| 581 | |
| 582 | def __init__(self, type=None, content=None, name=None): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 583 | """ |
| 584 | Initializer. Takes optional type, content, and name. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 585 | |
| 586 | The type, if given, must be a symbol type (>= 256). If the |
| 587 | type is None this matches *any* single node (leaf or not), |
| 588 | except if content is not None, in which it only matches |
| 589 | non-leaf nodes that also match the content pattern. |
| 590 | |
| 591 | The content, if not None, must be a sequence of Patterns that |
| 592 | must match the node's children exactly. If the content is |
| 593 | given, the type must not be None. |
| 594 | |
| 595 | If a name is given, the matching node is stored in the results |
| 596 | dict under that key. |
| 597 | """ |
| 598 | if type is not None: |
| 599 | assert type >= 256, type |
| 600 | if content is not None: |
Martin v. Löwis | 8a5f8ca | 2008-03-19 05:33:36 +0000 | [diff] [blame] | 601 | assert not isinstance(content, str), repr(content) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 602 | content = list(content) |
| 603 | for i, item in enumerate(content): |
| 604 | assert isinstance(item, BasePattern), (i, item) |
| 605 | if isinstance(item, WildcardPattern): |
| 606 | self.wildcards = True |
| 607 | self.type = type |
| 608 | self.content = content |
| 609 | self.name = name |
| 610 | |
| 611 | def _submatch(self, node, results=None): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 612 | """ |
| 613 | Match the pattern's content to the node's children. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 614 | |
| 615 | This assumes the node type matches and self.content is not None. |
| 616 | |
| 617 | Returns True if it matches, False if not. |
| 618 | |
| 619 | If results is not None, it must be a dict which will be |
| 620 | updated with the nodes matching named subpatterns. |
| 621 | |
| 622 | When returning False, the results dict may still be updated. |
| 623 | """ |
| 624 | if self.wildcards: |
| 625 | for c, r in generate_matches(self.content, node.children): |
| 626 | if c == len(node.children): |
| 627 | if results is not None: |
| 628 | results.update(r) |
| 629 | return True |
| 630 | return False |
| 631 | if len(self.content) != len(node.children): |
| 632 | return False |
| 633 | for subpattern, child in zip(self.content, node.children): |
| 634 | if not subpattern.match(child, results): |
| 635 | return False |
| 636 | return True |
| 637 | |
| 638 | |
| 639 | class WildcardPattern(BasePattern): |
| 640 | |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 641 | """ |
| 642 | A wildcard pattern can match zero or more nodes. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 643 | |
| 644 | This has all the flexibility needed to implement patterns like: |
| 645 | |
| 646 | .* .+ .? .{m,n} |
| 647 | (a b c | d e | f) |
| 648 | (...)* (...)+ (...)? (...){m,n} |
| 649 | |
| 650 | except it always uses non-greedy matching. |
| 651 | """ |
| 652 | |
| 653 | def __init__(self, content=None, min=0, max=HUGE, name=None): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 654 | """ |
| 655 | Initializer. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 656 | |
| 657 | Args: |
| 658 | content: optional sequence of subsequences of patterns; |
| 659 | if absent, matches one node; |
| 660 | if present, each subsequence is an alternative [*] |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 661 | min: optional minimum number of times to match, default 0 |
| 662 | max: optional maximum number of times to match, default HUGE |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 663 | name: optional name assigned to this match |
| 664 | |
| 665 | [*] Thus, if content is [[a, b, c], [d, e], [f, g, h]] this is |
| 666 | equivalent to (a b c | d e | f g h); if content is None, |
| 667 | this is equivalent to '.' in regular expression terms. |
| 668 | The min and max parameters work as follows: |
| 669 | min=0, max=maxint: .* |
| 670 | min=1, max=maxint: .+ |
| 671 | min=0, max=1: .? |
| 672 | min=1, max=1: . |
| 673 | If content is not None, replace the dot with the parenthesized |
| 674 | list of alternatives, e.g. (a b c | d e | f g h)* |
| 675 | """ |
| 676 | assert 0 <= min <= max <= HUGE, (min, max) |
| 677 | if content is not None: |
| 678 | content = tuple(map(tuple, content)) # Protect against alterations |
| 679 | # Check sanity of alternatives |
| 680 | assert len(content), repr(content) # Can't have zero alternatives |
| 681 | for alt in content: |
| 682 | assert len(alt), repr(alt) # Can have empty alternatives |
| 683 | self.content = content |
| 684 | self.min = min |
| 685 | self.max = max |
| 686 | self.name = name |
| 687 | |
| 688 | def optimize(self): |
| 689 | """Optimize certain stacked wildcard patterns.""" |
| 690 | subpattern = None |
| 691 | if (self.content is not None and |
| 692 | len(self.content) == 1 and len(self.content[0]) == 1): |
| 693 | subpattern = self.content[0][0] |
| 694 | if self.min == 1 and self.max == 1: |
| 695 | if self.content is None: |
| 696 | return NodePattern(name=self.name) |
| 697 | if subpattern is not None and self.name == subpattern.name: |
| 698 | return subpattern.optimize() |
| 699 | if (self.min <= 1 and isinstance(subpattern, WildcardPattern) and |
| 700 | subpattern.min <= 1 and self.name == subpattern.name): |
| 701 | return WildcardPattern(subpattern.content, |
| 702 | self.min*subpattern.min, |
| 703 | self.max*subpattern.max, |
| 704 | subpattern.name) |
| 705 | return self |
| 706 | |
| 707 | def match(self, node, results=None): |
| 708 | """Does this pattern exactly match a node?""" |
| 709 | return self.match_seq([node], results) |
| 710 | |
| 711 | def match_seq(self, nodes, results=None): |
| 712 | """Does this pattern exactly match a sequence of nodes?""" |
| 713 | for c, r in self.generate_matches(nodes): |
| 714 | if c == len(nodes): |
| 715 | if results is not None: |
| 716 | results.update(r) |
| 717 | if self.name: |
| 718 | results[self.name] = list(nodes) |
| 719 | return True |
| 720 | return False |
| 721 | |
| 722 | def generate_matches(self, nodes): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 723 | """ |
| 724 | Generator yielding matches for a sequence of nodes. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 725 | |
| 726 | Args: |
| 727 | nodes: sequence of nodes |
| 728 | |
| 729 | Yields: |
| 730 | (count, results) tuples where: |
| 731 | count: the match comprises nodes[:count]; |
| 732 | results: dict containing named submatches. |
| 733 | """ |
| 734 | if self.content is None: |
| 735 | # Shortcut for special case (see __init__.__doc__) |
Martin v. Löwis | 8a5f8ca | 2008-03-19 05:33:36 +0000 | [diff] [blame] | 736 | for count in range(self.min, 1 + min(len(nodes), self.max)): |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 737 | r = {} |
| 738 | if self.name: |
| 739 | r[self.name] = nodes[:count] |
| 740 | yield count, r |
Benjamin Peterson | 4cdf572 | 2008-07-17 02:21:56 +0000 | [diff] [blame] | 741 | elif self.name == "bare_name": |
| 742 | yield self._bare_name_matches(nodes) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 743 | else: |
Benjamin Peterson | 5cff931 | 2008-11-28 23:01:28 +0000 | [diff] [blame] | 744 | # The reason for this is that hitting the recursion limit usually |
| 745 | # results in some ugly messages about how RuntimeErrors are being |
Benjamin Peterson | d86e4c8 | 2011-03-06 17:14:30 -0600 | [diff] [blame] | 746 | # ignored. We only have to do this on CPython, though, because other |
| 747 | # implementations don't have this nasty bug in the first place. |
| 748 | if hasattr(sys, "getrefcount"): |
| 749 | save_stderr = sys.stderr |
| 750 | sys.stderr = StringIO() |
Benjamin Peterson | 7d8d9a5 | 2008-10-04 21:04:36 +0000 | [diff] [blame] | 751 | try: |
| 752 | for count, r in self._recursive_matches(nodes, 0): |
| 753 | if self.name: |
| 754 | r[self.name] = nodes[:count] |
| 755 | yield count, r |
| 756 | except RuntimeError: |
| 757 | # We fall back to the iterative pattern matching scheme if the recursive |
| 758 | # scheme hits the recursion limit. |
| 759 | for count, r in self._iterative_matches(nodes): |
| 760 | if self.name: |
| 761 | r[self.name] = nodes[:count] |
| 762 | yield count, r |
Benjamin Peterson | 5cff931 | 2008-11-28 23:01:28 +0000 | [diff] [blame] | 763 | finally: |
Benjamin Peterson | d86e4c8 | 2011-03-06 17:14:30 -0600 | [diff] [blame] | 764 | if hasattr(sys, "getrefcount"): |
| 765 | sys.stderr = save_stderr |
Benjamin Peterson | 7d8d9a5 | 2008-10-04 21:04:36 +0000 | [diff] [blame] | 766 | |
| 767 | def _iterative_matches(self, nodes): |
| 768 | """Helper to iteratively yield the matches.""" |
| 769 | nodelen = len(nodes) |
| 770 | if 0 >= self.min: |
| 771 | yield 0, {} |
| 772 | |
| 773 | results = [] |
| 774 | # generate matches that use just one alt from self.content |
| 775 | for alt in self.content: |
| 776 | for c, r in generate_matches(alt, nodes): |
| 777 | yield c, r |
| 778 | results.append((c, r)) |
| 779 | |
| 780 | # for each match, iterate down the nodes |
| 781 | while results: |
| 782 | new_results = [] |
| 783 | for c0, r0 in results: |
| 784 | # stop if the entire set of nodes has been matched |
| 785 | if c0 < nodelen and c0 <= self.max: |
| 786 | for alt in self.content: |
| 787 | for c1, r1 in generate_matches(alt, nodes[c0:]): |
| 788 | if c1 > 0: |
| 789 | r = {} |
| 790 | r.update(r0) |
| 791 | r.update(r1) |
| 792 | yield c0 + c1, r |
| 793 | new_results.append((c0 + c1, r)) |
| 794 | results = new_results |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 795 | |
Benjamin Peterson | 4cdf572 | 2008-07-17 02:21:56 +0000 | [diff] [blame] | 796 | def _bare_name_matches(self, nodes): |
| 797 | """Special optimized matcher for bare_name.""" |
| 798 | count = 0 |
| 799 | r = {} |
| 800 | done = False |
| 801 | max = len(nodes) |
| 802 | while not done and count < max: |
| 803 | done = True |
| 804 | for leaf in self.content: |
| 805 | if leaf[0].match(nodes[count], r): |
| 806 | count += 1 |
| 807 | done = False |
| 808 | break |
| 809 | r[self.name] = nodes[:count] |
| 810 | return count, r |
| 811 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 812 | def _recursive_matches(self, nodes, count): |
| 813 | """Helper to recursively yield the matches.""" |
| 814 | assert self.content is not None |
| 815 | if count >= self.min: |
Benjamin Peterson | 4cdf572 | 2008-07-17 02:21:56 +0000 | [diff] [blame] | 816 | yield 0, {} |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 817 | if count < self.max: |
| 818 | for alt in self.content: |
| 819 | for c0, r0 in generate_matches(alt, nodes): |
| 820 | for c1, r1 in self._recursive_matches(nodes[c0:], count+1): |
| 821 | r = {} |
| 822 | r.update(r0) |
| 823 | r.update(r1) |
| 824 | yield c0 + c1, r |
| 825 | |
| 826 | |
| 827 | class NegatedPattern(BasePattern): |
| 828 | |
| 829 | def __init__(self, content=None): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 830 | """ |
| 831 | Initializer. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 832 | |
| 833 | The argument is either a pattern or None. If it is None, this |
| 834 | only matches an empty sequence (effectively '$' in regex |
| 835 | lingo). If it is not None, this matches whenever the argument |
| 836 | pattern doesn't have any matches. |
| 837 | """ |
| 838 | if content is not None: |
| 839 | assert isinstance(content, BasePattern), repr(content) |
| 840 | self.content = content |
| 841 | |
| 842 | def match(self, node): |
| 843 | # We never match a node in its entirety |
| 844 | return False |
| 845 | |
| 846 | def match_seq(self, nodes): |
| 847 | # We only match an empty sequence of nodes in its entirety |
| 848 | return len(nodes) == 0 |
| 849 | |
| 850 | def generate_matches(self, nodes): |
| 851 | if self.content is None: |
| 852 | # Return a match if there is an empty sequence |
| 853 | if len(nodes) == 0: |
| 854 | yield 0, {} |
| 855 | else: |
| 856 | # Return a match if the argument pattern has no matches |
| 857 | for c, r in self.content.generate_matches(nodes): |
| 858 | return |
| 859 | yield 0, {} |
| 860 | |
| 861 | |
| 862 | def generate_matches(patterns, nodes): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 863 | """ |
| 864 | Generator yielding matches for a sequence of patterns and nodes. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 865 | |
| 866 | Args: |
| 867 | patterns: a sequence of patterns |
| 868 | nodes: a sequence of nodes |
| 869 | |
| 870 | Yields: |
| 871 | (count, results) tuples where: |
| 872 | count: the entire sequence of patterns matches nodes[:count]; |
| 873 | results: dict containing named submatches. |
| 874 | """ |
| 875 | if not patterns: |
| 876 | yield 0, {} |
| 877 | else: |
| 878 | p, rest = patterns[0], patterns[1:] |
| 879 | for c0, r0 in p.generate_matches(nodes): |
| 880 | if not rest: |
| 881 | yield c0, r0 |
| 882 | else: |
| 883 | for c1, r1 in generate_matches(rest, nodes[c0:]): |
| 884 | r = {} |
| 885 | r.update(r0) |
| 886 | r.update(r1) |
| 887 | yield c0 + c1, r |