-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntax_tree.lua
363 lines (363 loc) · 9.37 KB
/
syntax_tree.lua
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
local insert, remove, concat
do
local _obj_0 = table
insert, remove, concat = _obj_0.insert, _obj_0.remove, _obj_0.concat
end
local Source
Source = require("code_obj").Source
local List, Dict
do
local _obj_0 = require('containers')
List, Dict = _obj_0.List, _obj_0.Dict
end
local Files = require('files')
local unpack = unpack or table.unpack
local as_lua
as_lua = function(self)
if type(self) == 'number' then
return tostring(self)
end
do
local mt = getmetatable(self)
if mt then
do
local _as_lua = mt.as_lua
if _as_lua then
return _as_lua(self)
end
end
end
end
if self.as_lua then
return self:as_lua()
end
return error("Not supported: " .. tostring(self))
end
local SyntaxTree
do
local _class_0
local _base_0 = {
__tostring = function(self)
local bits
do
local _accum_0 = { }
local _len_0 = 1
for _index_0 = 1, #self do
local b = self[_index_0]
_accum_0[_len_0] = type(b) == 'string' and b:as_lua() or tostring(b)
_len_0 = _len_0 + 1
end
bits = _accum_0
end
for k, v in pairs(self) do
if not (bits[k] or k == 'type' or k == 'source') then
table.insert(bits, tostring(k) .. "=" .. tostring(type(v) == 'string' and v:as_lua() or v))
end
end
return tostring(self.type) .. "{" .. tostring(table.concat(bits, ", ")) .. "}"
end,
__eq = function(self, other)
if type(self) ~= type(other) or #self ~= #other or getmetatable(self) ~= getmetatable(other) then
return false
end
if self.type ~= other.type then
return false
end
for i = 1, #self do
if self[i] ~= other[i] then
return false
end
end
return true
end,
as_lua = function(self)
local bits
do
local _accum_0 = { }
local _len_0 = 1
for _index_0 = 1, #self do
local b = self[_index_0]
_accum_0[_len_0] = as_lua(b)
_len_0 = _len_0 + 1
end
bits = _accum_0
end
for k, v in pairs(self) do
if not (bits[k]) then
table.insert(bits, "[ " .. tostring(as_lua(k)) .. "]=" .. tostring(as_lua(v)))
end
end
return "SyntaxTree{" .. tostring(table.concat(bits, ", ")) .. "}"
end,
get_source_file = function(self)
return self.__class.source_code_for_tree[self]
end,
get_source_code = function(self)
return self.__class.source_code_for_tree[self]:sub(self.source.start, self.source.stop - 1)
end,
add = function(self, ...)
local n = #self
for i = 1, select('#', ...) do
self[n + i] = select(i, ...)
end
self.stub = nil
end,
with = function(self, fn)
if type(fn) == 'table' then
local replacements = fn
fn = function(t)
if t.type == "Var" then
do
local r = replacements[t:as_var()]
if r then
return r
end
end
end
end
end
local replacement = fn(self)
if replacement == false then
return nil
end
if replacement then
if SyntaxTree:is_instance(replacement) then
do
local _tbl_0 = { }
for k, v in pairs(replacement) do
_tbl_0[k] = v
end
replacement = _tbl_0
end
replacement.source = self.source
if self.comments then
replacement.comments = {
unpack(self.comments)
}
end
replacement = SyntaxTree(replacement)
end
else
replacement = {
source = self.source,
comments = self.comments and {
unpack(self.comments)
}
}
local changes = false
for k, v in pairs(self) do
local _continue_0 = false
repeat
replacement[k] = v
if SyntaxTree:is_instance(v) then
local r = v:with(fn)
if r == v or r == nil then
_continue_0 = true
break
end
changes = true
replacement[k] = r
end
_continue_0 = true
until true
if not _continue_0 then
break
end
end
if not (changes) then
return self
end
replacement = SyntaxTree(replacement)
end
return replacement
end,
contains = function(self, subtree)
if subtree == self then
return true
end
for k, v in pairs(self) do
if SyntaxTree:is_instance(v) then
if v:contains(subtree) then
return true
end
end
end
return false
end,
get_args = function(self)
assert(self.type == "Action" or self.type == "MethodCall", "Only actions and method calls have arguments")
local args = { }
if self.type == "MethodCall" then
args[1] = self[1]
for i = 2, #self do
local _list_0 = self[i]
for _index_0 = 1, #_list_0 do
local tok = _list_0[_index_0]
if type(tok) ~= 'string' then
args[#args + 1] = tok
end
end
end
else
for _index_0 = 1, #self do
local tok = self[_index_0]
if type(tok) ~= 'string' then
args[#args + 1] = tok
end
end
end
return args
end,
get_stub = function(self)
local _exp_0 = self.type
if "Action" == _exp_0 then
local stub_bits = { }
local arg_i = 1
for _index_0 = 1, #self do
local a = self[_index_0]
if type(a) == 'string' then
stub_bits[#stub_bits + 1] = a
else
stub_bits[#stub_bits + 1] = arg_i
arg_i = arg_i + 1
end
end
while type(stub_bits[#stub_bits]) == 'number' do
stub_bits[#stub_bits] = nil
end
return concat(stub_bits, " ")
elseif "MethodCall" == _exp_0 then
return "0, " .. table.concat((function()
local _accum_0 = { }
local _len_0 = 1
for i = 2, #self do
_accum_0[_len_0] = self[i]:get_stub()
_len_0 = _len_0 + 1
end
return _accum_0
end)(), "; ")
else
return error(tostring(self.type) .. "s do not have stubs")
end
end,
as_var = function(self)
assert(self.type == "Var")
if type(self[1]) == 'string' then
return self[1]
else
return self[1]:get_stub()
end
end,
matching = function(self, patt)
if patt.type == "Var" then
return {
[patt:as_var()] = self
}
end
if patt.type ~= self.type then
return nil
end
if patt.type == "Action" and patt:get_stub() ~= self:get_stub() then
return nil
end
if #self ~= #patt then
return nil
end
local match = { }
for i = 1, #self do
local v = self[i]
local pv = patt[i]
if type(v) ~= type(pv) then
return nil
end
if type(v) ~= 'table' then
if not (v == pv) then
return nil
end
else
local m = v:matching(pv)
if not (m) then
return nil
end
for mk, mv in pairs(m) do
if match[mk] and match[mk] ~= mv then
return nil
end
match[mk] = mv
end
end
end
return Dict(match)
end,
_breadth_first = function(self)
coroutine.yield(self)
for _index_0 = 1, #self do
local child = self[_index_0]
if getmetatable(child) == SyntaxTree.__base then
child:_breadth_first()
end
end
end,
breadth_first = function(self)
return coroutine.create(function()
return self:_breadth_first()
end)
end,
_depth_first = function(self)
coroutine.yield(self)
for _index_0 = 1, #self do
local child = self[_index_0]
if getmetatable(child) == SyntaxTree.__base then
child:_depth_first()
end
end
end,
depth_first = function(self)
return coroutine.create(function()
return self:_depth_first()
end)
end
}
_base_0.__index = _base_0
_class_0 = setmetatable({
__init = function() end,
__base = _base_0,
__name = "SyntaxTree"
}, {
__index = _base_0,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
local self = _class_0
self.source_code_for_tree = setmetatable({ }, {
__index = function(self, t)
local s = t.source
local f = Files.read(s.filename)
return f
end,
__mode = "k"
})
self.is_instance = function(self, t)
return type(t) == 'table' and getmetatable(t) == self.__base
end
SyntaxTree = _class_0
end
SyntaxTree.__base.__type = "a Syntax Tree"
getmetatable(SyntaxTree).__call = function(self, t, ...)
if type(t.source) == 'string' then
t.source = Source:from_string(t.source)
end
setmetatable(t, self.__base)
for i = 1, select("#", ...) do
t[i] = select(i, ...)
end
if t.type == 'Action' then
t.stub = t:get_stub()
end
return t
end
return SyntaxTree