Concrete implementation for interior nodes.
| 205 | return str(self).encode("ascii") |
| 206 | |
| 207 | class Node(Base): |
| 208 | |
| 209 | """Concrete implementation for interior nodes.""" |
| 210 | |
| 211 | def __init__(self,type, children, |
| 212 | context=None, |
| 213 | prefix=None, |
| 214 | fixers_applied=None): |
| 215 | """ |
| 216 | Initializer. |
| 217 | |
| 218 | Takes a type constant (a symbol number >= 256), a sequence of |
| 219 | child nodes, and an optional context keyword argument. |
| 220 | |
| 221 | As a side effect, the parent pointers of the children are updated. |
| 222 | """ |
| 223 | assert type >= 256, type |
| 224 | self.type = type |
| 225 | self.children = list(children) |
| 226 | for ch in self.children: |
| 227 | assert ch.parent is None, repr(ch) |
| 228 | ch.parent = self |
| 229 | if prefix is not None: |
| 230 | self.prefix = prefix |
| 231 | if fixers_applied: |
| 232 | self.fixers_applied = fixers_applied[:] |
| 233 | else: |
| 234 | self.fixers_applied = None |
| 235 | |
| 236 | def __repr__(self): |
| 237 | """Return a canonical string representation.""" |
| 238 | return "%s(%s, %r)" % (self.__class__.__name__, |
| 239 | type_repr(self.type), |
| 240 | self.children) |
| 241 | |
| 242 | def __unicode__(self): |
| 243 | """ |
| 244 | Return a pretty string representation. |
| 245 | |
| 246 | This reproduces the input source exactly. |
| 247 | """ |
| 248 | return "".join(map(str, self.children)) |
| 249 | |
| 250 | if sys.version_info > (3, 0): |
| 251 | __str__ = __unicode__ |
| 252 | |
| 253 | def _eq(self, other): |
| 254 | """Compare two nodes for equality.""" |
| 255 | return (self.type, self.children) == (other.type, other.children) |
| 256 | |
| 257 | def clone(self): |
| 258 | """Return a cloned (deep) copy of self.""" |
| 259 | return Node(self.type, [ch.clone() for ch in self.children], |
| 260 | fixers_applied=self.fixers_applied) |
| 261 | |
| 262 | def post_order(self): |
| 263 | """Return a post-order iterator for the tree.""" |
| 264 | for child in self.children: |
no outgoing calls