diff options
author | Sergio Queiroz <sqmedeiros@gmail.com> | 2016-11-17 15:55:24 -0300 |
---|---|---|
committer | Sergio Queiroz <sqmedeiros@gmail.com> | 2016-11-17 15:55:24 -0300 |
commit | 96284f8b4a6a25efd3c0c5ce9c7595604ba3143f (patch) | |
tree | 30d319f4a81d9a421b7b842be2889ef6e8e7f455 /examples/listIdRe2.lua | |
parent | 448762908fd822fbc101a4fe66fac9cd8aa913b5 (diff) | |
download | lpeglabel-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.lua | 50 |
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 @@ | |||
1 | local re = require 'relabel' | 1 | local re = require 'relabelrec' |
2 | 2 | ||
3 | local errinfo = { | 3 | local errinfo = { |
4 | {"errUndef", "undefined"}, | 4 | {"errUndef", "undefined"}, |
@@ -18,23 +18,45 @@ re.setlabels(labels) | |||
18 | 18 | ||
19 | local g = re.compile[[ | 19 | local 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 | ||
27 | local errors | ||
28 | |||
29 | function 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 | ||
33 | end | ||
34 | |||
35 | function sync (p) | ||
36 | return '( !(' .. p .. ') .)*' | ||
37 | end | ||
38 | |||
39 | local 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 | |||
27 | function mymatch (g, s) | 45 | function 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 |
35 | end | 57 | end |
36 | 58 | ||
37 | print(mymatch(g, "one,two")) | 59 | print(mymatch(grec, "one,two")) |
38 | print(mymatch(g, "one two")) | 60 | print(mymatch(grec, "one two three")) |
39 | print(mymatch(g, "one,\n two,\nthree,")) | 61 | print(mymatch(grec, "1,\n two, \n3,")) |
40 | 62 | print(mymatch(grec, "one\n two123, \nthree,")) | |