-
Notifications
You must be signed in to change notification settings - Fork 0
/
om_parser
259 lines (243 loc) · 6.1 KB
/
om_parser
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
local om_parser = {
tlist = {}
}
om_parser.tokenspecs = {
["TOK_COMMENT"] = {},
["TOK_EQUALS"] = {
needsLeft = {"TOK_NUMBER", "TOK_STRING", "TOK_NAME"},
needsRight = {"TOK_NUMBER", "TOK_STRING", "TOK_NAME"}
},
["TOK_ASSIGN"] = {
needsLeft = {"TOK_NAME"},
needsRight = {"TOK_NUMBER", "TOK_STRING", "TOK_NAME"}
},
["TOK_GT"] = {
needsLeft = {"TOK_NAME", "TOK_NUMBER"},
needsRight = {"TOK_NAME", "TOK_NUMBER"},
},
["TOK_GE"] = {
needsLeft = {"TOK_NAME", "TOK_NUMBER"},
needsRight = {"TOK_NAME", "TOK_NUMBER"}
},
["TOK_LT"] = {
needsLeft = {"TOK_NAME", "TOK_NUMBER"},
needsRight = {"TOK_NAME", "TOK_NUMBER"}
},
["TOK_LE"] = {
needsLeft = {"TOK_NAME", "TOK_NUMBER"},
needsRight = {"TOK_NAME", "TOK_NUMBER"}
},
["TOK_MUL"] = {
needsLeft = {"TOK_NAME", "TOK_NUMBER"},
needsRight = {"TOK_NAME", "TOK_NUMBER"}
},
["TOK_DIV"] = {
needsLeft = {"TOK_NAME", "TOK_NUMBER"},
needsRight = {"TOK_NAME", "TOK_NUMBER"}
},
["TOK_ADD"] = {
needsLeft = {"TOK_NAME", "TOK_NUMBER"},
needsRight = {"TOK_NAME", "TOK_NUMBER"}
},
["TOK_SUB"] = {
needsLeft = {"TOK_NAME", "TOK_NUMBER"},
needsRight = {"TOK_NAME", "TOK_NUMBER"}
},
["TOK_EQUALS"] = {
needsLeft = {"TOK_NAME", "TOK_NUMBER", "TOK_STRING"},
needsRight = {"TOK_NAME", "TOK_NUMBER", "TOK_STRING"}
},
["TOK_BRACKET_OPEN"] = {
},
["TOK_BRACKET_CLOSE"] = {
},
["TOK_PACKAGE"] = {
spaceLeft = 1,
needsRight = "TOK_NAME"
},
["TOK_IMPORT"] = {
spaceLeft = 1,
needsRight = "TOK_NAME"
},
["TOK_EXTENDS"] = {
spaceLeft = 1,
spaceRight = 1,
needsRight = "TOK_NAME"
},
["TOK_IMPLEMENTS"] = {
spaceLeft = 1,
spaceRight = 1,
needsRight = "TOK_NAME"
}
}
--[[
BEGIN PARSER
]]
function om_parser:lex(input)
om.lexer:resetState(input)
om.lexer:initialize()
while true do
local values = {om.lexer:lexer()}
self.tlist[#self.tlist + 1] = values
end
end
function om_parser:verify()
for a = 1, #self.tlist do
local t = self.tlist[a][1]
if self.tokenspecs[t] then
local spec = self.tokenspecs[t]
if spec.spaceLeft then
for b = -1, -spec.spaceLeft, -1 do
if self.tlist[a + b] then
if self.tlist[a + b] ~= "TOK_SPACE" then
error("Unexpected Symbol")
end
end
end
end
if spec.spaceRight then
for b = 1, spec.spaceRight do
if self.tlist[a + b] then
if self.tlist[a + b] ~= "TOK_SPACE" then
error("Unexpected Symbol")
end
end
end
end
if spec.needsLeft then
local counter = 1
while true do
if self.tlist[a - counter] == spec.needsLeft then
break
elseif self.tlist[a - counter] ~= "TOK_SPACE" then
error("Unexpected Symbol")
end
local counter = counter + 1
end
end
if spec.needsRight then
local counter = 1
while true do
if self.tlist[a - counter] == spec.needsRight then
break
elseif self.tlist[a - counter] ~= "TOK_SPACE" then
error("Unexpected Symbol")
end
local counter = counter + 1
end
end
end
end
end
function om_parser:unexpsym()
error("Error parsing tokens: Unexpected symbol")
end
function om_parser:parseBlock(pos)
local t = "TOK_SPACE"
local arg = nil
local tree = {}
local lf = {}
while true do
if t ~= "TOK_SPACE" then
if lf.tok == nil then
if t == "TOK_PRIVATE" then
lf = {acc = 2, tok = "assign", state = "m"}
elseif t == "TOK_PUBLIC" then
lf = {acc = 0, tok = "assign", state = "m"}
elseif t == "TOK_PROTECTED" then
lf = {acc = 1, tok = "assign", state = "m"}
elseif t == "TOK_FINAL" then
lf = {acc = 3, tok = "assign", state = "m", transiency = "final"}
elseif t == "TOK_I" then
lf = {acc = 3, tok = "assign", state = "m", statism = "static"}
elseif t == "TOK_TRANSIENT" then
lf = {acc = 3, tok = "assign", state = "m", transiency = "transient"}
elseif t == "TOK_NAME" then
lf = {acc = 3, tok = "assign", state = "n", type = arg}
elseif t == "TOK_NUMBER" then
lf = {acc = 3, tok = "assign", state = "n", atomtype = "number"}
elseif t == "TOK_BOOLEAN" then
lf = {acc = 3, tok = "assign", state = "n", atomtype = "boolean"}
elseif t == "TOK_STRING" then
lf = {acc = 3, tok = "assign", state = "n", atomtype = "string"}
elseif t == "TOK_MAP" then
lf = {acc = 3, tok = "assign", state = "n", atomtype = "map"}
elseif t == "TOK_CHAR" then
lf = {acc = 3, tok = "assign", state = "n", atomtype = "character"}
else
om.error.throwUnexpectedToken()
end
elseif lf.tok == "assign" then
if lf.state == "m" then
if t == "TOK_PRIVATE" then
lf.acc = 2
elseif t == "TOK_PUBLIC" then
lf.acc = 0
elseif t == "TOK_PROTECTED" then
lf.acc = 1
elseif t == "TOK_TRANSIENT" then
lf.transiency = "transient"
elseif t == "TOK_STATIC" then
lf.statism = "static"
elseif t == "TOK_FINAL" then
lf.transiency = "final"
elseif t == "TOK_NAME" then
lf.type = arg
lf.state = "n"
elseif t == "TOK_NUMBER" then
lf.atomtype = "number"
lf.state = "n"
elseif t == "TOK_BOOLEAN" then
lf.atomtype = "boolean"
lf.state = "n"
elseif t == "TOK_STRING" then
lf.atomtype = "boolean"
lf.state = "n"
elseif t == "TOK_CHAR" then
lf.atomtype = "character"
lf.state = "n"
elseif t == "TOK_MAP" then
lf.atomtype = "map"
lf.state = "n"
else
om.error.throwUnexpectedToken()
end
elseif lf.state == "n" then
if t == "TOK_NAME" then
lf.name = arg
lf.state = "s"
else
om.error.throwUnexpectedToken()
end
elseif lf.state == "s" then
if t == "TOK_ASSIGN" then
lf.state = "v"
else
om.error.throwUnexpectedToken()
end
elseif lf.state == "v" then
-- parse value
end
end
end
pos = pos + 1
t = self.tlist[pos][1]
arg = self.tlist[pos][2]
end
end
function om_parser:buildTree()
self:parseBlock(0)
end
function om_parser:buildBytecode()
end
function om_parser:loadstring(file)
self.input = om.input.new(file)
self:lex(input)
self:verify()
self:buildTree()
return loadstring(self:buildBytecode())
end
--[[
END PARSER
]]
return om_parser