aboutsummaryrefslogtreecommitdiff
path: root/examples/expRec.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/expRec.lua
parent772df00e061db3cd7d0af92c8ab65bc023d8121d (diff)
downloadlpeglabel-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.lua58
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"
2local re = require"relabel" 2local re = require"relabel"
3 3
4local labels = { 4local 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
10local 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)
17end
18
19local errors, subject 10local errors, subject
20 11
21local function expect(patt, labname) 12local function expect(patt, lab)
22 local i = labelindex(labname) 13 return patt + m.T(lab)
23 return patt + m.T(i)
24end 14end
25 15
26
27local num = m.R("09")^1 / tonumber 16local num = m.R("09")^1 / tonumber
28local op = m.S("+-") 17local op = m.S("+-")
29 18
@@ -41,22 +30,13 @@ local function compute(tokens)
41 return result 30 return result
42end 31end
43 32
44local 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
53function recorderror(pos, lab) 33function 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] })
56end 36end
57 37
58function record (labname) 38function record (lab)
59 return (m.Cp() * m.Cc(labelindex(labname))) / recorderror 39 return (m.Cp() * m.Cc(lab)) / recorderror
60end 40end
61 41
62function sync (p) 42function sync (p)
@@ -67,21 +47,23 @@ function defaultValue (p)
67 return p or m.Cc(1000) 47 return p or m.Cc(1000)
68end 48end
69 49
70local grec = m.P { 50local 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
80local function eval(input) 62local 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 = {}