aboutsummaryrefslogtreecommitdiff
path: root/examples/recoveryOpFail.lua
diff options
context:
space:
mode:
Diffstat (limited to 'examples/recoveryOpFail.lua')
-rw-r--r--examples/recoveryOpFail.lua19
1 files changed, 8 insertions, 11 deletions
diff --git a/examples/recoveryOpFail.lua b/examples/recoveryOpFail.lua
index d65b9e0..6ddc6a2 100644
--- a/examples/recoveryOpFail.lua
+++ b/examples/recoveryOpFail.lua
@@ -1,7 +1,7 @@
1local lpeg = require"lpeglabel" 1local lpeg = require"lpeglabel"
2 2
3local R, S, P, V = lpeg.R, lpeg.S, lpeg.P, lpeg.V 3local R, S, P, V = lpeg.R, lpeg.S, lpeg.P, lpeg.V
4local C, Cc, Ct, Cmt = lpeg.C, lpeg.Cc, lpeg.Ct, lpeg.Cmt 4local C, Cc, Ct, Cmt, Carg = lpeg.C, lpeg.Cc, lpeg.Ct, lpeg.Cmt, lpeg.Carg
5local T, Lc, Rec = lpeg.T, lpeg.Lc, lpeg.Rec 5local T, Lc, Rec = lpeg.T, lpeg.Lc, lpeg.Rec
6 6
7local labels = { 7local labels = {
@@ -21,16 +21,14 @@ local function labelindex(labname)
21 error("could not find label: " .. labname) 21 error("could not find label: " .. labname)
22end 22end
23 23
24local errors = {}
25
26local function expect(patt, labname, recpatt) 24local function expect(patt, labname, recpatt)
27 local i = labelindex(labname) 25 local i = labelindex(labname)
28 function recorderror(input, pos) 26 local function recorderror(input, pos, errors)
29 table.insert(errors, {i, pos}) 27 table.insert(errors, {i, pos})
30 return true 28 return true
31 end 29 end
32 if not recpatt then recpatt = P"" end 30 if not recpatt then recpatt = P"" end
33 return Rec(patt, Cmt("", recorderror) * recpatt) 31 return Rec(patt, Cmt(Carg(1), recorderror) * recpatt)
34end 32end
35 33
36local num = R("09")^1 / tonumber 34local num = R("09")^1 / tonumber
@@ -57,18 +55,18 @@ end
57 55
58local g = P { 56local g = P {
59 "Exp", 57 "Exp",
60 Exp = Ct(V"Term" * (C(op) * V"OpRecov")^0) / compute; 58 Exp = Ct(V"Term" * (C(op) * V"Operand")^0) / compute;
61 OpRecov = V"Operand";
62 Operand = expect(V"Term", "ExpTerm", Cc(0)); 59 Operand = expect(V"Term", "ExpTerm", Cc(0));
63 Term = num + V"Group"; 60 Term = num + V"Group";
64 Group = "(" * V"InnerExp" * expect(")", "MisClose", ""); 61 Group = "(" * V"InnerExp" * expect(")", "MisClose");
65 InnerExp = expect(V"Exp", "ExpExp", (P(1) - ")")^0 * Cc(0)); 62 InnerExp = expect(V"Exp", "ExpExp", (P(1) - ")")^0 * Cc(0));
66} 63}
67 64
68g = expect(g, "NoExp", P(1)^0) * expect(-P(1), "Extra") 65g = expect(g, "NoExp", P(1)^0) * expect(-P(1), "Extra")
69 66
70local function eval(input) 67local function eval(input)
71 local result, label, suffix = g:match(input) 68 local errors = {}
69 local result, label, suffix = g:match(input, 1, errors)
72 if #errors == 0 then 70 if #errors == 0 then
73 return result 71 return result
74 else 72 else
@@ -78,7 +76,6 @@ local function eval(input)
78 local msg = labels[err[1]][2] 76 local msg = labels[err[1]][2]
79 table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")") 77 table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")")
80 end 78 end
81 errors = {}
82 return nil, table.concat(out, "\n") 79 return nil, table.concat(out, "\n")
83 end 80 end
84end 81end