-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.py
69 lines (52 loc) · 1.65 KB
/
ast.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""
Classes representing the parsed grammar AST.
"""
class Node:
pass
class Root(Node):
def __init__(self, statements):
self.statements = statements
def __str__(self):
return ' ;\n'.join(map(str, self.statements)) + ' ;\n'
class TokenDefinition(Node):
def __init__(self, name, regexp):
self.name = name
self.regexp = regexp
def __str__(self):
return self.name + ' := "' + str(self.regexp) + '"'
class RuleDefinition(Node):
def __init__(self, name, expression):
self.name = name
self.expression = expression
def __str__(self):
return self.name + ' := ' + str(self.expression)
class AlternationExpr(Node):
def __init__(self, expressions):
self.expressions = expressions
def __str__(self):
return '( ' + ' | '.join(map(str, self.expressions)) + ' )'
class FollowExpr(Node):
def __init__(self, expressions):
self.expressions = expressions
def __str__(self):
return '( ' + ' => '.join(map(str, self.expressions)) + ' )'
class PostfixExpr(Node):
def __init__(self, operator, expression):
self.expression = expression
# Either '?', '*' or None
self.operator = operator
def __str__(self):
if self.operator is not None:
return str(self.expression) + self.operator
else:
return str(self.expression)
class TokenLiteral(Node):
def __init__(self, text):
self.text = text
def __str__(self):
return "'%s'" % (self.text,)
class RuleTokenId(Node):
def __init__(self, name):
self.name = name
def __str__(self):
return self.name