From cfc53ea643156a9993d37e85b0be6611a614704a Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 24 Sep 2021 22:55:39 -0700 Subject: Overhaul of lua API to use tables --- Lua/test.lua | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'Lua/test.lua') diff --git a/Lua/test.lua b/Lua/test.lua index df7a63d..c2fbb3c 100644 --- a/Lua/test.lua +++ b/Lua/test.lua @@ -10,24 +10,32 @@ bp.each = function(s, pat, index) return iter, {s, pat, index}, index end +local function repr(obj) + if type(obj) == 'table' then + local ret = {} + for k,v in pairs(obj) do table.insert(ret, ("%s=%s"):format(k, repr(v))) end + return ("{%s}"):format(table.concat(ret,",")) + elseif type(obj) == 'string' then + return string.format("%q", obj) + else + return tostring(obj) + end +end + print("Matching:") for m, i,j in bp.each("one two three", "(*`a-z) => '(@0)'") do - print(("%q @%d len=%d"):format(tostring(m),i,j)) + print(("%s @%d len=%d"):format(repr(m),i,j)) end print(("Replacing: %q (%d replacements)"):format(bp.replace("one two three", "+`a-z", "(@0)"))) print("Captures:") -local m = bp.match("one two three four", "_:` ;@first=+`a-z _ @second=(+`a-z => 'XX@0XX') _ @+`a-z _ @last=+`a-z") -local function quote(x) return ("%q"):format(tostring(x)) end -print("0", quote(m[0])) -print("first", quote(m.first)) -print("second", m.second) -print("1", m[1]) -print("last", m.last) - -print("Len:", #m, #tostring(m)) +local m = bp.match("one two three four", "@first=+`a-z _ @second=(+`a-z => 'XX@0XX') _ @+`a-z _ @last=+`a-z") +print(repr(m)) + +local m = bp.match("one two three four", "@dup=+`a-z _ @dup=+`a-z _ @two=(@a=+`a-z _ @b=+`a-z)") +print(repr(m)) print("Testing parse errors:") -- cgit v1.2.3