aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorSergio Queiroz <sqmedeiros@gmail.com>2016-11-14 17:15:27 -0300
committerSergio Queiroz <sqmedeiros@gmail.com>2016-11-14 17:15:27 -0300
commit448762908fd822fbc101a4fe66fac9cd8aa913b5 (patch)
treec7bc865aa66f557be6d3d9d422bcaec42a8be0be /examples
parentfd28f9d9e54f33bf7ae3a5e12dc71478f9c91aea (diff)
downloadlpeglabel-448762908fd822fbc101a4fe66fac9cd8aa913b5.tar.gz
lpeglabel-448762908fd822fbc101a4fe66fac9cd8aa913b5.tar.bz2
lpeglabel-448762908fd822fbc101a4fe66fac9cd8aa913b5.zip
Changing documentation and examples with recovery
Diffstat (limited to 'examples')
-rw-r--r--examples/listId1.lua12
-rw-r--r--examples/listId2.lua13
-rw-r--r--examples/listId2Rec2.lua67
3 files changed, 82 insertions, 10 deletions
diff --git a/examples/listId1.lua b/examples/listId1.lua
index 8976f5f..dee46e9 100644
--- a/examples/listId1.lua
+++ b/examples/listId1.lua
@@ -1,12 +1,14 @@
1local m = require'lpeglabel' 1local m = require'lpeglabelrec'
2local re = require'relabel' 2local re = require'relabelrec'
3
4local id = m.R'az'^1
3 5
4local g = m.P{ 6local g = m.P{
5 "S", 7 "S",
6 S = m.V"Id" * m.V"List", 8 S = m.V"Id" * m.V"List",
7 List = -m.P(1) + (m.V"Comma" + m.T(2)) * (m.V"Id" + m.T(1)) * m.V"List", 9 List = -m.P(1) + m.V"Comma" * m.V"Id" * m.V"List",
8 Id = m.V"Sp" * m.R'az'^1, 10 Id = m.V"Sp" * id + m.T(1),
9 Comma = m.V"Sp" * ",", 11 Comma = m.V"Sp" * "," + m.T(2),
10 Sp = m.S" \n\t"^0, 12 Sp = m.S" \n\t"^0,
11} 13}
12 14
diff --git a/examples/listId2.lua b/examples/listId2.lua
index 509fda4..46f0063 100644
--- a/examples/listId2.lua
+++ b/examples/listId2.lua
@@ -1,5 +1,5 @@
1local m = require'lpeglabel' 1local m = require'lpeglabelrec'
2local re = require'relabel' 2local re = require'relabelrec'
3 3
4local terror = {} 4local terror = {}
5 5
@@ -12,15 +12,18 @@ local errUndef = newError("undefined")
12local errId = newError("expecting an identifier") 12local errId = newError("expecting an identifier")
13local errComma = newError("expecting ','") 13local errComma = newError("expecting ','")
14 14
15local id = m.R'az'^1
16
15local g = m.P{ 17local g = m.P{
16 "S", 18 "S",
17 S = m.V"Id" * m.V"List", 19 S = m.V"Id" * m.V"List",
18 List = -m.P(1) + (m.V"Comma" + m.T(errComma)) * (m.V"Id" + m.T(errId)) * m.V"List", 20 List = -m.P(1) + m.V"Comma" * m.V"Id" * m.V"List",
19 Id = m.V"Sp" * m.R'az'^1, 21 Id = m.V"Sp" * id + m.T(errId),
20 Comma = m.V"Sp" * ",", 22 Comma = m.V"Sp" * "," + m.T(errComma),
21 Sp = m.S" \n\t"^0, 23 Sp = m.S" \n\t"^0,
22} 24}
23 25
26
24function mymatch (g, s) 27function mymatch (g, s)
25 local r, e, sfail = g:match(s) 28 local r, e, sfail = g:match(s)
26 if not r then 29 if not r then
diff --git a/examples/listId2Rec2.lua b/examples/listId2Rec2.lua
new file mode 100644
index 0000000..a347a5b
--- /dev/null
+++ b/examples/listId2Rec2.lua
@@ -0,0 +1,67 @@
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" * 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
41local grec = m.P{
42 "S",
43 S = m.Rec(m.Rec(g, m.V"ErrComma", errComma), m.V"ErrId", errId),
44 ErrComma = record(errComma) * sync(-m.P(1) + id),
45 ErrId = record(errId) * sync(-m.P(1) + ",")
46}
47
48
49function mymatch (g, s)
50 errors = {}
51 subject = s
52 local r, e, sfail = g:match(s)
53 if #errors > 0 then
54 local out = {}
55 for i, err in ipairs(errors) do
56 local msg = "Error at line " .. err.line .. " (col " .. err.col .. "): " .. err.msg
57 table.insert(out, msg)
58 end
59 return nil, table.concat(out, "\n")
60 end
61 return r
62end
63
64print(mymatch(grec, "one,two"))
65print(mymatch(grec, "one two three"))
66print(mymatch(grec, "1,\n two, \n3,"))
67print(mymatch(grec, "one\n two123, \nthree,"))