aboutsummaryrefslogtreecommitdiff
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
parent448762908fd822fbc101a4fe66fac9cd8aa913b5 (diff)
downloadlpeglabel-96284f8b4a6a25efd3c0c5ce9c7595604ba3143f.tar.gz
lpeglabel-96284f8b4a6a25efd3c0c5ce9c7595604ba3143f.tar.bz2
lpeglabel-96284f8b4a6a25efd3c0c5ce9c7595604ba3143f.zip
Updating examples using the recovery operator
-rw-r--r--examples/listId2Rec2Cap.lua79
-rw-r--r--examples/listIdRe1.lua8
-rw-r--r--examples/listIdRe2.lua50
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 @@
1local m = require'lpeglabelrec'
2local re = require'relabelrec'
3
4local terror = {}
5
6local function newError(s)
7 table.insert(terror, s)
8 return #terror
9end
10
11local errUndef = newError("undefined")
12local errId = newError("expecting an identifier")
13local errComma = newError("expecting ','")
14
15local id = m.R'az'^1
16
17local 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
26local subject, errors
27
28function recorderror(pos, lab)
29 local line, col = re.calcline(subject, pos)
30 table.insert(errors, { line = line, col = col, msg = terror[lab] })
31end
32
33function record (lab)
34 return (m.Cp() * m.Cc(lab)) / recorderror
35end
36
37function sync (p)
38 return (-p * m.P(1))^0
39end
40
41function defaultValue ()
42 return m.Cc"NONE"
43end
44
45local 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
53function 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
74end
75
76mymatch(grec, "one,two")
77mymatch(grec, "one two three")
78mymatch(grec, "1,\n two, \n3,")
79mymatch(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 @@
1local re = require 'relabel' 1local re = require 'relabelrec'
2 2
3local g = re.compile[[ 3local 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 @@
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,"))