aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSérgio Medeiros <sqmedeiros@gmail.com>2016-07-14 15:51:36 -0300
committerGitHub <noreply@github.com>2016-07-14 15:51:36 -0300
commit9c9f93b127edd8d7f4ac8fe72cb5b93ff38ec3c9 (patch)
treee8c91c22e9f9d6d3abd5a62009ae13d57ee1bab8
parentb764048fc85902100bdd26a68f83fb44098a97d3 (diff)
parent71e4d12c25093bf43284d39bc58db99689591b27 (diff)
downloadlpeglabel-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.lua18
-rw-r--r--examples/recovery.lua134
-rw-r--r--examples/recoveryRe.lua141
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
3local R, S, P, V, C, Ct, T = lpeg.R, lpeg.S, lpeg.P, lpeg.V, lpeg.C, lpeg.Ct, lpeg.T 3local 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.
5local labels = { 8local 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.
13local function expect(patt, labname) 21local 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
23local num = R("09")^1 / tonumber 31local num = R("09")^1 / tonumber
24local op = S("+-*/") 32local 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).
26local function compute(tokens) 37local 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
42end 53end
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.
44local g = P { 58local 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
51g = expect(g, "NoExp") * expect(-P(1), "Extra") 65g = 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.
53local function eval(input) 71local 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 @@
1local lpeg = require"lpeglabel"
2
3local R, S, P, V = lpeg.R, lpeg.S, lpeg.P, lpeg.V
4local C, Cc, Ct, Cmt = lpeg.C, lpeg.Cc, lpeg.Ct, lpeg.Cmt
5local 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.
10local 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.
21local 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)
28end
29
30-- The `errors` table will hold the list of errors recorded during parsing
31local 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.
37local 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)
44end
45
46local num = R("09")^1 / tonumber
47local 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).
52local 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
68end
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.
75local 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
90g = 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.
96local 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
110end
111
112print(eval "98-76*(54/32)")
113--> 37.125
114
115print(eval "(1+1-1*2/2")
116--> syntax error: missing a closing ')' after the expression (at index 11)
117
118print(eval "(1+)-1*(2/2)")
119--> syntax error: expected a term after the operator (at index 4)
120
121print(eval "(1+1)-1*(/2)")
122--> syntax error: expected an expression after the parenthesis (at index 10)
123
124print(eval "1+(1-(1*2))/2x")
125--> syntax error: extra chracters found after the expression (at index 14)
126
127print(eval "-1+(1-(1*2))/2")
128--> syntax error: no expression found (at index 1)
129
130print(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 @@
1local 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.
6local 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.
18local labels = {}
19local errmsgs = {}
20
21for i, err in ipairs(errinfo) do
22 labels[err[1]] = i
23 errmsgs[err[1]] = err[2]
24end
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.
29re.setlabels(labels)
30
31-- The `errors` table will hold the list of errors recorded during parsing
32local 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.
38local function recorderror(input, pos, label)
39 table.insert(errors, {label, pos})
40 return true
41end
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).
46local 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
62end
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.
69local 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.
102local 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
117end
118
119print(eval "98-76*(54/32)")
120--> 37.125
121
122print(eval "(1+1-1*2/2")
123--> syntax error: missing a closing ')' after the expression (line 1, col 10)
124
125print(eval "(1+)-1*(2/2)")
126--> syntax error: expected a term after the operator (line 1, col 4)
127
128print(eval "(1+1)-1*(/2)")
129--> syntax error: expected an expression after the parenthesis (line 1, col 10)
130
131print(eval "1+(1-(1*2))/2x")
132--> syntax error: extra characters found after the expression (line 1, col 14)
133
134print(eval "-1+(1-(1*2))/2")
135--> syntax error: no expression found (line 1, col 1)
136
137print(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)