aboutsummaryrefslogtreecommitdiff
path: root/examples/exp.lua
diff options
context:
space:
mode:
authorSérgio Queiroz <sqmedeiros@gmail.com>2017-12-28 14:29:49 -0300
committerSérgio Queiroz <sqmedeiros@gmail.com>2017-12-28 14:29:49 -0300
commit59da25ff241a83d8139e41199ef7a23f6e17fa65 (patch)
treea9ad13fa0f8ddb50855b716b349e4ded5d5fac8d /examples/exp.lua
parent772df00e061db3cd7d0af92c8ab65bc023d8121d (diff)
downloadlpeglabel-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.lua100
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
2lpeg = require'lpeglabel'
3
4local R, S, P, V = lpeg.R, lpeg.S, lpeg.P, lpeg.V
5local C, Cc, Ct, Cmt = lpeg.C, lpeg.Cc, lpeg.Ct, lpeg.Cmt
6local T = lpeg.T
7
8local 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
16local errors = {}
17
18local 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)
24end
25
26local num = R("09")^1 / tonumber
27local op = S("+-*/")
28
29local 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
45end
46
47local 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
59g = expect(g, "NoExp") * expect(-P(1), "Extra")
60
61local 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
75end
76
77assert(eval "98-76*(54/32)" == 37.125)
78--> 37.125
79
80local e, msg = eval "(1+1-1*2/2"
81assert(e == nil and msg == "syntax error: missing a closing ')' after the expression (at index 11)")
82
83e, msg = eval "(1+)-1*(2/2)"
84assert(e == nil and msg == "syntax error: expected a term after the operator (at index 4)")
85
86e, msg = eval "(1+1)-1*(/2)"
87assert(e == nil and msg == "syntax error: expected an expression after the parenthesis (at index 10)")
88
89e, msg = eval "1+(1-(1*2))/2x"
90assert(e == nil and msg == "syntax error: extra characters found after the expression (at index 14)")
91
92e, msg = eval "-1+(1-(1*2))/2"
93assert(e == nil and msg == "syntax error: no expression found (at index 1)")
94
95e, msg = eval "(1+1-1*(2/2+)-():"
96assert(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