aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/expressionRecovery.lua (renamed from examples/recoveryRecCap.lua)10
-rw-r--r--examples/recoveryOpFail.lua108
-rw-r--r--examples/recoveryOpLab.lua107
-rwxr-xr-xexamples/typedlua/test.lua9
-rw-r--r--examples/typedlua/tllexer.lua11
-rw-r--r--examples/typedlua/tlparser.lua2
-rw-r--r--testlabel.lua9
7 files changed, 29 insertions, 227 deletions
diff --git a/examples/recoveryRecCap.lua b/examples/expressionRecovery.lua
index c1b3a53..a4a3288 100644
--- a/examples/recoveryRecCap.lua
+++ b/examples/expressionRecovery.lua
@@ -68,7 +68,7 @@ function sync (p)
68end 68end
69 69
70function defaultValue (p) 70function defaultValue (p)
71 return p or m.Cc(0) 71 return p or m.Cc(1000)
72end 72end
73 73
74local recg = P { 74local recg = P {
@@ -91,6 +91,7 @@ local function eval(input)
91 for i, err in ipairs(errors) do 91 for i, err in ipairs(errors) do
92 local pos = err.col 92 local pos = err.col
93 local msg = err.msg 93 local msg = err.msg
94 print("sub", subject)
94 table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")") 95 table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")")
95 end 96 end
96 print(table.concat(out, "\n")) 97 print(table.concat(out, "\n"))
@@ -111,7 +112,7 @@ print(eval "1+3+-9")
111--> 1 + 3 + [0] - 9 112--> 1 + 3 + [0] - 9
112 113
113print(eval "1+()3+") 114print(eval "1+()3+")
114--> 1 + ([0]) [+] 3 + [0] 115--> 1 + ([0]) [3 +] [0]
115 116
116print(eval "8-(2+)-5") 117print(eval "8-(2+)-5")
117--> 8 - (2 + [0]) - 5 118--> 8 - (2 + [0]) - 5
@@ -119,3 +120,8 @@ print(eval "8-(2+)-5")
119print(eval "()") 120print(eval "()")
120 121
121print(eval "") 122print(eval "")
123
124print(eval "1+()+")
125
126print(eval "3)")
127
diff --git a/examples/recoveryOpFail.lua b/examples/recoveryOpFail.lua
deleted file mode 100644
index bd1beb7..0000000
--- a/examples/recoveryOpFail.lua
+++ /dev/null
@@ -1,108 +0,0 @@
1local lpeg = require"lpeglabelrec"
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, Rec = lpeg.T, lpeg.Lc, lpeg.Rec
6
7local labels = {
8 {"NoExp", "no expression found"},
9 {"Extra", "extra characters found after the expression"},
10 {"ExpTerm", "expected a term after the operator"},
11 {"ExpExp", "expected an expression after the parenthesis"},
12 {"MisClose", "missing a closing ')' after the expression"},
13}
14
15local function labelindex(labname)
16 for i, elem in ipairs(labels) do
17 if elem[1] == labname then
18 return i
19 end
20 end
21 error("could not find label: " .. labname)
22end
23
24local errors = {}
25
26local function expect(patt, labname, recpatt)
27 local i = labelindex(labname)
28 function recorderror(input, pos)
29 table.insert(errors, {i, pos})
30 return true
31 end
32 if not recpatt then recpatt = P"" end
33 return Rec(patt, Cmt("", recorderror) * recpatt)
34end
35
36local num = R("09")^1 / tonumber
37local op = S("+-*/")
38
39local function compute(tokens)
40 local result = tokens[1]
41 for i = 2, #tokens, 2 do
42 if 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 elseif tokens[i] == '/' then
49 result = result / tokens[i+1]
50 else
51 error('unknown operation: ' .. tokens[i])
52 end
53 end
54 return result
55end
56
57
58local g = P {
59 "Exp",
60 Exp = Ct(V"Term" * (C(op) * V"OpRecov")^0) / compute;
61 OpRecov = V"Operand";
62 Operand = expect(V"Term", "ExpTerm", Cc(0));
63 Term = num + V"Group";
64 Group = "(" * V"InnerExp" * expect(")", "MisClose", "");
65 InnerExp = expect(V"Exp", "ExpExp", (P(1) - ")")^0 * Cc(0));
66}
67
68g = expect(g, "NoExp", P(1)^0) * expect(-P(1), "Extra")
69
70local function eval(input)
71 local result, label, suffix = g:match(input)
72 if #errors == 0 then
73 return result
74 else
75 local out = {}
76 for i, err in ipairs(errors) do
77 local pos = err[2]
78 local msg = labels[err[1]][2]
79 table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")")
80 end
81 errors = {}
82 return nil, table.concat(out, "\n")
83 end
84end
85
86print(eval "98-76*(54/32)")
87--> 37.125
88
89print(eval "(1+1-1*2/2")
90--> syntax error: missing a closing ')' after the expression (at index 11)
91
92print(eval "(1+)-1*(2/2)")
93--> syntax error: expected a term after the operator (at index 4)
94
95print(eval "(1+1)-1*(/2)")
96--> syntax error: expected an expression after the parenthesis (at index 10)
97
98print(eval "1+(1-(1*2))/2x")
99--> syntax error: extra chracters found after the expression (at index 14)
100
101print(eval "-1+(1-(1*2))/2")
102--> syntax error: no expression found (at index 1)
103
104print(eval "(1+1-1*(2/2+)-():")
105--> syntax error: expected a term after the operator (at index 13)
106--> syntax error: expected an expression after the parenthesis (at index 16)
107--> syntax error: missing a closing ')' after the expression (at index 17)
108--> syntax error: extra characters found after the expression (at index 17)
diff --git a/examples/recoveryOpLab.lua b/examples/recoveryOpLab.lua
deleted file mode 100644
index 6697f8b..0000000
--- a/examples/recoveryOpLab.lua
+++ /dev/null
@@ -1,107 +0,0 @@
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, Rec = lpeg.T, lpeg.Lc, lpeg.Rec
6
7local labels = {
8 {"NoExp", "no expression found"},
9 {"Extra", "extra characters found after the expression"},
10 {"ExpTerm", "expected a term after the operator"},
11 {"ExpExp", "expected an expression after the parenthesis"},
12 {"MisClose", "missing a closing ')' after the expression"},
13}
14
15local function labelindex(labname)
16 for i, elem in ipairs(labels) do
17 if elem[1] == labname then
18 return i
19 end
20 end
21 error("could not find label: " .. labname)
22end
23
24local errors = {}
25
26local function expect(patt, labname)
27 local i = labelindex(labname)
28 function recorderror(input, pos)
29 table.insert(errors, {i, pos})
30 return true
31 end
32 return patt + Cmt("", recorderror) * T(i)
33end
34
35local num = R("09")^1 / tonumber
36local op = S("+-*/")
37
38local function compute(tokens)
39 local result = tokens[1]
40 for i = 2, #tokens, 2 do
41 if tokens[i] == '+' then
42 result = result + tokens[i+1]
43 elseif tokens[i] == '-' then
44 result = result - tokens[i+1]
45 elseif tokens[i] == '*' then
46 result = result * tokens[i+1]
47 elseif tokens[i] == '/' then
48 result = result / tokens[i+1]
49 else
50 error('unknown operation: ' .. tokens[i])
51 end
52 end
53 return result
54end
55
56
57local g = P {
58 "Exp",
59 Exp = Ct(V"Term" * (C(op) * V"OpRecov")^0) / compute;
60 OpRecov = Rec(V"Operand", Cc(0), labelindex("ExpTerm"));
61 Operand = expect(V"Term", "ExpTerm");
62 Term = num + Rec(V"Group", P"", labelindex("MisClose"));
63 Group = "(" * Rec(V"InnerExp", (P(1) - ")")^0 * Cc(0), labelindex("ExpExp")) * expect(")", "MisClose");
64 InnerExp = expect(V"Exp", "ExpExp");
65}
66
67g = expect(g, "NoExp") * expect(-P(1), "Extra")
68
69local function eval(input)
70 local result, label, suffix = g:match(input)
71 if #errors == 0 then
72 return result
73 else
74 local out = {}
75 for i, err in ipairs(errors) do
76 local pos = err[2]
77 local msg = labels[err[1]][2]
78 table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")")
79 end
80 errors = {}
81 return nil, table.concat(out, "\n")
82 end
83end
84
85print(eval "98-76*(54/32)")
86--> 37.125
87
88print(eval "(1+1-1*2/2")
89--> syntax error: missing a closing ')' after the expression (at index 11)
90
91print(eval "(1+)-1*(2/2)")
92--> syntax error: expected a term after the operator (at index 4)
93
94print(eval "(1+1)-1*(/2)")
95--> syntax error: expected an expression after the parenthesis (at index 10)
96
97print(eval "1+(1-(1*2))/2x")
98--> syntax error: extra chracters found after the expression (at index 14)
99
100print(eval "-1+(1-(1*2))/2")
101--> syntax error: no expression found (at index 1)
102
103print(eval "(1+1-1*(2/2+)-():")
104--> syntax error: expected a term after the operator (at index 13)
105--> syntax error: expected an expression after the parenthesis (at index 16)
106--> syntax error: missing a closing ')' after the expression (at index 17)
107--> syntax error: extra characters found after the expression (at index 17)
diff --git a/examples/typedlua/test.lua b/examples/typedlua/test.lua
index ed4e7a1..95474ba 100755
--- a/examples/typedlua/test.lua
+++ b/examples/typedlua/test.lua
@@ -401,15 +401,18 @@ assert(m == e)
401-- unfinished comments 401-- unfinished comments
402 402
403s = [=[ 403s = [=[
404--[[ testing 404--[[
405
406testing
405unfinished 407unfinished
408
406comment 409comment
407]=] 410 ]=]
408--[=[ 411--[=[
409test.lua:3:1: syntax error, unexpected 'comment', expecting '=', ',', 'String', '{', '(', ':', '[', '.' 412test.lua:3:1: syntax error, unexpected 'comment', expecting '=', ',', 'String', '{', '(', ':', '[', '.'
410]=] 413]=]
411e = [=[ 414e = [=[
412test.lua:1:1: unfinished long comment 415test.lua:1:2: unfinished long comment
413]=] 416]=]
414 417
415r, m = parse(s) 418r, m = parse(s)
diff --git a/examples/typedlua/tllexer.lua b/examples/typedlua/tllexer.lua
index 6517ba5..d6033ec 100644
--- a/examples/typedlua/tllexer.lua
+++ b/examples/typedlua/tllexer.lua
@@ -1,6 +1,6 @@
1local tllexer = {} 1local tllexer = {}
2 2
3local lpeg = require "lpeglabel" 3local lpeg = require "lpeglabelrec"
4lpeg.locale(lpeg) 4lpeg.locale(lpeg)
5 5
6local tlerror = require "tlerror" 6local tlerror = require "tlerror"
@@ -9,10 +9,6 @@ function tllexer.try (pat, label)
9 return pat + lpeg.T(tlerror.labels[label]) 9 return pat + lpeg.T(tlerror.labels[label])
10end 10end
11 11
12function tllexer.catch (pat, label)
13 return lpeg.Lc(pat, lpeg.P(false), tlerror.labels[label])
14end
15
16local function setffp (s, i, t, n) 12local function setffp (s, i, t, n)
17 if not t.ffp or i > t.ffp then 13 if not t.ffp or i > t.ffp then
18 t.ffp = i 14 t.ffp = i
@@ -45,7 +41,10 @@ local CloseEQ = lpeg.Cmt(Close * lpeg.Cb("init"),
45local LongString = Open * (lpeg.P(1) - CloseEQ)^0 * tllexer.try(Close, "LongString") / 41local LongString = Open * (lpeg.P(1) - CloseEQ)^0 * tllexer.try(Close, "LongString") /
46 function (s, o) return s end 42 function (s, o) return s end
47 43
48local Comment = lpeg.Lc(lpeg.P("--") * LongString / function () return end, 44local LongStringCm1 = Open * (lpeg.P(1) - CloseEQ)^0 * Close /
45 function (s, o) return s end
46
47local Comment = lpeg.Rec(lpeg.P"--" * #Open * (LongStringCm1 / function() return end + lpeg.T(tlerror.labels["LongString"])),
49 lpeg.T(tlerror.labels["LongComment"]), tlerror.labels["LongString"]) + 48 lpeg.T(tlerror.labels["LongComment"]), tlerror.labels["LongString"]) +
50 lpeg.P("--") * (lpeg.P(1) - lpeg.P("\n"))^0 49 lpeg.P("--") * (lpeg.P(1) - lpeg.P("\n"))^0
51 50
diff --git a/examples/typedlua/tlparser.lua b/examples/typedlua/tlparser.lua
index a301fa6..fe4fd5e 100644
--- a/examples/typedlua/tlparser.lua
+++ b/examples/typedlua/tlparser.lua
@@ -1,6 +1,6 @@
1local tlparser = {} 1local tlparser = {}
2 2
3local lpeg = require "lpeglabel" 3local lpeg = require "lpeglabelrec"
4lpeg.locale(lpeg) 4lpeg.locale(lpeg)
5 5
6local tllexer = require "tllexer" 6local tllexer = require "tllexer"
diff --git a/testlabel.lua b/testlabel.lua
index cb92657..d9bac64 100644
--- a/testlabel.lua
+++ b/testlabel.lua
@@ -173,6 +173,15 @@ p = m.Rec(#m.T(22), m.P"a", 22)
173r, l, serror = p:match("bbc") 173r, l, serror = p:match("bbc")
174assert(r == nil and l == 0 and serror == "bbc") 174assert(r == nil and l == 0 and serror == "bbc")
175 175
176p = m.Rec(#m.P("a") * m.T(22), m.T(15), 22)
177r, l, serror = p:match("abc")
178assert(r == nil and l == 15 and serror == "abc")
179
180p = m.Rec(#(m.P("a") * m.T(22)), m.T(15), 22)
181r, l, serror = p:match("abc")
182assert(r == nil and l == 15 and serror == "bc")
183
184
176 185
177-- tests related to repetition 186-- tests related to repetition
178p = m.T(1)^0 187p = m.T(1)^0