diff options
author | Sérgio Medeiros <sqmedeiros@gmail.com> | 2016-07-14 15:51:36 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-14 15:51:36 -0300 |
commit | 9c9f93b127edd8d7f4ac8fe72cb5b93ff38ec3c9 (patch) | |
tree | e8c91c22e9f9d6d3abd5a62009ae13d57ee1bab8 | |
parent | b764048fc85902100bdd26a68f83fb44098a97d3 (diff) | |
parent | 71e4d12c25093bf43284d39bc58db99689591b27 (diff) | |
download | lpeglabel-9c9f93b127edd8d7f4ac8fe72cb5b93ff38ec3c9.tar.gz lpeglabel-9c9f93b127edd8d7f4ac8fe72cb5b93ff38ec3c9.tar.bz2 lpeglabel-9c9f93b127edd8d7f4ac8fe72cb5b93ff38ec3c9.zip |
Merge pull request #10 from undecidabot/master
Adding examples of error recovery
-rw-r--r-- | examples/expect.lua | 18 | ||||
-rw-r--r-- | examples/recovery.lua | 134 | ||||
-rw-r--r-- | examples/recoveryRe.lua | 141 |
3 files changed, 293 insertions, 0 deletions
diff --git a/examples/expect.lua b/examples/expect.lua index 2b7e904..cb68d38 100644 --- a/examples/expect.lua +++ b/examples/expect.lua | |||
@@ -2,6 +2,9 @@ local lpeg = require"lpeglabel" | |||
2 | 2 | ||
3 | local R, S, P, V, C, Ct, T = lpeg.R, lpeg.S, lpeg.P, lpeg.V, lpeg.C, lpeg.Ct, lpeg.T | 3 | local R, S, P, V, C, Ct, T = lpeg.R, lpeg.S, lpeg.P, lpeg.V, lpeg.C, lpeg.Ct, lpeg.T |
4 | 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. | ||
5 | local labels = { | 8 | local labels = { |
6 | {"NoExp", "no expression found"}, | 9 | {"NoExp", "no expression found"}, |
7 | {"Extra", "extra characters found after the expression"}, | 10 | {"Extra", "extra characters found after the expression"}, |
@@ -10,6 +13,11 @@ local labels = { | |||
10 | {"MisClose", "missing a closing ')' after the expression"}, | 13 | {"MisClose", "missing a closing ')' after the expression"}, |
11 | } | 14 | } |
12 | 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. | ||
13 | local function expect(patt, labname) | 21 | local function expect(patt, labname) |
14 | for i, elem in ipairs(labels) do | 22 | for i, elem in ipairs(labels) do |
15 | if elem[1] == labname then | 23 | if elem[1] == labname then |
@@ -23,6 +31,9 @@ end | |||
23 | local num = R("09")^1 / tonumber | 31 | local num = R("09")^1 / tonumber |
24 | local op = S("+-*/") | 32 | local op = S("+-*/") |
25 | 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). | ||
26 | local function compute(tokens) | 37 | local function compute(tokens) |
27 | local result = tokens[1] | 38 | local result = tokens[1] |
28 | for i = 2, #tokens, 2 do | 39 | for i = 2, #tokens, 2 do |
@@ -41,6 +52,9 @@ local function compute(tokens) | |||
41 | return result | 52 | return result |
42 | end | 53 | end |
43 | 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. | ||
44 | local g = P { | 58 | local g = P { |
45 | "Exp", | 59 | "Exp", |
46 | Exp = Ct(V"Term" * (C(op) * expect(V"Term", "ExpTerm"))^0) / compute; | 60 | Exp = Ct(V"Term" * (C(op) * expect(V"Term", "ExpTerm"))^0) / compute; |
@@ -50,6 +64,10 @@ local g = P { | |||
50 | 64 | ||
51 | g = expect(g, "NoExp") * expect(-P(1), "Extra") | 65 | g = expect(g, "NoExp") * expect(-P(1), "Extra") |
52 | 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. | ||
53 | local function eval(input) | 71 | local function eval(input) |
54 | local result, label, suffix = g:match(input) | 72 | local result, label, suffix = g:match(input) |
55 | if result ~= nil then | 73 | if result ~= nil then |
diff --git a/examples/recovery.lua b/examples/recovery.lua new file mode 100644 index 0000000..3272ae7 --- /dev/null +++ b/examples/recovery.lua | |||
@@ -0,0 +1,134 @@ | |||
1 | local lpeg = require"lpeglabel" | ||
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) | ||
diff --git a/examples/recoveryRe.lua b/examples/recoveryRe.lua new file mode 100644 index 0000000..3b83d88 --- /dev/null +++ b/examples/recoveryRe.lua | |||
@@ -0,0 +1,141 @@ | |||
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) | ||