Concrete implementation for leaf nodes.
| 315 | |
| 316 | |
| 317 | class Leaf(Base): |
| 318 | |
| 319 | """Concrete implementation for leaf nodes.""" |
| 320 | |
| 321 | # Default values for instance variables |
| 322 | _prefix = "" # Whitespace and comments preceding this token in the input |
| 323 | lineno = 0 # Line where this token starts in the input |
| 324 | column = 0 # Column where this token tarts in the input |
| 325 | |
| 326 | def __init__(self, type, value, |
| 327 | context=None, |
| 328 | prefix=None, |
| 329 | fixers_applied=[]): |
| 330 | """ |
| 331 | Initializer. |
| 332 | |
| 333 | Takes a type constant (a token number < 256), a string value, and an |
| 334 | optional context keyword argument. |
| 335 | """ |
| 336 | assert 0 <= type < 256, type |
| 337 | if context is not None: |
| 338 | self._prefix, (self.lineno, self.column) = context |
| 339 | self.type = type |
| 340 | self.value = value |
| 341 | if prefix is not None: |
| 342 | self._prefix = prefix |
| 343 | self.fixers_applied = fixers_applied[:] |
| 344 | |
| 345 | def __repr__(self): |
| 346 | """Return a canonical string representation.""" |
| 347 | return "%s(%r, %r)" % (self.__class__.__name__, |
| 348 | self.type, |
| 349 | self.value) |
| 350 | |
| 351 | def __unicode__(self): |
| 352 | """ |
| 353 | Return a pretty string representation. |
| 354 | |
| 355 | This reproduces the input source exactly. |
| 356 | """ |
| 357 | return self.prefix + str(self.value) |
| 358 | |
| 359 | if sys.version_info > (3, 0): |
| 360 | __str__ = __unicode__ |
| 361 | |
| 362 | def _eq(self, other): |
| 363 | """Compare two nodes for equality.""" |
| 364 | return (self.type, self.value) == (other.type, other.value) |
| 365 | |
| 366 | def clone(self): |
| 367 | """Return a cloned (deep) copy of self.""" |
| 368 | return Leaf(self.type, self.value, |
| 369 | (self.prefix, (self.lineno, self.column)), |
| 370 | fixers_applied=self.fixers_applied) |
| 371 | |
| 372 | def leaves(self): |
| 373 | yield self |
| 374 |
no outgoing calls