diff options
author | Sergio Queiroz <sqmedeiros@gmail.com> | 2016-12-07 08:50:03 -0300 |
---|---|---|
committer | Sergio Queiroz <sqmedeiros@gmail.com> | 2016-12-07 08:50:03 -0300 |
commit | 2ea15094a1d4a9f422c983480aab7231951ee20e (patch) | |
tree | 119348d9249fa8bbc6ef3b5223cc12f34f179697 | |
parent | a4bdec34353ad1849f99e6ab0e5f2ad5e74c83b1 (diff) | |
download | lpeglabel-2ea15094a1d4a9f422c983480aab7231951ee20e.tar.gz lpeglabel-2ea15094a1d4a9f422c983480aab7231951ee20e.tar.bz2 lpeglabel-2ea15094a1d4a9f422c983480aab7231951ee20e.zip |
Renaming files and removing example based on another version
-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.lua | 98 |
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 @@ | |||
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 | -- 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. | ||
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 | -- 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. | ||
21 | local 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) | ||
29 | end | ||
30 | |||
31 | local num = R("09")^1 / tonumber | ||
32 | local 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). | ||
37 | local 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 | ||
53 | end | ||
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. | ||
58 | local 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 | |||
65 | g = 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. | ||
71 | local 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 | ||
80 | end | ||
81 | |||
82 | print(eval "98-76*(54/32)") | ||
83 | --> 37.125 | ||
84 | |||
85 | print(eval "(1+1-1*2/2") | ||
86 | --> syntax error: missing a closing ')' after the expression (at index 11) | ||
87 | |||
88 | print(eval "(1+)-1*(2/2)") | ||
89 | --> syntax error: expected a term after the operator (at index 4) | ||
90 | |||
91 | print(eval "(1+1)-1*(/2)") | ||
92 | --> syntax error: expected an expression after the parenthesis (at index 10) | ||
93 | |||
94 | print(eval "1+(1-(1*2))/2x") | ||
95 | --> syntax error: extra chracters found after the expression (at index 14) | ||
96 | |||
97 | print(eval "-1+(1-(1*2))/2") | ||
98 | --> syntax error: no expression found (at index 1) | ||