This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
parser.py
129 lines (97 loc) · 4.21 KB
/
parser.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from rply import ParserGenerator
import imp_ast as ast
import lexer
PRECEDENCE = [
('left', ["PLUS", "MINUS"]),
('left', ["MUL"]),
]
class Parser():
def __init__(self):
self.pg = ParserGenerator(lexer.TOKENS.keys())
@self.pg.production("program : statement")
def program(p):
return ast.Program(p[0])
@self.pg.production("function : FUNCTION PAREN_OPEN arguments PAREN_CLOSE BRACE_OPEN statement RETURN expression BRACE_CLOSE")
def function2(p):
return ast.Function(p[2], p[5], p[7])
@self.pg.production("expression : function")
def fun_exp(p):
return p[0]
@self.pg.production("expression : TEXT")
def text(p):
return ast.Text(p[0].value)
@self.pg.production("expression : NAME PAREN_OPEN parameters PAREN_CLOSE")
def function_call(p):
return ast.FunctionCall(p[0].value, p[2])
@self.pg.production("parameters : expression")
def parameter(p):
return [p[0]]
@self.pg.production("parameters : expression COMMA parameters")
def parameters(p):
return [p[0]] + p[2]
@self.pg.production("arguments : NAME")
def argument(p):
return [p[0].value]
@self.pg.production("arguments : NAME COMMA arguments")
def arguments(p):
return [p[0].value] + p[2]
@self.pg.production("statement : WHILE PAREN_OPEN expression PAREN_CLOSE BRACE_OPEN statement BRACE_CLOSE")
def loop(p):
return ast.While(p[2], p[5])
@self.pg.production("statement : FOR PAREN_OPEN NAME DOUBLE_COLON expression PAREN_CLOSE BRACE_OPEN statement BRACE_CLOSE")
def forloop(p):
return ast.For(p[2].value, p[4], p[7])
@self.pg.production("statement : IF PAREN_OPEN expression PAREN_CLOSE BRACE_OPEN statement BRACE_CLOSE ELSE BRACE_OPEN statement BRACE_CLOSE")
def if_else(p):
return ast.IfElse(p[2], p[5], p[9])
@self.pg.production("statement : IF PAREN_OPEN expression PAREN_CLOSE BRACE_OPEN statement BRACE_CLOSE NOELSE")
def if_noelse(p):
return ast.IfElse(p[2], p[5], ast.Skip())
@self.pg.production("statement : TRACE PAREN_OPEN expression PAREN_CLOSE")
def trace(p):
return ast.Trace(p[2])
@self.pg.production("statement : ERROR PAREN_OPEN PAREN_CLOSE")
def error(p):
return ast.Error()
@self.pg.production("statement : NAME EQUAL expression")
def assignment(p):
return ast.Assignment(p[0].value, p[2])
@self.pg.production("statement : statement SEMI_COLON statement")
def conjunction(p):
return ast.Conjunction(p[0], p[2])
@self.pg.production("statement : ")
def skip(p):
return ast.Skip()
@self.pg.production("expression : NUMBER")
def number(p):
return ast.Number(p[0].value)
@self.pg.production("expression : NAME")
def variable(p):
return ast.Variable(p[0].value)
@self.pg.production("expression : expression BINOP expression")
def binop(p):
op = p[1].value
return ast.BinaryOp(op, p[0], p[2])
@self.pg.production("expression : BUILTIN PAREN_OPEN expression PAREN_CLOSE")
def builtin(p):
return ast.BuiltIn(p[0].value, p[2])
@self.pg.production("expression : BRACK_OPEN parameters BRACK_CLOSE")
def native_list(p):
return ast.List(p[1])
@self.pg.production("expression : FIELDS PAREN_OPEN expression PAREN_CLOSE")
def fields(p):
return ast.Fields(p[2])
@self.pg.production("expression : expression BRACK_OPEN expression BRACK_CLOSE")
def index_access(p):
return ast.IndexAccess(p[0], p[2])
@self.pg.production("expression : PAREN_OPEN expression PAREN_CLOSE")
def paren(p):
return p[1]
@self.pg.production("expression : PAREN_OPEN PAREN_CLOSE")
def unit(p):
return ast.Unit()
@self.pg.error
def error_handle(token):
raise ValueError(token)
def get_parser(self):
return self.pg.build()