-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrosie-lpegtest.lua
306 lines (246 loc) · 8.15 KB
/
rosie-lpegtest.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
rosie = require "rosie"
lpeg = rosie._env.lpeg
json = rosie._env.json
load_module = rosie._env.load_module
termcolor = load_module("termcolor", "submodules/lua-modules")
test = load_module("test", "submodules/lua-modules")
heading, subheading, check = test.heading, test.subheading, test.check
test.start()
foo = lpeg.rcap((lpeg.R("09")^0), "foo")
foos = lpeg.rcap((foo * (lpeg.P" " * foo)^0), "many foos")
bar = lpeg.P(1) * lpeg.rcap(((lpeg.P"X" * foos)^1) * lpeg.P"abc", "bar")
function check_table(t, typename, s, e, subs)
check(type(t)=="table", nil, 1)
if type(t)=="table" then
check(type(t.s)=="number", nil, 1)
if s and type(t.s)=="number" then check(t.s==s); end
check(type(t.e)=="number", nil, 1)
if e and type(t.e)=="number" then check(t.e==e); end
check(type(t.type)=="string", nil, 1)
if typename and t.type then check(t.type==typename, nil, 1); end
if subs then
if subs ~= 0 then
check(type(t.subs)=="table", nil, 1);
if type(t.subs=="table") then check(#t.subs==subs, nil ,1); end
elseif subs==0 then check(not t.subs, nil, 1); end
end
end
end
heading("MISC")
subheading("Check for valid encoding type (a number)")
s, msg = bar:rmatch("kX7 3 2X1 8abcdef", 1, 99)
check(type(s)=="nil")
check(type(msg)=="string")
if type(msg)=="string" then check(msg:find("invalid encoding")); end
subheading("Create some large buffers that need extra action for gc")
-- error will occur if the gc causes a segfault... need to investigate with valgrind on linux.
for i=1,1 do
s, nextpos = bar:rmatch("kX7 3 2X1 8abcdef")
check(type(s)=="userdata")
check(#s>0)
for j = 1,100 do
lpeg.add(s, string.rep("x", 1000))
end
print("length of buffer: " .. tostring(#s))
s = nil
collectgarbage("collect")
collectgarbage("collect")
end
heading("JSON")
subheading("Captures using indices")
s = bar:rmatch("kX7 3 2X1 8abcdef", 1, 1, 0, 0) -- "json"
check(type(s)=="userdata")
t = json.decode(lpeg.getdata(s))
check_table(t, "bar", 2, 15, 2)
check(#t.subs==2)
for i,v in ipairs(t.subs) do check_table(v, "many foos"); end
s = bar:rmatch("kX7 X1 8 X9 X101 102 103 104abcdef", 1, 1, 0, 0) -- "json"
check(type(s)=="userdata")
t = json.decode(lpeg.getdata(s))
check_table(t, "bar", 2, 32, 4)
check(t.s==2)
check(t.e==32)
check(#t.subs==4)
for i,v in ipairs(t.subs) do
check_table(v, "many foos");
check(#v.subs > 0)
for i,v in ipairs(v.subs) do
check_table(v, "foo")
check(not v.subs)
end
end
s = bar:rmatch("kX X Xabcdef", 1, 1, 0, 0) -- "json"
check(type(s)=="userdata")
t = json.decode(lpeg.getdata(s))
check_table(t)
check(t.s==2)
check(t.e==10)
check(#t.subs==3)
for i,v in ipairs(t.subs) do
check_table(v, "many foos");
check(#v.subs > 0)
for i,v in ipairs(v.subs) do
check_table(v, "foo")
check(not v.subs)
check(v.s==v.e) -- these foos are empty
end
end
subheading("Aliasing")
foo = lpeg.rcap((lpeg.R("09")^0), "foo")
foos = (foo * (lpeg.P" " * foo)^0) -- no rcap
bar = lpeg.P(1) * lpeg.rcap(((lpeg.P"X" * foos)^1) * lpeg.P"abc", "bar")
s = bar:rmatch("kX7 3 2X1 8abcdef", 1, 1, 0, 0) -- "json"
check(type(s)=="userdata")
t = json.decode(lpeg.getdata(s))
check_table(t, "bar")
check(t.s==2)
check(t.e==15)
check(#t.subs==5)
for i,v in ipairs(t.subs) do
check_table(v, "foo");
check(not v.subs)
end
s = bar:rmatch("kX7 X1 8 X9 X101 102 103 104abcdef", 1, 1, 0, 0)
check(type(s)=="userdata")
t = json.decode(lpeg.getdata(s))
check_table(t)
check(t.s==2)
check(t.e==32)
check(#t.subs==11)
for i,v in ipairs(t.subs) do
check_table(v, "foo")
check(not v.subs)
end
s, err = bar:rmatch("kXabcdef", 1, 1, 0, 0)
check(type(s)=="userdata")
t = json.decode(lpeg.getdata(s))
check_table(t)
check(t.s==2)
check(t.e==6)
check(#t.subs==1)
check(t.subs[1].s==t.subs[1].e)
heading("BYTE ARRAY ENCODING")
foo = lpeg.rcap((lpeg.R("09")^0), "foo")
foos = lpeg.rcap((foo * (lpeg.P" " * foo)^0), "many foos")
bar = lpeg.P(1) * lpeg.rcap(((lpeg.P"X" * foos)^1) * lpeg.P"abc", "bar")
subheading("Captures using indices")
s = bar:rmatch("kX7 3 2X1 8abcdef", 1, 0, 0, 0)
check(type(s)=="userdata")
check(#s==116)
check_table(lpeg.decode(s), "bar", 2, 15, 2)
s = bar:rmatch("kX7 X1 8 X9 X101 102 103 104abcdef", 1, 0, 0, 0)
check(type(s)=="userdata")
check(#s==232)
check_table(lpeg.decode(s), "bar", 2, 32, 4)
s = bar:rmatch("kX X Xabcdef", 1, 0, 0, 0)
check(type(s)=="userdata")
check(#s==135)
check_table(lpeg.decode(s), "bar", 2, 10, 3)
for i,v in ipairs(t.subs) do
check_table(v, "foo", nil, nil, 0)
check(v.s==v.e) -- these foos are empty
end
subheading("Aliasing")
foo = lpeg.rcap((lpeg.R("09")^0), "foo")
foos = (foo * (lpeg.P" " * foo)^0) -- NO NAME, NO CALL TO r_capindices
bar = lpeg.P(1) * lpeg.rcap(((lpeg.P"X" * foos)^1) * lpeg.P"abc", "bar")
s = bar:rmatch("kX7 3 2X1 8abcdef", 1, 0, 0, 0)
check(type(s)=="userdata")
check(#s==78)
check_table(lpeg.decode(s), "bar", 2, 15, 5)
for i,v in ipairs(t.subs) do
check_table(v, "foo", nil, nil, 0);
end
s = bar:rmatch("kX7 X1 8 X9 X101 102 103 104abcdef", 1, 0, 0, 0)
check(type(s)=="userdata")
check(#s==156)
check_table(lpeg.decode(s), "bar", 2, 32, 11)
for i,v in ipairs(t.subs) do
check_table(v, "foo", nil, nil, 0)
end
s, err = bar:rmatch("kXabcdef", 1, 0, 0, 0)
check(type(s)=="userdata")
check(#s==26)
check_table(lpeg.decode(s), "bar", 2, 6, 1)
check(t.subs[1].s==t.subs[1].e)
heading("Long patterns") -- these generate an open/close instead of a full capture
txt = string.rep("abcdefghij", 30)
bar = lpeg.rcap(lpeg.P(txt), "BAR")
s, l = bar:rmatch(txt, 1)
ok, t = pcall(lpeg.decode, s)
check(ok)
check_table(t, "BAR")
check(t.e==301)
check(t.s==1)
s, l = bar:rmatch(txt, 1, 1)
ok, t = pcall(json.decode, lpeg.getdata(s))
check_table(t, "BAR", 1, 301)
heading("UNHANDLED CAPTURE TYPES")
foo = lpeg.rcap((lpeg.R("09")^0), "foo")
foos = lpeg.rcap((foo * (lpeg.P" " * foo)^0), "many foos")
subheading("Accidental use of match instead of rmatch")
ok, s, last = pcall(foos.match, foos, "123 4 567890")
check(not ok)
check(type(s)=="string")
check(s:find("invalid capture type"))
subheading("Match types other than Crosiecap when using rmatch")
-- First check that these patterns work, before we introduce an error
foo = lpeg.rcap((lpeg.R("09")^0), "foo")
foos = lpeg.rcap((foo * (lpeg.P" " * foo)^0), "many foos")
ok, s, last = pcall(foos.rmatch, foos, "123 4 567890")
check(ok)
check(type(s)=="userdata")
check(type(last)=="number")
function check_error(pat, input, msg)
ok, s, last = pcall(pat.rmatch, pat, input)
check(not ok)
check(type(s)=="string", nil, 1)
check(s:find(msg), "unexpected error message: " .. tostring(s), 1)
end
foos = lpeg.rcap((foo * (lpeg.P" " * lpeg.Cc("PROBLEM") * foo)^0), "many foos")
check_error(foos, "123 4 567890", "full capture error")
foos = (foo * (lpeg.P" " * lpeg.Cc("PROBLEM") * foo)^0)
check_error(foos, "123 4 567890", "open capture error")
heading("Grammars")
-- Equal numbers of a's and b's
equalcount = lpeg.C(lpeg.P{
"S"; -- initial rule name
S = "a" * lpeg.V"B" + "b" * lpeg.V"A" + "",
A = "a" * lpeg.V"S" + "b" * lpeg.V"A" * lpeg.V"A",
B = "b" * lpeg.V"S" + "a" * lpeg.V"B" * lpeg.V"B",
} * -1) * lpeg.Cp()
bal = "aabb"
s, n = equalcount:match(bal)
check(n==5, "grammar without using rosie lpeg extensions")
check(s==bal)
-- Entire grammar inside rcap
equalcount = lpeg.rcap(
lpeg.P{
"S"; -- initial rule name
S = "a" * lpeg.V"B" + "b" * lpeg.V"A" + "",
A = "a" * lpeg.V"S" + "b" * lpeg.V"A" * lpeg.V"A",
B = "b" * lpeg.V"S" + "a" * lpeg.V"B" * lpeg.V"B",
} * -1,
"S")
s, n = equalcount:rmatch(bal)
check(type(s)=="userdata")
t = lpeg.decode(s)
check_table(t, "S", 1, 5)
equalcount =
lpeg.P{
"S"; -- initial rule name
S = lpeg.rcap("a" * lpeg.V"B" + "b" * lpeg.V"A" + "", "S"),
A = lpeg.rcap("a" * lpeg.V"S" + "b" * lpeg.V"A" * lpeg.V"A", "A"),
B = lpeg.rcap("b" * lpeg.V"S" + "a" * lpeg.V"B" * lpeg.V"B", "B")
} * -1
s, n = equalcount:rmatch(bal)
check(type(s)=="userdata")
t = lpeg.decode(s)
check_table(t, "S", 1, 5, 1)
check_table(t.subs[1], "B", 2, 5, 2)
bal = "baab"
s, n = equalcount:rmatch(bal)
t = lpeg.decode(s)
check_table(t, "S", 1, 5, 1)
check_table(t.subs[1], "A", 2, 5, 1)
test.finish()