diff options
author | Sérgio Queiroz <sqmedeiros@gmail.com> | 2017-12-28 14:29:49 -0300 |
---|---|---|
committer | Sérgio Queiroz <sqmedeiros@gmail.com> | 2017-12-28 14:29:49 -0300 |
commit | 59da25ff241a83d8139e41199ef7a23f6e17fa65 (patch) | |
tree | a9ad13fa0f8ddb50855b716b349e4ded5d5fac8d /examples/exp.lua | |
parent | 772df00e061db3cd7d0af92c8ab65bc023d8121d (diff) | |
download | lpeglabel-59da25ff241a83d8139e41199ef7a23f6e17fa65.tar.gz lpeglabel-59da25ff241a83d8139e41199ef7a23f6e17fa65.tar.bz2 lpeglabel-59da25ff241a83d8139e41199ef7a23f6e17fa65.zip |
Updating examples to the new semantics
Diffstat (limited to 'examples/exp.lua')
-rw-r--r-- | examples/exp.lua | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/examples/exp.lua b/examples/exp.lua new file mode 100644 index 0000000..4afe83e --- /dev/null +++ b/examples/exp.lua | |||
@@ -0,0 +1,100 @@ | |||
1 | -- Matthew's recovery example | ||
2 | lpeg = require'lpeglabel' | ||
3 | |||
4 | local R, S, P, V = lpeg.R, lpeg.S, lpeg.P, lpeg.V | ||
5 | local C, Cc, Ct, Cmt = lpeg.C, lpeg.Cc, lpeg.Ct, lpeg.Cmt | ||
6 | local T = lpeg.T | ||
7 | |||
8 | local labels = { | ||
9 | NoExp = "no expression found", | ||
10 | Extra = "extra characters found after the expression", | ||
11 | ExpTerm = "expected a term after the operator", | ||
12 | ExpExp = "expected an expression after the parenthesis", | ||
13 | MisClose = "missing a closing ')' after the expression", | ||
14 | } | ||
15 | |||
16 | local errors = {} | ||
17 | |||
18 | local function expect(patt, labname) | ||
19 | function recorderror(input, pos) | ||
20 | table.insert(errors, {labname, pos}) | ||
21 | return true | ||
22 | end | ||
23 | return patt + Cmt("", recorderror) * T(labname) | ||
24 | end | ||
25 | |||
26 | local num = R("09")^1 / tonumber | ||
27 | local op = S("+-*/") | ||
28 | |||
29 | local function compute(tokens) | ||
30 | local result = tokens[1] | ||
31 | for i = 2, #tokens, 2 do | ||
32 | if tokens[i] == '+' then | ||
33 | result = result + tokens[i+1] | ||
34 | elseif tokens[i] == '-' then | ||
35 | result = result - tokens[i+1] | ||
36 | elseif tokens[i] == '*' then | ||
37 | result = result * tokens[i+1] | ||
38 | elseif tokens[i] == '/' then | ||
39 | result = result / tokens[i+1] | ||
40 | else | ||
41 | error('unknown operation: ' .. tokens[i]) | ||
42 | end | ||
43 | end | ||
44 | return result | ||
45 | end | ||
46 | |||
47 | local g = P { | ||
48 | "Exp", | ||
49 | Exp = Ct(V"Term" * (C(op) * V"Operand")^0) / compute; | ||
50 | Operand = expect(V"Term", "ExpTerm"); | ||
51 | Term = num + V"Group"; | ||
52 | Group = "(" * V"InnerExp" * expect(")", "MisClose"); | ||
53 | InnerExp = expect(V"Exp", "ExpExp"); | ||
54 | ExpTerm = Cc(0); | ||
55 | MisClose = P""; | ||
56 | ExpExp = (P(1) - ")")^0 * Cc(0); | ||
57 | } | ||
58 | |||
59 | g = expect(g, "NoExp") * expect(-P(1), "Extra") | ||
60 | |||
61 | local function eval(input) | ||
62 | local result, label, suffix = g:match(input) | ||
63 | if #errors == 0 then | ||
64 | return result | ||
65 | else | ||
66 | local out = {} | ||
67 | for i, err in ipairs(errors) do | ||
68 | local pos = err[2] | ||
69 | local msg = labels[err[1]] | ||
70 | table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")") | ||
71 | end | ||
72 | errors = {} | ||
73 | return nil, table.concat(out, "\n") | ||
74 | end | ||
75 | end | ||
76 | |||
77 | assert(eval "98-76*(54/32)" == 37.125) | ||
78 | --> 37.125 | ||
79 | |||
80 | local e, msg = eval "(1+1-1*2/2" | ||
81 | assert(e == nil and msg == "syntax error: missing a closing ')' after the expression (at index 11)") | ||
82 | |||
83 | e, msg = eval "(1+)-1*(2/2)" | ||
84 | assert(e == nil and msg == "syntax error: expected a term after the operator (at index 4)") | ||
85 | |||
86 | e, msg = eval "(1+1)-1*(/2)" | ||
87 | assert(e == nil and msg == "syntax error: expected an expression after the parenthesis (at index 10)") | ||
88 | |||
89 | e, msg = eval "1+(1-(1*2))/2x" | ||
90 | assert(e == nil and msg == "syntax error: extra characters found after the expression (at index 14)") | ||
91 | |||
92 | e, msg = eval "-1+(1-(1*2))/2" | ||
93 | assert(e == nil and msg == "syntax error: no expression found (at index 1)") | ||
94 | |||
95 | e, msg = eval "(1+1-1*(2/2+)-():" | ||
96 | assert(e == nil and msg == "syntax error: expected a term after the operator (at index 13)\n" .. | ||
97 | "syntax error: expected an expression after the parenthesis (at index 16)\n" .. | ||
98 | "syntax error: missing a closing ')' after the expression (at index 17)\n" .. | ||
99 | "syntax error: extra characters found after the expression (at index 17)") | ||
100 | |||