diff options
Diffstat (limited to 'test.lua')
-rwxr-xr-x | test.lua | 1386 |
1 files changed, 1386 insertions, 0 deletions
diff --git a/test.lua b/test.lua new file mode 100755 index 0000000..d486c03 --- /dev/null +++ b/test.lua | |||
@@ -0,0 +1,1386 @@ | |||
1 | #!/usr/bin/env lua5.1 | ||
2 | |||
3 | -- $Id: test.lua,v 1.101 2013/04/12 16:30:33 roberto Exp $ | ||
4 | |||
5 | -- require"strict" -- just to be pedantic | ||
6 | |||
7 | local m = require"lpeglabel" | ||
8 | |||
9 | |||
10 | -- for general use | ||
11 | local a, b, c, d, e, f, g, p, t | ||
12 | |||
13 | |||
14 | -- compatibility with Lua 5.2 | ||
15 | local unpack = rawget(table, "unpack") or unpack | ||
16 | local loadstring = rawget(_G, "loadstring") or load | ||
17 | |||
18 | |||
19 | -- most tests here do not need much stack space | ||
20 | m.setmaxstack(5) | ||
21 | |||
22 | local any = m.P(1) | ||
23 | local space = m.S" \t\n"^0 | ||
24 | |||
25 | local function checkeq (x, y, p) | ||
26 | if p then print(x,y) end | ||
27 | if type(x) ~= "table" then assert(x == y) | ||
28 | else | ||
29 | for k,v in pairs(x) do checkeq(v, y[k], p) end | ||
30 | for k,v in pairs(y) do checkeq(v, x[k], p) end | ||
31 | end | ||
32 | end | ||
33 | |||
34 | |||
35 | local mt = getmetatable(m.P(1)) | ||
36 | |||
37 | |||
38 | local allchar = {} | ||
39 | for i=0,255 do allchar[i + 1] = i end | ||
40 | allchar = string.char(unpack(allchar)) | ||
41 | assert(#allchar == 256) | ||
42 | |||
43 | local function cs2str (c) | ||
44 | return m.match(m.Cs((c + m.P(1)/"")^0), allchar) | ||
45 | end | ||
46 | |||
47 | local function eqcharset (c1, c2) | ||
48 | assert(cs2str(c1) == cs2str(c2)) | ||
49 | end | ||
50 | |||
51 | |||
52 | print"General tests for LPeg library" | ||
53 | |||
54 | assert(type(m.version()) == "string") | ||
55 | print("version " .. m.version()) | ||
56 | assert(m.type("alo") ~= "pattern") | ||
57 | assert(m.type(io.input) ~= "pattern") | ||
58 | assert(m.type(m.P"alo") == "pattern") | ||
59 | |||
60 | -- tests for some basic optimizations | ||
61 | assert(m.match(m.P(false) + "a", "a") == 2) | ||
62 | assert(m.match(m.P(true) + "a", "a") == 1) | ||
63 | assert(m.match("a" + m.P(false), "b") == nil) | ||
64 | assert(m.match("a" + m.P(true), "b") == 1) | ||
65 | |||
66 | assert(m.match(m.P(false) * "a", "a") == nil) | ||
67 | assert(m.match(m.P(true) * "a", "a") == 2) | ||
68 | assert(m.match("a" * m.P(false), "a") == nil) | ||
69 | assert(m.match("a" * m.P(true), "a") == 2) | ||
70 | |||
71 | assert(m.match(#m.P(false) * "a", "a") == nil) | ||
72 | assert(m.match(#m.P(true) * "a", "a") == 2) | ||
73 | assert(m.match("a" * #m.P(false), "a") == nil) | ||
74 | assert(m.match("a" * #m.P(true), "a") == 2) | ||
75 | |||
76 | |||
77 | -- tests for locale | ||
78 | do | ||
79 | assert(m.locale(m) == m) | ||
80 | local t = {} | ||
81 | assert(m.locale(t, m) == t) | ||
82 | local x = m.locale() | ||
83 | for n,v in pairs(x) do | ||
84 | assert(type(n) == "string") | ||
85 | eqcharset(v, m[n]) | ||
86 | end | ||
87 | end | ||
88 | |||
89 | |||
90 | assert(m.match(3, "aaaa")) | ||
91 | assert(m.match(4, "aaaa")) | ||
92 | assert(not m.match(5, "aaaa")) | ||
93 | assert(m.match(-3, "aa")) | ||
94 | assert(not m.match(-3, "aaa")) | ||
95 | assert(not m.match(-3, "aaaa")) | ||
96 | assert(not m.match(-4, "aaaa")) | ||
97 | assert(m.P(-5):match"aaaa") | ||
98 | |||
99 | assert(m.match("a", "alo") == 2) | ||
100 | assert(m.match("al", "alo") == 3) | ||
101 | assert(not m.match("alu", "alo")) | ||
102 | assert(m.match(true, "") == 1) | ||
103 | |||
104 | local digit = m.S"0123456789" | ||
105 | local upper = m.S"ABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
106 | local lower = m.S"abcdefghijklmnopqrstuvwxyz" | ||
107 | local letter = m.S"" + upper + lower | ||
108 | local alpha = letter + digit + m.R() | ||
109 | |||
110 | eqcharset(m.S"", m.P(false)) | ||
111 | eqcharset(upper, m.R("AZ")) | ||
112 | eqcharset(lower, m.R("az")) | ||
113 | eqcharset(upper + lower, m.R("AZ", "az")) | ||
114 | eqcharset(upper + lower, m.R("AZ", "cz", "aa", "bb", "90")) | ||
115 | eqcharset(digit, m.S"01234567" + "8" + "9") | ||
116 | eqcharset(upper, letter - lower) | ||
117 | eqcharset(m.S(""), m.R()) | ||
118 | assert(cs2str(m.S("")) == "") | ||
119 | |||
120 | eqcharset(m.S"\0", "\0") | ||
121 | eqcharset(m.S"\1\0\2", m.R"\0\2") | ||
122 | eqcharset(m.S"\1\0\2", m.R"\1\2" + "\0") | ||
123 | eqcharset(m.S"\1\0\2" - "\0", m.R"\1\2") | ||
124 | |||
125 | local word = alpha^1 * (1 - alpha)^0 | ||
126 | |||
127 | assert((word^0 * -1):match"alo alo") | ||
128 | assert(m.match(word^1 * -1, "alo alo")) | ||
129 | assert(m.match(word^2 * -1, "alo alo")) | ||
130 | assert(not m.match(word^3 * -1, "alo alo")) | ||
131 | |||
132 | assert(not m.match(word^-1 * -1, "alo alo")) | ||
133 | assert(m.match(word^-2 * -1, "alo alo")) | ||
134 | assert(m.match(word^-3 * -1, "alo alo")) | ||
135 | |||
136 | local eos = m.P(-1) | ||
137 | |||
138 | assert(m.match(digit^0 * letter * digit * eos, "1298a1")) | ||
139 | assert(not m.match(digit^0 * letter * eos, "1257a1")) | ||
140 | |||
141 | b = { | ||
142 | [1] = "(" * (((1 - m.S"()") + #m.P"(" * m.V(1))^0) * ")" | ||
143 | } | ||
144 | |||
145 | assert(m.match(b, "(al())()")) | ||
146 | assert(not m.match(b * eos, "(al())()")) | ||
147 | assert(m.match(b * eos, "((al())()(é))")) | ||
148 | assert(not m.match(b, "(al()()")) | ||
149 | |||
150 | assert(not m.match(letter^1 - "for", "foreach")) | ||
151 | assert(m.match(letter^1 - ("for" * eos), "foreach")) | ||
152 | assert(not m.match(letter^1 - ("for" * eos), "for")) | ||
153 | |||
154 | function basiclookfor (p) | ||
155 | return m.P { | ||
156 | [1] = p + (1 * m.V(1)) | ||
157 | } | ||
158 | end | ||
159 | |||
160 | function caplookfor (p) | ||
161 | return basiclookfor(p:C()) | ||
162 | end | ||
163 | |||
164 | assert(m.match(caplookfor(letter^1), " 4achou123...") == "achou") | ||
165 | a = {m.match(caplookfor(letter^1)^0, " two words, one more ")} | ||
166 | checkeq(a, {"two", "words", "one", "more"}) | ||
167 | |||
168 | assert(m.match( basiclookfor((#m.P(b) * 1) * m.Cp()), " ( (a)") == 7) | ||
169 | |||
170 | a = {m.match(m.C(digit^1 * m.Cc"d") + m.C(letter^1 * m.Cc"l"), "123")} | ||
171 | checkeq(a, {"123", "d"}) | ||
172 | |||
173 | a = {m.match(m.C(digit^1) * "d" * -1 + m.C(letter^1 * m.Cc"l"), "123d")} | ||
174 | checkeq(a, {"123"}) | ||
175 | |||
176 | a = {m.match(m.C(digit^1 * m.Cc"d") + m.C(letter^1 * m.Cc"l"), "abcd")} | ||
177 | checkeq(a, {"abcd", "l"}) | ||
178 | |||
179 | a = {m.match(m.Cc(10,20,30) * 'a' * m.Cp(), 'aaa')} | ||
180 | checkeq(a, {10,20,30,2}) | ||
181 | a = {m.match(m.Cp() * m.Cc(10,20,30) * 'a' * m.Cp(), 'aaa')} | ||
182 | checkeq(a, {1,10,20,30,2}) | ||
183 | a = m.match(m.Ct(m.Cp() * m.Cc(10,20,30) * 'a' * m.Cp()), 'aaa') | ||
184 | checkeq(a, {1,10,20,30,2}) | ||
185 | a = m.match(m.Ct(m.Cp() * m.Cc(7,8) * m.Cc(10,20,30) * 'a' * m.Cp()), 'aaa') | ||
186 | checkeq(a, {1,7,8,10,20,30,2}) | ||
187 | a = {m.match(m.Cc() * m.Cc() * m.Cc(1) * m.Cc(2,3,4) * m.Cc() * 'a', 'aaa')} | ||
188 | checkeq(a, {1,2,3,4}) | ||
189 | |||
190 | a = {m.match(m.Cp() * letter^1 * m.Cp(), "abcd")} | ||
191 | checkeq(a, {1, 5}) | ||
192 | |||
193 | |||
194 | t = {m.match({[1] = m.C(m.C(1) * m.V(1) + -1)}, "abc")} | ||
195 | checkeq(t, {"abc", "a", "bc", "b", "c", "c", ""}) | ||
196 | |||
197 | |||
198 | -- test for small capture boundary | ||
199 | for i = 250,260 do | ||
200 | assert(#m.match(m.C(i), string.rep('a', i)) == i) | ||
201 | assert(#m.match(m.C(m.C(i)), string.rep('a', i)) == i) | ||
202 | end | ||
203 | |||
204 | |||
205 | -- tests for any*n and any*-n | ||
206 | for n = 1, 550 do | ||
207 | local x_1 = string.rep('x', n - 1) | ||
208 | local x = x_1 .. 'a' | ||
209 | assert(not m.P(n):match(x_1)) | ||
210 | assert(m.P(n):match(x) == n + 1) | ||
211 | assert(n < 4 or m.match(m.P(n) + "xxx", x_1) == 4) | ||
212 | assert(m.C(n):match(x) == x) | ||
213 | assert(m.C(m.C(n)):match(x) == x) | ||
214 | assert(m.P(-n):match(x_1) == 1) | ||
215 | assert(not m.P(-n):match(x)) | ||
216 | assert(n < 13 or m.match(m.Cc(20) * ((n - 13) * m.P(10)) * 3, x) == 20) | ||
217 | local n3 = math.floor(n/3) | ||
218 | assert(m.match(n3 * m.Cp() * n3 * n3, x) == n3 + 1) | ||
219 | end | ||
220 | |||
221 | -- true values | ||
222 | assert(m.P(0):match("x") == 1) | ||
223 | assert(m.P(0):match("") == 1) | ||
224 | assert(m.C(0):match("x") == "") | ||
225 | |||
226 | assert(m.match(m.Cc(0) * m.P(10) + m.Cc(1) * "xuxu", "xuxu") == 1) | ||
227 | assert(m.match(m.Cc(0) * m.P(10) + m.Cc(1) * "xuxu", "xuxuxuxuxu") == 0) | ||
228 | assert(m.match(m.C(m.P(2)^1), "abcde") == "abcd") | ||
229 | p = m.Cc(0) * 1 + m.Cc(1) * 2 + m.Cc(2) * 3 + m.Cc(3) * 4 | ||
230 | |||
231 | |||
232 | -- test for alternation optimization | ||
233 | assert(m.match(m.P"a"^1 + "ab" + m.P"x"^0, "ab") == 2) | ||
234 | assert(m.match((m.P"a"^1 + "ab" + m.P"x"^0 * 1)^0, "ab") == 3) | ||
235 | assert(m.match(m.P"ab" + "cd" + "" + "cy" + "ak", "98") == 1) | ||
236 | assert(m.match(m.P"ab" + "cd" + "ax" + "cy", "ax") == 3) | ||
237 | assert(m.match("a" * m.P"b"^0 * "c" + "cd" + "ax" + "cy", "ax") == 3) | ||
238 | assert(m.match((m.P"ab" + "cd" + "ax" + "cy")^0, "ax") == 3) | ||
239 | assert(m.match(m.P(1) * "x" + m.S"" * "xu" + "ay", "ay") == 3) | ||
240 | assert(m.match(m.P"abc" + "cde" + "aka", "aka") == 4) | ||
241 | assert(m.match(m.S"abc" * "x" + "cde" + "aka", "ax") == 3) | ||
242 | assert(m.match(m.S"abc" * "x" + "cde" + "aka", "aka") == 4) | ||
243 | assert(m.match(m.S"abc" * "x" + "cde" + "aka", "cde") == 4) | ||
244 | assert(m.match(m.S"abc" * "x" + "ide" + m.S"ab" * "ka", "aka") == 4) | ||
245 | assert(m.match("ab" + m.S"abc" * m.P"y"^0 * "x" + "cde" + "aka", "ax") == 3) | ||
246 | assert(m.match("ab" + m.S"abc" * m.P"y"^0 * "x" + "cde" + "aka", "aka") == 4) | ||
247 | assert(m.match("ab" + m.S"abc" * m.P"y"^0 * "x" + "cde" + "aka", "cde") == 4) | ||
248 | assert(m.match("ab" + m.S"abc" * m.P"y"^0 * "x" + "ide" + m.S"ab" * "ka", "aka") == 4) | ||
249 | assert(m.match("ab" + m.S"abc" * m.P"y"^0 * "x" + "ide" + m.S"ab" * "ka", "ax") == 3) | ||
250 | assert(m.match(m.P(1) * "x" + "cde" + m.S"ab" * "ka", "aka") == 4) | ||
251 | assert(m.match(m.P(1) * "x" + "cde" + m.P(1) * "ka", "aka") == 4) | ||
252 | assert(m.match(m.P(1) * "x" + "cde" + m.P(1) * "ka", "cde") == 4) | ||
253 | assert(m.match(m.P"eb" + "cd" + m.P"e"^0 + "x", "ee") == 3) | ||
254 | assert(m.match(m.P"ab" + "cd" + m.P"e"^0 + "x", "abcd") == 3) | ||
255 | assert(m.match(m.P"ab" + "cd" + m.P"e"^0 + "x", "eeex") == 4) | ||
256 | assert(m.match(m.P"ab" + "cd" + m.P"e"^0 + "x", "cd") == 3) | ||
257 | assert(m.match(m.P"ab" + "cd" + m.P"e"^0 + "x", "x") == 1) | ||
258 | assert(m.match(m.P"ab" + "cd" + m.P"e"^0 + "x" + "", "zee") == 1) | ||
259 | assert(m.match(m.P"ab" + "cd" + m.P"e"^1 + "x", "abcd") == 3) | ||
260 | assert(m.match(m.P"ab" + "cd" + m.P"e"^1 + "x", "eeex") == 4) | ||
261 | assert(m.match(m.P"ab" + "cd" + m.P"e"^1 + "x", "cd") == 3) | ||
262 | assert(m.match(m.P"ab" + "cd" + m.P"e"^1 + "x", "x") == 2) | ||
263 | assert(m.match(m.P"ab" + "cd" + m.P"e"^1 + "x" + "", "zee") == 1) | ||
264 | assert(not m.match(("aa" * m.P"bc"^-1 + "aab") * "e", "aabe")) | ||
265 | |||
266 | assert(m.match("alo" * (m.P"\n" + -1), "alo") == 4) | ||
267 | |||
268 | |||
269 | -- bug in 0.12 (rc1) | ||
270 | assert(m.match((m.P"\128\187\191" + m.S"abc")^0, "\128\187\191") == 4) | ||
271 | |||
272 | assert(m.match(m.S"\0\128\255\127"^0, string.rep("\0\128\255\127", 10)) == | ||
273 | 4*10 + 1) | ||
274 | |||
275 | -- optimizations with optional parts | ||
276 | assert(m.match(("ab" * -m.P"c")^-1, "abc") == 1) | ||
277 | assert(m.match(("ab" * #m.P"c")^-1, "abd") == 1) | ||
278 | assert(m.match(("ab" * m.B"c")^-1, "ab") == 1) | ||
279 | assert(m.match(("ab" * m.P"cd"^0)^-1, "abcdcdc") == 7) | ||
280 | |||
281 | assert(m.match(m.P"ab"^-1 - "c", "abcd") == 3) | ||
282 | |||
283 | p = ('Aa' * ('Bb' * ('Cc' * m.P'Dd'^0)^0)^0)^-1 | ||
284 | assert(p:match("AaBbCcDdBbCcDdDdDdBb") == 21) | ||
285 | |||
286 | |||
287 | pi = "3.14159 26535 89793 23846 26433 83279 50288 41971 69399 37510" | ||
288 | assert(m.match(m.Cs((m.P"1" / "a" + m.P"5" / "b" + m.P"9" / "c" + 1)^0), pi) == | ||
289 | m.match(m.Cs((m.P(1) / {["1"] = "a", ["5"] = "b", ["9"] = "c"})^0), pi)) | ||
290 | print"+" | ||
291 | |||
292 | |||
293 | -- tests for capture optimizations | ||
294 | assert(m.match((m.P(3) + 4 * m.Cp()) * "a", "abca") == 5) | ||
295 | t = {m.match(((m.P"a" + m.Cp()) * m.P"x")^0, "axxaxx")} | ||
296 | checkeq(t, {3, 6}) | ||
297 | |||
298 | |||
299 | -- tests for numbered captures | ||
300 | p = m.C(1) | ||
301 | assert(m.match(m.C(m.C(p * m.C(2)) * m.C(3)) / 3, "abcdefgh") == "a") | ||
302 | assert(m.match(m.C(m.C(p * m.C(2)) * m.C(3)) / 1, "abcdefgh") == "abcdef") | ||
303 | assert(m.match(m.C(m.C(p * m.C(2)) * m.C(3)) / 4, "abcdefgh") == "bc") | ||
304 | assert(m.match(m.C(m.C(p * m.C(2)) * m.C(3)) / 0, "abcdefgh") == 7) | ||
305 | |||
306 | a, b, c = m.match(p * (m.C(p * m.C(2)) * m.C(3) / 4) * p, "abcdefgh") | ||
307 | assert(a == "a" and b == "efg" and c == "h") | ||
308 | |||
309 | -- test for table captures | ||
310 | t = m.match(m.Ct(letter^1), "alo") | ||
311 | checkeq(t, {}) | ||
312 | |||
313 | t, n = m.match(m.Ct(m.C(letter)^1) * m.Cc"t", "alo") | ||
314 | assert(n == "t" and table.concat(t) == "alo") | ||
315 | |||
316 | t = m.match(m.Ct(m.C(m.C(letter)^1)), "alo") | ||
317 | assert(table.concat(t, ";") == "alo;a;l;o") | ||
318 | |||
319 | t = m.match(m.Ct(m.C(m.C(letter)^1)), "alo") | ||
320 | assert(table.concat(t, ";") == "alo;a;l;o") | ||
321 | |||
322 | t = m.match(m.Ct(m.Ct((m.Cp() * letter * m.Cp())^1)), "alo") | ||
323 | assert(table.concat(t[1], ";") == "1;2;2;3;3;4") | ||
324 | |||
325 | t = m.match(m.Ct(m.C(m.C(1) * 1 * m.C(1))), "alo") | ||
326 | checkeq(t, {"alo", "a", "o"}) | ||
327 | |||
328 | |||
329 | -- tests for groups | ||
330 | p = m.Cg(1) -- no capture | ||
331 | assert(p:match('x') == 'x') | ||
332 | p = m.Cg(m.P(true)/function () end * 1) -- no value | ||
333 | assert(p:match('x') == 'x') | ||
334 | p = m.Cg(m.Cg(m.Cg(m.C(1)))) | ||
335 | assert(p:match('x') == 'x') | ||
336 | p = m.Cg(m.Cg(m.Cg(m.C(1))^0) * m.Cg(m.Cc(1) * m.Cc(2))) | ||
337 | t = {p:match'abc'} | ||
338 | checkeq(t, {'a', 'b', 'c', 1, 2}) | ||
339 | |||
340 | p = m.Ct(m.Cg(m.Cc(10), "hi") * m.C(1)^0 * m.Cg(m.Cc(20), "ho")) | ||
341 | t = p:match'' | ||
342 | checkeq(t, {hi = 10, ho = 20}) | ||
343 | t = p:match'abc' | ||
344 | checkeq(t, {hi = 10, ho = 20, 'a', 'b', 'c'}) | ||
345 | |||
346 | |||
347 | -- test for error messages | ||
348 | local function checkerr (msg, ...) | ||
349 | assert(m.match({ m.P(msg) + 1 * m.V(1) }, select(2, pcall(...)))) | ||
350 | end | ||
351 | |||
352 | checkerr("rule '1' may be left recursive", m.match, { m.V(1) * 'a' }, "a") | ||
353 | checkerr("rule '1' used outside a grammar", m.match, m.V(1), "") | ||
354 | checkerr("rule 'hiii' used outside a grammar", m.match, m.V('hiii'), "") | ||
355 | checkerr("rule 'hiii' undefined in given grammar", m.match, { m.V('hiii') }, "") | ||
356 | checkerr("undefined in given grammar", m.match, { m.V{} }, "") | ||
357 | |||
358 | checkerr("rule 'A' is not a pattern", m.P, { m.P(1), A = {} }) | ||
359 | checkerr("grammar has no initial rule", m.P, { [print] = {} }) | ||
360 | |||
361 | -- grammar with a long call chain before left recursion | ||
362 | p = {'a', | ||
363 | a = m.V'b' * m.V'c' * m.V'd' * m.V'a', | ||
364 | b = m.V'c', | ||
365 | c = m.V'd', | ||
366 | d = m.V'e', | ||
367 | e = m.V'f', | ||
368 | f = m.V'g', | ||
369 | g = m.P'' | ||
370 | } | ||
371 | checkerr("rule 'a' may be left recursive", m.match, p, "a") | ||
372 | |||
373 | |||
374 | -- tests for non-pattern as arguments to pattern functions | ||
375 | |||
376 | p = { ('a' * m.V(1))^-1 } * m.P'b' * { 'a' * m.V(2); m.V(1)^-1 } | ||
377 | assert(m.match(p, "aaabaac") == 7) | ||
378 | |||
379 | p = m.P'abc' * 2 * -5 * true * 'de' -- mix of numbers and strings and booleans | ||
380 | |||
381 | assert(p:match("abc01de") == 8) | ||
382 | assert(p:match("abc01de3456") == nil) | ||
383 | |||
384 | p = 'abc' * (2 * (-5 * (true * m.P'de'))) | ||
385 | |||
386 | assert(p:match("abc01de") == 8) | ||
387 | assert(p:match("abc01de3456") == nil) | ||
388 | |||
389 | p = { m.V(2), m.P"abc" } * | ||
390 | (m.P{ "xx", xx = m.P"xx" } + { "x", x = m.P"a" * m.V"x" + "" }) | ||
391 | assert(p:match("abcaaaxx") == 7) | ||
392 | assert(p:match("abcxx") == 6) | ||
393 | |||
394 | |||
395 | -- a large table capture | ||
396 | t = m.match(m.Ct(m.C('a')^0), string.rep("a", 10000)) | ||
397 | assert(#t == 10000 and t[1] == 'a' and t[#t] == 'a') | ||
398 | |||
399 | print('+') | ||
400 | |||
401 | |||
402 | -- bug in 0.10 (rechecking a grammar, after tail-call optimization) | ||
403 | m.P{ m.P { (m.P(3) + "xuxu")^0 * m.V"xuxu", xuxu = m.P(1) } } | ||
404 | |||
405 | local V = m.V | ||
406 | |||
407 | local Space = m.S(" \n\t")^0 | ||
408 | local Number = m.C(m.R("09")^1) * Space | ||
409 | local FactorOp = m.C(m.S("+-")) * Space | ||
410 | local TermOp = m.C(m.S("*/")) * Space | ||
411 | local Open = "(" * Space | ||
412 | local Close = ")" * Space | ||
413 | |||
414 | |||
415 | local function f_factor (v1, op, v2, d) | ||
416 | assert(d == nil) | ||
417 | if op == "+" then return v1 + v2 | ||
418 | else return v1 - v2 | ||
419 | end | ||
420 | end | ||
421 | |||
422 | |||
423 | local function f_term (v1, op, v2, d) | ||
424 | assert(d == nil) | ||
425 | if op == "*" then return v1 * v2 | ||
426 | else return v1 / v2 | ||
427 | end | ||
428 | end | ||
429 | |||
430 | G = m.P{ "Exp", | ||
431 | Exp = m.Cf(V"Factor" * m.Cg(FactorOp * V"Factor")^0, f_factor); | ||
432 | Factor = m.Cf(V"Term" * m.Cg(TermOp * V"Term")^0, f_term); | ||
433 | Term = Number / tonumber + Open * V"Exp" * Close; | ||
434 | } | ||
435 | |||
436 | G = Space * G * -1 | ||
437 | |||
438 | for _, s in ipairs{" 3 + 5*9 / (1+1) ", "3+4/2", "3+3-3- 9*2+3*9/1- 8"} do | ||
439 | assert(m.match(G, s) == loadstring("return "..s)()) | ||
440 | end | ||
441 | |||
442 | |||
443 | -- test for grammars (errors deep in calling non-terminals) | ||
444 | g = m.P{ | ||
445 | [1] = m.V(2) + "a", | ||
446 | [2] = "a" * m.V(3) * "x", | ||
447 | [3] = "b" * m.V(3) + "c" | ||
448 | } | ||
449 | |||
450 | assert(m.match(g, "abbbcx") == 7) | ||
451 | assert(m.match(g, "abbbbx") == 2) | ||
452 | |||
453 | |||
454 | -- tests for \0 | ||
455 | assert(m.match(m.R("\0\1")^1, "\0\1\0") == 4) | ||
456 | assert(m.match(m.S("\0\1ab")^1, "\0\1\0a") == 5) | ||
457 | assert(m.match(m.P(1)^3, "\0\1\0a") == 5) | ||
458 | assert(not m.match(-4, "\0\1\0a")) | ||
459 | assert(m.match("\0\1\0a", "\0\1\0a") == 5) | ||
460 | assert(m.match("\0\0\0", "\0\0\0") == 4) | ||
461 | assert(not m.match("\0\0\0", "\0\0")) | ||
462 | |||
463 | |||
464 | -- tests for predicates | ||
465 | assert(not m.match(-m.P("a") * 2, "alo")) | ||
466 | assert(m.match(- -m.P("a") * 2, "alo") == 3) | ||
467 | assert(m.match(#m.P("a") * 2, "alo") == 3) | ||
468 | assert(m.match(##m.P("a") * 2, "alo") == 3) | ||
469 | assert(not m.match(##m.P("c") * 2, "alo")) | ||
470 | assert(m.match(m.Cs((##m.P("a") * 1 + m.P(1)/".")^0), "aloal") == "a..a.") | ||
471 | assert(m.match(m.Cs((#((#m.P"a")/"") * 1 + m.P(1)/".")^0), "aloal") == "a..a.") | ||
472 | assert(m.match(m.Cs((- -m.P("a") * 1 + m.P(1)/".")^0), "aloal") == "a..a.") | ||
473 | assert(m.match(m.Cs((-((-m.P"a")/"") * 1 + m.P(1)/".")^0), "aloal") == "a..a.") | ||
474 | |||
475 | p = -m.P'a' * m.Cc(1) + -m.P'b' * m.Cc(2) + -m.P'c' * m.Cc(3) | ||
476 | assert(p:match('a') == 2 and p:match('') == 1 and p:match('b') == 1) | ||
477 | |||
478 | p = -m.P'a' * m.Cc(10) + #m.P'a' * m.Cc(20) | ||
479 | assert(p:match('a') == 20 and p:match('') == 10 and p:match('b') == 10) | ||
480 | |||
481 | |||
482 | |||
483 | -- look-behind predicate | ||
484 | assert(not m.match(m.B'a', 'a')) | ||
485 | assert(m.match(1 * m.B'a', 'a') == 2) | ||
486 | assert(not m.match(m.B(1), 'a')) | ||
487 | assert(m.match(1 * m.B(1), 'a') == 2) | ||
488 | assert(m.match(-m.B(1), 'a') == 1) | ||
489 | assert(m.match(m.B(250), string.rep('a', 250)) == nil) | ||
490 | assert(m.match(250 * m.B(250), string.rep('a', 250)) == 251) | ||
491 | assert(not pcall(m.B, 260)) | ||
492 | |||
493 | B = #letter * -m.B(letter) + -letter * m.B(letter) | ||
494 | x = m.Ct({ (B * m.Cp())^-1 * (1 * m.V(1) + m.P(true)) }) | ||
495 | checkeq(m.match(x, 'ar cal c'), {1,3,4,7,9,10}) | ||
496 | checkeq(m.match(x, ' ar cal '), {2,4,5,8}) | ||
497 | checkeq(m.match(x, ' '), {}) | ||
498 | checkeq(m.match(x, 'aloalo'), {1,7}) | ||
499 | |||
500 | assert(m.match(B, "a") == 1) | ||
501 | assert(m.match(1 * B, "a") == 2) | ||
502 | assert(not m.B(1 - letter):match("")) | ||
503 | assert((-m.B(letter)):match("") == 1) | ||
504 | |||
505 | assert((4 * m.B(letter, 4)):match("aaaaaaaa") == 5) | ||
506 | assert(not (4 * m.B(#letter * 5)):match("aaaaaaaa")) | ||
507 | assert((4 * -m.B(#letter * 5)):match("aaaaaaaa") == 5) | ||
508 | |||
509 | -- look-behind with grammars | ||
510 | assert(m.match('a' * m.B{'x', x = m.P(3)}, 'aaa') == nil) | ||
511 | assert(m.match('aa' * m.B{'x', x = m.P('aaa')}, 'aaaa') == nil) | ||
512 | assert(m.match('aaa' * m.B{'x', x = m.P('aaa')}, 'aaaaa') == 4) | ||
513 | |||
514 | |||
515 | |||
516 | -- bug in 0.9 | ||
517 | assert(m.match(('a' * #m.P'b'), "ab") == 2) | ||
518 | assert(not m.match(('a' * #m.P'b'), "a")) | ||
519 | |||
520 | assert(not m.match(#m.S'567', "")) | ||
521 | assert(m.match(#m.S'567' * 1, "6") == 2) | ||
522 | |||
523 | |||
524 | -- tests for Tail Calls | ||
525 | |||
526 | --labeled failure | ||
527 | p = m.P{ 'a' * m.V(1) + '' } | ||
528 | assert(p:match(string.rep('a', 1000)) == 1001) | ||
529 | |||
530 | -- create a grammar for a simple DFA for even number of 0s and 1s | ||
531 | -- | ||
532 | -- ->1 <---0---> 2 | ||
533 | -- ^ ^ | ||
534 | -- | | | ||
535 | -- 1 1 | ||
536 | -- | | | ||
537 | -- V V | ||
538 | -- 3 <---0---> 4 | ||
539 | -- | ||
540 | -- this grammar should keep no backtracking information | ||
541 | |||
542 | p = m.P{ | ||
543 | [1] = '0' * m.V(2) + '1' * m.V(3) + -1, | ||
544 | [2] = '0' * m.V(1) + '1' * m.V(4), | ||
545 | [3] = '0' * m.V(4) + '1' * m.V(1), | ||
546 | [4] = '0' * m.V(3) + '1' * m.V(2), | ||
547 | } | ||
548 | |||
549 | -- labeled failure | ||
550 | assert(p:match(string.rep("00", 10000))) | ||
551 | assert(p:match(string.rep("01", 10000))) | ||
552 | assert(p:match(string.rep("011", 10000))) | ||
553 | assert(not p:match(string.rep("011", 10000) .. "1")) | ||
554 | assert(not p:match(string.rep("011", 10001))) | ||
555 | |||
556 | |||
557 | -- this grammar does need backtracking info. | ||
558 | local lim = 10000 | ||
559 | p = m.P{ '0' * m.V(1) + '0' } | ||
560 | assert(not pcall(m.match, p, string.rep("0", lim))) | ||
561 | m.setmaxstack(2*lim) | ||
562 | assert(not pcall(m.match, p, string.rep("0", lim))) | ||
563 | m.setmaxstack(2*lim + 4) | ||
564 | assert(pcall(m.match, p, string.rep("0", lim))) | ||
565 | |||
566 | -- this repetition should not need stack space (only the call does) | ||
567 | p = m.P{ ('a' * m.V(1))^0 * 'b' + 'c' } | ||
568 | m.setmaxstack(200) | ||
569 | -- labeled failure | ||
570 | assert(p:match(string.rep('a', 180) .. 'c' .. string.rep('b', 180)) == 362) | ||
571 | |||
572 | m.setmaxstack(5) -- restore original limit | ||
573 | |||
574 | -- tests for optional start position | ||
575 | assert(m.match("a", "abc", 1)) | ||
576 | assert(m.match("b", "abc", 2)) | ||
577 | assert(m.match("c", "abc", 3)) | ||
578 | assert(not m.match(1, "abc", 4)) | ||
579 | assert(m.match("a", "abc", -3)) | ||
580 | assert(m.match("b", "abc", -2)) | ||
581 | assert(m.match("c", "abc", -1)) | ||
582 | assert(m.match("abc", "abc", -4)) -- truncate to position 1 | ||
583 | |||
584 | assert(m.match("", "abc", 10)) -- empty string is everywhere! | ||
585 | assert(m.match("", "", 10)) | ||
586 | assert(not m.match(1, "", 1)) | ||
587 | assert(not m.match(1, "", -1)) | ||
588 | assert(not m.match(1, "", 0)) | ||
589 | |||
590 | print("+") | ||
591 | |||
592 | |||
593 | -- tests for argument captures | ||
594 | assert(not pcall(m.Carg, 0)) | ||
595 | assert(not pcall(m.Carg, -1)) | ||
596 | assert(not pcall(m.Carg, 2^18)) | ||
597 | assert(not pcall(m.match, m.Carg(1), 'a', 1)) | ||
598 | assert(m.match(m.Carg(1), 'a', 1, print) == print) | ||
599 | x = {m.match(m.Carg(1) * m.Carg(2), '', 1, 10, 20)} | ||
600 | checkeq(x, {10, 20}) | ||
601 | |||
602 | assert(m.match(m.Cmt(m.Cg(m.Carg(3), "a") * | ||
603 | m.Cmt(m.Cb("a"), function (s,i,x) | ||
604 | assert(s == "a" and i == 1); | ||
605 | return i, x+1 | ||
606 | end) * | ||
607 | m.Carg(2), function (s,i,a,b,c) | ||
608 | assert(s == "a" and i == 1 and c == nil); | ||
609 | return i, 2*a + 3*b | ||
610 | end) * "a", | ||
611 | "a", 1, false, 100, 1000) == 2*1001 + 3*100) | ||
612 | |||
613 | |||
614 | -- tests for Lua functions | ||
615 | |||
616 | t = {} | ||
617 | s = "" | ||
618 | p = m.P(function (s1, i) assert(s == s1); t[#t + 1] = i; return nil end) * false | ||
619 | s = "hi, this is a test" | ||
620 | assert(m.match(((p - m.P(-1)) + 2)^0, s) == string.len(s) + 1) | ||
621 | assert(#t == string.len(s)/2 and t[1] == 1 and t[2] == 3) | ||
622 | |||
623 | assert(not m.match(p, s)) | ||
624 | |||
625 | p = mt.__add(function (s, i) return i end, function (s, i) return nil end) | ||
626 | assert(m.match(p, "alo")) | ||
627 | |||
628 | p = mt.__mul(function (s, i) return i end, function (s, i) return nil end) | ||
629 | assert(not m.match(p, "alo")) | ||
630 | |||
631 | |||
632 | t = {} | ||
633 | p = function (s1, i) assert(s == s1); t[#t + 1] = i; return i end | ||
634 | s = "hi, this is a test" | ||
635 | assert(m.match((m.P(1) * p)^0, s) == string.len(s) + 1) | ||
636 | assert(#t == string.len(s) and t[1] == 2 and t[2] == 3) | ||
637 | |||
638 | t = {} | ||
639 | p = m.P(function (s1, i) assert(s == s1); t[#t + 1] = i; | ||
640 | return i <= s1:len() and i end) * 1 | ||
641 | s = "hi, this is a test" | ||
642 | assert(m.match(p^0, s) == string.len(s) + 1) | ||
643 | assert(#t == string.len(s) + 1 and t[1] == 1 and t[2] == 2) | ||
644 | |||
645 | p = function (s1, i) return m.match(m.P"a"^1, s1, i) end | ||
646 | assert(m.match(p, "aaaa") == 5) | ||
647 | assert(m.match(p, "abaa") == 2) | ||
648 | assert(not m.match(p, "baaa")) | ||
649 | |||
650 | assert(not pcall(m.match, function () return 2^20 end, s)) | ||
651 | assert(not pcall(m.match, function () return 0 end, s)) | ||
652 | assert(not pcall(m.match, function (s, i) return i - 1 end, s)) | ||
653 | assert(not pcall(m.match, m.P(1)^0 * function (_, i) return i - 1 end, s)) | ||
654 | assert(m.match(m.P(1)^0 * function (_, i) return i end * -1, s)) | ||
655 | assert(not pcall(m.match, m.P(1)^0 * function (_, i) return i + 1 end, s)) | ||
656 | assert(m.match(m.P(function (s, i) return s:len() + 1 end) * -1, s)) | ||
657 | assert(not pcall(m.match, m.P(function (s, i) return s:len() + 2 end) * -1, s)) | ||
658 | assert(not m.match(m.P(function (s, i) return s:len() end) * -1, s)) | ||
659 | assert(m.match(m.P(1)^0 * function (_, i) return true end, s) == | ||
660 | string.len(s) + 1) | ||
661 | for i = 1, string.len(s) + 1 do | ||
662 | assert(m.match(function (_, _) return i end, s) == i) | ||
663 | end | ||
664 | |||
665 | p = (m.P(function (s, i) return i%2 == 0 and i end) * 1 | ||
666 | + m.P(function (s, i) return i%2 ~= 0 and i + 2 <= s:len() and i end) * 3)^0 | ||
667 | * -1 | ||
668 | assert(p:match(string.rep('a', 14000))) | ||
669 | |||
670 | -- tests for Function Replacements | ||
671 | f = function (a, ...) if a ~= "x" then return {a, ...} end end | ||
672 | |||
673 | t = m.match(m.C(1)^0/f, "abc") | ||
674 | checkeq(t, {"a", "b", "c"}) | ||
675 | |||
676 | t = m.match(m.C(1)^0/f/f, "abc") | ||
677 | checkeq(t, {{"a", "b", "c"}}) | ||
678 | |||
679 | t = m.match(m.P(1)^0/f/f, "abc") -- no capture | ||
680 | checkeq(t, {{"abc"}}) | ||
681 | |||
682 | t = m.match((m.P(1)^0/f * m.Cp())/f, "abc") | ||
683 | checkeq(t, {{"abc"}, 4}) | ||
684 | |||
685 | t = m.match((m.C(1)^0/f * m.Cp())/f, "abc") | ||
686 | checkeq(t, {{"a", "b", "c"}, 4}) | ||
687 | |||
688 | t = m.match((m.C(1)^0/f * m.Cp())/f, "xbc") | ||
689 | checkeq(t, {4}) | ||
690 | |||
691 | t = m.match(m.C(m.C(1)^0)/f, "abc") | ||
692 | checkeq(t, {"abc", "a", "b", "c"}) | ||
693 | |||
694 | g = function (...) return 1, ... end | ||
695 | t = {m.match(m.C(1)^0/g/g, "abc")} | ||
696 | checkeq(t, {1, 1, "a", "b", "c"}) | ||
697 | |||
698 | t = {m.match(m.Cc(nil,nil,4) * m.Cc(nil,3) * m.Cc(nil, nil) / g / g, "")} | ||
699 | t1 = {1,1,nil,nil,4,nil,3,nil,nil} | ||
700 | for i=1,10 do assert(t[i] == t1[i]) end | ||
701 | |||
702 | t = {m.match((m.C(1) / function (x) return x, x.."x" end)^0, "abc")} | ||
703 | checkeq(t, {"a", "ax", "b", "bx", "c", "cx"}) | ||
704 | |||
705 | t = m.match(m.Ct((m.C(1) / function (x,y) return y, x end * m.Cc(1))^0), "abc") | ||
706 | checkeq(t, {nil, "a", 1, nil, "b", 1, nil, "c", 1}) | ||
707 | |||
708 | -- tests for Query Replacements | ||
709 | |||
710 | assert(m.match(m.C(m.C(1)^0)/{abc = 10}, "abc") == 10) | ||
711 | assert(m.match(m.C(1)^0/{a = 10}, "abc") == 10) | ||
712 | assert(m.match(m.S("ba")^0/{ab = 40}, "abc") == 40) | ||
713 | t = m.match(m.Ct((m.S("ba")/{a = 40})^0), "abc") | ||
714 | checkeq(t, {40}) | ||
715 | |||
716 | assert(m.match(m.Cs((m.C(1)/{a=".", d=".."})^0), "abcdde") == ".bc....e") | ||
717 | assert(m.match(m.Cs((m.C(1)/{f="."})^0), "abcdde") == "abcdde") | ||
718 | assert(m.match(m.Cs((m.C(1)/{d="."})^0), "abcdde") == "abc..e") | ||
719 | assert(m.match(m.Cs((m.C(1)/{e="."})^0), "abcdde") == "abcdd.") | ||
720 | assert(m.match(m.Cs((m.C(1)/{e=".", f="+"})^0), "eefef") == "..+.+") | ||
721 | assert(m.match(m.Cs((m.C(1))^0), "abcdde") == "abcdde") | ||
722 | assert(m.match(m.Cs(m.C(m.C(1)^0)), "abcdde") == "abcdde") | ||
723 | assert(m.match(1 * m.Cs(m.P(1)^0), "abcdde") == "bcdde") | ||
724 | assert(m.match(m.Cs((m.C('0')/'x' + 1)^0), "abcdde") == "abcdde") | ||
725 | assert(m.match(m.Cs((m.C('0')/'x' + 1)^0), "0ab0b0") == "xabxbx") | ||
726 | assert(m.match(m.Cs((m.C('0')/'x' + m.P(1)/{b=3})^0), "b0a0b") == "3xax3") | ||
727 | assert(m.match(m.P(1)/'%0%0'/{aa = -3} * 'x', 'ax') == -3) | ||
728 | assert(m.match(m.C(1)/'%0%1'/{aa = 'z'}/{z = -3} * 'x', 'ax') == -3) | ||
729 | |||
730 | assert(m.match(m.Cs(m.Cc(0) * (m.P(1)/"")), "4321") == "0") | ||
731 | |||
732 | assert(m.match(m.Cs((m.P(1) / "%0")^0), "abcd") == "abcd") | ||
733 | assert(m.match(m.Cs((m.P(1) / "%0.%0")^0), "abcd") == "a.ab.bc.cd.d") | ||
734 | assert(m.match(m.Cs((m.P("a") / "%0.%0" + 1)^0), "abcad") == "a.abca.ad") | ||
735 | assert(m.match(m.C("a") / "%1%%%0", "a") == "a%a") | ||
736 | assert(m.match(m.Cs((m.P(1) / ".xx")^0), "abcd") == ".xx.xx.xx.xx") | ||
737 | assert(m.match(m.Cp() * m.P(3) * m.Cp()/"%2%1%1 - %0 ", "abcde") == | ||
738 | "411 - abc ") | ||
739 | |||
740 | assert(pcall(m.match, m.P(1)/"%0", "abc")) | ||
741 | assert(not pcall(m.match, m.P(1)/"%1", "abc")) -- out of range | ||
742 | assert(not pcall(m.match, m.P(1)/"%9", "abc")) -- out of range | ||
743 | |||
744 | p = m.C(1) | ||
745 | p = p * p; p = p * p; p = p * p * m.C(1) / "%9 - %1" | ||
746 | assert(p:match("1234567890") == "9 - 1") | ||
747 | |||
748 | assert(m.match(m.Cc(print), "") == print) | ||
749 | |||
750 | -- too many captures (just ignore extra ones) | ||
751 | p = m.C(1)^0 / "%2-%9-%0-%9" | ||
752 | assert(p:match"01234567890123456789" == "1-8-01234567890123456789-8") | ||
753 | s = string.rep("12345678901234567890", 20) | ||
754 | assert(m.match(m.C(1)^0 / "%9-%1-%0-%3", s) == "9-1-" .. s .. "-3") | ||
755 | |||
756 | -- string captures with non-string subcaptures | ||
757 | p = m.Cc('alo') * m.C(1) / "%1 - %2 - %1" | ||
758 | assert(p:match'x' == 'alo - x - alo') | ||
759 | |||
760 | assert(not pcall(m.match, m.Cc(true) / "%1", "a")) | ||
761 | |||
762 | -- long strings for string capture | ||
763 | l = 10000 | ||
764 | s = string.rep('a', l) .. string.rep('b', l) .. string.rep('c', l) | ||
765 | |||
766 | p = (m.C(m.P'a'^1) * m.C(m.P'b'^1) * m.C(m.P'c'^1)) / '%3%2%1' | ||
767 | |||
768 | assert(p:match(s) == string.rep('c', l) .. | ||
769 | string.rep('b', l) .. | ||
770 | string.rep('a', l)) | ||
771 | |||
772 | print"+" | ||
773 | |||
774 | -- accumulator capture | ||
775 | function f (x) return x + 1 end | ||
776 | assert(m.match(m.Cf(m.Cc(0) * m.C(1)^0, f), "alo alo") == 7) | ||
777 | |||
778 | t = {m.match(m.Cf(m.Cc(1,2,3), error), "")} | ||
779 | checkeq(t, {1}) | ||
780 | p = m.Cf(m.Ct(true) * m.Cg(m.C(m.R"az"^1) * "=" * m.C(m.R"az"^1) * ";")^0, | ||
781 | rawset) | ||
782 | t = p:match("a=b;c=du;xux=yuy;") | ||
783 | checkeq(t, {a="b", c="du", xux="yuy"}) | ||
784 | |||
785 | |||
786 | -- errors in accumulator capture | ||
787 | |||
788 | -- very long match (forces fold to be a pair open-close) producing with | ||
789 | -- no initial capture | ||
790 | assert(not pcall(m.match, m.Cf(m.P(500), print), string.rep('a', 600))) | ||
791 | |||
792 | -- nested capture produces no initial value | ||
793 | assert(not pcall(m.match, m.Cf(m.P(1) / {}, print), "alo")) | ||
794 | |||
795 | |||
796 | -- tests for loop checker | ||
797 | |||
798 | local function haveloop (p) | ||
799 | assert(not pcall(function (p) return p^0 end, m.P(p))) | ||
800 | end | ||
801 | |||
802 | haveloop(m.P("x")^-4) | ||
803 | assert(m.match(((m.P(0) + 1) * m.S"al")^0, "alo") == 3) | ||
804 | assert(m.match((("x" + #m.P(1))^-4 * m.S"al")^0, "alo") == 3) | ||
805 | haveloop("") | ||
806 | haveloop(m.P("x")^0) | ||
807 | haveloop(m.P("x")^-1) | ||
808 | haveloop(m.P("x") + 1 + 2 + m.P("a")^-1) | ||
809 | haveloop(-m.P("ab")) | ||
810 | haveloop(- -m.P("ab")) | ||
811 | haveloop(# #(m.P("ab") + "xy")) | ||
812 | haveloop(- #m.P("ab")^0) | ||
813 | haveloop(# -m.P("ab")^1) | ||
814 | haveloop(#m.V(3)) | ||
815 | haveloop(m.V(3) + m.V(1) + m.P('a')^-1) | ||
816 | haveloop({[1] = m.V(2) * m.V(3), [2] = m.V(3), [3] = m.P(0)}) | ||
817 | assert(m.match(m.P{[1] = m.V(2) * m.V(3), [2] = m.V(3), [3] = m.P(1)}^0, "abc") | ||
818 | == 3) | ||
819 | assert(m.match(m.P""^-3, "a") == 1) | ||
820 | |||
821 | local function find (p, s) | ||
822 | return m.match(basiclookfor(p), s) | ||
823 | end | ||
824 | |||
825 | |||
826 | local function badgrammar (g, expected) | ||
827 | local stat, msg = pcall(m.P, g) | ||
828 | assert(not stat) | ||
829 | if expected then assert(find(expected, msg)) end | ||
830 | end | ||
831 | |||
832 | badgrammar({[1] = m.V(1)}, "rule '1'") | ||
833 | badgrammar({[1] = m.V(2)}, "rule '2'") -- invalid non-terminal | ||
834 | badgrammar({[1] = m.V"x"}, "rule 'x'") -- invalid non-terminal | ||
835 | badgrammar({[1] = m.V{}}, "rule '(a table)'") -- invalid non-terminal | ||
836 | badgrammar({[1] = #m.P("a") * m.V(1)}, "rule '1'") -- left-recursive | ||
837 | badgrammar({[1] = -m.P("a") * m.V(1)}, "rule '1'") -- left-recursive | ||
838 | badgrammar({[1] = -1 * m.V(1)}, "rule '1'") -- left-recursive | ||
839 | badgrammar({[1] = -1 + m.V(1)}, "rule '1'") -- left-recursive | ||
840 | badgrammar({[1] = 1 * m.V(2), [2] = m.V(2)}, "rule '2'") -- left-recursive | ||
841 | badgrammar({[1] = 1 * m.V(2)^0, [2] = m.P(0)}, "rule '1'") -- inf. loop | ||
842 | badgrammar({ m.V(2), m.V(3)^0, m.P"" }, "rule '2'") -- inf. loop | ||
843 | badgrammar({ m.V(2) * m.V(3)^0, m.V(3)^0, m.P"" }, "rule '1'") -- inf. loop | ||
844 | badgrammar({"x", x = #(m.V(1) * 'a') }, "rule '1'") -- inf. loop | ||
845 | badgrammar({ -(m.V(1) * 'a') }, "rule '1'") -- inf. loop | ||
846 | badgrammar({"x", x = m.P'a'^-1 * m.V"x"}, "rule 'x'") -- left recursive | ||
847 | badgrammar({"x", x = m.P'a' * m.V"y"^1, y = #m.P(1)}, "rule 'x'") | ||
848 | |||
849 | assert(m.match({'a' * -m.V(1)}, "aaa") == 2) | ||
850 | assert(m.match({'a' * -m.V(1)}, "aaaa") == nil) | ||
851 | |||
852 | |||
853 | -- good x bad grammars | ||
854 | m.P{ ('a' * m.V(1))^-1 } | ||
855 | m.P{ -('a' * m.V(1)) } | ||
856 | m.P{ ('abc' * m.V(1))^-1 } | ||
857 | m.P{ -('abc' * m.V(1)) } | ||
858 | badgrammar{ #m.P('abc') * m.V(1) } | ||
859 | badgrammar{ -('a' + m.V(1)) } | ||
860 | m.P{ #('a' * m.V(1)) } | ||
861 | badgrammar{ #('a' + m.V(1)) } | ||
862 | m.P{ m.B{ m.P'abc' } * 'a' * m.V(1) } | ||
863 | badgrammar{ m.B{ m.P'abc' } * m.V(1) } | ||
864 | badgrammar{ ('a' + m.P'bcd')^-1 * m.V(1) } | ||
865 | |||
866 | |||
867 | -- simple tests for maximum sizes: | ||
868 | local p = m.P"a" | ||
869 | for i=1,14 do p = p * p end | ||
870 | |||
871 | p = {} | ||
872 | for i=1,100 do p[i] = m.P"a" end | ||
873 | p = m.P(p) | ||
874 | |||
875 | |||
876 | -- strange values for rule labels | ||
877 | |||
878 | p = m.P{ "print", | ||
879 | print = m.V(print), | ||
880 | [print] = m.V(_G), | ||
881 | [_G] = m.P"a", | ||
882 | } | ||
883 | |||
884 | assert(p:match("a")) | ||
885 | |||
886 | -- initial rule | ||
887 | g = {} | ||
888 | for i = 1, 10 do g["i"..i] = "a" * m.V("i"..i+1) end | ||
889 | g.i11 = m.P"" | ||
890 | for i = 1, 10 do | ||
891 | g[1] = "i"..i | ||
892 | local p = m.P(g) | ||
893 | assert(p:match("aaaaaaaaaaa") == 11 - i + 1) | ||
894 | end | ||
895 | |||
896 | print"+" | ||
897 | |||
898 | |||
899 | -- tests for back references | ||
900 | assert(not pcall(m.match, m.Cb('x'), '')) | ||
901 | assert(not pcall(m.match, m.Cg(1, 'a') * m.Cb('b'), 'a')) | ||
902 | |||
903 | p = m.Cg(m.C(1) * m.C(1), "k") * m.Ct(m.Cb("k")) | ||
904 | t = p:match("ab") | ||
905 | checkeq(t, {"a", "b"}) | ||
906 | |||
907 | |||
908 | t = {} | ||
909 | function foo (p) t[#t + 1] = p; return p .. "x" end | ||
910 | |||
911 | p = m.Cg(m.C(2) / foo, "x") * m.Cb"x" * | ||
912 | m.Cg(m.Cb('x') / foo, "x") * m.Cb"x" * | ||
913 | m.Cg(m.Cb('x') / foo, "x") * m.Cb"x" * | ||
914 | m.Cg(m.Cb('x') / foo, "x") * m.Cb"x" | ||
915 | x = {p:match'ab'} | ||
916 | checkeq(x, {'abx', 'abxx', 'abxxx', 'abxxxx'}) | ||
917 | checkeq(t, {'ab', | ||
918 | 'ab', 'abx', | ||
919 | 'ab', 'abx', 'abxx', | ||
920 | 'ab', 'abx', 'abxx', 'abxxx'}) | ||
921 | |||
922 | |||
923 | |||
924 | -- tests for match-time captures | ||
925 | |||
926 | p = m.P'a' * (function (s, i) return (s:sub(i, i) == 'b') and i + 1 end) | ||
927 | + 'acd' | ||
928 | |||
929 | assert(p:match('abc') == 3) | ||
930 | assert(p:match('acd') == 4) | ||
931 | |||
932 | local function id (s, i, ...) | ||
933 | return true, ... | ||
934 | end | ||
935 | |||
936 | assert(m.Cmt(m.Cs((m.Cmt(m.S'abc' / { a = 'x', c = 'y' }, id) + | ||
937 | m.R'09'^1 / string.char + | ||
938 | m.P(1))^0), id):match"acb98+68c" == "xyb\98+\68y") | ||
939 | |||
940 | p = m.P{'S', | ||
941 | S = m.V'atom' * space | ||
942 | + m.Cmt(m.Ct("(" * space * (m.Cmt(m.V'S'^1, id) + m.P(true)) * ")" * space), id), | ||
943 | atom = m.Cmt(m.C(m.R("AZ", "az", "09")^1), id) | ||
944 | } | ||
945 | x = p:match"(a g () ((b) c) (d (e)))" | ||
946 | checkeq(x, {'a', 'g', {}, {{'b'}, 'c'}, {'d', {'e'}}}); | ||
947 | |||
948 | x = {(m.Cmt(1, id)^0):match(string.rep('a', 500))} | ||
949 | assert(#x == 500) | ||
950 | |||
951 | local function id(s, i, x) | ||
952 | if x == 'a' then return i, 1, 3, 7 | ||
953 | else return nil, 2, 4, 6, 8 | ||
954 | end | ||
955 | end | ||
956 | |||
957 | p = ((m.P(id) * 1 + m.Cmt(2, id) * 1 + m.Cmt(1, id) * 1))^0 | ||
958 | assert(table.concat{p:match('abababab')} == string.rep('137', 4)) | ||
959 | |||
960 | local function ref (s, i, x) | ||
961 | return m.match(x, s, i - x:len()) | ||
962 | end | ||
963 | |||
964 | assert(m.Cmt(m.P(1)^0, ref):match('alo') == 4) | ||
965 | assert((m.P(1) * m.Cmt(m.P(1)^0, ref)):match('alo') == 4) | ||
966 | assert(not (m.P(1) * m.Cmt(m.C(1)^0, ref)):match('alo')) | ||
967 | |||
968 | ref = function (s,i,x) return i == tonumber(x) and i, 'xuxu' end | ||
969 | |||
970 | assert(m.Cmt(1, ref):match'2') | ||
971 | assert(not m.Cmt(1, ref):match'1') | ||
972 | assert(m.Cmt(m.P(1)^0, ref):match'03') | ||
973 | |||
974 | function ref (s, i, a, b) | ||
975 | if a == b then return i, a:upper() end | ||
976 | end | ||
977 | |||
978 | p = m.Cmt(m.C(m.R"az"^1) * "-" * m.C(m.R"az"^1), ref) | ||
979 | p = (any - p)^0 * p * any^0 * -1 | ||
980 | |||
981 | assert(p:match'abbbc-bc ddaa' == 'BC') | ||
982 | |||
983 | do -- match-time captures cannot be optimized away | ||
984 | local touch = 0 | ||
985 | f = m.P(function () touch = touch + 1; return true end) | ||
986 | |||
987 | local function check(n) n = n or 1; assert(touch == n); touch = 0 end | ||
988 | |||
989 | assert(m.match(f * false + 'b', 'a') == nil); check() | ||
990 | assert(m.match(f * false + 'b', '') == nil); check() | ||
991 | assert(m.match( (f * 'a')^0 * 'b', 'b') == 2); check() | ||
992 | assert(m.match( (f * 'a')^0 * 'b', '') == nil); check() | ||
993 | assert(m.match( (f * 'a')^-1 * 'b', 'b') == 2); check() | ||
994 | assert(m.match( (f * 'a')^-1 * 'b', '') == nil); check() | ||
995 | assert(m.match( ('b' + f * 'a')^-1 * 'b', '') == nil); check() | ||
996 | assert(m.match( (m.P'b'^-1 * f * 'a')^-1 * 'b', '') == nil); check() | ||
997 | assert(m.match( (-m.P(1) * m.P'b'^-1 * f * 'a')^-1 * 'b', '') == nil); | ||
998 | check() | ||
999 | assert(m.match( (f * 'a' + 'b')^-1 * 'b', '') == nil); check() | ||
1000 | assert(m.match(f * 'a' + f * 'b', 'b') == 2); check(2) | ||
1001 | assert(m.match(f * 'a' + f * 'b', 'a') == 2); check(1) | ||
1002 | assert(m.match(-f * 'a' + 'b', 'b') == 2); check(1) | ||
1003 | assert(m.match(-f * 'a' + 'b', '') == nil); check(1) | ||
1004 | end | ||
1005 | |||
1006 | c = '[' * m.Cg(m.P'='^0, "init") * '[' * | ||
1007 | { m.Cmt(']' * m.C(m.P'='^0) * ']' * m.Cb("init"), function (_, _, s1, s2) | ||
1008 | return s1 == s2 end) | ||
1009 | + 1 * m.V(1) } / 0 | ||
1010 | |||
1011 | assert(c:match'[==[]]====]]]]==]===[]' == 18) | ||
1012 | assert(c:match'[[]=]====]=]]]==]===[]' == 14) | ||
1013 | assert(not c:match'[[]=]====]=]=]==]===[]') | ||
1014 | |||
1015 | |||
1016 | -- old bug: optimization of concat with fail removed match-time capture | ||
1017 | p = m.Cmt(0, function (s) p = s end) * m.P(false) | ||
1018 | assert(not p:match('alo')) | ||
1019 | assert(p == 'alo') | ||
1020 | |||
1021 | |||
1022 | -- ensure that failed match-time captures are not kept on Lua stack | ||
1023 | do | ||
1024 | local t = {__mode = "kv"}; setmetatable(t,t) | ||
1025 | local c = 0 | ||
1026 | |||
1027 | local function foo (s,i) | ||
1028 | collectgarbage(); | ||
1029 | assert(next(t) == "__mode" and next(t, "__mode") == nil) | ||
1030 | local x = {} | ||
1031 | t[x] = true | ||
1032 | c = c + 1 | ||
1033 | return i, x | ||
1034 | end | ||
1035 | |||
1036 | local p = m.P{ m.Cmt(0, foo) * m.P(false) + m.P(1) * m.V(1) + m.P"" } | ||
1037 | p:match(string.rep('1', 10)) | ||
1038 | assert(c == 11) | ||
1039 | end | ||
1040 | |||
1041 | p = (m.P(function () return true, "a" end) * 'a' | ||
1042 | + m.P(function (s, i) return i, "aa", 20 end) * 'b' | ||
1043 | + m.P(function (s,i) if i <= #s then return i, "aaa" end end) * 1)^0 | ||
1044 | |||
1045 | t = {p:match('abacc')} | ||
1046 | checkeq(t, {'a', 'aa', 20, 'a', 'aaa', 'aaa'}) | ||
1047 | |||
1048 | |||
1049 | ------------------------------------------------------------------- | ||
1050 | -- Tests for 're' module | ||
1051 | ------------------------------------------------------------------- | ||
1052 | |||
1053 | local re = require "re" | ||
1054 | |||
1055 | local match, compile = re.match, re.compile | ||
1056 | |||
1057 | assert(match("a", ".") == 2) | ||
1058 | assert(match("a", "''") == 1) | ||
1059 | assert(match("", " ! . ") == 1) | ||
1060 | assert(not match("a", " ! . ")) | ||
1061 | assert(match("abcde", " ( . . ) * ") == 5) | ||
1062 | assert(match("abbcde", " [a-c] +") == 5) | ||
1063 | assert(match("0abbc1de", "'0' [a-c]+ '1'") == 7) | ||
1064 | assert(match("0zz1dda", "'0' [^a-c]+ 'a'") == 8) | ||
1065 | assert(match("abbc--", " [a-c] + +") == 5) | ||
1066 | assert(match("abbc--", " [ac-] +") == 2) | ||
1067 | assert(match("abbc--", " [-acb] + ") == 7) | ||
1068 | assert(not match("abbcde", " [b-z] + ")) | ||
1069 | assert(match("abb\"de", '"abb"["]"de"') == 7) | ||
1070 | assert(match("abceeef", "'ac' ? 'ab' * 'c' { 'e' * } / 'abceeef' ") == "eee") | ||
1071 | assert(match("abceeef", "'ac'? 'ab'* 'c' { 'f'+ } / 'abceeef' ") == 8) | ||
1072 | local t = {match("abceefe", "( ( & 'e' {} ) ? . ) * ")} | ||
1073 | checkeq(t, {4, 5, 7}) | ||
1074 | local t = {match("abceefe", "((&&'e' {})? .)*")} | ||
1075 | checkeq(t, {4, 5, 7}) | ||
1076 | local t = {match("abceefe", "( ( ! ! 'e' {} ) ? . ) *")} | ||
1077 | checkeq(t, {4, 5, 7}) | ||
1078 | local t = {match("abceefe", "(( & ! & ! 'e' {})? .)*")} | ||
1079 | checkeq(t, {4, 5, 7}) | ||
1080 | |||
1081 | assert(match("cccx" , "'ab'? ('ccc' / ('cde' / 'cd'*)? / 'ccc') 'x'+") == 5) | ||
1082 | assert(match("cdx" , "'ab'? ('ccc' / ('cde' / 'cd'*)? / 'ccc') 'x'+") == 4) | ||
1083 | assert(match("abcdcdx" , "'ab'? ('ccc' / ('cde' / 'cd'*)? / 'ccc') 'x'+") == 8) | ||
1084 | |||
1085 | assert(match("abc", "a <- (. a)?") == 4) | ||
1086 | b = "balanced <- '(' ([^()] / balanced)* ')'" | ||
1087 | assert(match("(abc)", b)) | ||
1088 | assert(match("(a(b)((c) (d)))", b)) | ||
1089 | assert(not match("(a(b ((c) (d)))", b)) | ||
1090 | |||
1091 | b = compile[[ balanced <- "(" ([^()] / balanced)* ")" ]] | ||
1092 | assert(b == m.P(b)) | ||
1093 | assert(b:match"((((a))(b)))") | ||
1094 | |||
1095 | local g = [[ | ||
1096 | S <- "0" B / "1" A / "" -- balanced strings | ||
1097 | A <- "0" S / "1" A A -- one more 0 | ||
1098 | B <- "1" S / "0" B B -- one more 1 | ||
1099 | ]] | ||
1100 | assert(match("00011011", g) == 9) | ||
1101 | |||
1102 | local g = [[ | ||
1103 | S <- ("0" B / "1" A)* | ||
1104 | A <- "0" / "1" A A | ||
1105 | B <- "1" / "0" B B | ||
1106 | ]] | ||
1107 | assert(match("00011011", g) == 9) | ||
1108 | assert(match("000110110", g) == 9) | ||
1109 | assert(match("011110110", g) == 3) | ||
1110 | assert(match("000110010", g) == 1) | ||
1111 | |||
1112 | s = "aaaaaaaaaaaaaaaaaaaaaaaa" | ||
1113 | assert(match(s, "'a'^3") == 4) | ||
1114 | assert(match(s, "'a'^0") == 1) | ||
1115 | assert(match(s, "'a'^+3") == s:len() + 1) | ||
1116 | assert(not match(s, "'a'^+30")) | ||
1117 | assert(match(s, "'a'^-30") == s:len() + 1) | ||
1118 | assert(match(s, "'a'^-5") == 6) | ||
1119 | for i = 1, s:len() do | ||
1120 | assert(match(s, string.format("'a'^+%d", i)) >= i + 1) | ||
1121 | assert(match(s, string.format("'a'^-%d", i)) <= i + 1) | ||
1122 | assert(match(s, string.format("'a'^%d", i)) == i + 1) | ||
1123 | end | ||
1124 | assert(match("01234567890123456789", "[0-9]^3+") == 19) | ||
1125 | |||
1126 | |||
1127 | assert(match("01234567890123456789", "({....}{...}) -> '%2%1'") == "4560123") | ||
1128 | t = match("0123456789", "{| {.}* |}") | ||
1129 | checkeq(t, {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}) | ||
1130 | assert(match("012345", "{| (..) -> '%0%0' |}")[1] == "0101") | ||
1131 | |||
1132 | assert(match("abcdef", "( {.} {.} {.} {.} {.} ) -> 3") == "c") | ||
1133 | assert(match("abcdef", "( {:x: . :} {.} {.} {.} {.} ) -> 3") == "d") | ||
1134 | assert(match("abcdef", "( {:x: . :} {.} {.} {.} {.} ) -> 0") == 6) | ||
1135 | |||
1136 | assert(not match("abcdef", "{:x: ({.} {.} {.}) -> 2 :} =x")) | ||
1137 | assert(match("abcbef", "{:x: ({.} {.} {.}) -> 2 :} =x")) | ||
1138 | |||
1139 | eqcharset(compile"[]]", "]") | ||
1140 | eqcharset(compile"[][]", m.S"[]") | ||
1141 | eqcharset(compile"[]-]", m.S"-]") | ||
1142 | eqcharset(compile"[-]", m.S"-") | ||
1143 | eqcharset(compile"[az-]", m.S"a-z") | ||
1144 | eqcharset(compile"[-az]", m.S"a-z") | ||
1145 | eqcharset(compile"[a-z]", m.R"az") | ||
1146 | eqcharset(compile"[]['\"]", m.S[[]['"]]) | ||
1147 | |||
1148 | eqcharset(compile"[^]]", any - "]") | ||
1149 | eqcharset(compile"[^][]", any - m.S"[]") | ||
1150 | eqcharset(compile"[^]-]", any - m.S"-]") | ||
1151 | eqcharset(compile"[^]-]", any - m.S"-]") | ||
1152 | eqcharset(compile"[^-]", any - m.S"-") | ||
1153 | eqcharset(compile"[^az-]", any - m.S"a-z") | ||
1154 | eqcharset(compile"[^-az]", any - m.S"a-z") | ||
1155 | eqcharset(compile"[^a-z]", any - m.R"az") | ||
1156 | eqcharset(compile"[^]['\"]", any - m.S[[]['"]]) | ||
1157 | |||
1158 | -- tests for comments in 're' | ||
1159 | e = compile[[ | ||
1160 | A <- _B -- \t \n %nl .<> <- -> -- | ||
1161 | _B <- 'x' --]] | ||
1162 | assert(e:match'xy' == 2) | ||
1163 | |||
1164 | -- tests for 're' with pre-definitions | ||
1165 | defs = {digits = m.R"09", letters = m.R"az", _=m.P"__"} | ||
1166 | e = compile("%letters (%letters / %digits)*", defs) | ||
1167 | assert(e:match"x123" == 5) | ||
1168 | e = compile("%_", defs) | ||
1169 | assert(e:match"__" == 3) | ||
1170 | |||
1171 | e = compile([[ | ||
1172 | S <- A+ | ||
1173 | A <- %letters+ B | ||
1174 | B <- %digits+ | ||
1175 | ]], defs) | ||
1176 | |||
1177 | e = compile("{[0-9]+'.'?[0-9]*} -> sin", math) | ||
1178 | assert(e:match("2.34") == math.sin(2.34)) | ||
1179 | |||
1180 | |||
1181 | function eq (_, _, a, b) return a == b end | ||
1182 | |||
1183 | c = re.compile([[ | ||
1184 | longstring <- '[' {:init: '='* :} '[' close | ||
1185 | close <- ']' =init ']' / . close | ||
1186 | ]]) | ||
1187 | |||
1188 | assert(c:match'[==[]]===]]]]==]===[]' == 17) | ||
1189 | assert(c:match'[[]=]====]=]]]==]===[]' == 14) | ||
1190 | assert(not c:match'[[]=]====]=]=]==]===[]') | ||
1191 | |||
1192 | c = re.compile" '[' {:init: '='* :} '[' (!(']' =init ']') .)* ']' =init ']' !. " | ||
1193 | |||
1194 | assert(c:match'[==[]]===]]]]==]') | ||
1195 | assert(c:match'[[]=]====]=][]==]===[]]') | ||
1196 | assert(not c:match'[[]=]====]=]=]==]===[]') | ||
1197 | |||
1198 | assert(re.find("hi alalo", "{:x:..:} =x") == 4) | ||
1199 | assert(re.find("hi alalo", "{:x:..:} =x", 4) == 4) | ||
1200 | assert(not re.find("hi alalo", "{:x:..:} =x", 5)) | ||
1201 | assert(re.find("hi alalo", "{'al'}", 5) == 6) | ||
1202 | assert(re.find("hi aloalolo", "{:x:..:} =x") == 8) | ||
1203 | assert(re.find("alo alohi x x", "{:word:%w+:}%W*(=word)!%w") == 11) | ||
1204 | |||
1205 | -- re.find discards any captures | ||
1206 | local a,b,c = re.find("alo", "{.}{'o'}") | ||
1207 | assert(a == 2 and b == 3 and c == nil) | ||
1208 | |||
1209 | local function match (s,p) | ||
1210 | local i,e = re.find(s,p) | ||
1211 | if i then return s:sub(i, e) end | ||
1212 | end | ||
1213 | assert(match("alo alo", '[a-z]+') == "alo") | ||
1214 | assert(match("alo alo", '{:x: [a-z]+ :} =x') == nil) | ||
1215 | assert(match("alo alo", "{:x: [a-z]+ :} ' ' =x") == "alo alo") | ||
1216 | |||
1217 | assert(re.gsub("alo alo", "[abc]", "x") == "xlo xlo") | ||
1218 | assert(re.gsub("alo alo", "%w+", ".") == ". .") | ||
1219 | assert(re.gsub("hi, how are you", "[aeiou]", string.upper) == | ||
1220 | "hI, hOw ArE yOU") | ||
1221 | |||
1222 | s = 'hi [[a comment[=]=] ending here]] and [=[another]]=]]' | ||
1223 | c = re.compile" '[' {:i: '='* :} '[' (!(']' =i ']') .)* ']' { =i } ']' " | ||
1224 | assert(re.gsub(s, c, "%2") == 'hi and =]') | ||
1225 | assert(re.gsub(s, c, "%0") == s) | ||
1226 | assert(re.gsub('[=[hi]=]', c, "%2") == '=') | ||
1227 | |||
1228 | assert(re.find("", "!.") == 1) | ||
1229 | assert(re.find("alo", "!.") == 4) | ||
1230 | |||
1231 | function addtag (s, i, t, tag) t.tag = tag; return i, t end | ||
1232 | |||
1233 | c = re.compile([[ | ||
1234 | doc <- block !. | ||
1235 | block <- (start {| (block / { [^<]+ })* |} end?) => addtag | ||
1236 | start <- '<' {:tag: [a-z]+ :} '>' | ||
1237 | end <- '</' { =tag } '>' | ||
1238 | ]], {addtag = addtag}) | ||
1239 | |||
1240 | x = c:match[[ | ||
1241 | <x>hi<b>hello</b>but<b>totheend</x>]] | ||
1242 | checkeq(x, {tag='x', 'hi', {tag = 'b', 'hello'}, 'but', | ||
1243 | {'totheend'}}) | ||
1244 | |||
1245 | |||
1246 | -- tests for look-ahead captures | ||
1247 | x = {re.match("alo", "&(&{.}) !{'b'} {&(...)} &{..} {...} {!.}")} | ||
1248 | checkeq(x, {"", "alo", ""}) | ||
1249 | |||
1250 | assert(re.match("aloalo", | ||
1251 | "{~ (((&'al' {.}) -> 'A%1' / (&%l {.}) -> '%1%1') / .)* ~}") | ||
1252 | == "AallooAalloo") | ||
1253 | |||
1254 | -- bug in 0.9 (and older versions), due to captures in look-aheads | ||
1255 | x = re.compile[[ {~ (&(. ([a-z]* -> '*')) ([a-z]+ -> '+') ' '*)* ~} ]] | ||
1256 | assert(x:match"alo alo" == "+ +") | ||
1257 | |||
1258 | -- valid capture in look-ahead (used inside the look-ahead itself) | ||
1259 | x = re.compile[[ | ||
1260 | S <- &({:two: .. :} . =two) {[a-z]+} / . S | ||
1261 | ]] | ||
1262 | assert(x:match("hello aloaLo aloalo xuxu") == "aloalo") | ||
1263 | |||
1264 | |||
1265 | p = re.compile[[ | ||
1266 | block <- {| {:ident:space*:} line | ||
1267 | ((=ident !space line) / &(=ident space) block)* |} | ||
1268 | line <- {[^%nl]*} %nl | ||
1269 | space <- '_' -- should be ' ', but '_' is simpler for editors | ||
1270 | ]] | ||
1271 | |||
1272 | t= p:match[[ | ||
1273 | 1 | ||
1274 | __1.1 | ||
1275 | __1.2 | ||
1276 | ____1.2.1 | ||
1277 | ____ | ||
1278 | 2 | ||
1279 | __2.1 | ||
1280 | ]] | ||
1281 | checkeq(t, {"1", {"1.1", "1.2", {"1.2.1", "", ident = "____"}, ident = "__"}, | ||
1282 | "2", {"2.1", ident = "__"}, ident = ""}) | ||
1283 | |||
1284 | |||
1285 | -- nested grammars | ||
1286 | p = re.compile[[ | ||
1287 | s <- a b !. | ||
1288 | b <- ( x <- ('b' x)? ) | ||
1289 | a <- ( x <- 'a' x? ) | ||
1290 | ]] | ||
1291 | |||
1292 | assert(p:match'aaabbb') | ||
1293 | assert(p:match'aaa') | ||
1294 | assert(not p:match'bbb') | ||
1295 | assert(not p:match'aaabbba') | ||
1296 | |||
1297 | -- testing groups | ||
1298 | t = {re.match("abc", "{:S <- {:.:} {S} / '':}")} | ||
1299 | checkeq(t, {"a", "bc", "b", "c", "c", ""}) | ||
1300 | |||
1301 | t = re.match("1234", "{| {:a:.:} {:b:.:} {:c:.{.}:} |}") | ||
1302 | checkeq(t, {a="1", b="2", c="4"}) | ||
1303 | t = re.match("1234", "{|{:a:.:} {:b:{.}{.}:} {:c:{.}:}|}") | ||
1304 | checkeq(t, {a="1", b="2", c="4"}) | ||
1305 | t = re.match("12345", "{| {:.:} {:b:{.}{.}:} {:{.}{.}:} |}") | ||
1306 | checkeq(t, {"1", b="2", "4", "5"}) | ||
1307 | t = re.match("12345", "{| {:.:} {:{:b:{.}{.}:}:} {:{.}{.}:} |}") | ||
1308 | checkeq(t, {"1", "23", "4", "5"}) | ||
1309 | t = re.match("12345", "{| {:.:} {{:b:{.}{.}:}} {:{.}{.}:} |}") | ||
1310 | checkeq(t, {"1", "23", "4", "5"}) | ||
1311 | |||
1312 | |||
1313 | -- testing pre-defined names | ||
1314 | assert(os.setlocale("C") == "C") | ||
1315 | |||
1316 | function eqlpeggsub (p1, p2) | ||
1317 | local s1 = cs2str(re.compile(p1)) | ||
1318 | local s2 = string.gsub(allchar, "[^" .. p2 .. "]", "") | ||
1319 | -- if s1 ~= s2 then print(#s1,#s2) end | ||
1320 | assert(s1 == s2) | ||
1321 | end | ||
1322 | |||
1323 | |||
1324 | eqlpeggsub("%w", "%w") | ||
1325 | eqlpeggsub("%a", "%a") | ||
1326 | eqlpeggsub("%l", "%l") | ||
1327 | eqlpeggsub("%u", "%u") | ||
1328 | eqlpeggsub("%p", "%p") | ||
1329 | eqlpeggsub("%d", "%d") | ||
1330 | eqlpeggsub("%x", "%x") | ||
1331 | eqlpeggsub("%s", "%s") | ||
1332 | eqlpeggsub("%c", "%c") | ||
1333 | |||
1334 | eqlpeggsub("%W", "%W") | ||
1335 | eqlpeggsub("%A", "%A") | ||
1336 | eqlpeggsub("%L", "%L") | ||
1337 | eqlpeggsub("%U", "%U") | ||
1338 | eqlpeggsub("%P", "%P") | ||
1339 | eqlpeggsub("%D", "%D") | ||
1340 | eqlpeggsub("%X", "%X") | ||
1341 | eqlpeggsub("%S", "%S") | ||
1342 | eqlpeggsub("%C", "%C") | ||
1343 | |||
1344 | eqlpeggsub("[%w]", "%w") | ||
1345 | eqlpeggsub("[_%w]", "_%w") | ||
1346 | eqlpeggsub("[^%w]", "%W") | ||
1347 | eqlpeggsub("[%W%S]", "%W%S") | ||
1348 | |||
1349 | re.updatelocale() | ||
1350 | |||
1351 | -- testing nested substitutions x string captures | ||
1352 | |||
1353 | p = re.compile[[ | ||
1354 | text <- {~ item* ~} | ||
1355 | item <- macro / [^()] / '(' item* ')' | ||
1356 | arg <- ' '* {~ (!',' item)* ~} | ||
1357 | args <- '(' arg (',' arg)* ')' | ||
1358 | macro <- ('apply' args) -> '%1(%2)' | ||
1359 | / ('add' args) -> '%1 + %2' | ||
1360 | / ('mul' args) -> '%1 * %2' | ||
1361 | ]] | ||
1362 | |||
1363 | assert(p:match"add(mul(a,b), apply(f,x))" == "a * b + f(x)") | ||
1364 | |||
1365 | rev = re.compile[[ R <- (!.) -> '' / ({.} R) -> '%2%1']] | ||
1366 | |||
1367 | assert(rev:match"0123456789" == "9876543210") | ||
1368 | |||
1369 | |||
1370 | -- testing error messages in re | ||
1371 | |||
1372 | local function errmsg (p, err) | ||
1373 | local s, msg = pcall(re.compile, p) | ||
1374 | assert(not s and string.find(msg, err)) | ||
1375 | end | ||
1376 | |||
1377 | errmsg('aaaa', "rule 'aaaa'") | ||
1378 | errmsg('a', 'outside') | ||
1379 | errmsg('b <- a', 'undefined') | ||
1380 | errmsg("x <- 'a' x <- 'b'", 'already defined') | ||
1381 | errmsg("'a' -", "near '-'") | ||
1382 | |||
1383 | |||
1384 | print"OK" | ||
1385 | |||
1386 | |||