aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/recoveryOpFail.lua108
-rw-r--r--examples/recoveryOpLab.lua107
2 files changed, 215 insertions, 0 deletions
diff --git a/examples/recoveryOpFail.lua b/examples/recoveryOpFail.lua
new file mode 100644
index 0000000..d65b9e0
--- /dev/null
+++ b/examples/recoveryOpFail.lua
@@ -0,0 +1,108 @@
1local lpeg = require"lpeglabel"
2
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
5local T, Lc, Rec = lpeg.T, lpeg.Lc, lpeg.Rec
6
7local labels = {
8 {"NoExp", "no expression found"},
9 {"Extra", "extra characters found after the expression"},
10 {"ExpTerm", "expected a term after the operator"},
11 {"ExpExp", "expected an expression after the parenthesis"},
12 {"MisClose", "missing a closing ')' after the expression"},
13}
14
15local function labelindex(labname)
16 for i, elem in ipairs(labels) do
17 if elem[1] == labname then
18 return i
19 end
20 end
21 error("could not find label: " .. labname)
22end
23
24local errors = {}
25
26local function expect(patt, labname, recpatt)
27 local i = labelindex(labname)
28 function recorderror(input, pos)
29 table.insert(errors, {i, pos})
30 return true
31 end
32 if not recpatt then recpatt = P"" end
33 return Rec(patt, Cmt("", recorderror) * recpatt)
34end
35
36local num = R("09")^1 / tonumber
37local op = S("+-*/")
38
39local function compute(tokens)
40 local result = tokens[1]
41 for i = 2, #tokens, 2 do
42 if tokens[i] == '+' then
43 result = result + tokens[i+1]
44 elseif tokens[i] == '-' then
45 result = result - tokens[i+1]
46 elseif tokens[i] == '*' then
47 result = result * tokens[i+1]
48 elseif tokens[i] == '/' then
49 result = result / tokens[i+1]
50 else
51 error('unknown operation: ' .. tokens[i])
52 end
53 end
54 return result
55end
56
57
58local g = P {
59 "Exp",
60 Exp = Ct(V"Term" * (C(op) * V"OpRecov")^0) / compute;
61 OpRecov = V"Operand";
62 Operand = expect(V"Term", "ExpTerm", Cc(0));
63 Term = num + V"Group";
64 Group = "(" * V"InnerExp" * expect(")", "MisClose", "");
65 InnerExp = expect(V"Exp", "ExpExp", (P(1) - ")")^0 * Cc(0));
66}
67
68g = expect(g, "NoExp", P(1)^0) * expect(-P(1), "Extra")
69
70local function eval(input)
71 local result, label, suffix = g:match(input)
72 if #errors == 0 then
73 return result
74 else
75 local out = {}
76 for i, err in ipairs(errors) do
77 local pos = err[2]
78 local msg = labels[err[1]][2]
79 table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")")
80 end
81 errors = {}
82 return nil, table.concat(out, "\n")
83 end
84end
85
86print(eval "98-76*(54/32)")
87--> 37.125
88
89print(eval "(1+1-1*2/2")
90--> syntax error: missing a closing ')' after the expression (at index 11)
91
92print(eval "(1+)-1*(2/2)")
93--> syntax error: expected a term after the operator (at index 4)
94
95print(eval "(1+1)-1*(/2)")
96--> syntax error: expected an expression after the parenthesis (at index 10)
97
98print(eval "1+(1-(1*2))/2x")
99--> syntax error: extra chracters found after the expression (at index 14)
100
101print(eval "-1+(1-(1*2))/2")
102--> syntax error: no expression found (at index 1)
103
104print(eval "(1+1-1*(2/2+)-():")
105--> syntax error: expected a term after the operator (at index 13)
106--> syntax error: expected an expression after the parenthesis (at index 16)
107--> syntax error: missing a closing ')' after the expression (at index 17)
108--> syntax error: extra characters found after the expression (at index 17)
diff --git a/examples/recoveryOpLab.lua b/examples/recoveryOpLab.lua
new file mode 100644
index 0000000..6697f8b
--- /dev/null
+++ b/examples/recoveryOpLab.lua
@@ -0,0 +1,107 @@
1local lpeg = require"lpeglabel"
2
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
5local T, Lc, Rec = lpeg.T, lpeg.Lc, lpeg.Rec
6
7local labels = {
8 {"NoExp", "no expression found"},
9 {"Extra", "extra characters found after the expression"},
10 {"ExpTerm", "expected a term after the operator"},
11 {"ExpExp", "expected an expression after the parenthesis"},
12 {"MisClose", "missing a closing ')' after the expression"},
13}
14
15local function labelindex(labname)
16 for i, elem in ipairs(labels) do
17 if elem[1] == labname then
18 return i
19 end
20 end
21 error("could not find label: " .. labname)
22end
23
24local errors = {}
25
26local function expect(patt, labname)
27 local i = labelindex(labname)
28 function recorderror(input, pos)
29 table.insert(errors, {i, pos})
30 return true
31 end
32 return patt + Cmt("", recorderror) * T(i)
33end
34
35local num = R("09")^1 / tonumber
36local op = S("+-*/")
37
38local function compute(tokens)
39 local result = tokens[1]
40 for i = 2, #tokens, 2 do
41 if tokens[i] == '+' then
42 result = result + tokens[i+1]
43 elseif tokens[i] == '-' then
44 result = result - tokens[i+1]
45 elseif tokens[i] == '*' then
46 result = result * tokens[i+1]
47 elseif tokens[i] == '/' then
48 result = result / tokens[i+1]
49 else
50 error('unknown operation: ' .. tokens[i])
51 end
52 end
53 return result
54end
55
56
57local g = P {
58 "Exp",
59 Exp = Ct(V"Term" * (C(op) * V"OpRecov")^0) / compute;
60 OpRecov = Rec(V"Operand", Cc(0), labelindex("ExpTerm"));
61 Operand = expect(V"Term", "ExpTerm");
62 Term = num + Rec(V"Group", P"", labelindex("MisClose"));
63 Group = "(" * Rec(V"InnerExp", (P(1) - ")")^0 * Cc(0), labelindex("ExpExp")) * expect(")", "MisClose");
64 InnerExp = expect(V"Exp", "ExpExp");
65}
66
67g = expect(g, "NoExp") * expect(-P(1), "Extra")
68
69local function eval(input)
70 local result, label, suffix = g:match(input)
71 if #errors == 0 then
72 return result
73 else
74 local out = {}
75 for i, err in ipairs(errors) do
76 local pos = err[2]
77 local msg = labels[err[1]][2]
78 table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")")
79 end
80 errors = {}
81 return nil, table.concat(out, "\n")
82 end
83end
84
85print(eval "98-76*(54/32)")
86--> 37.125
87
88print(eval "(1+1-1*2/2")
89--> syntax error: missing a closing ')' after the expression (at index 11)
90
91print(eval "(1+)-1*(2/2)")
92--> syntax error: expected a term after the operator (at index 4)
93
94print(eval "(1+1)-1*(/2)")
95--> syntax error: expected an expression after the parenthesis (at index 10)
96
97print(eval "1+(1-(1*2))/2x")
98--> syntax error: extra chracters found after the expression (at index 14)
99
100print(eval "-1+(1-(1*2))/2")
101--> syntax error: no expression found (at index 1)
102
103print(eval "(1+1-1*(2/2+)-():")
104--> syntax error: expected a term after the operator (at index 13)
105--> syntax error: expected an expression after the parenthesis (at index 16)
106--> syntax error: missing a closing ')' after the expression (at index 17)
107--> syntax error: extra characters found after the expression (at index 17)