diff options
-rw-r--r-- | examples/expectWithRecov.lua | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/examples/expectWithRecov.lua b/examples/expectWithRecov.lua new file mode 100644 index 0000000..8d894c1 --- /dev/null +++ b/examples/expectWithRecov.lua | |||
@@ -0,0 +1,106 @@ | |||
1 | local lpeg = require"lpeglabel" | ||
2 | |||
3 | local R, S, P, V = lpeg.R, lpeg.S, lpeg.P, lpeg.V | ||
4 | local C, Cc, Ct, Cmt = lpeg.C, lpeg.Cc, lpeg.Ct, lpeg.Cmt | ||
5 | local T, Lc = lpeg.T, lpeg.Lc | ||
6 | |||
7 | local 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 | |||
15 | local 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) | ||
22 | end | ||
23 | |||
24 | local errors = {} | ||
25 | |||
26 | local 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) | ||
33 | end | ||
34 | |||
35 | local num = R("09")^1 / tonumber | ||
36 | local op = S("+-*/") | ||
37 | |||
38 | local 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 | ||
54 | end | ||
55 | |||
56 | local g = P { | ||
57 | "Exp", | ||
58 | Exp = Ct(V"Term" * (C(op) * V"OpRecov")^0) / compute; | ||
59 | OpRecov = Lc(V"Operand", Cc(0), labelIndex("ExpTerm")); | ||
60 | Operand = expect(V"Term", "ExpTerm"); | ||
61 | Term = num + V"Group"; | ||
62 | Group = "(" * V"InnerExp" * Lc(expect(")", "MisClose"), P"", labelIndex("MisClose")); | ||
63 | InnerExp = Lc(expect(V"Exp", "ExpExp"), (P(1) - ")")^0 * Cc(0), labelIndex("ExpExp")); | ||
64 | } | ||
65 | |||
66 | g = expect(g, "NoExp") * expect(-P(1), "Extra") | ||
67 | |||
68 | local function eval(input) | ||
69 | local result, label, suffix = g:match(input) | ||
70 | if #errors == 0 then | ||
71 | return result | ||
72 | else | ||
73 | local out = "" | ||
74 | for i, err in ipairs(errors) do | ||
75 | local pos = err[2] | ||
76 | local msg = labels[err[1]][2] | ||
77 | out = out .. "syntax error: " .. msg .. " (at index " .. pos .. ")\n" | ||
78 | end | ||
79 | errors = {} | ||
80 | return nil, out | ||
81 | end | ||
82 | end | ||
83 | |||
84 | print(eval "98-76*(54/32)") | ||
85 | --> 37.125 | ||
86 | |||
87 | print(eval "(1+1-1*2/2") | ||
88 | --> syntax error: missing a closing ')' after the expression (at index 11) | ||
89 | |||
90 | print(eval "(1+)-1*(2/2)") | ||
91 | --> syntax error: expected a term after the operator (at index 4) | ||
92 | |||
93 | print(eval "(1+1)-1*(/2)") | ||
94 | --> syntax error: expected an expression after the parenthesis (at index 10) | ||
95 | |||
96 | print(eval "1+(1-(1*2))/2x") | ||
97 | --> syntax error: extra chracters found after the expression (at index 14) | ||
98 | |||
99 | print(eval "-1+(1-(1*2))/2") | ||
100 | --> syntax error: no expression found (at index 1) | ||
101 | |||
102 | print(eval "(1+1-1*(2/2+)-():") | ||
103 | --> syntax error: expected a term after the operator (at index 13) | ||
104 | --> syntax error: expected an expression after the parenthesis (at index 16) | ||
105 | --> syntax error: missing a closing ')' after the expression (at index 17) | ||
106 | --> syntax error: extra characters found after the expression (at index 17) | ||