diff options
Diffstat (limited to 'examples/expect.lua')
-rw-r--r-- | examples/expect.lua | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/examples/expect.lua b/examples/expect.lua new file mode 100644 index 0000000..2b7e904 --- /dev/null +++ b/examples/expect.lua | |||
@@ -0,0 +1,80 @@ | |||
1 | local lpeg = require"lpeglabel" | ||
2 | |||
3 | local R, S, P, V, C, Ct, T = lpeg.R, lpeg.S, lpeg.P, lpeg.V, lpeg.C, lpeg.Ct, lpeg.T | ||
4 | |||
5 | local labels = { | ||
6 | {"NoExp", "no expression found"}, | ||
7 | {"Extra", "extra characters found after the expression"}, | ||
8 | {"ExpTerm", "expected a term after the operator"}, | ||
9 | {"ExpExp", "expected an expression after the parenthesis"}, | ||
10 | {"MisClose", "missing a closing ')' after the expression"}, | ||
11 | } | ||
12 | |||
13 | local function expect(patt, labname) | ||
14 | for i, elem in ipairs(labels) do | ||
15 | if elem[1] == labname then | ||
16 | return patt + T(i) | ||
17 | end | ||
18 | end | ||
19 | |||
20 | error("could not find label: " .. labname) | ||
21 | end | ||
22 | |||
23 | local num = R("09")^1 / tonumber | ||
24 | local op = S("+-*/") | ||
25 | |||
26 | local function compute(tokens) | ||
27 | local result = tokens[1] | ||
28 | for i = 2, #tokens, 2 do | ||
29 | if tokens[i] == '+' then | ||
30 | result = result + tokens[i+1] | ||
31 | elseif tokens[i] == '-' then | ||
32 | result = result - tokens[i+1] | ||
33 | elseif tokens[i] == '*' then | ||
34 | result = result * tokens[i+1] | ||
35 | elseif tokens[i] == '/' then | ||
36 | result = result / tokens[i+1] | ||
37 | else | ||
38 | error('unknown operation: ' .. tokens[i]) | ||
39 | end | ||
40 | end | ||
41 | return result | ||
42 | end | ||
43 | |||
44 | local g = P { | ||
45 | "Exp", | ||
46 | Exp = Ct(V"Term" * (C(op) * expect(V"Term", "ExpTerm"))^0) / compute; | ||
47 | Term = num + V"Group"; | ||
48 | Group = "(" * expect(V"Exp", "ExpExp") * expect(")", "MisClose"); | ||
49 | } | ||
50 | |||
51 | g = expect(g, "NoExp") * expect(-P(1), "Extra") | ||
52 | |||
53 | local function eval(input) | ||
54 | local result, label, suffix = g:match(input) | ||
55 | if result ~= nil then | ||
56 | return result | ||
57 | else | ||
58 | local pos = input:len() - suffix:len() + 1 | ||
59 | local msg = labels[label][2] | ||
60 | return nil, "syntax error: " .. msg .. " (at index " .. pos .. ")" | ||
61 | end | ||
62 | end | ||
63 | |||
64 | print(eval "98-76*(54/32)") | ||
65 | --> 37.125 | ||
66 | |||
67 | print(eval "(1+1-1*2/2") | ||
68 | --> syntax error: missing a closing ')' after the expression (at index 11) | ||
69 | |||
70 | print(eval "(1+)-1*(2/2)") | ||
71 | --> syntax error: expected a term after the operator (at index 4) | ||
72 | |||
73 | print(eval "(1+1)-1*(/2)") | ||
74 | --> syntax error: expected an expression after the parenthesis (at index 10) | ||
75 | |||
76 | print(eval "1+(1-(1*2))/2x") | ||
77 | --> syntax error: extra chracters found after the expression (at index 14) | ||
78 | |||
79 | print(eval "-1+(1-(1*2))/2") | ||
80 | --> syntax error: no expression found (at index 1) | ||