forked from vsbenas/parser-gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser-gen-tests.lua
160 lines (125 loc) · 2.64 KB
/
parser-gen-tests.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
local pg = require "parser-gen"
local peg = require "peg-parser"
local re = require "relabel"
local eq = require "equals"
local equals = eq.equals
local pr = peg.print_r
-- terminals
-- space allowed
rule = pg.compile [[
rule <- 'a'
]]
str = "a a aa "
res = pg.parse(str,rule)
assert(res)
-- space not allowed
rule = pg.compile [[
RULE <- 'a' 'b'
]]
str = "a b"
res = pg.parse(str,rule)
assert(not res)
-- space not allowed 2
rule = pg.compile [[
rule <- 'a' 'b'
SKIP <- ''
SYNC <- ''
]]
str = "a b"
res = pg.parse(str,rule)
assert(not res)
-- custom space
rule = pg.compile [[
rule <- 'a' 'b'
SKIP <- DOT
DOT <- '.'
]]
str = "a...b"
res = pg.parse(str,rule)
assert(res)
-- non terminals
-- space allowed
rule = pg.compile [[
rule <- A B
A <- 'a'
B <- 'b'
]]
str = "a b"
res, err = pg.parse(str,rule)
assert(res)
-- no spaces allowed
rule = pg.compile [[
RULE <- A B
A <- 'a'
B <- 'b'
]]
str = "a b"
res = pg.parse(str,rule)
assert(not res)
-- space in the beginning and end of string
rule = pg.compile [[
rule <- A B
A <- 'a'
B <- 'b'
]]
str = " a b "
res = pg.parse(str,rule)
assert(res)
-- TESTING CAPTURES
r = pg.compile([[ rule <- {| {:'a' 'b':}* |}
]],_,_,true)
res = pg.parse("ababab", r)
assert(equals(res,{"ab","ab","ab"}))
-- space in capture
rule = pg.compile([[ rule <- {| {: 'a' :}* |}
]],_,_,true)
str = " a a a "
res = pg.parse(str,rule)
assert(equals(res,{"a","a","a"})) -- fails
-- TESTING ERROR LABELS
local labs = {errName = "Error number 1",errName2 = "Error number 2"}
pg.setlabels(labs)
rule = pg.compile [[ rule <- 'a' / %{errName}
SYNC <- ''
]]
local errorcalled = false
local function err(desc, line, col, sfail, recexp)
errorcalled = true
assert(desc == "Error number 1")
end
res = pg.parse("b",rule,err)
assert(errorcalled)
-- TESTING ERROR RECOVERY
local labs = {errName = "Error number 1",errName2 = "Error number 2"}
pg.setlabels(labs)
rule = pg.compile [[
rule <- As //{errName,errName2} Bs
As <- 'a'* / %{errName2}
Bs <- 'b'*
]]
res1 = pg.parse(" a a a",rule)
res2 = pg.parse("b b b ",rule)
assert(res1 and res2)
-- TESTING ERROR GENERATION
pg.setlabels({})
rule = pg.compile([[
rule <- A B C
A <- 'a'
B <- 'b'
C <- 'c'
]],_,true)
res1, errs = pg.parse("ab",rule)
assert(errs[1]["msg"] == "Expected C")
-- TESTING RECOVERY GENERATION
-- SELF-DESCRIPTION
pg.setlabels(peg.labels)
gram = pg.compile(peg.gram, peg.defs,_,true)
res1, errs = pg.parse(peg.gram,gram)
assert(res1) -- parse succesful
--[[ this test is invalid since tool added ^LABEL syntax
r = re.compile(peg.gram,peg.defs)
res2 = r:match(peg.gram)
--peg.print_r(res2)
assert(equals(res1, res2))
]]--
print("all tests succesful")