aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/expRec.lua (renamed from examples/expressionRecovery.lua)0
-rw-r--r--examples/expRecAut.lua (renamed from examples/expressionRecAut.lua)0
-rw-r--r--examples/expect.lua98
3 files changed, 0 insertions, 98 deletions
diff --git a/examples/expressionRecovery.lua b/examples/expRec.lua
index c5cbcca..c5cbcca 100644
--- a/examples/expressionRecovery.lua
+++ b/examples/expRec.lua
diff --git a/examples/expressionRecAut.lua b/examples/expRecAut.lua
index e098078..e098078 100644
--- a/examples/expressionRecAut.lua
+++ b/examples/expRecAut.lua
diff --git a/examples/expect.lua b/examples/expect.lua
deleted file mode 100644
index cb68d38..0000000
--- a/examples/expect.lua
+++ /dev/null
@@ -1,98 +0,0 @@
1local lpeg = require"lpeglabel"
2
3local R, S, P, V, C, Ct, T = lpeg.R, lpeg.S, lpeg.P, lpeg.V, lpeg.C, lpeg.Ct, lpeg.T
4
5-- The `labels` table contains the list of labels that we will be using
6-- as well as the corresponding error message for each label, which will
7-- be used in our error reporting later on.
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
16-- The `expect` function takes a pattern and a label defined in
17-- the `labels` table and returns a pattern that throws the specified
18-- label if the original pattern fails to match.
19-- Note: LPegLabel requires us to use integers for the labels, so we
20-- use the index of the label in the `labels` table to represent it.
21local function expect(patt, labname)
22 for i, elem in ipairs(labels) do
23 if elem[1] == labname then
24 return patt + T(i)
25 end
26 end
27
28 error("could not find label: " .. labname)
29end
30
31local num = R("09")^1 / tonumber
32local op = S("+-*/")
33
34-- The `compute` function takes an alternating list of numbers and
35-- operators and computes the result of applying the operations
36-- to the numbers in a left to right order (no operator precedence).
37local function compute(tokens)
38 local result = tokens[1]
39 for i = 2, #tokens, 2 do
40 if tokens[i] == '+' then
41 result = result + tokens[i+1]
42 elseif tokens[i] == '-' then
43 result = result - tokens[i+1]
44 elseif tokens[i] == '*' then
45 result = result * tokens[i+1]
46 elseif tokens[i] == '/' then
47 result = result / tokens[i+1]
48 else
49 error('unknown operation: ' .. tokens[i])
50 end
51 end
52 return result
53end
54
55-- Our grammar is a simple arithmetic expression of integers that
56-- does not take operator precedence into account but allows grouping
57-- via parenthesis.
58local g = P {
59 "Exp",
60 Exp = Ct(V"Term" * (C(op) * expect(V"Term", "ExpTerm"))^0) / compute;
61 Term = num + V"Group";
62 Group = "(" * expect(V"Exp", "ExpExp") * expect(")", "MisClose");
63}
64
65g = expect(g, "NoExp") * expect(-P(1), "Extra")
66
67-- The `eval` function takes an input string to match against the grammar
68-- we've just defined. If the input string matches, then the result of the
69-- computation is returned, otherwise we return the error message and
70-- position of the first failure encountered.
71local function eval(input)
72 local result, label, suffix = g:match(input)
73 if result ~= nil then
74 return result
75 else
76 local pos = input:len() - suffix:len() + 1
77 local msg = labels[label][2]
78 return nil, "syntax error: " .. msg .. " (at index " .. pos .. ")"
79 end
80end
81
82print(eval "98-76*(54/32)")
83--> 37.125
84
85print(eval "(1+1-1*2/2")
86--> syntax error: missing a closing ')' after the expression (at index 11)
87
88print(eval "(1+)-1*(2/2)")
89--> syntax error: expected a term after the operator (at index 4)
90
91print(eval "(1+1)-1*(/2)")
92--> syntax error: expected an expression after the parenthesis (at index 10)
93
94print(eval "1+(1-(1*2))/2x")
95--> syntax error: extra chracters found after the expression (at index 14)
96
97print(eval "-1+(1-(1*2))/2")
98--> syntax error: no expression found (at index 1)