-
Notifications
You must be signed in to change notification settings - Fork 2
/
ometa.lua
346 lines (296 loc) · 9.25 KB
/
ometa.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
package.path = './model/?.lua;./ometa/?.lua;./lib/?.lua;./?.lua;' .. package.path
local tostring, tonumber, select, type, getmetatable, setmetatable, rawget
= tostring, tonumber, select, type, getmetatable, setmetatable, rawget
local Types = require 'types'
local dataType, class = Types.dataType, Types.class
local Any, Array = Types.Any, Types.Array
local Streams = require 'streams'
local utils = require 'utils'
local unpack = unpack or table.unpack
local Record = dataType {
name = 'Record',
super = {};
}
local depth = 0
local cache = {}
local OMeta
OMeta = {
use = function(grammar)
local om = cache[grammar]
if not om then
om = OMeta.Input {grammar = grammar}
cache[grammar] = om
end
return om
end;
_do = function(oast, trans)
local last = require('ometa_ast2lua_ast_' .. (trans or 'reference')).toLuaAst(oast)
local lsrc = require 'lua_ast2source'.trans:matchMixed(last)
local gmod = loadstring(lsrc)
return gmod()
end;
doFile = function(path, trans)
local oast = require 'ometa_lua_grammar'.OMetaInLuaGrammar.block:matchFile(path)
return OMeta._do(oast, trans)
end;
doString = function(str, trans)
local oast = require 'ometa_lua_grammar'.OMetaInLuaGrammar.block:matchString(str)
return OMeta._do(oast, trans)
end;
}
OMeta.Input = class {
name = 'Input',
super = {Any};
memoizeParameters = false;
apply = function(self, ruleImpl)
local entryState = self.stream
local ruleType = type(ruleImpl)
if ruleType == 'function' then
local pass, result = ruleImpl(self)
if not pass then self.stream = entryState end
return pass, result
end
if ruleType == 'table' then
local behavior = ruleImpl.behavior
if not behavior then
-- "plain" type as a rule
if ruleImpl:isInstance(self.stream._head) then
return self:next()
else
return false, self.stream._head
end
else
return self:_memoize(ruleImpl, entryState, entryState, entryState)
end
end
-- primitive Lua value (string|number|boolean)
return self:applyWithArgs(self.grammar.exactly, ruleImpl)
end,
applyWithArgs = function(self, ruleImpl, ...)
local entryState = self.stream
if type(ruleImpl) == 'function' then
local pass, result = ruleImpl(self, ...)
if not pass then self.stream = entryState end
return pass, result
end
local behavior = ruleImpl.behavior
if not behavior then
-- expected behavior not yet specified
print('warning:', 'object does not implement rule behavior', ruleImpl)
return false
end
local argsn = select('#', ...)
local fnarity = ruleImpl.arity and ruleImpl.arity ~= -1 and ruleImpl.arity or argsn
if fnarity < argsn then
self.stream = self.stream:prepend(argsn - fnarity, select(fnarity + 1, ...))
end
if fnarity ~= 0 and not self.memoizeParameters then
local pass, result = behavior(self, ...)
if not pass then self.stream = entryState end
return pass, result
end
return self:_memoize(ruleImpl, entryState,
self.stream,
fnarity == 0 and self.stream or self.stream:prepend(fnarity, ...), ...)
end,
_memoize = function(self, ruleImpl, entryState, undoState, memoState, ...)
ruleImpl.count = ruleImpl.count + 1
local record = memoState.memo[ruleImpl]
if record == nil then
record = Record {
failer = true,
failerUsed = false
}
memoState.memo[ruleImpl] = record
local pass, result = ruleImpl.behavior(self, ...)
if not pass then
self.stream = entryState
return false, result
end
record.pass = pass
record.result = result
record.nextState = self.stream
record.failer = false
if record.failerUsed then
local sentinelState = self.stream
while true do
self.stream = undoState
local pass, result = ruleImpl.behavior(self, ...)
if not pass
or self.stream == sentinelState
then break
end
record.pass = pass
record.result = result
record.nextState = self.stream
end
end
else
ruleImpl.hits = ruleImpl.hits + 1
if record.failer then
record.failerUsed = true
return false
end
ruleImpl.cache = ruleImpl.cache + 1
end
self.stream = record.nextState
return record.pass, record.result
end,
applyForeign = function(self, target, ruleImpl)
local input = OMeta.use(target)
input.stream = self.stream
local pass, result = input:apply(ruleImpl)
if not pass then return false, result end
self.stream = input.stream
return pass, result
end,
applyForeignWithArgs = function(self, target, ruleImpl, ...)
local input = OMeta.use(target)
input.stream = self.stream
local pass, result = input:applyWithArgs(ruleImpl, ...)
if not pass then return false, result end
self.stream = input.stream
return pass, result
end,
next = function(self)
local result = self.stream:head()
if result == nil then return false end
self.stream = self.stream:tail()
return true, result
end,
collect = function(self, count)
local result, tl = Array {}, self.stream
for i = 1, count do
local hd = tl:head()
if hd == nil then return false, result end
result[i], tl = hd, tl:tail()
end
self.stream = tl
return true, result
end;
property = function(self, index)
self.stream = self.stream:property(index)
end;
match = function(self, ruleImpl, ...)
local pass, result
if not ... then
pass, result = self:apply(ruleImpl)
else
pass, result = self:applyWithArgs(ruleImpl, ...)
end
if not pass then
local stream = self.stream
while stream.tl ~= nil do
stream = stream.tl
end
if type(stream._source) == 'string' then
print(stream._source:sub(1, stream._index))
end
error('match failed at: ' .. self.stream._index .. ' - ' .. stream._index)
end
return result
end;
forString = function(self, str)
self.stream = Streams.StringInputStream:new(str)
return self
end,
forTable = function(self, tab)
self.stream = Streams.TableInputStream:new(tab)
return self
end,
forMixed = function(self, ...)
local res, num, len = Array {}, 0, select('#', ...)
for si = 1, len do
local element = select(si, ...)
if element ~= nil then
if type(element) == 'string' then
local last = #element
while last ~= 0 and element:byte(last) <= 32 do last = last - 1 end
if last ~= 0 then
local first = 1
while element:byte(first) <= 32 do first = first + 1 end
for sii = first, last do
num = num + 1
res[num] = element:sub(sii, sii)
end
end
else
-- if Array:isInstance(element) and #element ~= 0 then
-- for i = 1, #element do
-- num = num + 1
-- res[num] = element[i]
-- end
-- else
num = num + 1
res[num] = element
-- end
end
end
end
self.stream = Streams.TableInputStream:new(res)
return self
end,
forFile = function(self, path, binary)
self.stream = (binary and Streams.BinaryInputStream or Streams.StringInputStream):new(utils.readFile(path))
return self
end;
}
OMeta.Grammar = class {
name = 'Grammar',
super = {Any};
constructor = function(class, init)
for k, v in pairs(init) do
if OMeta.Rule:isInstance(v) then
v.grammar = init
end
end
return init
end;
merge = function(self, source)
for k, mrule in pairs(source) do
if OMeta.Rule:isInstance(mrule) then
local lrule = rawget(self, k)
if lrule == nil then
lrule = OMeta.Rule {
name = mrule.name,
grammar = self,
arity = mrule.arity,
behavior = mrule.behavior;
}
self[k] = lrule
else
if lrule.behavior ~= mrule.behavior then
local qname = k
if self._grammarName then qname = self._grammarName .. '::' .. qname end
print('merge conflict (rule ignored) for ' .. qname)
end
end
end
end
return self
end;
}
OMeta.Rule = class {
name = 'Rule',
super = {Any};
constructor = function(class, init)
if init.arity == nil then init.arity = -1 end
if init.count == nil then init.count = 0 end
if init.cache == nil then init.cache = 0 end
if init.hits == nil then init.hits = 0 end
return init
end;
matchString = function(self, str, ...)
return OMeta.use(self.grammar):forString(str):match(self, ...)
end;
matchTable = function(self, tab, ...)
return OMeta.use(self.grammar):forTable(tab):match(self, ...)
end;
matchMixed = function(self, ...)
return OMeta.use(self.grammar):forMixed(...):match(self)
end;
matchFile = function(self, path, binary, ...)
return OMeta.use(self.grammar):forFile(path, binary):match(self, ...)
end;
}
return OMeta