aboutsummaryrefslogtreecommitdiff
path: root/examples/recoveryOpLab.lua
diff options
context:
space:
mode:
Diffstat (limited to 'examples/recoveryOpLab.lua')
-rw-r--r--examples/recoveryOpLab.lua107
1 files changed, 0 insertions, 107 deletions
diff --git a/examples/recoveryOpLab.lua b/examples/recoveryOpLab.lua
deleted file mode 100644
index 6697f8b..0000000
--- a/examples/recoveryOpLab.lua
+++ /dev/null
@@ -1,107 +0,0 @@
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)