-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.lua
323 lines (262 loc) · 6.4 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
local m = require'lpeglabel'
local re = require'relabel'
local errinfo = require'pegparser.syntax_errors'
local predef = require'pegparser.predef'
local g = {}
local defs = {}
local lasttk = {}
local function copyp (p)
local aux = p
for k, v in pairs(p) do
aux.k = v
end
aux.p1, aux.p2 = nil, nil
return aux
end
local newNode = function (tag, p1, p2)
if type(tag) == "table" then
local newp = copyp(tag)
newp.p1, newp.p2 = p1, p2
return newp
else
return { tag = tag, p1 = p1, p2 = p2 }
end
end
defs.newString = function (v, quote)
if #v == 0 then
return newNode('empty')
end
g.tokens[v] = true
lasttk[v] = true
return newNode('char', v, quote or "'")
end
defs.newAny = function (v)
return newNode('any')
end
defs.newVar = function (v)
g.vars[#g.vars + 1] = v
return newNode('var', v)
end
defs.newClass = function (l)
return newNode('set', l)
end
local function binOpLeft (tag, l)
--build a left-associateve AST for an operator
local p = l[1]
for i=2, #l do
p = newNode(tag, p, l[i])
end
return p
end
local function binOpRight (tag, l)
--build a right-associateve AST for an operator
local n = #l
local p = l[n]
for i = n - 1, 1, -1 do
p = newNode(tag, l[i], p)
end
return p
end
defs.newAnd = function (p)
return newNode('and', p)
end
defs.newNot = function (p)
return newNode('not', p)
end
defs.newSeq = function (...)
return binOpLeft('con', {...})
end
-- the algorithm that inserts labels
-- gives a better result when we consider
-- a right-associative choice
defs.newOrd = function (...)
return binOpRight('ord', {...})
end
defs.newDef = function (v)
if not predef[v] then
error("undefined name: " .. tostring(v))
end
return newNode('def', v)
end
defs.newThrow = function (lab)
return newNode('throw', lab)
end
defs.newConstCap = function (p)
return newNode('constCap', p)
end
defs.newPosCap = function ()
return newNode('posCap')
end
defs.newSimpCap = function (p)
return newNode('simpCap', p)
end
defs.newTabCap = function (p)
return newNode('tabCap', p)
end
defs.newAnonCap = function (p)
return newNode('anonCap', p)
end
defs.newNameCap = function (p1, p2)
return newNode('nameCap', p1, p2)
end
defs.newSuffix = function (p, ...)
local l = { ... }
local i = 1
while i <= #l do
local v = l[i]
if v == '*' then
p = newNode('star', p)
i = i + 1
elseif v == '+' then
p = newNode('plus', p)
i = i + 1
elseif v == '?' then
p = newNode('opt', p)
i = i + 1
else
p = newNode('ord', p, defs.newThrow(l[i+1]))
i = i + 2
end
end
return p
end
defs.isLexRule = function (s)
local ch = string.sub(s, 1, 1)
return ch >= 'A' and ch <= 'Z'
end
defs.isErrRule = function (s)
return string.find(s, 'Err_')
end
defs.newRule = function (k, v)
g.prules[k] = v
g.plist[#g.plist + 1] = k
if defs.isLexRule(k) then
for k, v in pairs(lasttk) do
g.tokens[k] = nil
end
end
lasttk = {}
end
defs.isSimpleExp = function (p)
local tag = p.tag
return tag == 'empty' or tag == 'char' or tag == 'any' or
tag == 'set' or tag == 'var' or tag == 'throw' or
tag == 'posCap' or tag == 'def'
end
defs.repSymbol = function (p)
local tag = p.tag
assert(tag == 'opt' or tag == 'plus' or tag == 'star', p.tag)
if p.tag == 'star' then
return '*'
elseif p.tag == 'plus' then
return '+'
else
return '?'
end
end
defs.predSymbol = function (p)
local tag = p.tag
assert(tag == 'not' or tag == 'and', p.tag)
if p.tag == 'not' then
return '!'
else
return '&'
end
end
defs.matchEmpty = function (p)
local tag = p.tag
if tag == 'empty' or tag == 'not' or tag == 'and' or
tag == 'posCap' or tag == 'star' or tag == 'opt' or
tag == 'throw' then
return true
elseif tag == 'def' then
return false
elseif tag == 'char' or tag == 'set' or tag == 'any' or
tag == 'plus' then
return false
elseif tag == 'con' then
return defs.matchEmpty(p.p1) and defs.matchEmpty(p.p2)
elseif tag == 'ord' then
return defs.matchEmpty(p.p1) or defs.matchEmpty(p.p2)
elseif tag == 'var' then
return defs.matchEmpty(g.prules[p.p1])
elseif tag == 'simpCap' or tag == 'tabCap' or tag == 'anonCap' then
return defs.matchEmpty(p.p1)
elseif tag == 'nameCap' then
return defs.matchEmpty(p.p2)
else
print(p)
error("Unknown tag" .. tostring(p.tag))
end
end
local function setSkip (g)
local space = defs.newClass{' ','\t','\n','\v','\f','\r'}
if g.prules['COMMENT'] then
space = defs.newOrd(space, defs.newVar('COMMENT'))
end
local skip = defs.newSuffix(space, '*')
local s = 'SPACE'
if not g.prules[s] then
g.plist[#g.plist+1] = s
end
g.prules[s] = space
local s = 'SKIP'
if not g.prules[s] then
g.plist[#g.plist+1] = s
end
g.prules[s] = skip
end
local peg = [[
grammar <- S rule+^Rule (!.)^Extra
rule <- (name S arrow^Arrow exp^ExpRule) -> newRule
exp <- (seq ('/' S seq^SeqExp)*) -> newOrd
seq <- (prefix (S prefix)*) -> newSeq
prefix <- '&' S prefix^AndPred -> newAnd /
'!' S prefix^NotPred -> newNot / suffix
suffix <- (primary ({'+'} S / {'*'} S / {'?'} S / {'^'} S name)*) -> newSuffix
primary <- '(' S exp^ExpPri ')'^RParPri S / string / class / any / var / def / throw
string <- ("'" {(!"'" .)*} {"'"}^SingQuote S /
'"' {(!'"' .)*} {'"'}^DoubQuote S) -> newString
class <- '[' {| (({(.'-'.)} / (!']' {.}))+)^EmptyClass |} -> newClass ']'^RBraClass S
any <- '.' -> newAny S
var <- name -> newVar !arrow
name <- {[a-zA-Z_] [a-zA-Z0-9_]*} S
def <- '%' S name -> newDef
throw <- '%{' S name^NameThrow -> newThrow '}'^RCurThrow S
arrow <- '<-' S
S <- (%s / '--' [^%nl]*)* --spaces and comments
]]
local ppk = re.compile(peg, defs)
defs.initgrammar = function(t)
local g = {}
if t then
for k, v in pairs(t) do
g[k] = v
end
else
g.plist = {}
g.prules = {}
g.tokens = {}
g.vars = {}
g.unique = {}
end
return g
end
defs.match = function (s)
g = defs.initgrammar()
local r, lab, pos = ppk:match(s)
if not r then
local line, col = re.calcline(s, pos)
local msg = line .. ':' .. col .. ':'
return r, msg .. (errinfo[lab] or lab), pos
else
setSkip(g)
for i, v in ipairs(g.vars) do
assert(g.prules[v] ~= nil, "Rule '" .. v .. "' was not defined")
end
g.init = g.plist[1]
return g
end
end
defs.newNode = newNode
return defs