aboutsummaryrefslogtreecommitdiff
path: root/examples/listIdRe2.lua
diff options
context:
space:
mode:
authorSergio Queiroz <sqmedeiros@gmail.com>2016-11-17 15:55:24 -0300
committerSergio Queiroz <sqmedeiros@gmail.com>2016-11-17 15:55:24 -0300
commit96284f8b4a6a25efd3c0c5ce9c7595604ba3143f (patch)
tree30d319f4a81d9a421b7b842be2889ef6e8e7f455 /examples/listIdRe2.lua
parent448762908fd822fbc101a4fe66fac9cd8aa913b5 (diff)
downloadlpeglabel-96284f8b4a6a25efd3c0c5ce9c7595604ba3143f.tar.gz
lpeglabel-96284f8b4a6a25efd3c0c5ce9c7595604ba3143f.tar.bz2
lpeglabel-96284f8b4a6a25efd3c0c5ce9c7595604ba3143f.zip
Updating examples using the recovery operator
Diffstat (limited to 'examples/listIdRe2.lua')
-rw-r--r--examples/listIdRe2.lua50
1 files changed, 36 insertions, 14 deletions
diff --git a/examples/listIdRe2.lua b/examples/listIdRe2.lua
index fe30535..070bcdb 100644
--- a/examples/listIdRe2.lua
+++ b/examples/listIdRe2.lua
@@ -1,4 +1,4 @@
1local re = require 'relabel' 1local re = require 'relabelrec'
2 2
3local errinfo = { 3local errinfo = {
4 {"errUndef", "undefined"}, 4 {"errUndef", "undefined"},
@@ -18,23 +18,45 @@ re.setlabels(labels)
18 18
19local g = re.compile[[ 19local g = re.compile[[
20 S <- Id List 20 S <- Id List
21 List <- !. / (',' / %{errComma}) (Id / %{errId}) List 21 List <- !. / Comma Id List
22 Id <- Sp [a-z]+ 22 Id <- Sp [a-z]+ / %{errId}
23 Comma <- Sp ',' 23 Comma <- Sp ',' / %{errComma}
24 Sp <- %s* 24 Sp <- %s*
25]] 25]]
26 26
27local errors
28
29function recorderror (subject, pos, label)
30 local line, col = re.calcline(subject, pos)
31 table.insert(errors, { line = line, col = col, msg = errmsgs[labels[label]] })
32 return true
33end
34
35function sync (p)
36 return '( !(' .. p .. ') .)*'
37end
38
39local grec = re.compile(
40 "S <- %g //{errComma} ErrComma //{errId} ErrId" .. "\n" ..
41 "ErrComma <- ('' -> 'errComma' => recorderror) " .. sync('!. / [a-z]+') .. "\n" ..
42 "ErrId <- ('' -> 'errId' => recorderror) (!(!. / ',') .)*"
43 , {g = g, recorderror = recorderror})
44
27function mymatch (g, s) 45function mymatch (g, s)
28 local r, e, sfail = g:match(s) 46 errors = {}
29 if not r then 47 local r, e, sfail = g:match(s)
30 local line, col = re.calcline(s, #s - #sfail) 48 if #errors > 0 then
31 local msg = "Error at line " .. line .. " (col " .. col .. "): " 49 local out = {}
32 return r, msg .. errmsgs[e] .. " before '" .. sfail .. "'" 50 for i, err in ipairs(errors) do
51 local msg = "Error at line " .. err.line .. " (col " .. err.col .. "): " .. err.msg
52 table.insert(out, msg)
53 end
54 return nil, table.concat(out, "\n")
33 end 55 end
34 return r 56 return r
35end 57end
36 58
37print(mymatch(g, "one,two")) 59print(mymatch(grec, "one,two"))
38print(mymatch(g, "one two")) 60print(mymatch(grec, "one two three"))
39print(mymatch(g, "one,\n two,\nthree,")) 61print(mymatch(grec, "1,\n two, \n3,"))
40 62print(mymatch(grec, "one\n two123, \nthree,"))