-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamparser.lua
More file actions
334 lines (281 loc) · 7.65 KB
/
streamparser.lua
File metadata and controls
334 lines (281 loc) · 7.65 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
local module = {}
local dbg = require("debugger")
local error = require("error")
-- syntax.base contains the root-node
local syntax = require("lua_syntax_tree")
local keywords = {
"local", "function",
"return", "break",
"nil", "true",
"false", "if",
"else", "end",
"elseif", "do",
"for", "repeat",
"and", "or",
"not", "in",
"until", "while"
}
local function check_keyword(node)
if node == nil then return end
if node.type ~= "identifier" then return end
for k in keywords do
if node.content == k then
node.type = "keyword"; return
end
end
end
--[[
]]
local func_call,
varlist,
var,
prefixexp,
args,
do_block,
while_block,
repeat_block,
if_block,
for_block,
func_decl,
local_func_decl,
local_namelist,
exp,
local_decl
local function eat(stream, t_type)
local t = check_keyword(stream.peek())
error.assert(t.type == t_type, t, "Unexpected token")
end
local function maybe_eat(stream, t_type)
local t = stream.peek()
check_keyword(t)
if t.type == t_type then stream.next(); return true end
return false
end
local function ensure(stream, t_type)
local t = check_keyword(stream.peek())
error.assert(t.type == t_type, t, "Expected token "..t_type)
end
local function make_var(stream)
if stream.peek().type == "identifier" then
return {type="var", name=stream.next().content}
else return nil end
end
local function prefixexp(stream)
local peek = check_keyword(stream.peek())
-- Name or '('
if not (peek.type == "identifier" or peek.type == "lparen") then
return nil
end
local n, is_var
if peek.type == "identifier" then
n = {type="prefixexp",var=make_var(stream)}
is_var = true
else -- can only be lparen aka expression within parens
eat(stream, "lparen")
n = {type="prefixexp",exp=exp(stream)}
eat(stream, "rparen")
is_var = false
end
if maybe_eat(stream, "lsqbracket") then
n = {type="prefixexp", prefixexp=n, exp=exp(stream)}
eat(stream, "rsqbracket")
elseif maybe_eat(stream, "dot") then
error.assert(stream.peek().type == "identifier", stream.peek(),
"Expected identifier")
n = {type="prefixexp", prefixexp=n, var=stream.peek().content}
eat(stream, "identifier")
end
end
-- Parse Prefix as far as possible
-- The type of the last expression is returned as second value
-- The root node is returned as first value
-- Use "next" of nodes to traverse
local function eval_prefix(stream)
local peek = check_keyword(stream.peek())
local prefix
error.assert(peek.type == "identifier" or
peek.type == "lparen", peek, "Expected identifier or expression")
local last_type = ""
-- Eat Name or ( exp ) as first prefix
-- This handles the case of
if peek.type == "identifier" then
prefix = {type="var", name=peek.content}
eat("identifier")
last_type = "var"
elseif peek.type == "lparen" then
prefix = {type="exp", exp=exp(stream)}
eat("rparen")
last_type = "exp"
end
local parsed = true
while parsed do
local new_exp
parsed = false
peek = check_keyword(stream.peek())
if peek.type == "lsqbracket" then
-- exp (var)
-- ]
new_exp = {type="var_index", exp=exp(stream)}
eat("rsqbracket")
parsed = true
last_type = "var_index"
elseif peek.type == "dot" then
-- Name (var)
eat("dot")
ensure("identifier")
new_exp = {type="var_member", name=stream.peek().content}
eat("identifier")
parsed = true
last_type = "var_member"
elseif peek.type == "colon" then
-- Name (func)
-- Args
eat("colon")
ensure("identifier")
new_exp = {type="func_call",
name=stream.peek().content,
args=args(stream)}
parsed = true
last_type = "func_call"
end
local a = args(stream)
if a ~= nil then
-- Var with arguments (func)
new_exp = {type="func_call", args=a}
parsed = true
last_type = "func_call"
end
if parsed then
prefix.next = new_exp
end
end
return prefix, last_type
end--the world
-- Parse variable, has to begin with either identifier or a leftparen
-- since we can have expression following member access
local function var(stream)
end
local function for_block(AST, stream, node)
-- "for" keyword has been found, invalid parse gives error
-- For keyword can have 2 different layouts,
-- basic indexed iteration, or simple foreach iterator
-- both end up in this function, so we need to distinguish
-- which one is being parsed
end
local function local_decl(AST, stream, node)
-- Current node is a "local" keyword, invalid parse gives error
-- We can only have a function declaration
-- or a namelist, anything else is not permitted
-- Do note that function decl has different rules than a simple
-- global function declaration
end
local function varlist_or_call(AST,stream,node)
-- Parse either a variable list definition
-- or a function call
-- No specific node found, no error if nothing can be parsed
end
-- Lookup table for different keywords
-- used for simpler decision making on which
-- method to call based on content
local stat_lookup = {
["do"] = do_block,
["while"] = while_block,
["repeat"] = repeat_block,
["if"] = if_block,
["for"] = for_block,
["function"] = func_decl,
["local"] = local_decl
}
local function parse_stat(stream)
local node = stream.next()
check_keyword(node)
if node.type == "keyword" then
error.assert(stat_lookup[node.content] ~= nil, node, "Unexpected keyword")
return stat_lookup[node.content](stream,node)
else
-- Fallback, varlist or func call is only statements left possible
local n = varlist_or_call(stream,node)
error.assert(n ~= nil, node, "Unable to parse statement")
return n
end
end
local function parse_block(stream)
local block = {}
block.statements = {}
local stat = nil
repeat
stat = parse_stat(stream)
if stat ~= nil then
table.insert(block.statements, stat)
maybe_eat(stream, "semicolon")
end
until stat == nil
-- Laststat
local tmp = stream.peek()
check_keyword(tmp)
if tmp.type == "keyword" then
if tmp.content == "return" then
block.laststat = { type="return",
explist = parse_explist(stream)}
elseif tmp.content == "break" then
block.laststat = { type = "break"}
end
end
maybe_eat(stream, "semicolon")
return block
end
local function parse_stream(stream)
-- loop syntax.base and try to match each element
local AST = {}
AST.block = parse_block(stream)
return AST
end
local function make_node(syntax_node)
if syntax_node.type == nil then return nil end
local res = {}
res.type = syntax_node.type
res.subnodes = {}
return res
end
local function _parse(tok, syntax_base, ast)
syntax_base = syntax_base or syntax.base
ast = ast or {}
for k,v in ipairs(syntax_base) do
-- First level list of possible parse-entries
local node = make_node(v)
for k2,v2 in ipairs(v) do
-- Individual token parses (possibly terminals or recursive entries)
local is_term = v2.terminal or false
local is_repeating = v2.repeated or false
local is_optional = v2.optional or false
-- In case this entry has a validation-function then we need to validate the stream contents before accepting this node
local has_validation_func = v2.validation_func or false
local val = v2[1] -- first entry is the value of this entry
local next = tok.next()
if is_term then
-- Compare
if has_validation_func and val(next.content) then
assert(node)
end
end
if type(val) == "table" then
-- This entry is a recursive type
repeat
local res = _parse(tok, val, ast)
assert(is_optional or not (is_optional and res == nil) or is_repeating)
ast = res
until not is_repeating or (is_repeating and res == nil)
end
end
end
return ast
end
module.parse = function(tok)
assert(tok)
tok:push()
--ast = parse_stream(tok)
local ast = _parse(tok)
tok:pop()
return ast
end
return module