aboutsummaryrefslogtreecommitdiff
path: root/examples/listIdRe2.lua
diff options
context:
space:
mode:
Diffstat (limited to 'examples/listIdRe2.lua')
-rw-r--r--examples/listIdRe2.lua31
1 files changed, 20 insertions, 11 deletions
diff --git a/examples/listIdRe2.lua b/examples/listIdRe2.lua
index 070bcdb..6bab6ba 100644
--- a/examples/listIdRe2.lua
+++ b/examples/listIdRe2.lua
@@ -19,7 +19,7 @@ re.setlabels(labels)
19local g = re.compile[[ 19local g = re.compile[[
20 S <- Id List 20 S <- Id List
21 List <- !. / Comma Id List 21 List <- !. / Comma Id List
22 Id <- Sp [a-z]+ / %{errId} 22 Id <- Sp {[a-z]+} / %{errId}
23 Comma <- Sp ',' / %{errComma} 23 Comma <- Sp ',' / %{errComma}
24 Sp <- %s* 24 Sp <- %s*
25]] 25]]
@@ -38,25 +38,34 @@ end
38 38
39local grec = re.compile( 39local grec = re.compile(
40 "S <- %g //{errComma} ErrComma //{errId} ErrId" .. "\n" .. 40 "S <- %g //{errComma} ErrComma //{errId} ErrId" .. "\n" ..
41 "ErrComma <- ('' -> 'errComma' => recorderror) " .. sync('!. / [a-z]+') .. "\n" .. 41 "ErrComma <- ('' -> 'errComma' => recorderror) " .. sync('[a-z]+') .. "\n" ..
42 "ErrId <- ('' -> 'errId' => recorderror) (!(!. / ',') .)*" 42 "ErrId <- ('' -> 'errId' => recorderror) " .. sync('","') .. "-> default"
43 , {g = g, recorderror = recorderror}) 43 , {g = g, recorderror = recorderror, default = "NONE"})
44 44
45function mymatch (g, s) 45function mymatch (g, s)
46 errors = {} 46 errors = {}
47 local r, e, sfail = g:match(s) 47 subject = s
48 io.write("Input: ", s, "\n")
49 local r = { g:match(s) }
50 io.write("Captures (separated by ';'): ")
51 for k, v in pairs(r) do
52 io.write(v .. "; ")
53 end
54 io.write("\nSyntactic errors found: " .. #errors)
48 if #errors > 0 then 55 if #errors > 0 then
56 io.write("\n")
49 local out = {} 57 local out = {}
50 for i, err in ipairs(errors) do 58 for i, err in ipairs(errors) do
51 local msg = "Error at line " .. err.line .. " (col " .. err.col .. "): " .. err.msg 59 local msg = "Error at line " .. err.line .. " (col " .. err.col .. "): " .. err.msg
52 table.insert(out, msg) 60 table.insert(out, msg)
53 end 61 end
54 return nil, table.concat(out, "\n") 62 io.write(table.concat(out, "\n"))
55 end 63 end
56 return r 64 print("\n")
65 return r
57end 66end
58 67
59print(mymatch(grec, "one,two")) 68mymatch(grec, "one,two")
60print(mymatch(grec, "one two three")) 69mymatch(grec, "one two three")
61print(mymatch(grec, "1,\n two, \n3,")) 70mymatch(grec, "1,\n two, \n3,")
62print(mymatch(grec, "one\n two123, \nthree,")) 71mymatch(grec, "one\n two123, \nthree,")