forked from vsbenas/parser-gen
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser-gen_tests2.lua
More file actions
336 lines (257 loc) · 6.83 KB
/
parser-gen_tests2.lua
File metadata and controls
336 lines (257 loc) · 6.83 KB
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
local function req(src, mod)
mod = mod or ""
local m = pcall(require, src) and require(src)
or pcall(require, mod.."."..src) and require( mod.."."..src) -- luapower sub module path
or nil
assert(m, 'parser-gen depend on "'..src..'" from "'..mod..'". Please check your path')
return m
end
local pg = require "parser-gen"
local peg = require "peg-parser"
local re = req("relabel", "lpeglabel")
local eq = req("equals", "parser-gen")
local equals = eq.equals
local serpent = pcall(require, "serpent") and require "serpent" or function(...) print(...) end
local pr = peg.print_r
local res,res1,res2, errs
local function cleanGram(s) -- remove leading and ending spaces and tabs
return (s:gsub("^%s*(.-)%s*$", "%1"):gsub("[\9 ]+", " "))
end
local function tst_ast(str, gram, options)
-- rule round trip
options = options or {}
local ast = peg.pegToAST(gram, options['definitions'])
local gram2 = pg.astToPEG(ast, {recovery=false, skipspaces=false, nocaptures=true, re_useext=true})
print(serpent.block(cleanGram(gram)))
print(serpent.block(cleanGram(gram2)))
if not options['not_exact_grammar'] then
-- assert(cleanGram(gram) == cleanGram(gram2))
end
local c1,e1 = pg.parse(str, gram, options)
local c2,e2 = pg.parse(str, gram2, options)
assert(equals(c1, c2)) -- same captures
assert(equals(e1, e2)) -- same error
local m1, me1 = pg.parse( str, pg.compile( gram, options))
local ast3, options2 = pg.genErrorRecovery(ast, options)
local gram3, options3 = pg.astToPEG(ast3, options2)
if not options3.re_useext then
local p3 = re.compile(gram3, options3['definitions'])
local m3, me3 = p3:match(str)
assert( equals(m1, m3))
end
return m1, me1
end
-- test the ast to PEG round trip
-- terminals
-- space allowed
local rule = [[
rule <- 'a'
]]
local str = "a a aa "
tst_ast(str, rule)
-- space not allowed
-- space not allowed
rule = [[
RULE <- 'a' 'b'
]]
str = "a b"
tst_ast(str, rule)
-- space not allowed 2
rule = [[
rule <- 'a' 'b'
SKIP <- ''
SYNC <- ''
]]
str = "a b"
tst_ast(str, rule)
-- custom space
rule = [[
rule <- 'a' 'b'
SKIP <- DOT
DOT <- '.'
]]
str = "a...b"
--tst_ast(str, rule)
-- non terminals
-- space allowed
rule = [[
rule <- A B
A <- 'a'
B <- 'b'
]]
str = "a b"
tst_ast(str, rule)
-- no spaces allowed
rule = [[
RULE <- A B
A <- 'a'
B <- 'b'
]]
str = "a b"
tst_ast(str, rule)
-- space in the beginning and end of string
rule = [[
rule <- A B
A <- 'a'
B <- 'b'
]]
str = " a b "
tst_ast(str, rule)
rule = [[ R <- 'a' ( 'b' / 'c' )]]
str = 'ab'
--res = tst_ast(str, rule, {not_exact_grammar = true})
-- testing ranges
rule = [[ r <- {[a1b]* } ]]
str = "a1b"
res, err1 = pg.parse(str, rule, {nocaptures=true})
local res2 = re.compile(rule):match(str)
assert(equals(res, res2 ))
res = pg.parse(str, rule,{nocaptures=false})
-- testing space in class
rule = [[ r <- {[a1b]*} ]]
str = "a 1b"
res,err1 = tst_ast(str, rule, {skipspaces = true, nocaptures=true} )
assert(equals(res,"a"))
local res2 = re.compile(rule):match(str)
rule = [[ r <- [a1]* ]]
str = "a a"
local res = tst_ast(str, rule)
assert(res[1]== "a")
-- testing quote in class
rule = [[ r <- {[b']*} ]]
str = "b'b"
res = tst_ast(str, rule)
assert( res[1] =="b'b")
rule = [[ r <- {[a-z_A-Z]*} ]]
str = "e_E_"
res = tst_ast(str, rule)
assert(res[1] =="e_E_")
rule = [[ r <- {[^A-Z]*} ]]
str = "aaaA"
res = tst_ast(str, rule)
assert(res[1] =="aaa")
-- testing quote
rule =[[
string <- "'"""
]]
res, errs = tst_ast('"""', rule )
rule =[[
string <- ('"' {[^"]*} '"' / "'" {[^']*} "'")
]]
res, errs = tst_ast("'bob'", rule )
-- TESTING CAPTURES
rule = [[ rule <- {| {:'a' 'b':}* |} ]]
str = "ababab"
tst_ast(str, rule, {nocaptures=true})
-- space in capture
rule =[[ rule <- {| {:'a':}* |}
]]
str = " a a a "
res = tst_ast(str, rule, {nocaptures=true})
assert(equals(res,{"a","a","a"}))
-- testing fragment
rule = [[rule <- A
fragment A <- [a-z]*
]]
str = " abc def "
res = tst_ast(str, rule, {re_useext=true})
-- TESTING ERROR LABELS
local labs = {errName = "Error number 1",errName2 = "Error number 2"}
rule = [[ rule <- 'a' / %{errName}
SYNC <- ''
]]
str = "b"
local errorcalled = false
res = tst_ast(str, rule, {labels=labs , errorfunction= function (desc, line, col, sfail, recexp)
errorcalled = true
assert(desc == "Error number 1")
end
} )
assert(errorcalled)
-- TESTING ERROR RECOVERY
labs = {errName = "Error number 1",errName2 = "Error number 2"}
rule = [[
rule <- As / %{errName}
As <- 'a'* / %{errName2}
errName <- 'b'*
]]
res1 = tst_ast(" a a a", rule, {labels=labs} )
res2 = tst_ast("b b b ", rule, {labels=labs} )
assert(res1 and res2)
labs = {errName = "Error number 1",errName2 = "Error number 2"}
rule = [[
rule <- As^errName
As <- 'a'* / %{errName2}
errName2 <- 'b'*
]]
res1 = tst_ast(" a a a", rule, {labels=labs} )
res2 = tst_ast("b b b ", rule, {labels=labs} )
assert(res1 and res2)
-- TESTING ERROR GENERATION
rule =[[
rule <- A B C
A <- 'a'
B <- 'b'
C <- 'c'
]]
res, errs = tst_ast("ab", rule, {generrors=true})
assert(errs[1]["msg"] == "Expected C")
-- TESTING trace
rule = [[
rule <- A B
A <- 'a'
B <- 'b'
]]
str = "a b"
print( "trace :", str )
local c,e = pg.parse(str, rule, {trace = true})
rule =[[
rule <- A B C
A <- 'a'
B <- 'b'
C <- 'c'
]]
print( "trace :", str )
res, errs = pg.parse("ab", rule, {generrors=true, trace = true})
-- TESTING RECOVERY GENERATION.
local function tableMerge(t1, t2)
for k,v in pairs(t2) do
if type(v) == "table" then
if type(t1[k] or false) == "table" then
tableMerge(t1[k] or {}, t2[k] or {})
else
t1[k] = v
end
else
t1[k] = v
end
end
return t1
end
rule = [[
rule <- A B
A <- 'a'
B <- 'b'
]]
-- SELF-DESCRIPTION
--local res1, errs = tst_ast(peg.gram, peg.gram, {nocaptures=true, labels=peg.errinfo, definitions = {foldtable = peg.foldtable, concat = peg.concat}, not_exact_grammar = true})
local options ={nocaptures=true, labels=peg.errinfo, definitions = {foldtable = peg.foldtable, concat = peg.concat}}
local str = peg.gram
local ast = peg.pegToAST(peg.gram, {foldtable = peg.foldtable, concat = peg.concat})
local gram3 = pg.astToPEG(ast, {recovery=false, skipspaces=false, nocaptures=true, re_useext=true})
print(gram3)
local optionstrace = tableMerge({trace=true}, options)
local c1,e1 = pg.parse(rule, peg.gram, options)
print( "====================")
print( "trace :", rule )
print( "====================")
local c1t,e1t = pg.parse(rule, peg.gram, optionstrace)
local c3,e3 = pg.parse(rule, gram3, options)
print( "====================")
print( "trace :", rule )
print( "====================")
local c3t,e3t = pg.parse(rule, gram3, optionstrace)
assert(equals(c1, c3)) -- same captures
assert(equals(e1, e3)) -- same error
assert(res1) -- parse succesful
print("all tests succesful")