-
Notifications
You must be signed in to change notification settings - Fork 0
/
emitter.lua
330 lines (257 loc) · 7.15 KB
/
emitter.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
--[[
MIT License
Copyright (c) 2019 emmachase
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]
local util = require("util")
local emitter = {}
-- local counter = 0
-- local function makeName()
-- counter = counter + 1
-- return "n" .. counter
-- end
--[[
local states = {
s1 = function(nextChar) ... end,
...
}
local acceptStates = {s1 = true, ...}
return function match(str)
local strlen = #str
local state = s1
local allMatches, ai = {}, 1
for startChar = 1, strlen do -- Conditional upon properties.clampStart
local ci = 0
while state and ci <= strlen do
if acceptStates[state] then
if ci == strlen then -- Conditional upon properties.clampEnd
allMatches[ai] = {str:sub(1, ci), startChar, ci + startChar - 1}
end
end
state = states[state](str:sub(ci + 1, ci + 1))
ci = ci + 1
end
end
return unpack(allMatches)
end
]]
local function generateFunction(state)
if #state.edges == 0 then
return "function() end"
end
local output = "function(char)"
local dests = {}
for i = 1, #state.edges do
local edge = state.edges[i]
local dest = edge.dest
dests[dest] = dests[dest] or {}
dests[dest][#dests[dest] + 1] = edge.condition
end
local prefix = "if"
for dest, conds in pairs(dests) do
output = output .. "\n " .. prefix
table.sort(conds)
local ranges = {}
local singles = {}
while #conds > 0 do
if #conds == 1 then
singles[#singles + 1] = conds[1]
break
elseif #conds == 2 then
singles[#singles + 1] = conds[1]
singles[#singles + 1] = conds[2]
break
end
local val, index = conds[1]:byte(), 2
while conds[index]:byte() - index + 1 == val do
index = index + 1
if index > #conds then
break
end
end
index = index - 1
if index == 1 then
singles[#singles + 1] = table.remove(conds, 1)
elseif index == 2 then
singles[#singles + 1] = table.remove(conds, 1)
singles[#singles + 1] = table.remove(conds, 1)
else
ranges[#ranges + 1] = {string.char(val), string.char(val + index - 1)}
for i = 1, index do
table.remove(conds, 1)
end
end
end
local first = true
for i = 1, #ranges do
local range = ranges[i]
if first then
first = false
else
output = output .. " or"
end
output = output .. " (char >= " .. range[1]:byte() .. " and char <= " .. range[2]:byte() .. ")"
-- if range[1] == "]" then
-- output = output .. "%]"
-- elseif range[1]:match("[a-zA-Z]") then
-- output = output .. range[1]
-- else
-- output = output .. "\\" .. range[1]:byte()
-- end
-- output = output .. "-"
-- if range[2] == "]" then
-- output = output .. "%]"
-- elseif range[2]:match("[a-zA-Z]") then
-- output = output .. range[2]
-- else
-- output = output .. "\\" .. range[2]:byte()
-- end
end
for i = 1, #singles do
if first then
first = false
else
output = output .. " or"
end
output = output .. " char == " .. singles[i]:byte()
-- if singles[i]:match("[a-zA-Z]") then
-- output = output .. singles[i]
-- else
-- output = output .. "%\\" .. singles[i]:byte()
-- end
end
output = output .. " then return " .. dest
prefix = "elseif"
end
output = output .. " end\n end"
return output
end
--[[
machine = {
states = {
[sName] = {edges = {}}
},
startState = sName,
acceptStates = {[sName] = true}
}
]]
local function numericize(dfa)
local newMachine = util.deepClone(dfa)
local oldNames = {}
local newNames = {}
local counter = 0
for k, v in pairs(dfa.states) do
counter = counter + 1
newMachine.states[k] = nil
newMachine.states[counter] = v
newNames[k] = counter
oldNames[counter] = k
end
for i = 1, counter do
local oldEdges = dfa.states[oldNames[i]].edges
local newEdges = newMachine.states[i].edges
local n = #oldEdges
for j = 1, n do
newEdges[j].dest = newNames[oldEdges[j].dest]
end
end
newMachine.startState = newNames[dfa.startState]
for k, v in pairs(dfa.acceptStates) do
newMachine.acceptStates[k] = nil
newMachine.acceptStates[newNames[k]] = v
end
return newMachine
end
function emitter.generateLua(dfa)
dfa = numericize(dfa)
local output = [[
local unpack = unpack or table.unpack
local states = {
]]
for i = 1, #dfa.states do
local state = dfa.states[i]
output = output .. " " .. generateFunction(state) .. ",\n"
end
output = output .. [[}
local stateEntries = {
]]
for i = 1, #dfa.states do
output = output .. " {"
local state = dfa.states[i]
local entries = util.nub(state.enter)
for j = 1, #entries do
output = output .. "'" .. entries[j] .. "',"
end
output = output .. "},\n"
end
output = output .. [[}
local acceptStates = {]]
for state in pairs(dfa.acceptStates) do
output = output .. "[" .. state .."] = true,"
end
output = output .. [[}
return function(str)
local strlen = #str
local allMatches, ai = {}, 1
]]
if dfa.properties.clampStart then
output = output .. "local startChar = 1 do\n"
else
output = output .. "for startChar = 1, strlen do\n"
end
output = output .. [[
local state = ]]
output = output .. dfa.startState
output = output .. [[
local ci = startChar - 1
while state and ci <= strlen do
if acceptStates[state] then
]]
if dfa.properties.clampEnd then
output = output .. [[
if ci == strlen then
]]
else
output = output .. [[
do
]]
end
output = output .. [[allMatches[ai] = {str:sub(startChar, ci), startChar, ci}
ai = ai + 1
end
end
local char = str:sub(ci + 1, ci + 1):byte()
if char then
state = states[state](char)
end
ci = ci + 1
end
end
local result
for i = 1, #allMatches do
if (not result) or #allMatches[i][1] > #result[1] then
result = allMatches[i]
end
end
if result then
return unpack(result)
end
end
]]
return output
end
return emitter