aboutsummaryrefslogtreecommitdiff
path: root/examples/listId2Rec2.lua
diff options
context:
space:
mode:
authorSérgio Queiroz <sqmedeiros@gmail.com>2017-12-28 14:29:49 -0300
committerSérgio Queiroz <sqmedeiros@gmail.com>2017-12-28 14:29:49 -0300
commit59da25ff241a83d8139e41199ef7a23f6e17fa65 (patch)
treea9ad13fa0f8ddb50855b716b349e4ded5d5fac8d /examples/listId2Rec2.lua
parent772df00e061db3cd7d0af92c8ab65bc023d8121d (diff)
downloadlpeglabel-59da25ff241a83d8139e41199ef7a23f6e17fa65.tar.gz
lpeglabel-59da25ff241a83d8139e41199ef7a23f6e17fa65.tar.bz2
lpeglabel-59da25ff241a83d8139e41199ef7a23f6e17fa65.zip
Updating examples to the new semantics
Diffstat (limited to 'examples/listId2Rec2.lua')
-rw-r--r--examples/listId2Rec2.lua52
1 files changed, 20 insertions, 32 deletions
diff --git a/examples/listId2Rec2.lua b/examples/listId2Rec2.lua
index 3506095..09db5a6 100644
--- a/examples/listId2Rec2.lua
+++ b/examples/listId2Rec2.lua
@@ -1,51 +1,39 @@
1local m = require'lpeglabel' 1local m = require'lpeglabel'
2local re = require'relabel' 2local re = require'relabel'
3 3
4local terror = {} 4local terror = {
5 5 ErrId = "expecting an identifier",
6local function newError(s) 6 ErrComma = "expecting ','"
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} 7}
25 8
26local subject, errors 9local subject, errors
27 10
28function recorderror(pos, lab) 11local function recorderror(pos, lab)
29 local line, col = re.calcline(subject, pos) 12 local line, col = re.calcline(subject, pos)
30 table.insert(errors, { line = line, col = col, msg = terror[lab] }) 13 table.insert(errors, { line = line, col = col, msg = terror[lab] })
31end 14end
32 15
33function record (lab) 16local function record (lab)
34 return (m.Cp() * m.Cc(lab)) / recorderror 17 return (m.Cp() * m.Cc(lab)) / recorderror
35end 18end
36 19
37function sync (p) 20local function sync (p)
38 return (-p * m.P(1))^0 21 return (-p * m.P(1))^0
39end 22end
40 23
41local grec = m.P{ 24local id = m.R'az'^1
25
26local g = m.P{
42 "S", 27 "S",
43 S = m.Rec(m.Rec(g, m.V"ErrComma", errComma), m.V"ErrId", errId), 28 S = m.V"Id" * m.V"List",
44 ErrComma = record(errComma) * sync(id), 29 List = -m.P(1) + m.V"Comma" * m.V"Id" * m.V"List",
45 ErrId = record(errId) * sync(m.P",") 30 Id = m.V"Sp" * id + m.T'ErrId',
31 Comma = m.V"Sp" * "," + m.T'ErrComma',
32 Sp = m.S" \n\t"^0,
33 ErrId = record('ErrId') * sync(m.P","),
34 ErrComma = record('ErrComma') * sync(id),
46} 35}
47 36
48
49function mymatch (g, s) 37function mymatch (g, s)
50 errors = {} 38 errors = {}
51 subject = s 39 subject = s
@@ -61,7 +49,7 @@ function mymatch (g, s)
61 return r 49 return r
62end 50end
63 51
64print(mymatch(grec, "one,two")) 52print(mymatch(g, "one,two"))
65print(mymatch(grec, "one two three")) 53print(mymatch(g, "one two three"))
66print(mymatch(grec, "1,\n two, \n3,")) 54print(mymatch(g, "1,\n two, \n3,"))
67print(mymatch(grec, "one\n two123, \nthree,")) 55print(mymatch(g, "one\n two123, \nthree,"))