diff options
Diffstat (limited to 'test.lua')
-rwxr-xr-x | test.lua | 40 |
1 files changed, 31 insertions, 9 deletions
@@ -1,6 +1,6 @@ | |||
1 | #!/usr/bin/env lua5.1 | 1 | #!/usr/bin/env lua |
2 | 2 | ||
3 | -- $Id: test.lua,v 1.106 2015/03/04 17:31:33 roberto Exp $ | 3 | -- $Id: test.lua,v 1.109 2015/09/28 17:01:25 roberto Exp $ |
4 | 4 | ||
5 | -- require"strict" -- just to be pedantic | 5 | -- require"strict" -- just to be pedantic |
6 | 6 | ||
@@ -16,9 +16,6 @@ local unpack = rawget(table, "unpack") or unpack | |||
16 | local loadstring = rawget(_G, "loadstring") or load | 16 | local loadstring = rawget(_G, "loadstring") or load |
17 | 17 | ||
18 | 18 | ||
19 | -- most tests here do not need much stack space | ||
20 | m.setmaxstack(5) | ||
21 | |||
22 | local any = m.P(1) | 19 | local any = m.P(1) |
23 | local space = m.S" \t\n"^0 | 20 | local space = m.S" \t\n"^0 |
24 | 21 | ||
@@ -291,6 +288,13 @@ assert(m.match(m.P"ab"^-1 - "c", "abcd") == 3) | |||
291 | 288 | ||
292 | p = ('Aa' * ('Bb' * ('Cc' * m.P'Dd'^0)^0)^0)^-1 | 289 | p = ('Aa' * ('Bb' * ('Cc' * m.P'Dd'^0)^0)^0)^-1 |
293 | assert(p:match("AaBbCcDdBbCcDdDdDdBb") == 21) | 290 | assert(p:match("AaBbCcDdBbCcDdDdDdBb") == 21) |
291 | |||
292 | |||
293 | -- bug in 0.12.2 | ||
294 | -- p = { ('ab' ('c' 'ef'?)*)? } | ||
295 | p = m.C(('ab' * ('c' * m.P'ef'^-1)^0)^-1) | ||
296 | s = "abcefccefc" | ||
297 | assert(s == p:match(s)) | ||
294 | 298 | ||
295 | 299 | ||
296 | pi = "3.14159 26535 89793 23846 26433 83279 50288 41971 69399 37510" | 300 | pi = "3.14159 26535 89793 23846 26433 83279 50288 41971 69399 37510" |
@@ -352,10 +356,16 @@ checkeq(t, {hi = 10, ho = 20}) | |||
352 | t = p:match'abc' | 356 | t = p:match'abc' |
353 | checkeq(t, {hi = 10, ho = 20, 'a', 'b', 'c'}) | 357 | checkeq(t, {hi = 10, ho = 20, 'a', 'b', 'c'}) |
354 | 358 | ||
359 | -- non-string group names | ||
360 | p = m.Ct(m.Cg(1, print) * m.Cg(1, 23.5) * m.Cg(1, io)) | ||
361 | t = p:match('abcdefghij') | ||
362 | assert(t[print] == 'a' and t[23.5] == 'b' and t[io] == 'c') | ||
363 | |||
355 | 364 | ||
356 | -- test for error messages | 365 | -- test for error messages |
357 | local function checkerr (msg, f, ...) | 366 | local function checkerr (msg, f, ...) |
358 | local st, err = pcall(f, ...) | 367 | local st, err = pcall(f, ...) |
368 | print(st, err) | ||
359 | assert(not st and m.match({ m.P(msg) + 1 * m.V(1) }, err)) | 369 | assert(not st and m.match({ m.P(msg) + 1 * m.V(1) }, err)) |
360 | end | 370 | end |
361 | 371 | ||
@@ -594,9 +604,9 @@ assert(not p:match(string.rep("011", 10001))) | |||
594 | -- this grammar does need backtracking info. | 604 | -- this grammar does need backtracking info. |
595 | local lim = 10000 | 605 | local lim = 10000 |
596 | p = m.P{ '0' * m.V(1) + '0' } | 606 | p = m.P{ '0' * m.V(1) + '0' } |
597 | checkerr("too many pending", m.match, p, string.rep("0", lim)) | 607 | checkerr("stack overflow", m.match, p, string.rep("0", lim)) |
598 | m.setmaxstack(2*lim) | 608 | m.setmaxstack(2*lim) |
599 | checkerr("too many pending", m.match, p, string.rep("0", lim)) | 609 | checkerr("stack overflow", m.match, p, string.rep("0", lim)) |
600 | m.setmaxstack(2*lim + 4) | 610 | m.setmaxstack(2*lim + 4) |
601 | assert(m.match(p, string.rep("0", lim)) == lim + 1) | 611 | assert(m.match(p, string.rep("0", lim)) == lim + 1) |
602 | 612 | ||
@@ -605,7 +615,7 @@ p = m.P{ ('a' * m.V(1))^0 * 'b' + 'c' } | |||
605 | m.setmaxstack(200) | 615 | m.setmaxstack(200) |
606 | assert(p:match(string.rep('a', 180) .. 'c' .. string.rep('b', 180)) == 362) | 616 | assert(p:match(string.rep('a', 180) .. 'c' .. string.rep('b', 180)) == 362) |
607 | 617 | ||
608 | m.setmaxstack(5) -- restore original limit | 618 | m.setmaxstack(100) -- restore low limit |
609 | 619 | ||
610 | -- tests for optional start position | 620 | -- tests for optional start position |
611 | assert(m.match("a", "abc", 1)) | 621 | assert(m.match("a", "abc", 1)) |
@@ -737,6 +747,10 @@ t = {m.match(m.Cc(nil,nil,4) * m.Cc(nil,3) * m.Cc(nil, nil) / g / g, "")} | |||
737 | t1 = {1,1,nil,nil,4,nil,3,nil,nil} | 747 | t1 = {1,1,nil,nil,4,nil,3,nil,nil} |
738 | for i=1,10 do assert(t[i] == t1[i]) end | 748 | for i=1,10 do assert(t[i] == t1[i]) end |
739 | 749 | ||
750 | -- bug in 0.12.2: ktable with only nil could be eliminated when joining | ||
751 | -- with a pattern without ktable | ||
752 | assert((m.P"aaa" * m.Cc(nil)):match"aaa" == nil) | ||
753 | |||
740 | t = {m.match((m.C(1) / function (x) return x, x.."x" end)^0, "abc")} | 754 | t = {m.match((m.C(1) / function (x) return x, x.."x" end)^0, "abc")} |
741 | checkeq(t, {"a", "ax", "b", "bx", "c", "cx"}) | 755 | checkeq(t, {"a", "ax", "b", "bx", "c", "cx"}) |
742 | 756 | ||
@@ -944,6 +958,13 @@ p = m.Cg(m.C(1) * m.C(1), "k") * m.Ct(m.Cb("k")) | |||
944 | t = p:match("ab") | 958 | t = p:match("ab") |
945 | checkeq(t, {"a", "b"}) | 959 | checkeq(t, {"a", "b"}) |
946 | 960 | ||
961 | p = m.P(true) | ||
962 | for i = 1, 10 do p = p * m.Cg(1, i) end | ||
963 | for i = 1, 10 do | ||
964 | local p = p * m.Cb(i) | ||
965 | assert(p:match('abcdefghij') == string.sub('abcdefghij', i, i)) | ||
966 | end | ||
967 | |||
947 | 968 | ||
948 | t = {} | 969 | t = {} |
949 | function foo (p) t[#t + 1] = p; return p .. "x" end | 970 | function foo (p) t[#t + 1] = p; return p .. "x" end |
@@ -1420,7 +1441,8 @@ errmsg('aaaa', "rule 'aaaa'") | |||
1420 | errmsg('a', 'outside') | 1441 | errmsg('a', 'outside') |
1421 | errmsg('b <- a', 'undefined') | 1442 | errmsg('b <- a', 'undefined') |
1422 | errmsg("x <- 'a' x <- 'b'", 'already defined') | 1443 | errmsg("x <- 'a' x <- 'b'", 'already defined') |
1423 | -- errmsg("'a' -", "near '-'") | 1444 | -- error message is different because Matthew rewrote 're.lua' |
1445 | --errmsg("'a' -", "near '-'") | ||
1424 | 1446 | ||
1425 | 1447 | ||
1426 | print"OK" | 1448 | print"OK" |