aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSérgio Queiroz <sqmedeiros@gmail.com>2017-10-04 10:23:06 -0300
committerSérgio Queiroz <sqmedeiros@gmail.com>2017-10-04 10:23:06 -0300
commit54443578aaef5d2198ab772c7f5f53b330f80a7d (patch)
tree4bc60524f6b6027e14e77e761f8e5738e60ae23e
parent02dc7e194e8b26e267221ad10b7655645bd1d467 (diff)
downloadlpeglabel-54443578aaef5d2198ab772c7f5f53b330f80a7d.tar.gz
lpeglabel-54443578aaef5d2198ab772c7f5f53b330f80a7d.tar.bz2
lpeglabel-54443578aaef5d2198ab772c7f5f53b330f80a7d.zip
Returning the error position instead of a suffix of the original string (Issue 18)
-rw-r--r--lptree.c2
-rw-r--r--testlabel.lua382
2 files changed, 192 insertions, 192 deletions
diff --git a/lptree.c b/lptree.c
index 19d6b1a..810e267 100644
--- a/lptree.c
+++ b/lptree.c
@@ -1243,7 +1243,7 @@ static int lp_match (lua_State *L) {
1243 if (r == NULL) { /* labeled failure begin */ 1243 if (r == NULL) { /* labeled failure begin */
1244 lua_pushnil(L); 1244 lua_pushnil(L);
1245 lua_pushinteger(L, labelf); 1245 lua_pushinteger(L, labelf);
1246 lua_pushstring(L, sfail); /* Pushing the subject where the failure occurred */ 1246 lua_pushinteger(L, sfail - (s + i) + 1); /* subject position related to the error */
1247 return 3; 1247 return 3;
1248 } /* labeled failure end */ 1248 } /* labeled failure end */
1249 return getcaptures(L, s, r, ptop); 1249 return getcaptures(L, s, r, ptop);
diff --git a/testlabel.lua b/testlabel.lua
index a8439e4..2d9233a 100644
--- a/testlabel.lua
+++ b/testlabel.lua
@@ -1,6 +1,6 @@
1local m = require 'lpeglabel' 1local m = require 'lpeglabel'
2 2
3local p, r, l, s, serror 3local p, r, l, s, poserr
4 4
5local function checklabeq (x, ...) 5local function checklabeq (x, ...)
6 y = { ... } 6 y = { ... }
@@ -27,52 +27,52 @@ end
27p = m.P"a"^0 * m.P"b" + m.P"c" 27p = m.P"a"^0 * m.P"b" + m.P"c"
28checklabeq({4, nil, nil}, p:match("aabk")) 28checklabeq({4, nil, nil}, p:match("aabk"))
29checklabeq({2, nil, nil}, p:match("ck")) 29checklabeq({2, nil, nil}, p:match("ck"))
30checklabeq({nil, 0, "dk"}, p:match("dk")) 30checklabeq({nil, 0, 1}, p:match("dk"))
31checklabeq({nil, 0, "k"}, p:match("aak")) 31checklabeq({nil, 0, 3}, p:match("aak"))
32 32
33p = (m.P"a" + m.P"c")^0 * m.P"b" + m.P"c" 33p = (m.P"a" + m.P"c")^0 * m.P"b" + m.P"c"
34checklabeq({4, nil, nil}, p:match("aabk")) 34checklabeq({4, nil, nil}, p:match("aabk"))
35checklabeq({2, nil, nil}, p:match("ck")) 35checklabeq({2, nil, nil}, p:match("ck"))
36checklabeq({nil, 0, "dk"}, p:match("dk")) 36checklabeq({nil, 0, 1}, p:match("dk"))
37checklabeq({nil, 0, "k"}, p:match("aak")) 37checklabeq({nil, 0, 3}, p:match("aak"))
38 38
39p = m.P"a"^0 * m.P"b" + m.P(1)^0 * m.P(1) 39p = m.P"a"^0 * m.P"b" + m.P(1)^0 * m.P(1)
40checklabeq({4, nil, nil}, p:match("aabk")) 40checklabeq({4, nil, nil}, p:match("aabk"))
41checklabeq({nil, 0, ""}, p:match("ck")) 41checklabeq({nil, 0, 3}, p:match("ck"))
42checklabeq({nil, 0, ""}, p:match("aak")) 42checklabeq({nil, 0, 4}, p:match("aak"))
43 43
44p = m.P(1) * m.P"a" + m.P"c" 44p = m.P(1) * m.P"a" + m.P"c"
45checklabeq({3, nil, nil}, p:match("bac")) 45checklabeq({3, nil, nil}, p:match("bac"))
46checklabeq({2, nil, nil}, p:match("c")) 46checklabeq({2, nil, nil}, p:match("c"))
47checklabeq({nil, 0, ""}, p:match("x")) 47checklabeq({nil, 0, 2}, p:match("x"))
48checklabeq({nil, 0, "x"}, p:match("kx")) 48checklabeq({nil, 0, 2}, p:match("kx"))
49 49
50p = m.P"a"^0 * m.P(1) * m.P(1) + m.P"a"^0 * m.P"c" 50p = m.P"a"^0 * m.P(1) * m.P(1) + m.P"a"^0 * m.P"c"
51checklabeq({5, nil, nil}, p:match("aabc")) 51checklabeq({5, nil, nil}, p:match("aabc"))
52checklabeq({4, nil, nil}, p:match("aac")) 52checklabeq({4, nil, nil}, p:match("aac"))
53checklabeq({nil, 0, ""}, p:match("aak")) 53checklabeq({nil, 0, 4}, p:match("aak"))
54checklabeq({nil, 0, ""}, p:match("x")) 54checklabeq({nil, 0, 2}, p:match("x"))
55 55
56p = m.P"a"^0 * m.P(1) * m.P(1) + m.P"a"^0 * m.P"c" 56p = m.P"a"^0 * m.P(1) * m.P(1) + m.P"a"^0 * m.P"c"
57checklabeq({5, nil, nil}, p:match("aabc")) 57checklabeq({5, nil, nil}, p:match("aabc"))
58checklabeq({4, nil, nil}, p:match("aac")) 58checklabeq({4, nil, nil}, p:match("aac"))
59checklabeq({nil, 0, ""}, p:match("aak")) 59checklabeq({nil, 0, 4}, p:match("aak"))
60checklabeq({nil, 0, ""}, p:match("x")) 60checklabeq({nil, 0, 2}, p:match("x"))
61 61
62p = m.Cmt(m.P"a"^0, function() return nil end) + m.P"x" 62p = m.Cmt(m.P"a"^0, function() return nil end) + m.P"x"
63checklabeq({2, nil, nil}, p:match("xabc")) 63checklabeq({2, nil, nil}, p:match("xabc"))
64checklabeq({nil, 0, "c"}, p:match("aac")) 64checklabeq({nil, 0, 3}, p:match("aac"))
65checklabeq({nil, 0, "kx"}, p:match("kx")) 65checklabeq({nil, 0, 1}, p:match("kx"))
66 66
67p = m.P"b" * -m.P"a" + m.P"c" 67p = m.P"b" * -m.P"a" + m.P"c"
68checklabeq({nil, 0, "a"}, p:match("ba")) 68checklabeq({nil, 0, 2}, p:match("ba"))
69checklabeq({nil, 0, "kx"}, p:match("kx")) 69checklabeq({nil, 0, 1}, p:match("kx"))
70 70
71p = (m.P"c" + m.P"a") * m.P("b" + m.P"d") + m.P"xxx" 71p = (m.P"c" + m.P"a") * m.P("b" + m.P"d") + m.P"xxx"
72checklabeq({nil, 0, "kk"}, p:match("kk")) 72checklabeq({nil, 0, 1}, p:match("kk"))
73checklabeq({nil, 0, "k"}, p:match("ak")) 73checklabeq({nil, 0, 2}, p:match("ak"))
74checklabeq({nil, 0, "y"}, p:match("xxy")) 74checklabeq({nil, 0, 3}, p:match("xxy"))
75checklabeq({nil, 0, "yz"}, p:match("xyz")) 75checklabeq({nil, 0, 2}, p:match("xyz"))
76 76
77print"+" 77print"+"
78 78
@@ -80,13 +80,13 @@ print"+"
80-- throws a label 80-- throws a label
81p = m.T(1) 81p = m.T(1)
82s = "abc" 82s = "abc"
83r, l, serror = p:match(s) 83r, l, poserr = p:match(s)
84assert(r == nil and l == 1 and serror == "abc") 84assert(r == nil and l == 1 and poserr == 1)
85 85
86-- throws a label, choice does not catch labels 86-- throws a label, choice does not catch labels
87p = m.T(1) + m.P"a" 87p = m.T(1) + m.P"a"
88r, l, serror = p:match(s) 88r, l, poserr = p:match(s)
89assert(r == nil and l == 1 and serror == "abc") 89assert(r == nil and l == 1 and poserr == 1)
90 90
91-- again throws a label that is not caught by choice 91-- again throws a label that is not caught by choice
92local g = m.P{ 92local g = m.P{
@@ -95,22 +95,22 @@ local g = m.P{
95 A = m.T(1), 95 A = m.T(1),
96 B = m.P"a" 96 B = m.P"a"
97} 97}
98r, l, serror = g:match(s) 98r, l, poserr = g:match(s)
99assert(r == nil and l == 1 and serror == "abc") 99assert(r == nil and l == 1 and poserr == 1)
100 100
101-- throws a label in a position that is not the farthest one 101-- throws a label in a position that is not the farthest one
102-- but it is the position that should be reported 102-- but it is the position that should be reported
103p = m.P(1) * m.P"a" + m.T(11) 103p = m.P(1) * m.P"a" + m.T(11)
104checklabeq({3, nil, nil}, p:match("bac")) 104checklabeq({3, nil, nil}, p:match("bac"))
105checklabeq({nil, 11, "c"}, p:match("c")) 105checklabeq({nil, 11, 1}, p:match("c"))
106checklabeq({nil, 11, "x"}, p:match("x")) 106checklabeq({nil, 11, 1}, p:match("x"))
107checklabeq({nil, 11, "kx"}, p:match("kx")) 107checklabeq({nil, 11, 1}, p:match("kx"))
108 108
109 109
110-- throws a label that is not caught by the recovery operator 110-- throws a label that is not caught by the recovery operator
111p = m.Rec(m.T(2), m.P"a", 1, 3) 111p = m.Rec(m.T(2), m.P"a", 1, 3)
112r, l, serror = p:match(s) 112r, l, poserr = p:match(s)
113assert(r == nil and l == 2 and serror == "abc") 113assert(r == nil and l == 2 and poserr == 1)
114 114
115-- wraps the previous pattern with a recovery that catches label "2" 115-- wraps the previous pattern with a recovery that catches label "2"
116p = m.Rec(p, m.P"a", 2) 116p = m.Rec(p, m.P"a", 2)
@@ -123,14 +123,14 @@ assert(p:match(s) == 2)
123-- "fail" is label "0" 123-- "fail" is label "0"
124-- throws the "fail" label after the recovery 124-- throws the "fail" label after the recovery
125s = "bola" 125s = "bola"
126r, l, serror = p:match("bola") 126r, l, poserr = p:match("bola")
127assert(r == nil and l == 0 and serror == "bola") 127assert(r == nil and l == 0 and poserr == 1)
128 128
129-- Recovery does not catch "fail" by default 129-- Recovery does not catch "fail" by default
130p = m.Rec(m.P"b", m.P"a", 1) 130p = m.Rec(m.P"b", m.P"a", 1)
131 131
132r, l, serror = p:match("abc") 132r, l, poserr = p:match("abc")
133assert(r == nil and l == 0 and serror == "abc") 133assert(r == nil and l == 0 and poserr == 1)
134 134
135assert(p:match("bola") == 2) 135assert(p:match("bola") == 2)
136 136
@@ -139,14 +139,14 @@ assert(p:match("bola") == 2)
139p = m.Rec((m.P"a" + m.T(1)) * m.T(3), (m.P"a" + m.P"b"), 1, 3) 139p = m.Rec((m.P"a" + m.T(1)) * m.T(3), (m.P"a" + m.P"b"), 1, 3)
140assert(p:match("aac") == 3) 140assert(p:match("aac") == 3)
141assert(p:match("abc") == 3) 141assert(p:match("abc") == 3)
142r, l, serror = p:match("acc") 142r, l, poserr = p:match("acc")
143assert(r == nil and l == 0 and serror == "cc") 143assert(r == nil and l == 0 and poserr == 2)
144 144
145--throws 1, recovery pattern matches 'b', throw 3, and rec pat mathces 'a' 145--throws 1, recovery pattern matches 'b', throw 3, and rec pat mathces 'a'
146assert(p:match("bac") == 3) 146assert(p:match("bac") == 3)
147 147
148r, l, serror = p:match("cab") 148r, l, poserr = p:match("cab")
149assert(r == nil and l == 0 and serror == "cab") 149assert(r == nil and l == 0 and poserr == 1)
150 150
151 151
152-- associativity 152-- associativity
@@ -157,8 +157,8 @@ p = m.Rec(m.Rec(m.P"a" + m.T(1), m.P"b" + m.T(2), 1), m.P"c", 2)
157assert(p:match("abc") == 2) 157assert(p:match("abc") == 2)
158assert(p:match("bac") == 2) 158assert(p:match("bac") == 2)
159assert(p:match("cab") == 2) 159assert(p:match("cab") == 2)
160r, l, serror = p:match("dab") 160r, l, poserr = p:match("dab")
161assert(r == nil and l == 0 and serror == "dab") 161assert(r == nil and l == 0 and poserr == 1)
162 162
163 163
164-- righ-associativity 164-- righ-associativity
@@ -167,8 +167,8 @@ p = m.Rec(m.P"a" + m.T(1), m.Rec(m.P"b" + m.T(2), m.P"c", 2), 1)
167assert(p:match("abc") == 2) 167assert(p:match("abc") == 2)
168assert(p:match("bac") == 2) 168assert(p:match("bac") == 2)
169assert(p:match("cab") == 2) 169assert(p:match("cab") == 2)
170r, l, serror = p:match("dab") 170r, l, poserr = p:match("dab")
171assert(r == nil and l == 0 and serror == "dab") 171assert(r == nil and l == 0 and poserr == 1)
172 172
173 173
174-- associativity -> in this case the error thrown by p1 is only 174-- associativity -> in this case the error thrown by p1 is only
@@ -178,50 +178,50 @@ assert(r == nil and l == 0 and serror == "dab")
178-- ("a" //{1} "b") //{2} "c" 178-- ("a" //{1} "b") //{2} "c"
179p = m.Rec(m.Rec(m.P"a" + m.T(2), m.P"b" + m.T(2), 1), m.P"c", 2) 179p = m.Rec(m.Rec(m.P"a" + m.T(2), m.P"b" + m.T(2), 1), m.P"c", 2)
180assert(p:match("abc") == 2) 180assert(p:match("abc") == 2)
181r, l, serror = p:match("bac") 181r, l, poserr = p:match("bac")
182assert(r == nil and l == 0 and serror == "bac") 182assert(r == nil and l == 0 and poserr == 1)
183assert(p:match("cab") == 2) 183assert(p:match("cab") == 2)
184r, l, serror = p:match("dab") 184r, l, poserr = p:match("dab")
185assert(r == nil and l == 0 and serror == "dab") 185assert(r == nil and l == 0 and poserr == 1)
186 186
187 187
188-- righ-associativity 188-- righ-associativity
189-- "a" //{1} ("b" //{2} "c") 189-- "a" //{1} ("b" //{2} "c")
190p = m.Rec(m.P"a" + m.T(2), m.Rec(m.P"b" + m.T(2), m.P"c", 2), 1) 190p = m.Rec(m.P"a" + m.T(2), m.Rec(m.P"b" + m.T(2), m.P"c", 2), 1)
191assert(p:match("abc") == 2) 191assert(p:match("abc") == 2)
192r, l, serror = p:match("bac") 192r, l, poserr = p:match("bac")
193assert(r == nil and l == 2 and serror == "bac") 193assert(r == nil and l == 2 and poserr == 1)
194r, l, serror = p:match("cab") 194r, l, poserr = p:match("cab")
195assert(r == nil and l == 2 and serror == "cab") 195assert(r == nil and l == 2 and poserr == 1)
196r, l, serror = p:match("dab") 196r, l, poserr = p:match("dab")
197assert(r == nil and l == 2 and serror == "dab") 197assert(r == nil and l == 2 and poserr == 1)
198 198
199 199
200 200
201-- tests related to predicates 201-- tests related to predicates
202p = #m.T(1) + m.P"a" 202p = #m.T(1) + m.P"a"
203r, l, serror = p:match("abc") 203r, l, poserr = p:match("abc")
204assert(r == nil and l == 1 and serror == "abc") 204assert(r == nil and l == 1 and poserr == 1)
205 205
206p = ##m.T(1) + m.P"a" 206p = ##m.T(1) + m.P"a"
207r, l, serror = p:match("abc") 207r, l, poserr = p:match("abc")
208assert(r == nil and l == 1 and serror == "abc") 208assert(r == nil and l == 1 and poserr == 1)
209 209
210p = -m.T(1) * m.P"a" 210p = -m.T(1) * m.P"a"
211r, l, serror = p:match("abc") 211r, l, poserr = p:match("abc")
212assert(r == nil and l == 1 and serror == "abc") 212assert(r == nil and l == 1 and poserr == 1)
213 213
214p = -m.T(1) * m.P"a" 214p = -m.T(1) * m.P"a"
215r, l, serror = p:match("bbc") 215r, l, poserr = p:match("bbc")
216assert(r == nil and l == 1 and serror == "bbc") 216assert(r == nil and l == 1 and poserr == 1)
217 217
218p = -(-m.T(1)) * m.P"a" 218p = -(-m.T(1)) * m.P"a"
219r, l, serror = p:match("abc") 219r, l, poserr = p:match("abc")
220assert(r == nil and l == 1 and serror == "abc") 220assert(r == nil and l == 1 and poserr == 1)
221 221
222p = m.Rec(-m.T(22), m.P"a", 22) 222p = m.Rec(-m.T(22), m.P"a", 22)
223r, l, serror = p:match("abc") 223r, l, poserr = p:match("abc")
224assert(r == nil and l == 0 and serror == "bc") 224assert(r == nil and l == 0 and poserr == 2)
225 225
226assert(p:match("bbc") == 1) 226assert(p:match("bbc") == 1)
227 227
@@ -235,16 +235,16 @@ p = m.Rec(m.T(22), #m.P"a", 22)
235assert(p:match("abc") == 1) 235assert(p:match("abc") == 1)
236 236
237p = m.Rec(#m.T(22), m.P"a", 22) 237p = m.Rec(#m.T(22), m.P"a", 22)
238r, l, serror = p:match("bbc") 238r, l, poserr = p:match("bbc")
239assert(r == nil and l == 0 and serror == "bbc") 239assert(r == nil and l == 0 and poserr == 1)
240 240
241p = m.Rec(#m.P("a") * m.T(22), m.T(15), 22) 241p = m.Rec(#m.P("a") * m.T(22), m.T(15), 22)
242r, l, serror = p:match("abc") 242r, l, poserr = p:match("abc")
243assert(r == nil and l == 15 and serror == "abc") 243assert(r == nil and l == 15 and poserr == 1)
244 244
245p = m.Rec(#(m.P("a") * m.T(22)), m.T(15), 22) 245p = m.Rec(#(m.P("a") * m.T(22)), m.T(15), 22)
246r, l, serror = p:match("abc") 246r, l, poserr = p:match("abc")
247assert(r == nil and l == 15 and serror == "bc") 247assert(r == nil and l == 15 and poserr == 2)
248 248
249p = m.Lc(#m.T(22), m.P"a", 22) 249p = m.Lc(#m.T(22), m.P"a", 22)
250assert(p:match("abc") == 2) 250assert(p:match("abc") == 2)
@@ -256,27 +256,27 @@ p = m.Lc(m.T(22), #m.P"a", 22)
256assert(p:match("abc") == 1) 256assert(p:match("abc") == 1)
257 257
258p = m.Lc(#m.T(22), m.P"a", 22) 258p = m.Lc(#m.T(22), m.P"a", 22)
259r, l, serror = p:match("bbc") 259r, l, poserr = p:match("bbc")
260assert(r == nil and l == 0 and serror == "bbc") 260assert(r == nil and l == 0 and poserr == 1)
261 261
262p = m.Lc(#m.P("a") * m.T(22), m.T(15), 22) 262p = m.Lc(#m.P("a") * m.T(22), m.T(15), 22)
263r, l, serror = p:match("abc") 263r, l, poserr = p:match("abc")
264assert(r == nil and l == 15 and serror == "abc") 264assert(r == nil and l == 15 and poserr == 1)
265 265
266p = m.Lc(#(m.P("a") * m.T(22)), m.T(15), 22) 266p = m.Lc(#(m.P("a") * m.T(22)), m.T(15), 22)
267r, l, serror = p:match("abc") 267r, l, poserr = p:match("abc")
268assert(r == nil and l == 15 and serror == "abc") 268assert(r == nil and l == 15 and poserr == 1)
269 269
270 270
271 271
272-- tests related to repetition 272-- tests related to repetition
273p = m.T(1)^0 273p = m.T(1)^0
274r, l, serror = p:match("ab") 274r, l, poserr = p:match("ab")
275assert(r == nil and l == 1 and serror == "ab") 275assert(r == nil and l == 1 and poserr == 1)
276 276
277p = (m.P"a" + m.T(1))^0 277p = (m.P"a" + m.T(1))^0
278r, l, serror = p:match("aa") 278r, l, poserr = p:match("aa")
279assert(r == nil and l == 1 and serror == "") 279assert(r == nil and l == 1 and poserr == 3)
280 280
281 281
282-- Bug reported by Matthew Allen 282-- Bug reported by Matthew Allen
@@ -311,8 +311,8 @@ g = m.P{
311 B = m.T(1), 311 B = m.T(1),
312} 312}
313assert(g:match("ab") == 2) 313assert(g:match("ab") == 2)
314r, l, serror = g:match("bc") 314r, l, poserr = g:match("bc")
315assert(r == nil and l == 0 and serror == "bc") 315assert(r == nil and l == 0 and poserr == 1)
316 316
317 317
318--[[ 318--[[
@@ -328,22 +328,22 @@ g = m.P{
328} 328}
329assert(g:match("a;a;") == 5) 329assert(g:match("a;a;") == 5)
330 330
331r, l, serror = g:match("a;a") 331r, l, poserr = g:match("a;a")
332assert(r == nil and l == 1 and serror == "") 332assert(r == nil and l == 1 and poserr == 4)
333 333
334 334
335-- %1 //{1,3} %2 //{2} 'a' 335-- %1 //{1,3} %2 //{2} 'a'
336p = m.Rec(m.Rec(m.T(1), m.T(2), 1, 3), m.P"a", 2) 336p = m.Rec(m.Rec(m.T(1), m.T(2), 1, 3), m.P"a", 2)
337assert(p:match("abc") == 2) 337assert(p:match("abc") == 2)
338 338
339r, l, serror = p:match("") 339r, l, poserr = p:match("")
340assert(r == nil and l == 0 and serror == "") 340assert(r == nil and l == 0 and poserr == 1)
341 341
342p = m.Rec(m.T(1), m.Rec(m.T(2), m.P"a", 2), 1, 3) 342p = m.Rec(m.T(1), m.Rec(m.T(2), m.P"a", 2), 1, 3)
343assert(p:match("abc") == 2) 343assert(p:match("abc") == 2)
344 344
345r, l, serror = p:match("") 345r, l, poserr = p:match("")
346assert(r == nil and l == 0 and serror == "") 346assert(r == nil and l == 0 and poserr == 1)
347 347
348-- labeled choice 348-- labeled choice
349--[[ 349--[[
@@ -358,8 +358,8 @@ g = m.P{
358 B = m.T(1), 358 B = m.T(1),
359} 359}
360assert(g:match("ab") == 2) 360assert(g:match("ab") == 2)
361r, l, serror = g:match("bc") 361r, l, poserr = g:match("bc")
362assert(r == nil and l == 0 and serror == "bc") 362assert(r == nil and l == 0 and poserr == 1)
363 363
364 364
365--[[ 365--[[
@@ -375,37 +375,37 @@ g = m.P{
375} 375}
376assert(g:match("a;a;") == 5) 376assert(g:match("a;a;") == 5)
377 377
378r, l, serror = g:match("a;a") 378r, l, poserr = g:match("a;a")
379assert(r == nil and l == 1 and serror == "") 379assert(r == nil and l == 1 and poserr == 4)
380 380
381 381
382-- %1 /{1,3} %2 /{2} 'a' 382-- %1 /{1,3} %2 /{2} 'a'
383p = m.Lc(m.Lc(m.T(1), m.T(2), 1, 3), m.P"a", 2) 383p = m.Lc(m.Lc(m.T(1), m.T(2), 1, 3), m.P"a", 2)
384assert(p:match("abc") == 2) 384assert(p:match("abc") == 2)
385 385
386r, l, serror = p:match("") 386r, l, poserr = p:match("")
387assert(r == nil and l == 0 and serror == "") 387assert(r == nil and l == 0 and poserr == 1)
388 388
389p = m.Lc(m.T(1), m.Lc(m.T(2), m.P"a", 2), 1, 3) 389p = m.Lc(m.T(1), m.Lc(m.T(2), m.P"a", 2), 1, 3)
390assert(p:match("abc") == 2) 390assert(p:match("abc") == 2)
391 391
392r, l, serror = p:match("") 392r, l, poserr = p:match("")
393assert(r == nil and l == 0 and serror == "") 393assert(r == nil and l == 0 and poserr == 1)
394 394
395 395
396-- Infinte Loop TODO: check the semantics 396-- Infinte Loop TODO: check the semantics
397-- %1 //{1} %1 397-- %1 //{1} %1
398p = m.Rec(m.T(1), m.T(1), 1) 398p = m.Rec(m.T(1), m.T(1), 1)
399--r, l, serror = p:match("ab") 399--r, l, poserr = p:match("ab")
400--assert(r == nil and l == 1 and serror == "ab") 400--assert(r == nil and l == 1 and poserr == "ab")
401 401
402-- %1 //{1} 'a' (!. / %1) 402-- %1 //{1} 'a' (!. / %1)
403p = m.Rec(m.T(1), m.P"a" * (-m.P(1) + m.T(1)), 1) 403p = m.Rec(m.T(1), m.P"a" * (-m.P(1) + m.T(1)), 1)
404r, l, serror = p:match("ab") 404r, l, poserr = p:match("ab")
405assert(r == nil and l == 0 and serror == "b") 405assert(r == nil and l == 0 and poserr == 2)
406 406
407r, l, serror = p:match("cd") 407r, l, poserr = p:match("cd")
408assert(r == nil and l == 0 and serror == "cd") 408assert(r == nil and l == 0 and poserr == 1)
409 409
410-- %1 //{1} . (!. / %1) 410-- %1 //{1} . (!. / %1)
411p = m.Rec(m.T(1), m.P(1) * (-m.P(1) + m.T(1)), 1) 411p = m.Rec(m.T(1), m.P(1) * (-m.P(1) + m.T(1)), 1)
@@ -447,8 +447,8 @@ assert(p:match("a") == 2)
447 447
448p = m.T(255) 448p = m.T(255)
449s = "abc" 449s = "abc"
450r, l, serror = p:match(s) 450r, l, poserr = p:match(s)
451assert(r == nil and l == 255 and serror == "abc") 451assert(r == nil and l == 255 and poserr == 1)
452 452
453 453
454 454
@@ -496,20 +496,20 @@ s = "unsigned a a"
496assert(g:match(s) == #s + 1) --4 496assert(g:match(s) == #s + 1) --4
497 497
498s = "b" 498s = "b"
499r, l, serror = g:match(s) 499r, l, poserr = g:match(s)
500assert(r == nil and l == 5 and serror == "b") 500assert(r == nil and l == 5 and poserr == 1)
501 501
502s = "unsigned" 502s = "unsigned"
503r, l, serror = g:match(s) 503r, l, poserr = g:match(s)
504assert(r == nil and l == 5 and serror == s) 504assert(r == nil and l == 5 and poserr == 1)
505 505
506s = "unsigned a" 506s = "unsigned a"
507r, l, serror = g:match(s) 507r, l, poserr = g:match(s)
508assert(r == nil and l == 5 and serror == s) 508assert(r == nil and l == 5 and poserr == 1)
509 509
510s = "unsigned int" 510s = "unsigned int"
511r, l, serror = g:match(s) 511r, l, poserr = g:match(s)
512assert(r == nil and l == 5 and serror == s) 512assert(r == nil and l == 5 and poserr == 1)
513 513
514 514
515print("+") 515print("+")
@@ -520,39 +520,39 @@ local re = require 'relabel'
520g = re.compile[['a' //{4,9} [a-z] 520g = re.compile[['a' //{4,9} [a-z]
521]] 521]]
522assert(g:match("a") == 2) 522assert(g:match("a") == 2)
523r, l, serror = g:match("b") 523r, l, poserr = g:match("b")
524assert(r == nil and l == 0 and serror == "b") 524assert(r == nil and l == 0 and poserr == 1)
525 525
526g = re.compile[['a' //{4,9} [a-f] //{5, 7} [a-z] 526g = re.compile[['a' //{4,9} [a-f] //{5, 7} [a-z]
527]] 527]]
528assert(g:match("a") == 2) 528assert(g:match("a") == 2)
529r, l, serror = g:match("b") 529r, l, poserr = g:match("b")
530assert(r == nil and l == 0 and serror == "b") 530assert(r == nil and l == 0 and poserr == 1)
531 531
532g = re.compile[[%{1} //{4,9} [a-z] 532g = re.compile[[%{1} //{4,9} [a-z]
533]] 533]]
534r, l, serror = g:match("a") 534r, l, poserr = g:match("a")
535assert(r == nil and l == 1 and serror == "a") 535assert(r == nil and l == 1 and poserr == 1)
536 536
537 537
538g = re.compile[[%{1} //{4,1} [a-f] 538g = re.compile[[%{1} //{4,1} [a-f]
539]] 539]]
540assert(g:match("a") == 2) 540assert(g:match("a") == 2)
541r, l, serror = g:match("h") 541r, l, poserr = g:match("h")
542assert(r == nil and l == 0 and serror == "h") 542assert(r == nil and l == 0 and poserr == 1)
543 543
544g = re.compile[[[a-f]%{9} //{4,9} [a-c]%{7} //{5, 7} [a-z] ]] 544g = re.compile[[[a-f]%{9} //{4,9} [a-c]%{7} //{5, 7} [a-z] ]]
545r, l, serror = g:match("a") 545r, l, poserr = g:match("a")
546assert(r == nil and l == 0 and serror == "") 546assert(r == nil and l == 0 and poserr == 2)
547r, l, serror = g:match("aa") 547r, l, poserr = g:match("aa")
548assert(r == nil and l == 0 and serror == "") 548assert(r == nil and l == 0 and poserr == 3)
549assert(g:match("aaa") == 4) 549assert(g:match("aaa") == 4)
550 550
551r, l, serror = g:match("ad") 551r, l, poserr = g:match("ad")
552assert(r == nil and l == 0 and serror == "d") 552assert(r == nil and l == 0 and poserr == 2)
553 553
554r, l, serror = g:match("g") 554r, l, poserr = g:match("g")
555assert(r == nil and l == 0 and serror == "g") 555assert(r == nil and l == 0 and poserr == 1)
556 556
557 557
558--[[ grammar based on Figure 8 of paper submitted to SCP (using the recovery operator) 558--[[ grammar based on Figure 8 of paper submitted to SCP (using the recovery operator)
@@ -584,17 +584,17 @@ assert(g:match(s) == #s + 1) --3
584s = "unsigned a a" 584s = "unsigned a a"
585assert(g:match(s) == #s + 1) --4 585assert(g:match(s) == #s + 1) --4
586s = "b" 586s = "b"
587r, l, serror = g:match(s) 587r, l, poserr = g:match(s)
588assert(r == nil and l == 5 and serror == s) 588assert(r == nil and l == 5 and poserr == 1)
589s = "unsigned" 589s = "unsigned"
590r, l, serror = g:match(s) 590r, l, poserr = g:match(s)
591assert(r == nil and l == 5 and serror == s) 591assert(r == nil and l == 5 and poserr == 1)
592s = "unsigned a" 592s = "unsigned a"
593r, l, serror = g:match(s) 593r, l, poserr = g:match(s)
594assert(r == nil and l == 5 and serror == s) 594assert(r == nil and l == 5 and poserr == 1)
595s = "unsigned int" 595s = "unsigned int"
596r, l, serror = g:match(s) 596r, l, poserr = g:match(s)
597assert(r == nil and l == 5 and serror == s) 597assert(r == nil and l == 5 and poserr == 1)
598 598
599 599
600------------------------------------------ 600------------------------------------------
@@ -604,8 +604,8 @@ assert(r == nil and l == 5 and serror == s)
604-- throws a label that is not caught by labeled choice 604-- throws a label that is not caught by labeled choice
605s = "abc" 605s = "abc"
606p = m.Lc(m.T(2), m.P"a", 1, 3) 606p = m.Lc(m.T(2), m.P"a", 1, 3)
607r, l, serror = p:match(s) 607r, l, poserr = p:match(s)
608assert(r == nil and l == 2 and serror == "abc") 608assert(r == nil and l == 2 and poserr == 1)
609 609
610-- modifies previous pattern 610-- modifies previous pattern
611-- adds another labeled choice to catch label "2" 611-- adds another labeled choice to catch label "2"
@@ -619,14 +619,14 @@ assert(p:match(s) == 2)
619-- "fail" is label "0" 619-- "fail" is label "0"
620-- throws the "fail" label that is not caught by the labeled choice 620-- throws the "fail" label that is not caught by the labeled choice
621s = "bola" 621s = "bola"
622r, l, serror = p:match("bola") 622r, l, poserr = p:match("bola")
623assert(r == nil and l == 0 and serror == "bola") 623assert(r == nil and l == 0 and poserr == 1)
624 624
625-- labeled choice does not catch "fail" 625-- labeled choice does not catch "fail"
626p = m.Lc(m.P"b", m.P"a", 1) 626p = m.Lc(m.P"b", m.P"a", 1)
627 627
628r, l, serror = p:match("abc") 628r, l, poserr = p:match("abc")
629assert(r == nil and l == 0 and serror == "abc") 629assert(r == nil and l == 0 and poserr == 1)
630assert(p:match("bola") == 2) 630assert(p:match("bola") == 2)
631 631
632-- labeled choice catches "1" or "3" 632-- labeled choice catches "1" or "3"
@@ -642,8 +642,8 @@ p = m.Lc(m.Lc(m.P"a" + m.T(1), m.P"b" + m.T(2), 1), m.P"c", 2)
642assert(p:match("abc") == 2) 642assert(p:match("abc") == 2)
643assert(p:match("bac") == 2) 643assert(p:match("bac") == 2)
644assert(p:match("cab") == 2) 644assert(p:match("cab") == 2)
645r, l, serror = p:match("dab") 645r, l, poserr = p:match("dab")
646assert(r == nil and l == 0 and serror == "dab") 646assert(r == nil and l == 0 and poserr == 1)
647 647
648 648
649-- righ-associativity 649-- righ-associativity
@@ -652,8 +652,8 @@ p = m.Lc(m.P"a" + m.T(1), m.Lc(m.P"b" + m.T(2), m.P"c", 2), 1)
652assert(p:match("abc") == 2) 652assert(p:match("abc") == 2)
653assert(p:match("bac") == 2) 653assert(p:match("bac") == 2)
654assert(p:match("cab") == 2) 654assert(p:match("cab") == 2)
655r, l, serror = p:match("dab") 655r, l, poserr = p:match("dab")
656assert(r == nil and l == 0 and serror == "dab") 656assert(r == nil and l == 0 and poserr == 1)
657 657
658 658
659-- associativity -> in this case the error thrown by p1 is only 659-- associativity -> in this case the error thrown by p1 is only
@@ -663,23 +663,23 @@ assert(r == nil and l == 0 and serror == "dab")
663-- ("a" /{1} "b") /{2} "c" 663-- ("a" /{1} "b") /{2} "c"
664p = m.Lc(m.Lc(m.P"a" + m.T(2), m.P"b" + m.T(2), 1), m.P"c", 2) 664p = m.Lc(m.Lc(m.P"a" + m.T(2), m.P"b" + m.T(2), 1), m.P"c", 2)
665assert(p:match("abc") == 2) 665assert(p:match("abc") == 2)
666r, l, serror = p:match("bac") 666r, l, poserr = p:match("bac")
667assert(r == nil and l == 0 and serror == "bac") 667assert(r == nil and l == 0 and poserr == 1)
668assert(p:match("cab") == 2) 668assert(p:match("cab") == 2)
669r, l, serror = p:match("dab") 669r, l, poserr = p:match("dab")
670assert(r == nil and l == 0 and serror == "dab") 670assert(r == nil and l == 0 and poserr == 1)
671 671
672 672
673-- righ-associativity 673-- righ-associativity
674-- "a" /{1} ("b" /{2} "c") 674-- "a" /{1} ("b" /{2} "c")
675p = m.Lc(m.P"a" + m.T(2), m.Lc(m.P"b" + m.T(2), m.P"c", 2), 1) 675p = m.Lc(m.P"a" + m.T(2), m.Lc(m.P"b" + m.T(2), m.P"c", 2), 1)
676assert(p:match("abc") == 2) 676assert(p:match("abc") == 2)
677r, l, serror = p:match("bac") 677r, l, poserr = p:match("bac")
678assert(r == nil and l == 2 and serror == "bac") 678assert(r == nil and l == 2 and poserr == 1)
679r, l, serror = p:match("cab") 679r, l, poserr = p:match("cab")
680assert(r == nil and l == 2 and serror == "cab") 680assert(r == nil and l == 2 and poserr == 1)
681r, l, serror = p:match("dab") 681r, l, poserr = p:match("dab")
682assert(r == nil and l == 2 and serror == "dab") 682assert(r == nil and l == 2 and poserr == 1)
683 683
684 684
685 685
@@ -713,17 +713,17 @@ assert(g:match(s) == #s + 1) --3
713s = "unsigned a a" 713s = "unsigned a a"
714assert(g:match(s) == #s + 1) --4 714assert(g:match(s) == #s + 1) --4
715s = "b" 715s = "b"
716r, l, serror = g:match(s) 716r, l, poserr = g:match(s)
717assert(r == nil and l == 5 and serror == s) 717assert(r == nil and l == 5 and poserr == 1)
718s = "unsigned" 718s = "unsigned"
719r, l, serror = g:match(s) 719r, l, poserr = g:match(s)
720assert(r == nil and l == 5 and serror == s) 720assert(r == nil and l == 5 and poserr == 1)
721s = "unsigned a" 721s = "unsigned a"
722r, l, serror = g:match(s) 722r, l, poserr = g:match(s)
723assert(r == nil and l == 5 and serror == s) 723assert(r == nil and l == 5 and poserr == 1)
724s = "unsigned int" 724s = "unsigned int"
725r, l, serror = g:match(s) 725r, l, poserr = g:match(s)
726assert(r == nil and l == 5 and serror == s) 726assert(r == nil and l == 5 and poserr == 1)
727 727
728 728
729 729
@@ -933,15 +933,15 @@ print("+")
933 933
934p = m.Rec("a", "b", 3) 934p = m.Rec("a", "b", 3)
935assert(p:match("a") == 2) 935assert(p:match("a") == 2)
936checklabeq({nil, 0, "b"}, p:match("b")) 936checklabeq({nil, 0, 1}, p:match("b"))
937checklabeq({nil, 0, "c"}, p:match("c")) 937checklabeq({nil, 0, 1}, p:match("c"))
938 938
939p = m.Rec(m.T(3), "b", 1) 939p = m.Rec(m.T(3), "b", 1)
940checklabeq({nil, 3, "a"}, p:match("a")) 940checklabeq({nil, 3, 1}, p:match("a"))
941checklabeq({nil, 3, "b"}, p:match("b")) 941checklabeq({nil, 3, 1}, p:match("b"))
942 942
943p = m.Rec(m.T(3), "b", 3) 943p = m.Rec(m.T(3), "b", 3)
944checklabeq({nil, 0, "a"}, p:match("a")) 944checklabeq({nil, 0, 1}, p:match("a"))
945assert(p:match("b") == 2) 945assert(p:match("b") == 2)
946 946
947--[[ 947--[[
@@ -960,11 +960,11 @@ assert(g:match("abc") == 4)
960assert(g:match("aabc") == 5) 960assert(g:match("aabc") == 5)
961assert(g:match("aadc") == 5) 961assert(g:match("aadc") == 5)
962assert(g:match("dc") == 3) 962assert(g:match("dc") == 3)
963checklabeq({nil, 0, "bc"}, g:match("bbc")) 963checklabeq({nil, 0, 2}, g:match("bbc"))
964assert(g:match("xxc") == 4) 964assert(g:match("xxc") == 4)
965assert(g:match("c") == 2) 965assert(g:match("c") == 2)
966checklabeq({nil, 0, ""}, g:match("fail")) 966checklabeq({nil, 0, 5}, g:match("fail"))
967checklabeq({nil, 0, ""}, g:match("aaxx")) 967checklabeq({nil, 0, 5}, g:match("aaxx"))
968 968
969 969
970--[[ 970--[[
@@ -982,14 +982,14 @@ g = m.P{
982assert(g:match("abc") == 4) 982assert(g:match("abc") == 4)
983assert(g:match("aabc") == 5) 983assert(g:match("aabc") == 5)
984assert(g:match("aadc") == 5) 984assert(g:match("aadc") == 5)
985checklabeq({nil, 0, "bc"}, g:match("bc")) 985checklabeq({nil, 0, 1}, g:match("bc"))
986checklabeq({nil, 0, "bbc"}, g:match("bbc")) 986checklabeq({nil, 0, 1}, g:match("bbc"))
987checklabeq({nil, 0, "b"}, g:match("abb")) 987checklabeq({nil, 0, 3}, g:match("abb"))
988checklabeq({nil, 0, ""}, g:match("axx")) 988checklabeq({nil, 0, 4}, g:match("axx"))
989assert(g:match("accc") == 5) 989assert(g:match("accc") == 5)
990assert(g:match("axxc") == 5) 990assert(g:match("axxc") == 5)
991checklabeq({nil, 0, "c"}, g:match("c")) 991checklabeq({nil, 0, 1}, g:match("c"))
992checklabeq({nil, 0, "fail"}, g:match("fail")) 992checklabeq({nil, 0, 1}, g:match("fail"))
993 993
994 994
995 995
@@ -1114,10 +1114,10 @@ local g = m.P{
1114assert(g:match("a.") == 3) 1114assert(g:match("a.") == 3)
1115assert(g:match("aa.") == 4) 1115assert(g:match("aa.") == 4)
1116assert(g:match(".") == 2) 1116assert(g:match(".") == 2)
1117checklabeq({nil, 1, "ba."}, g:match("ba.")) 1117checklabeq({nil, 1, 1}, g:match("ba."))
1118checklabeq({nil, 1, "ba."}, g:match("aba.")) 1118checklabeq({nil, 1, 2}, g:match("aba."))
1119checklabeq({nil, 1, "cba."}, g:match("cba.")) 1119checklabeq({nil, 1, 1}, g:match("cba."))
1120checklabeq({nil, 2, "a"}, g:match("a.a")) 1120checklabeq({nil, 2, 3}, g:match("a.a"))
1121 1121
1122 1122
1123local g2 = m.P{ 1123local g2 = m.P{
@@ -1131,8 +1131,8 @@ assert(g2:match("aa.") == 4)
1131assert(g2:match(".") == 2) 1131assert(g2:match(".") == 2)
1132assert(g2:match("ba.") == 4) 1132assert(g2:match("ba.") == 4)
1133assert(g2:match("aba.") == 5) 1133assert(g2:match("aba.") == 5)
1134checklabeq({nil, 3, "cba."}, g2:match("cba.")) 1134checklabeq({nil, 3, 1}, g2:match("cba."))
1135checklabeq({nil, 2, "a"}, g2:match("a.a")) 1135checklabeq({nil, 2, 3}, g2:match("a.a"))
1136 1136
1137local g3 = m.P{ 1137local g3 = m.P{
1138 "S", 1138 "S",
@@ -1146,9 +1146,9 @@ assert(g3:match(".") == 2)
1146assert(g3:match("ba.") == 4) 1146assert(g3:match("ba.") == 4)
1147assert(g3:match("aba.") == 5) 1147assert(g3:match("aba.") == 5)
1148assert(g3:match("cba.") == 5) 1148assert(g3:match("cba.") == 5)
1149checklabeq({nil, 4, "a"}, g3:match("a.a")) 1149checklabeq({nil, 4, 3}, g3:match("a.a"))
1150checklabeq({nil, 4, "dc"}, g3:match("dc")) 1150checklabeq({nil, 4, 1}, g3:match("dc"))
1151checklabeq({nil, 4, "d"}, g3:match(".d")) 1151checklabeq({nil, 4, 2}, g3:match(".d"))
1152 1152
1153 1153
1154-- testing more captures 1154-- testing more captures
@@ -1158,7 +1158,7 @@ local g = re.compile[[
1158]] 1158]]
1159 1159
1160checkeq({"523", "624", "346", "888"} , {g:match("523 624 346\n888")}) 1160checkeq({"523", "624", "346", "888"} , {g:match("523 624 346\n888")})
1161checkeq({nil, 5, "a 123"}, {g:match("44 a 123")}) 1161checkeq({nil, 5, 4}, {g:match("44 a 123")})
1162 1162
1163local g2 = m.Rec(g, ((-m.R("09") * m.P(1))^0) / "58", 5) 1163local g2 = m.Rec(g, ((-m.R("09") * m.P(1))^0) / "58", 5)
1164 1164
@@ -1172,7 +1172,7 @@ local g = re.compile[[
1172]] 1172]]
1173 1173
1174checkeq({"523", "624", "346", "888"} , {g:match("523 624 346\n888")}) 1174checkeq({"523", "624", "346", "888"} , {g:match("523 624 346\n888")})
1175checkeq({nil, 5, "a 123"}, {g:match("44 a 123")}) 1175checkeq({nil, 5, 4}, {g:match("44 a 123")})
1176 1176
1177local g2 = m.Rec(g, ((-m.R("09") * m.P(1))^0) / "58", 5) 1177local g2 = m.Rec(g, ((-m.R("09") * m.P(1))^0) / "58", 5)
1178 1178