Skip to content

Commit c55d4bf

Browse files
committed
Add token property to base AST class
1 parent 85557b1 commit c55d4bf

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

src/ast/ast.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# ----------------------------------------------------------------------
1111

1212
from typing import Callable, Any
13+
1314
import types
1415
from .tree import Tree
1516

@@ -20,7 +21,9 @@
2021
class Ast(Tree):
2122
""" Adds some methods for easier coding...
2223
"""
23-
pass
24+
@property
25+
def token(self):
26+
return self.__class__
2427

2528

2629
class NodeVisitor:
@@ -45,14 +48,13 @@ def visit(self, node):
4548

4649
def _visit(self, node):
4750
methname = 'visit_' + node.token
48-
meth = getattr(self, methname, None)
49-
if meth is None:
50-
meth = self.generic_visit
51+
meth = getattr(self, methname, self.generic_visit)
52+
5153
return meth(node)
5254

5355
@staticmethod
5456
def generic_visit(node: Ast):
55-
raise RuntimeError("No {}() method defined".format('visit_' + node.token))
57+
raise RuntimeError(f"No visit_{node.token}() method defined")
5658

5759
def filter_inorder(self,
5860
node,

0 commit comments

Comments
 (0)