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 | |
parent | 448762908fd822fbc101a4fe66fac9cd8aa913b5 (diff) | |
download | lpeglabel-96284f8b4a6a25efd3c0c5ce9c7595604ba3143f.tar.gz lpeglabel-96284f8b4a6a25efd3c0c5ce9c7595604ba3143f.tar.bz2 lpeglabel-96284f8b4a6a25efd3c0c5ce9c7595604ba3143f.zip |
Updating examples using the recovery operator
-rw-r--r-- | examples/listId2Rec2Cap.lua | 79 | ||||
-rw-r--r-- | examples/listIdRe1.lua | 8 | ||||
-rw-r--r-- | examples/listIdRe2.lua | 50 |
3 files changed, 119 insertions, 18 deletions
diff --git a/examples/listId2Rec2Cap.lua b/examples/listId2Rec2Cap.lua new file mode 100644 index 0000000..f9fc2bd --- /dev/null +++ b/examples/listId2Rec2Cap.lua | |||
@@ -0,0 +1,79 @@ | |||
1 | local m = require'lpeglabelrec' | ||
2 | local re = require'relabelrec' | ||
3 | |||
4 | local terror = {} | ||
5 | |||
6 | local function newError(s) | ||
7 | table.insert(terror, s) | ||
8 | return #terror | ||
9 | end | ||
10 | |||
11 | local errUndef = newError("undefined") | ||
12 | local errId = newError("expecting an identifier") | ||
13 | local errComma = newError("expecting ','") | ||
14 | |||
15 | local id = m.R'az'^1 | ||
16 | |||
17 | local g = m.P{ | ||
18 | "S", | ||
19 | S = m.V"Id" * m.V"List", | ||
20 | List = -m.P(1) + m.V"Comma" * m.V"Id" * m.V"List", | ||
21 | Id = m.V"Sp" * m.C(id) + m.T(errId), | ||
22 | Comma = m.V"Sp" * "," + m.T(errComma), | ||
23 | Sp = m.S" \n\t"^0, | ||
24 | } | ||
25 | |||
26 | local subject, errors | ||
27 | |||
28 | function recorderror(pos, lab) | ||
29 | local line, col = re.calcline(subject, pos) | ||
30 | table.insert(errors, { line = line, col = col, msg = terror[lab] }) | ||
31 | end | ||
32 | |||
33 | function record (lab) | ||
34 | return (m.Cp() * m.Cc(lab)) / recorderror | ||
35 | end | ||
36 | |||
37 | function sync (p) | ||
38 | return (-p * m.P(1))^0 | ||
39 | end | ||
40 | |||
41 | function defaultValue () | ||
42 | return m.Cc"NONE" | ||
43 | end | ||
44 | |||
45 | local grec = m.P{ | ||
46 | "S", | ||
47 | S = m.Rec(m.Rec(g, m.V"ErrComma", errComma), m.V"ErrId", errId), | ||
48 | ErrComma = record(errComma) * sync(-m.P(1) + id), | ||
49 | ErrId = record(errId) * sync(-m.P(1) + ",") * defaultValue() | ||
50 | } | ||
51 | |||
52 | |||
53 | function mymatch (g, s) | ||
54 | errors = {} | ||
55 | subject = s | ||
56 | io.write("Input: ", s, "\n") | ||
57 | local r = { g:match(s) } | ||
58 | io.write("Captures (separated by ';'): ") | ||
59 | for k, v in pairs(r) do | ||
60 | io.write(v .. "; ") | ||
61 | end | ||
62 | io.write("\nSyntactic errors found: " .. #errors) | ||
63 | if #errors > 0 then | ||
64 | io.write("\n") | ||
65 | local out = {} | ||
66 | for i, err in ipairs(errors) do | ||
67 | local msg = "Error at line " .. err.line .. " (col " .. err.col .. "): " .. err.msg | ||
68 | table.insert(out, msg) | ||
69 | end | ||
70 | io.write(table.concat(out, "\n")) | ||
71 | end | ||
72 | print("\n") | ||
73 | return r | ||
74 | end | ||
75 | |||
76 | mymatch(grec, "one,two") | ||
77 | mymatch(grec, "one two three") | ||
78 | mymatch(grec, "1,\n two, \n3,") | ||
79 | mymatch(grec, "one\n two123, \nthree,") | ||
diff --git a/examples/listIdRe1.lua b/examples/listIdRe1.lua index d092566..3988a3b 100644 --- a/examples/listIdRe1.lua +++ b/examples/listIdRe1.lua | |||
@@ -1,10 +1,10 @@ | |||
1 | local re = require 'relabel' | 1 | local re = require 'relabelrec' |
2 | 2 | ||
3 | local g = re.compile[[ | 3 | local g = re.compile[[ |
4 | S <- Id List | 4 | S <- Id List |
5 | List <- !. / (',' / %{2}) (Id / %{1}) List | 5 | List <- !. / Comma Id List |
6 | Id <- Sp [a-z]+ | 6 | Id <- Sp [a-z]+ / %{2} |
7 | Comma <- Sp ',' | 7 | Comma <- Sp ',' / %{3} |
8 | Sp <- %s* | 8 | Sp <- %s* |
9 | ]] | 9 | ]] |
10 | 10 | ||
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,")) | |