-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.lua
400 lines (325 loc) · 9.66 KB
/
parser.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
-- Regex Parser
-- Parses to an internal IL representation used for the construction of an NFA
--[[
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 parser = {}
function parser.lexRegex(regexStr)
local termEaten
local function peek()
return regexStr:sub(1, 1)
end
local pos = 0
local function eatc()
local c = peek()
termEaten = termEaten .. c
regexStr = regexStr:sub(2)
pos = pos + 1
return c
end
local switchTable = {
["|"] = "union",
["*"] = function()
if peek() == "?" then
eatc()
return "ng-star"
end
return "star"
end,
["+"] = function()
if peek() == "?" then
eatc()
return "ng-plus"
end
return "plus"
end,
["?"] = "optional",
["("] = "l-paren",
[")"] = "r-paren",
["{"] = "l-bracket",
["}"] = "r-bracket",
["."] = "any",
["^"] = "start",
["$"] = "eos",
["\\"] = function()
local metas = {d = "[0-9]", w = "[a-zA-Z]", n = "\n"}
local c = eatc()
if metas[c] then
regexStr = metas[c] .. regexStr
pos = pos - #metas[c]
return false
end
termEaten = termEaten:sub(2)
return "char"
end,
["["] = function()
if peek() == "^" then
eatc()
return "open-negset"
end
return "open-set"
end,
["]"] = "close-set",
["-"] = "range"
}
local tokens = {}
while #regexStr > 0 do
termEaten = ""
local c = eatc()
local lexFn = switchTable[c]
local ret = "char"
if lexFn then
if type(lexFn) == "string" then
ret = lexFn
else
ret = lexFn()
end
end
if ret then
tokens[#tokens + 1] = {
type = ret,
source = termEaten,
position = pos
}
end
end
tokens[#tokens + 1] = {type = "eof", source = "", position = pos + 1}
return tokens
end
--[[
Grammar:
<RE> ::= <simple-RE> <union-list>
<union-list> ::= "|" <simple-RE> <union-list> | <lambda>
<simple-RE> ::= <basic-RE> <basic-RE-list>
<basic-RE-list> ::= <basic-RE> <basic-RE-list> | <lambda>
<basic-RE> ::= <star> | <plus> | <ng-star> | <ng-plus> | <quantifier> | <elementary-RE>
<star> ::= <elementary-RE> "*"
<plus> ::= <elementary-RE> "+"
<ng-star> ::= <elementary-RE> "*?"
<ng-plus> ::= <elementary-RE> "+?"
<quantifier> ::= <elementary-RE> "{" <quantity> "}"
<quantity> ::= <digit> "," <digit> | <digit> ","
<elementary-RE> ::= <group> | <any> | <eos> | <char> | <set>
<group> ::= "(" <RE> ")"
<any> ::= "."
<eos> ::= "$"
<char> ::= any non metacharacter | "\" metacharacter
<set> ::= <positive-set> | <negative-set>
<positive-set> ::= "[" <set-items> "]"
<negative-set> ::= "[^" <set-items> "]"
<set-items> ::= <set-item> | <set-item> <set-items>
<set-item> ::= <range> | <char>
<range> ::= <char> "-" <char>
Special Chars: | * + *? +? ( ) . $ \ [ [^ ] -
]]
function parser.parse(tokenList)
local RE, unionList, simpleRE, basicRE, basicREList, elementaryRE, quantifier, group, set, setItems, setItem
local parseTable = {
unionList = {["union"] = 1, default = 2},
basicREList = {["union"] = 2, ["r-paren"] = 2, ["eof"] = 2, default = 1},
elementaryRE = {["l-paren"] = 1, ["any"] = 2, ["char"] = 3, ["open-set"] = 4, ["open-negset"] = 4},
setItems = {["close-set"] = 1, default = 2}
}
local function eat()
return table.remove(tokenList, 1)
end
local function uneat(token)
table.insert(tokenList, 1, token)
end
local function expect(token, source)
local tok = eat()
if tok.type ~= token then
error("Unexpected token '" .. tok.type .. "' at position " .. tok.position, 0)
end
if source and not tok.source:match(source) then
error("Unexpected '" .. tok.source .. "' at position " .. tok.position, 0)
end
return tok
end
local function getMyType(name, index)
local parseFn = parseTable[name][tokenList[index or 1].type] or parseTable[name].default
if not parseFn then
error("Unexpected token '" .. tokenList[index or 1].type .. "' at position " .. tokenList[index or 1].position, 0)
end
return parseFn
end
local function unrollLoop(container)
local list, i = {}, 1
while container do
list[i], i = container[1], i + 1
container = container[2]
end
return unpack(list)
end
-- <RE> ::= <simple-RE> <union-list>
function RE()
return {type = "RE", simpleRE(), unrollLoop(unionList())}
end
-- <union-list> ::= "|" <simple-RE> <union-list> | <lambda>
function unionList()
local parseFn = getMyType("unionList")
if parseFn == 1 then
eat()
return {type = "unionList", simpleRE(), unionList()}
else
return
end
end
-- <simple-RE> ::= <basic-RE> <basic-RE-list>
function simpleRE()
return {type = "simpleRE", basicRE(), unrollLoop(basicREList())}
end
-- <basic-RE> ::= <star> | <plus> | <ng-star> | <ng-plus> | <quantifier> | <elementary-RE>
function basicRE()
local atom = elementaryRE()
local token = eat()
local type = token.type
if type == "star" then
return {type = "star", atom}
elseif type == "plus" then
return {type = "plus", atom}
elseif type == "ng-star" then
return {type = "ng-star", atom}
elseif type == "ng-plus" then
return {type = "ng-plus", atom}
elseif type == "optional" then
return {type = "optional", atom}
elseif type == "l-bracket" then
uneat(token)
return {type = "quantifier", atom, quantifier = quantifier()}
else
uneat(token)
return {type = "atom", atom}
end
end
-- <quantifier> ::= <elementary-RE> "{" <quantity> "}"
-- <quantity> ::= <digit> "," <digit> | <digit> ","
function quantifier()
expect("l-bracket")
local firstDigit = ""
do
local nextTok = expect("char", "%d")
local src = nextTok.source
repeat
firstDigit = firstDigit .. src
nextTok = eat()
src = nextTok.source
until src:match("%D")
uneat(nextTok)
end
if tokenList[1].type == "r-bracket" then
eat()
local count = tonumber(firstDigit)
return {type = "count", count = count}
end
expect("char", ",")
local secondDigit = ""
if tokenList[1].source:match("%d") then
local src, nextTok = ""
repeat
secondDigit = secondDigit .. src
nextTok = eat()
src = nextTok.source
until src:match("%D")
uneat(nextTok)
end
expect("r-bracket")
return {type = "range", min = tonumber(firstDigit), max = tonumber(secondDigit) or math.huge}
end
-- <basic-RE-list> ::= <basic-RE> <basic-RE-list> | <lambda>
function basicREList()
local parseFn = getMyType("basicREList")
if parseFn == 1 then
return {type = "basicREList", basicRE(), basicREList()}
else
return
end
end
-- <elementary-RE> ::= <group> | <any> | <char> | <set>
function elementaryRE()
local parseFn = getMyType("elementaryRE")
if parseFn == 1 then
return group()
elseif parseFn == 2 then
eat()
return {type = "any"}
elseif parseFn == 3 then
local token = eat()
return {type = "char", value = token.source}
elseif parseFn == 4 then
return set()
end
end
-- <group> ::= "(" <RE> ")"
function group()
eat()
local rexp = RE()
eat()
return {type = "group", rexp}
end
--<set> ::= <positive-set> | <negative-set>
function set()
local openToken = eat()
local ret
if openToken.type == "open-set" then
ret = {type = "set", unrollLoop(setItems())}
else -- open-negset
ret = {type = "negset", unrollLoop(setItems())}
end
eat()
return ret
end
-- <set-items> ::= <set-item> | <set-item> <set-items>
function setItems()
local firstItem = setItem()
local parseFn = getMyType("setItems")
if parseFn == 1 then
return {type = "setItems", firstItem}
else
return {type = "setItems", firstItem, setItems()}
end
end
-- <set-item> ::= <range> | <char>
function setItem()
if tokenList[2].type == "range" then
return {type = "range", start = eat().source, finish = (eat() and eat()).source}
else
return {type = "char", value = eat().source}
end
end
local props = {
clampStart = false,
clampEnd = false
}
if tokenList[1].type == "start" then
props.clampStart = true
table.remove(tokenList, 1)
end
if tokenList[#tokenList - 1].type == "eos" then
props.clampEnd = true
table.remove(tokenList, #tokenList - 1)
end
if #tokenList == 1 then
error("Empty regex", 0)
end
local ret = RE()
ret.properties = props
return ret
end
return parser