aboutsummaryrefslogtreecommitdiff
path: root/Lua/test.lua
blob: 792a534112febf467e7c088850bfe1dd17763288 (plain)
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
local bp = require 'bp'

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 in bp.matches("(*`a-z) => '(@0)'", "one two  three") do
    print(repr(m))
end

print(("Replacing: %q (%d replacements)"):format(bp.replace("+`a-z", "(@0)", "one two  three")))


print("Captures:")
local m = bp.match("@first=+`a-z _ @second=(+`a-z => 'XX@0XX') _ @+`a-z _ @last=+`a-z", "one two three four")
print(repr(m))

local m = bp.match("@dup=+`a-z _ @dup=+`a-z _ @two=(@a=+`a-z _ @b=+`a-z)", "one two three four")
print(repr(m))


print("Testing parse errors:")
local ok, msg = pcall(function()
    bp.match(".;//;;; wtf", "xxx")
end)
if not ok then print(("\x1B[41;30mParse error:\x1B[0;1;31m %s\x1B[m\n"):format(msg)) end

print("Testing builtins:")
print(bp.match("parens", "...(foo())..."))


print("Testing pat objects")
local pat = bp.compile("+`a-z")
print(pat)
print(pat:match("...foo..."))
print(pat:match("...baz..."))
print(pat:replace("{@0}", "...baz..."))

for m in pat:matches("hello world") do
    print(m)
end