diff options
author | Sergio Queiroz <sqmedeiros@gmail.com> | 2016-12-14 08:16:29 -0300 |
---|---|---|
committer | Sergio Queiroz <sqmedeiros@gmail.com> | 2016-12-14 08:16:29 -0300 |
commit | 98e54ce388233170cd3a3feb2bcc4449670ccabc (patch) | |
tree | b02c02adf0dbdfe0d700d20cb204ed834b99e740 /examples | |
parent | 20cb6e1443f3b79a4db3fedf25bc2eff91ef7d70 (diff) | |
download | lpeglabel-98e54ce388233170cd3a3feb2bcc4449670ccabc.tar.gz lpeglabel-98e54ce388233170cd3a3feb2bcc4449670ccabc.tar.bz2 lpeglabel-98e54ce388233170cd3a3feb2bcc4449670ccabc.zip |
Removing files related to the previous version (with labeled ordered choice)
Diffstat (limited to 'examples')
-rw-r--r-- | examples/recoveryRe.lua | 141 |
1 files changed, 0 insertions, 141 deletions
diff --git a/examples/recoveryRe.lua b/examples/recoveryRe.lua deleted file mode 100644 index 3b83d88..0000000 --- a/examples/recoveryRe.lua +++ /dev/null | |||
@@ -1,141 +0,0 @@ | |||
1 | local re = require"relabel" | ||
2 | |||
3 | -- The `errinfo` table contains the list of labels that we will be using | ||
4 | -- as well as the corresponding error message for each label, which will | ||
5 | -- be used in our error reporting later on. | ||
6 | local errinfo = { | ||
7 | {"NoExp", "no expression found"}, | ||
8 | {"Extra", "extra characters found after the expression"}, | ||
9 | {"ExpTerm", "expected a term after the operator"}, | ||
10 | {"ExpExp", "expected an expression after the parenthesis"}, | ||
11 | {"MisClose", "missing a closing ')' after the expression"}, | ||
12 | } | ||
13 | |||
14 | -- We split the errinfo table into two tables: `labels` which is a | ||
15 | -- mapping from the label names to its integer representation, and | ||
16 | -- `errmsgs` which is a mapping from the label names to its | ||
17 | -- corresponding error message. | ||
18 | local labels = {} | ||
19 | local errmsgs = {} | ||
20 | |||
21 | for i, err in ipairs(errinfo) do | ||
22 | labels[err[1]] = i | ||
23 | errmsgs[err[1]] = err[2] | ||
24 | end | ||
25 | |||
26 | -- The `labels` table is especially useful for making our re grammar more | ||
27 | -- readable through the use of the `setlabels` function which allows us | ||
28 | -- to use the label names directly in the re grammar instead of the integers. | ||
29 | re.setlabels(labels) | ||
30 | |||
31 | -- The `errors` table will hold the list of errors recorded during parsing | ||
32 | local errors = {} | ||
33 | |||
34 | -- The `recorderror` function simply records the label and position of | ||
35 | -- the failure (index in input string) into the `errors` table. | ||
36 | -- Note: The unused `input` parameter is necessary, as this will be called | ||
37 | -- by LPeg's match-time capture. | ||
38 | local function recorderror(input, pos, label) | ||
39 | table.insert(errors, {label, pos}) | ||
40 | return true | ||
41 | end | ||
42 | |||
43 | -- The `compute` function takes an alternating list of numbers and | ||
44 | -- operators and computes the result of applying the operations | ||
45 | -- to the numbers in a left to right order (no operator precedence). | ||
46 | local function compute(tokens) | ||
47 | local result = tokens[1] | ||
48 | for i = 2, #tokens, 2 do | ||
49 | if tokens[i] == '+' then | ||
50 | result = result + tokens[i+1] | ||
51 | elseif tokens[i] == '-' then | ||
52 | result = result - tokens[i+1] | ||
53 | elseif tokens[i] == '*' then | ||
54 | result = result * tokens[i+1] | ||
55 | elseif tokens[i] == '/' then | ||
56 | result = result / tokens[i+1] | ||
57 | else | ||
58 | error('unknown operation: ' .. tokens[i]) | ||
59 | end | ||
60 | end | ||
61 | return result | ||
62 | end | ||
63 | |||
64 | -- Our grammar is a simple arithmetic expression of integers that | ||
65 | -- does not take operator precedence into account but allows grouping | ||
66 | -- via parenthesis. We have incorporated some error recovery startegies | ||
67 | -- to our grammar so that it may resume parsing even after encountering | ||
68 | -- an error, which allows us to report more errors. | ||
69 | local g = re.compile([[ | ||
70 | S <- (Exp / ErrNoExp) (!. / ErrExtra) | ||
71 | Exp <- {| Term (op Operand)* |} -> compute | ||
72 | -- If we encounter a missing term/operand, we return a dummy instead. | ||
73 | Operand <- Term / ErrExpTerm /{ExpTerm} dummy | ||
74 | Term <- num / Group | ||
75 | -- If we encounter a missing closing parenthesis, we ignore it. | ||
76 | Group <- "(" InnerExp (")" / ErrMisClose /{MisClose} "") | ||
77 | -- If we encounter a missing inner expression, we skip to the next | ||
78 | -- closing parenthesis, and return a dummy in its place. | ||
79 | InnerExp <- Exp / ErrExpExp /{ExpExp} [^)]* dummy | ||
80 | |||
81 | op <- {[-+*/]} | ||
82 | num <- [0-9]+ -> tonumber | ||
83 | |||
84 | -- Before throwing an error, we make sure to record it first. | ||
85 | ErrNoExp <- ("" -> "NoExp" => recorderror) %{NoExp} | ||
86 | ErrExtra <- ("" -> "Extra" => recorderror) %{Extra} | ||
87 | ErrExpTerm <- ("" -> "ExpTerm" => recorderror) %{ExpTerm} | ||
88 | ErrExpExp <- ("" -> "ExpExp" => recorderror) %{ExpExp} | ||
89 | ErrMisClose <- ("" -> "MisClose" => recorderror) %{MisClose} | ||
90 | |||
91 | dummy <- "" -> "0" -> tonumber | ||
92 | ]], { | ||
93 | compute = compute; | ||
94 | recorderror = recorderror; | ||
95 | tonumber = tonumber; | ||
96 | }) | ||
97 | |||
98 | -- The `eval` function takes an input string to match against the grammar | ||
99 | -- we've just defined. If the input string matches, then the result of the | ||
100 | -- computation is returned, otherwise we return the error messages and | ||
101 | -- positions of all the failures encountered. | ||
102 | local function eval(input) | ||
103 | local result, label, suffix = g:match(input) | ||
104 | if #errors == 0 then | ||
105 | return result | ||
106 | else | ||
107 | local out = {} | ||
108 | for i, err in ipairs(errors) do | ||
109 | local pos = err[2] | ||
110 | local msg = errmsgs[err[1]] | ||
111 | local line, col = re.calcline(input, pos) | ||
112 | table.insert(out, "syntax error: " .. msg .. " (line " .. line .. ", col " .. col .. ")") | ||
113 | end | ||
114 | errors = {} | ||
115 | return nil, table.concat(out, "\n") | ||
116 | end | ||
117 | end | ||
118 | |||
119 | print(eval "98-76*(54/32)") | ||
120 | --> 37.125 | ||
121 | |||
122 | print(eval "(1+1-1*2/2") | ||
123 | --> syntax error: missing a closing ')' after the expression (line 1, col 10) | ||
124 | |||
125 | print(eval "(1+)-1*(2/2)") | ||
126 | --> syntax error: expected a term after the operator (line 1, col 4) | ||
127 | |||
128 | print(eval "(1+1)-1*(/2)") | ||
129 | --> syntax error: expected an expression after the parenthesis (line 1, col 10) | ||
130 | |||
131 | print(eval "1+(1-(1*2))/2x") | ||
132 | --> syntax error: extra characters found after the expression (line 1, col 14) | ||
133 | |||
134 | print(eval "-1+(1-(1*2))/2") | ||
135 | --> syntax error: no expression found (line 1, col 1) | ||
136 | |||
137 | print(eval "(1+1-1*(2/2+)-():") | ||
138 | --> syntax error: expected a term after the operator (line 1, col 13) | ||
139 | --> syntax error: expected an expression after the parenthesis (line 1, col 16) | ||
140 | --> syntax error: missing a closing ')' after the expression (line 1, col 17) | ||
141 | --> syntax error: extra characters found after the expression (line 1, col 17) | ||