aboutsummaryrefslogtreecommitdiff
path: root/test.lua
diff options
context:
space:
mode:
Diffstat (limited to 'test.lua')
-rwxr-xr-xtest.lua40
1 files changed, 31 insertions, 9 deletions
diff --git a/test.lua b/test.lua
index 86cda61..73297c6 100755
--- a/test.lua
+++ b/test.lua
@@ -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
16local loadstring = rawget(_G, "loadstring") or load 16local loadstring = rawget(_G, "loadstring") or load
17 17
18 18
19-- most tests here do not need much stack space
20m.setmaxstack(5)
21
22local any = m.P(1) 19local any = m.P(1)
23local space = m.S" \t\n"^0 20local space = m.S" \t\n"^0
24 21
@@ -291,6 +288,13 @@ assert(m.match(m.P"ab"^-1 - "c", "abcd") == 3)
291 288
292p = ('Aa' * ('Bb' * ('Cc' * m.P'Dd'^0)^0)^0)^-1 289p = ('Aa' * ('Bb' * ('Cc' * m.P'Dd'^0)^0)^0)^-1
293assert(p:match("AaBbCcDdBbCcDdDdDdBb") == 21) 290assert(p:match("AaBbCcDdBbCcDdDdDdBb") == 21)
291
292
293-- bug in 0.12.2
294-- p = { ('ab' ('c' 'ef'?)*)? }
295p = m.C(('ab' * ('c' * m.P'ef'^-1)^0)^-1)
296s = "abcefccefc"
297assert(s == p:match(s))
294 298
295 299
296pi = "3.14159 26535 89793 23846 26433 83279 50288 41971 69399 37510" 300pi = "3.14159 26535 89793 23846 26433 83279 50288 41971 69399 37510"
@@ -352,10 +356,16 @@ checkeq(t, {hi = 10, ho = 20})
352t = p:match'abc' 356t = p:match'abc'
353checkeq(t, {hi = 10, ho = 20, 'a', 'b', 'c'}) 357checkeq(t, {hi = 10, ho = 20, 'a', 'b', 'c'})
354 358
359-- non-string group names
360p = m.Ct(m.Cg(1, print) * m.Cg(1, 23.5) * m.Cg(1, io))
361t = p:match('abcdefghij')
362assert(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
357local function checkerr (msg, f, ...) 366local 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))
360end 370end
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.
595local lim = 10000 605local lim = 10000
596p = m.P{ '0' * m.V(1) + '0' } 606p = m.P{ '0' * m.V(1) + '0' }
597checkerr("too many pending", m.match, p, string.rep("0", lim)) 607checkerr("stack overflow", m.match, p, string.rep("0", lim))
598m.setmaxstack(2*lim) 608m.setmaxstack(2*lim)
599checkerr("too many pending", m.match, p, string.rep("0", lim)) 609checkerr("stack overflow", m.match, p, string.rep("0", lim))
600m.setmaxstack(2*lim + 4) 610m.setmaxstack(2*lim + 4)
601assert(m.match(p, string.rep("0", lim)) == lim + 1) 611assert(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' }
605m.setmaxstack(200) 615m.setmaxstack(200)
606assert(p:match(string.rep('a', 180) .. 'c' .. string.rep('b', 180)) == 362) 616assert(p:match(string.rep('a', 180) .. 'c' .. string.rep('b', 180)) == 362)
607 617
608m.setmaxstack(5) -- restore original limit 618m.setmaxstack(100) -- restore low limit
609 619
610-- tests for optional start position 620-- tests for optional start position
611assert(m.match("a", "abc", 1)) 621assert(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, "")}
737t1 = {1,1,nil,nil,4,nil,3,nil,nil} 747t1 = {1,1,nil,nil,4,nil,3,nil,nil}
738for i=1,10 do assert(t[i] == t1[i]) end 748for 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
752assert((m.P"aaa" * m.Cc(nil)):match"aaa" == nil)
753
740t = {m.match((m.C(1) / function (x) return x, x.."x" end)^0, "abc")} 754t = {m.match((m.C(1) / function (x) return x, x.."x" end)^0, "abc")}
741checkeq(t, {"a", "ax", "b", "bx", "c", "cx"}) 755checkeq(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"))
944t = p:match("ab") 958t = p:match("ab")
945checkeq(t, {"a", "b"}) 959checkeq(t, {"a", "b"})
946 960
961p = m.P(true)
962for i = 1, 10 do p = p * m.Cg(1, i) end
963for i = 1, 10 do
964 local p = p * m.Cb(i)
965 assert(p:match('abcdefghij') == string.sub('abcdefghij', i, i))
966end
967
947 968
948t = {} 969t = {}
949function foo (p) t[#t + 1] = p; return p .. "x" end 970function foo (p) t[#t + 1] = p; return p .. "x" end
@@ -1420,7 +1441,8 @@ errmsg('aaaa', "rule 'aaaa'")
1420errmsg('a', 'outside') 1441errmsg('a', 'outside')
1421errmsg('b <- a', 'undefined') 1442errmsg('b <- a', 'undefined')
1422errmsg("x <- 'a' x <- 'b'", 'already defined') 1443errmsg("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
1426print"OK" 1448print"OK"