| 1 |
@dataclass |
| 2 |
class DebugVisitor(Visitor[T]): |
| 3 |
tree_depth: int = 0 |
| 4 |
|
| 5 |
def visit_default(self, node: LN) -> Iterator[T]: |
| 6 |
indent = ' ' * (2 * self.tree_depth) |
| 7 |
if isinstance(node, Node): |
| 8 |
_type = type_repr(node.type) |
| 9 |
out(f'{indent}{_type}', fg='yellow') |
| 10 |
self.tree_depth += 1 |
| 11 |
for child in node.children: |
| 12 |
yield from self.visit(child) |
| 13 |
|
| 14 |
self.tree_depth -= 1 |
| 15 |
out(f'{indent}/{_type}', fg='yellow', bold=False) |
| 16 |
else: |
| 17 |
_type = token.tok_name.get(node.type, str(node.type)) |
| 18 |
out(f'{indent}{_type}', fg='blue', nl=False) |
| 19 |
if node.prefix: |
| 20 |
# We don't have to handle prefixes for `Node` objects since |
| 21 |
# that delegates to the first child anyway. |
| 22 |
out(f' {node.prefix!r}', fg='green', bold=False, nl=False) |
| 23 |
out(f' {node.value!r}', fg='blue', bold=False) |
| 24 |
|
| 25 |
@classmethod |
| 26 |
def show(cls, code: str) -> None: |
| 27 |
"""Pretty-prints a given string of `code`. |
| 28 |
|
| 29 |
Convenience method for debugging. |
| 30 |
""" |
| 31 |
v: DebugVisitor[None] = DebugVisitor() |
| 32 |
list(v.visit(lib2to3_parse(code))) |