aboutsummaryrefslogtreecommitdiff
path: root/examples/expRecAut.lua
diff options
context:
space:
mode:
Diffstat (limited to 'examples/expRecAut.lua')
-rw-r--r--examples/expRecAut.lua37
1 files changed, 19 insertions, 18 deletions
diff --git a/examples/expRecAut.lua b/examples/expRecAut.lua
index b8280a7..18d7606 100644
--- a/examples/expRecAut.lua
+++ b/examples/expRecAut.lua
@@ -5,13 +5,11 @@ local num = m.R("09")^1 / tonumber
5local op = m.S("+-") 5local op = m.S("+-")
6 6
7local labels = {} 7local labels = {}
8local nlabels = 0
9 8
10local function newError(lab, msg, psync, pcap) 9local function newError(lab, msg, psync, pcap)
11 nlabels = nlabels + 1
12 psync = psync or m.P(-1) 10 psync = psync or m.P(-1)
13 pcap = pcap or m.P"" 11 pcap = pcap or m.P""
14 labels[lab] = { id = nlabels, msg = msg, psync = psync, pcap = pcap } 12 labels[lab] = { msg = msg, psync = psync, pcap = pcap }
15end 13end
16 14
17newError("ExpTermFirst", "expected an expression", op + ")", m.Cc(1000)) 15newError("ExpTermFirst", "expected an expression", op + ")", m.Cc(1000))
@@ -21,9 +19,8 @@ newError("Extra", "extra characters found after the expression")
21 19
22local errors, subject 20local errors, subject
23 21
24local function expect(patt, labname) 22local function expect(patt, lab)
25 local i = labels[labname].id 23 return patt + m.T(lab)
26 return patt + m.T(i)
27end 24end
28 25
29local function compute(tokens) 26local function compute(tokens)
@@ -40,15 +37,6 @@ local function compute(tokens)
40 return result 37 return result
41end 38end
42 39
43local g = m.P {
44 "Exp",
45 Exp = m.Ct(m.V"OperandFirst" * (m.C(op) * m.V"Operand")^0) / compute,
46 OperandFirst = expect(m.V"Term", "ExpTermFirst"),
47 Operand = expect(m.V"Term", "ExpTermOp"),
48 Term = num + m.V"Group",
49 Group = "(" * m.V"Exp" * expect(")", "MisClose"),
50}
51
52function recorderror(pos, lab) 40function recorderror(pos, lab)
53 local line, col = re.calcline(subject, pos) 41 local line, col = re.calcline(subject, pos)
54 table.insert(errors, { line = line, col = col, msg = labels[lab].msg }) 42 table.insert(errors, { line = line, col = col, msg = labels[lab].msg })
@@ -66,16 +54,29 @@ function defaultValue (p)
66 return p or m.Cc(1000) 54 return p or m.Cc(1000)
67end 55end
68 56
69local grec = g * expect(m.P(-1), "Extra") 57local g = {
58 "Exp",
59 Exp = m.Ct(m.V"OperandFirst" * (m.C(op) * m.V"Operand")^0) / compute,
60 OperandFirst = expect(m.V"Term", "ExpTermFirst"),
61 Operand = expect(m.V"Term", "ExpTermOp"),
62 Term = num + m.V"Group",
63 Group = "(" * m.V"Exp" * expect(")", "MisClose"),
64}
65
66-- set first rule
67g[1] = "S"
68g["S"] = g["Exp"] * expect(m.P(-1), "Extra")
70for k, v in pairs(labels) do 69for k, v in pairs(labels) do
71 grec = m.Rec(grec, record(k) * sync(v.psync) * v.pcap, v.id) 70 g[k] = record(k) * sync(v.psync) * v.pcap
72end 71end
73 72
73g = m.P(g)
74
74local function eval(input) 75local function eval(input)
75 errors = {} 76 errors = {}
76 io.write("Input: ", input, "\n") 77 io.write("Input: ", input, "\n")
77 subject = input 78 subject = input
78 local result, label, suffix = grec:match(input) 79 local result, label, suffix = g:match(input)
79 io.write("Syntactic errors found: " .. #errors, "\n") 80 io.write("Syntactic errors found: " .. #errors, "\n")
80 if #errors > 0 then 81 if #errors > 0 then
81 local out = {} 82 local out = {}