diff options
-rw-r--r-- | examples/recovery.lua | 134 |
1 files changed, 0 insertions, 134 deletions
diff --git a/examples/recovery.lua b/examples/recovery.lua deleted file mode 100644 index bb17b54..0000000 --- a/examples/recovery.lua +++ /dev/null | |||
@@ -1,134 +0,0 @@ | |||
1 | local lpeg = require"lpeglabelrec" | ||
2 | |||
3 | local R, S, P, V = lpeg.R, lpeg.S, lpeg.P, lpeg.V | ||
4 | local C, Cc, Ct, Cmt = lpeg.C, lpeg.Cc, lpeg.Ct, lpeg.Cmt | ||
5 | local T, Lc = lpeg.T, lpeg.Lc | ||
6 | |||
7 | -- The `labels` table contains the list of labels that we will be using | ||
8 | -- as well as the corresponding error message for each label, which will | ||
9 | -- be used in our error reporting later on. | ||
10 | local labels = { | ||
11 | {"NoExp", "no expression found"}, | ||
12 | {"Extra", "extra characters found after the expression"}, | ||
13 | {"ExpTerm", "expected a term after the operator"}, | ||
14 | {"ExpExp", "expected an expression after the parenthesis"}, | ||
15 | {"MisClose", "missing a closing ')' after the expression"}, | ||
16 | } | ||
17 | |||
18 | -- The `labelindex` function gives us the index of a label in the | ||
19 | -- `labels` table, which serves as the integer representation of the label. | ||
20 | -- We need this because LPegLabel requires us to use integers for the labels. | ||
21 | local function labelindex(labname) | ||
22 | for i, elem in ipairs(labels) do | ||
23 | if elem[1] == labname then | ||
24 | return i | ||
25 | end | ||
26 | end | ||
27 | error("could not find label: " .. labname) | ||
28 | end | ||
29 | |||
30 | -- The `errors` table will hold the list of errors recorded during parsing | ||
31 | local errors = {} | ||
32 | |||
33 | -- The `expect` function takes a pattern and a label and returns a pattern | ||
34 | -- that throws the specified label if the original pattern fails to match. | ||
35 | -- Before throwing the label, it records the label to be thrown along with | ||
36 | -- the position of the failure (index in input string) into the `errors` table. | ||
37 | local function expect(patt, labname) | ||
38 | local i = labelindex(labname) | ||
39 | function recorderror(input, pos) | ||
40 | table.insert(errors, {i, pos}) | ||
41 | return true | ||
42 | end | ||
43 | return patt + Cmt("", recorderror) * T(i) | ||
44 | end | ||
45 | |||
46 | local num = R("09")^1 / tonumber | ||
47 | local op = S("+-*/") | ||
48 | |||
49 | -- The `compute` function takes an alternating list of numbers and | ||
50 | -- operators and computes the result of applying the operations | ||
51 | -- to the numbers in a left to right order (no operator precedence). | ||
52 | local function compute(tokens) | ||
53 | local result = tokens[1] | ||
54 | for i = 2, #tokens, 2 do | ||
55 | if tokens[i] == '+' then | ||
56 | result = result + tokens[i+1] | ||
57 | elseif tokens[i] == '-' then | ||
58 | result = result - tokens[i+1] | ||
59 | elseif tokens[i] == '*' then | ||
60 | result = result * tokens[i+1] | ||
61 | elseif tokens[i] == '/' then | ||
62 | result = result / tokens[i+1] | ||
63 | else | ||
64 | error('unknown operation: ' .. tokens[i]) | ||
65 | end | ||
66 | end | ||
67 | return result | ||
68 | end | ||
69 | |||
70 | -- Our grammar is a simple arithmetic expression of integers that | ||
71 | -- does not take operator precedence into account but allows grouping | ||
72 | -- via parenthesis. We have incorporated some error recovery startegies | ||
73 | -- to our grammar so that it may resume parsing even after encountering | ||
74 | -- an error, which allows us to report more errors. | ||
75 | local g = P { | ||
76 | "Exp", | ||
77 | Exp = Ct(V"Term" * (C(op) * V"OpRecov")^0) / compute; | ||
78 | -- `OpRecov` handles missing terms/operands by returning a dummy (zero). | ||
79 | OpRecov = Lc(V"Operand", Cc(0), labelindex("ExpTerm")); | ||
80 | Operand = expect(V"Term", "ExpTerm"); | ||
81 | Term = num + V"Group"; | ||
82 | -- `Group` handles missing closing parenthesis by simply ignoring it. | ||
83 | -- Like all the others, the error is still recorded of course. | ||
84 | Group = "(" * V"InnerExp" * Lc(expect(")", "MisClose"), P"", labelindex("MisClose")); | ||
85 | -- `InnerExp` handles missing expressions by skipping to the next closing | ||
86 | -- parenthesis. A dummy (zero) is returned in place of the expression. | ||
87 | InnerExp = Lc(expect(V"Exp", "ExpExp"), (P(1) - ")")^0 * Cc(0), labelindex("ExpExp")); | ||
88 | } | ||
89 | |||
90 | g = expect(g, "NoExp") * expect(-P(1), "Extra") | ||
91 | |||
92 | -- The `eval` function takes an input string to match against the grammar | ||
93 | -- we've just defined. If the input string matches, then the result of the | ||
94 | -- computation is returned, otherwise we return the error messages and | ||
95 | -- positions of all the failures encountered. | ||
96 | local function eval(input) | ||
97 | local result, label, suffix = g:match(input) | ||
98 | if #errors == 0 then | ||
99 | return result | ||
100 | else | ||
101 | local out = {} | ||
102 | for i, err in ipairs(errors) do | ||
103 | local pos = err[2] | ||
104 | local msg = labels[err[1]][2] | ||
105 | table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")") | ||
106 | end | ||
107 | errors = {} | ||
108 | return nil, table.concat(out, "\n") | ||
109 | end | ||
110 | end | ||
111 | |||
112 | print(eval "98-76*(54/32)") | ||
113 | --> 37.125 | ||
114 | |||
115 | print(eval "(1+1-1*2/2") | ||
116 | --> syntax error: missing a closing ')' after the expression (at index 11) | ||
117 | |||
118 | print(eval "(1+)-1*(2/2)") | ||
119 | --> syntax error: expected a term after the operator (at index 4) | ||
120 | |||
121 | print(eval "(1+1)-1*(/2)") | ||
122 | --> syntax error: expected an expression after the parenthesis (at index 10) | ||
123 | |||
124 | print(eval "1+(1-(1*2))/2x") | ||
125 | --> syntax error: extra chracters found after the expression (at index 14) | ||
126 | |||
127 | print(eval "-1+(1-(1*2))/2") | ||
128 | --> syntax error: no expression found (at index 1) | ||
129 | |||
130 | print(eval "(1+1-1*(2/2+)-():") | ||
131 | --> syntax error: expected a term after the operator (at index 13) | ||
132 | --> syntax error: expected an expression after the parenthesis (at index 16) | ||
133 | --> syntax error: missing a closing ')' after the expression (at index 17) | ||
134 | --> syntax error: extra characters found after the expression (at index 17) | ||