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/expRec.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/expRec.lua')
-rw-r--r-- | examples/expRec.lua | 58 |
1 files changed, 20 insertions, 38 deletions
diff --git a/examples/expRec.lua b/examples/expRec.lua index 52bd40a..d111ab0 100644 --- a/examples/expRec.lua +++ b/examples/expRec.lua | |||
@@ -2,28 +2,17 @@ local m = require"lpeglabel" | |||
2 | local re = require"relabel" | 2 | local re = require"relabel" |
3 | 3 | ||
4 | local labels = { | 4 | local labels = { |
5 | {"ExpTermFirst", "expected an expression"}, | 5 | ExpTermFirst = "expected an expression", |
6 | {"ExpTermOp", "expected a term after the operator"}, | 6 | ExpTermOp = "expected a term after the operator", |
7 | {"MisClose", "missing a closing ')' after the expression"}, | 7 | MisClose = "missing a closing ')' after the expression", |
8 | } | 8 | } |
9 | 9 | ||
10 | local function labelindex(labname) | ||
11 | for i, elem in ipairs(labels) do | ||
12 | if elem[1] == labname then | ||
13 | return i | ||
14 | end | ||
15 | end | ||
16 | error("could not find label: " .. labname) | ||
17 | end | ||
18 | |||
19 | local errors, subject | 10 | local errors, subject |
20 | 11 | ||
21 | local function expect(patt, labname) | 12 | local function expect(patt, lab) |
22 | local i = labelindex(labname) | 13 | return patt + m.T(lab) |
23 | return patt + m.T(i) | ||
24 | end | 14 | end |
25 | 15 | ||
26 | |||
27 | local num = m.R("09")^1 / tonumber | 16 | local num = m.R("09")^1 / tonumber |
28 | local op = m.S("+-") | 17 | local op = m.S("+-") |
29 | 18 | ||
@@ -41,22 +30,13 @@ local function compute(tokens) | |||
41 | return result | 30 | return result |
42 | end | 31 | end |
43 | 32 | ||
44 | local g = m.P { | ||
45 | "Exp", | ||
46 | Exp = m.Ct(m.V"OperandFirst" * (m.C(op) * m.V"Operand")^0) / compute, | ||
47 | OperandFirst = expect(m.V"Term", "ExpTermFirst"), | ||
48 | Operand = expect(m.V"Term", "ExpTermOp"), | ||
49 | Term = num + m.V"Group", | ||
50 | Group = "(" * m.V"Exp" * expect(")", "MisClose"), | ||
51 | } | ||
52 | |||
53 | function recorderror(pos, lab) | 33 | function recorderror(pos, lab) |
54 | local line, col = re.calcline(subject, pos) | 34 | local line, col = re.calcline(subject, pos) |
55 | table.insert(errors, { line = line, col = col, msg = labels[lab][2] }) | 35 | table.insert(errors, { line = line, col = col, msg = labels[lab] }) |
56 | end | 36 | end |
57 | 37 | ||
58 | function record (labname) | 38 | function record (lab) |
59 | return (m.Cp() * m.Cc(labelindex(labname))) / recorderror | 39 | return (m.Cp() * m.Cc(lab)) / recorderror |
60 | end | 40 | end |
61 | 41 | ||
62 | function sync (p) | 42 | function sync (p) |
@@ -67,21 +47,23 @@ function defaultValue (p) | |||
67 | return p or m.Cc(1000) | 47 | return p or m.Cc(1000) |
68 | end | 48 | end |
69 | 49 | ||
70 | local grec = m.P { | 50 | local g = m.P { |
71 | "S", | 51 | "Exp", |
72 | S = m.Rec(m.V"A", m.V"ErrExpTermFirst", labelindex("ExpTermFirst")), -- default value is 0 | 52 | Exp = m.Ct(m.V"OperandFirst" * (m.C(op) * m.V"Operand")^0) / compute, |
73 | A = m.Rec(m.V"Sg", m.V"ErrExpTermOp", labelindex("ExpTermOp")), | 53 | OperandFirst = expect(m.V"Term", "ExpTermFirst"), |
74 | Sg = m.Rec(g, m.V"ErrMisClose", labelindex("MisClose")), | 54 | Operand = expect(m.V"Term", "ExpTermOp"), |
75 | ErrExpTermFirst = record("ExpTermFirst") * sync(op + ")") * defaultValue(), | 55 | Term = num + m.V"Group", |
76 | ErrExpTermOp = record("ExpTermOp") * sync(op + ")") * defaultValue(), | 56 | Group = "(" * m.V"Exp" * expect(")", "MisClose"), |
77 | ErrMisClose = record("MisClose") * sync(m.P")") * defaultValue(m.P""), | 57 | ExpTermFirst = record("ExpTermFirst") * sync(op + ")") * defaultValue(), |
58 | ExpTermOp = record("ExpTermOp") * sync(op + ")") * defaultValue(), | ||
59 | MisClose = record("MisClose") * sync(m.P")") * defaultValue(m.P""), | ||
78 | } | 60 | } |
79 | 61 | ||
80 | local function eval(input) | 62 | local function eval(input) |
81 | errors = {} | 63 | errors = {} |
82 | io.write("Input: ", input, "\n") | 64 | io.write("Input: ", input, "\n") |
83 | subject = input | 65 | subject = input |
84 | local result, label, suffix = grec:match(input) | 66 | local result, label, suffix = g:match(input) |
85 | io.write("Syntactic errors found: " .. #errors, "\n") | 67 | io.write("Syntactic errors found: " .. #errors, "\n") |
86 | if #errors > 0 then | 68 | if #errors > 0 then |
87 | local out = {} | 69 | local out = {} |