diff options
author | Sérgio Queiroz <sqmedeiros@gmail.com> | 2017-12-28 14:29:49 -0300 |
---|---|---|
committer | Sérgio Queiroz <sqmedeiros@gmail.com> | 2017-12-28 14:29:49 -0300 |
commit | 59da25ff241a83d8139e41199ef7a23f6e17fa65 (patch) | |
tree | a9ad13fa0f8ddb50855b716b349e4ded5d5fac8d /examples/listId2Rec2.lua | |
parent | 772df00e061db3cd7d0af92c8ab65bc023d8121d (diff) | |
download | lpeglabel-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.lua | 52 |
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 @@ | |||
1 | local m = require'lpeglabel' | 1 | local m = require'lpeglabel' |
2 | local re = require'relabel' | 2 | local re = require'relabel' |
3 | 3 | ||
4 | local terror = {} | 4 | local terror = { |
5 | 5 | ErrId = "expecting an identifier", | |
6 | local function newError(s) | 6 | ErrComma = "expecting ','" |
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" * id + m.T(errId), | ||
22 | Comma = m.V"Sp" * "," + m.T(errComma), | ||
23 | Sp = m.S" \n\t"^0, | ||
24 | } | 7 | } |
25 | 8 | ||
26 | local subject, errors | 9 | local subject, errors |
27 | 10 | ||
28 | function recorderror(pos, lab) | 11 | local 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] }) |
31 | end | 14 | end |
32 | 15 | ||
33 | function record (lab) | 16 | local function record (lab) |
34 | return (m.Cp() * m.Cc(lab)) / recorderror | 17 | return (m.Cp() * m.Cc(lab)) / recorderror |
35 | end | 18 | end |
36 | 19 | ||
37 | function sync (p) | 20 | local function sync (p) |
38 | return (-p * m.P(1))^0 | 21 | return (-p * m.P(1))^0 |
39 | end | 22 | end |
40 | 23 | ||
41 | local grec = m.P{ | 24 | local id = m.R'az'^1 |
25 | |||
26 | local 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 | |||
49 | function mymatch (g, s) | 37 | function 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 |
62 | end | 50 | end |
63 | 51 | ||
64 | print(mymatch(grec, "one,two")) | 52 | print(mymatch(g, "one,two")) |
65 | print(mymatch(grec, "one two three")) | 53 | print(mymatch(g, "one two three")) |
66 | print(mymatch(grec, "1,\n two, \n3,")) | 54 | print(mymatch(g, "1,\n two, \n3,")) |
67 | print(mymatch(grec, "one\n two123, \nthree,")) | 55 | print(mymatch(g, "one\n two123, \nthree,")) |