diff options
| author | Sérgio Queiroz <sqmedeiros@gmail.com> | 2017-10-04 10:23:06 -0300 |
|---|---|---|
| committer | Sérgio Queiroz <sqmedeiros@gmail.com> | 2017-10-04 10:23:06 -0300 |
| commit | 54443578aaef5d2198ab772c7f5f53b330f80a7d (patch) | |
| tree | 4bc60524f6b6027e14e77e761f8e5738e60ae23e | |
| parent | 02dc7e194e8b26e267221ad10b7655645bd1d467 (diff) | |
| download | lpeglabel-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.c | 2 | ||||
| -rw-r--r-- | testlabel.lua | 382 |
2 files changed, 192 insertions, 192 deletions
| @@ -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 @@ | |||
| 1 | local m = require 'lpeglabel' | 1 | local m = require 'lpeglabel' |
| 2 | 2 | ||
| 3 | local p, r, l, s, serror | 3 | local p, r, l, s, poserr |
| 4 | 4 | ||
| 5 | local function checklabeq (x, ...) | 5 | local function checklabeq (x, ...) |
| 6 | y = { ... } | 6 | y = { ... } |
| @@ -27,52 +27,52 @@ end | |||
| 27 | p = m.P"a"^0 * m.P"b" + m.P"c" | 27 | p = m.P"a"^0 * m.P"b" + m.P"c" |
| 28 | checklabeq({4, nil, nil}, p:match("aabk")) | 28 | checklabeq({4, nil, nil}, p:match("aabk")) |
| 29 | checklabeq({2, nil, nil}, p:match("ck")) | 29 | checklabeq({2, nil, nil}, p:match("ck")) |
| 30 | checklabeq({nil, 0, "dk"}, p:match("dk")) | 30 | checklabeq({nil, 0, 1}, p:match("dk")) |
| 31 | checklabeq({nil, 0, "k"}, p:match("aak")) | 31 | checklabeq({nil, 0, 3}, p:match("aak")) |
| 32 | 32 | ||
| 33 | p = (m.P"a" + m.P"c")^0 * m.P"b" + m.P"c" | 33 | p = (m.P"a" + m.P"c")^0 * m.P"b" + m.P"c" |
| 34 | checklabeq({4, nil, nil}, p:match("aabk")) | 34 | checklabeq({4, nil, nil}, p:match("aabk")) |
| 35 | checklabeq({2, nil, nil}, p:match("ck")) | 35 | checklabeq({2, nil, nil}, p:match("ck")) |
| 36 | checklabeq({nil, 0, "dk"}, p:match("dk")) | 36 | checklabeq({nil, 0, 1}, p:match("dk")) |
| 37 | checklabeq({nil, 0, "k"}, p:match("aak")) | 37 | checklabeq({nil, 0, 3}, p:match("aak")) |
| 38 | 38 | ||
| 39 | p = m.P"a"^0 * m.P"b" + m.P(1)^0 * m.P(1) | 39 | p = m.P"a"^0 * m.P"b" + m.P(1)^0 * m.P(1) |
| 40 | checklabeq({4, nil, nil}, p:match("aabk")) | 40 | checklabeq({4, nil, nil}, p:match("aabk")) |
| 41 | checklabeq({nil, 0, ""}, p:match("ck")) | 41 | checklabeq({nil, 0, 3}, p:match("ck")) |
| 42 | checklabeq({nil, 0, ""}, p:match("aak")) | 42 | checklabeq({nil, 0, 4}, p:match("aak")) |
| 43 | 43 | ||
| 44 | p = m.P(1) * m.P"a" + m.P"c" | 44 | p = m.P(1) * m.P"a" + m.P"c" |
| 45 | checklabeq({3, nil, nil}, p:match("bac")) | 45 | checklabeq({3, nil, nil}, p:match("bac")) |
| 46 | checklabeq({2, nil, nil}, p:match("c")) | 46 | checklabeq({2, nil, nil}, p:match("c")) |
| 47 | checklabeq({nil, 0, ""}, p:match("x")) | 47 | checklabeq({nil, 0, 2}, p:match("x")) |
| 48 | checklabeq({nil, 0, "x"}, p:match("kx")) | 48 | checklabeq({nil, 0, 2}, p:match("kx")) |
| 49 | 49 | ||
| 50 | p = m.P"a"^0 * m.P(1) * m.P(1) + m.P"a"^0 * m.P"c" | 50 | p = m.P"a"^0 * m.P(1) * m.P(1) + m.P"a"^0 * m.P"c" |
| 51 | checklabeq({5, nil, nil}, p:match("aabc")) | 51 | checklabeq({5, nil, nil}, p:match("aabc")) |
| 52 | checklabeq({4, nil, nil}, p:match("aac")) | 52 | checklabeq({4, nil, nil}, p:match("aac")) |
| 53 | checklabeq({nil, 0, ""}, p:match("aak")) | 53 | checklabeq({nil, 0, 4}, p:match("aak")) |
| 54 | checklabeq({nil, 0, ""}, p:match("x")) | 54 | checklabeq({nil, 0, 2}, p:match("x")) |
| 55 | 55 | ||
| 56 | p = m.P"a"^0 * m.P(1) * m.P(1) + m.P"a"^0 * m.P"c" | 56 | p = m.P"a"^0 * m.P(1) * m.P(1) + m.P"a"^0 * m.P"c" |
| 57 | checklabeq({5, nil, nil}, p:match("aabc")) | 57 | checklabeq({5, nil, nil}, p:match("aabc")) |
| 58 | checklabeq({4, nil, nil}, p:match("aac")) | 58 | checklabeq({4, nil, nil}, p:match("aac")) |
| 59 | checklabeq({nil, 0, ""}, p:match("aak")) | 59 | checklabeq({nil, 0, 4}, p:match("aak")) |
| 60 | checklabeq({nil, 0, ""}, p:match("x")) | 60 | checklabeq({nil, 0, 2}, p:match("x")) |
| 61 | 61 | ||
| 62 | p = m.Cmt(m.P"a"^0, function() return nil end) + m.P"x" | 62 | p = m.Cmt(m.P"a"^0, function() return nil end) + m.P"x" |
| 63 | checklabeq({2, nil, nil}, p:match("xabc")) | 63 | checklabeq({2, nil, nil}, p:match("xabc")) |
| 64 | checklabeq({nil, 0, "c"}, p:match("aac")) | 64 | checklabeq({nil, 0, 3}, p:match("aac")) |
| 65 | checklabeq({nil, 0, "kx"}, p:match("kx")) | 65 | checklabeq({nil, 0, 1}, p:match("kx")) |
| 66 | 66 | ||
| 67 | p = m.P"b" * -m.P"a" + m.P"c" | 67 | p = m.P"b" * -m.P"a" + m.P"c" |
| 68 | checklabeq({nil, 0, "a"}, p:match("ba")) | 68 | checklabeq({nil, 0, 2}, p:match("ba")) |
| 69 | checklabeq({nil, 0, "kx"}, p:match("kx")) | 69 | checklabeq({nil, 0, 1}, p:match("kx")) |
| 70 | 70 | ||
| 71 | p = (m.P"c" + m.P"a") * m.P("b" + m.P"d") + m.P"xxx" | 71 | p = (m.P"c" + m.P"a") * m.P("b" + m.P"d") + m.P"xxx" |
| 72 | checklabeq({nil, 0, "kk"}, p:match("kk")) | 72 | checklabeq({nil, 0, 1}, p:match("kk")) |
| 73 | checklabeq({nil, 0, "k"}, p:match("ak")) | 73 | checklabeq({nil, 0, 2}, p:match("ak")) |
| 74 | checklabeq({nil, 0, "y"}, p:match("xxy")) | 74 | checklabeq({nil, 0, 3}, p:match("xxy")) |
| 75 | checklabeq({nil, 0, "yz"}, p:match("xyz")) | 75 | checklabeq({nil, 0, 2}, p:match("xyz")) |
| 76 | 76 | ||
| 77 | print"+" | 77 | print"+" |
| 78 | 78 | ||
| @@ -80,13 +80,13 @@ print"+" | |||
| 80 | -- throws a label | 80 | -- throws a label |
| 81 | p = m.T(1) | 81 | p = m.T(1) |
| 82 | s = "abc" | 82 | s = "abc" |
| 83 | r, l, serror = p:match(s) | 83 | r, l, poserr = p:match(s) |
| 84 | assert(r == nil and l == 1 and serror == "abc") | 84 | assert(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 |
| 87 | p = m.T(1) + m.P"a" | 87 | p = m.T(1) + m.P"a" |
| 88 | r, l, serror = p:match(s) | 88 | r, l, poserr = p:match(s) |
| 89 | assert(r == nil and l == 1 and serror == "abc") | 89 | assert(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 |
| 92 | local g = m.P{ | 92 | local 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 | } |
| 98 | r, l, serror = g:match(s) | 98 | r, l, poserr = g:match(s) |
| 99 | assert(r == nil and l == 1 and serror == "abc") | 99 | assert(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 |
| 103 | p = m.P(1) * m.P"a" + m.T(11) | 103 | p = m.P(1) * m.P"a" + m.T(11) |
| 104 | checklabeq({3, nil, nil}, p:match("bac")) | 104 | checklabeq({3, nil, nil}, p:match("bac")) |
| 105 | checklabeq({nil, 11, "c"}, p:match("c")) | 105 | checklabeq({nil, 11, 1}, p:match("c")) |
| 106 | checklabeq({nil, 11, "x"}, p:match("x")) | 106 | checklabeq({nil, 11, 1}, p:match("x")) |
| 107 | checklabeq({nil, 11, "kx"}, p:match("kx")) | 107 | checklabeq({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 |
| 111 | p = m.Rec(m.T(2), m.P"a", 1, 3) | 111 | p = m.Rec(m.T(2), m.P"a", 1, 3) |
| 112 | r, l, serror = p:match(s) | 112 | r, l, poserr = p:match(s) |
| 113 | assert(r == nil and l == 2 and serror == "abc") | 113 | assert(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" |
| 116 | p = m.Rec(p, m.P"a", 2) | 116 | p = 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 |
| 125 | s = "bola" | 125 | s = "bola" |
| 126 | r, l, serror = p:match("bola") | 126 | r, l, poserr = p:match("bola") |
| 127 | assert(r == nil and l == 0 and serror == "bola") | 127 | assert(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 |
| 130 | p = m.Rec(m.P"b", m.P"a", 1) | 130 | p = m.Rec(m.P"b", m.P"a", 1) |
| 131 | 131 | ||
| 132 | r, l, serror = p:match("abc") | 132 | r, l, poserr = p:match("abc") |
| 133 | assert(r == nil and l == 0 and serror == "abc") | 133 | assert(r == nil and l == 0 and poserr == 1) |
| 134 | 134 | ||
| 135 | assert(p:match("bola") == 2) | 135 | assert(p:match("bola") == 2) |
| 136 | 136 | ||
| @@ -139,14 +139,14 @@ assert(p:match("bola") == 2) | |||
| 139 | p = m.Rec((m.P"a" + m.T(1)) * m.T(3), (m.P"a" + m.P"b"), 1, 3) | 139 | p = m.Rec((m.P"a" + m.T(1)) * m.T(3), (m.P"a" + m.P"b"), 1, 3) |
| 140 | assert(p:match("aac") == 3) | 140 | assert(p:match("aac") == 3) |
| 141 | assert(p:match("abc") == 3) | 141 | assert(p:match("abc") == 3) |
| 142 | r, l, serror = p:match("acc") | 142 | r, l, poserr = p:match("acc") |
| 143 | assert(r == nil and l == 0 and serror == "cc") | 143 | assert(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' |
| 146 | assert(p:match("bac") == 3) | 146 | assert(p:match("bac") == 3) |
| 147 | 147 | ||
| 148 | r, l, serror = p:match("cab") | 148 | r, l, poserr = p:match("cab") |
| 149 | assert(r == nil and l == 0 and serror == "cab") | 149 | assert(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) | |||
| 157 | assert(p:match("abc") == 2) | 157 | assert(p:match("abc") == 2) |
| 158 | assert(p:match("bac") == 2) | 158 | assert(p:match("bac") == 2) |
| 159 | assert(p:match("cab") == 2) | 159 | assert(p:match("cab") == 2) |
| 160 | r, l, serror = p:match("dab") | 160 | r, l, poserr = p:match("dab") |
| 161 | assert(r == nil and l == 0 and serror == "dab") | 161 | assert(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) | |||
| 167 | assert(p:match("abc") == 2) | 167 | assert(p:match("abc") == 2) |
| 168 | assert(p:match("bac") == 2) | 168 | assert(p:match("bac") == 2) |
| 169 | assert(p:match("cab") == 2) | 169 | assert(p:match("cab") == 2) |
| 170 | r, l, serror = p:match("dab") | 170 | r, l, poserr = p:match("dab") |
| 171 | assert(r == nil and l == 0 and serror == "dab") | 171 | assert(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" |
| 179 | p = m.Rec(m.Rec(m.P"a" + m.T(2), m.P"b" + m.T(2), 1), m.P"c", 2) | 179 | p = m.Rec(m.Rec(m.P"a" + m.T(2), m.P"b" + m.T(2), 1), m.P"c", 2) |
| 180 | assert(p:match("abc") == 2) | 180 | assert(p:match("abc") == 2) |
| 181 | r, l, serror = p:match("bac") | 181 | r, l, poserr = p:match("bac") |
| 182 | assert(r == nil and l == 0 and serror == "bac") | 182 | assert(r == nil and l == 0 and poserr == 1) |
| 183 | assert(p:match("cab") == 2) | 183 | assert(p:match("cab") == 2) |
| 184 | r, l, serror = p:match("dab") | 184 | r, l, poserr = p:match("dab") |
| 185 | assert(r == nil and l == 0 and serror == "dab") | 185 | assert(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") |
| 190 | p = m.Rec(m.P"a" + m.T(2), m.Rec(m.P"b" + m.T(2), m.P"c", 2), 1) | 190 | p = m.Rec(m.P"a" + m.T(2), m.Rec(m.P"b" + m.T(2), m.P"c", 2), 1) |
| 191 | assert(p:match("abc") == 2) | 191 | assert(p:match("abc") == 2) |
| 192 | r, l, serror = p:match("bac") | 192 | r, l, poserr = p:match("bac") |
| 193 | assert(r == nil and l == 2 and serror == "bac") | 193 | assert(r == nil and l == 2 and poserr == 1) |
| 194 | r, l, serror = p:match("cab") | 194 | r, l, poserr = p:match("cab") |
| 195 | assert(r == nil and l == 2 and serror == "cab") | 195 | assert(r == nil and l == 2 and poserr == 1) |
| 196 | r, l, serror = p:match("dab") | 196 | r, l, poserr = p:match("dab") |
| 197 | assert(r == nil and l == 2 and serror == "dab") | 197 | assert(r == nil and l == 2 and poserr == 1) |
| 198 | 198 | ||
| 199 | 199 | ||
| 200 | 200 | ||
| 201 | -- tests related to predicates | 201 | -- tests related to predicates |
| 202 | p = #m.T(1) + m.P"a" | 202 | p = #m.T(1) + m.P"a" |
| 203 | r, l, serror = p:match("abc") | 203 | r, l, poserr = p:match("abc") |
| 204 | assert(r == nil and l == 1 and serror == "abc") | 204 | assert(r == nil and l == 1 and poserr == 1) |
| 205 | 205 | ||
| 206 | p = ##m.T(1) + m.P"a" | 206 | p = ##m.T(1) + m.P"a" |
| 207 | r, l, serror = p:match("abc") | 207 | r, l, poserr = p:match("abc") |
| 208 | assert(r == nil and l == 1 and serror == "abc") | 208 | assert(r == nil and l == 1 and poserr == 1) |
| 209 | 209 | ||
| 210 | p = -m.T(1) * m.P"a" | 210 | p = -m.T(1) * m.P"a" |
| 211 | r, l, serror = p:match("abc") | 211 | r, l, poserr = p:match("abc") |
| 212 | assert(r == nil and l == 1 and serror == "abc") | 212 | assert(r == nil and l == 1 and poserr == 1) |
| 213 | 213 | ||
| 214 | p = -m.T(1) * m.P"a" | 214 | p = -m.T(1) * m.P"a" |
| 215 | r, l, serror = p:match("bbc") | 215 | r, l, poserr = p:match("bbc") |
| 216 | assert(r == nil and l == 1 and serror == "bbc") | 216 | assert(r == nil and l == 1 and poserr == 1) |
| 217 | 217 | ||
| 218 | p = -(-m.T(1)) * m.P"a" | 218 | p = -(-m.T(1)) * m.P"a" |
| 219 | r, l, serror = p:match("abc") | 219 | r, l, poserr = p:match("abc") |
| 220 | assert(r == nil and l == 1 and serror == "abc") | 220 | assert(r == nil and l == 1 and poserr == 1) |
| 221 | 221 | ||
| 222 | p = m.Rec(-m.T(22), m.P"a", 22) | 222 | p = m.Rec(-m.T(22), m.P"a", 22) |
| 223 | r, l, serror = p:match("abc") | 223 | r, l, poserr = p:match("abc") |
| 224 | assert(r == nil and l == 0 and serror == "bc") | 224 | assert(r == nil and l == 0 and poserr == 2) |
| 225 | 225 | ||
| 226 | assert(p:match("bbc") == 1) | 226 | assert(p:match("bbc") == 1) |
| 227 | 227 | ||
| @@ -235,16 +235,16 @@ p = m.Rec(m.T(22), #m.P"a", 22) | |||
| 235 | assert(p:match("abc") == 1) | 235 | assert(p:match("abc") == 1) |
| 236 | 236 | ||
| 237 | p = m.Rec(#m.T(22), m.P"a", 22) | 237 | p = m.Rec(#m.T(22), m.P"a", 22) |
| 238 | r, l, serror = p:match("bbc") | 238 | r, l, poserr = p:match("bbc") |
| 239 | assert(r == nil and l == 0 and serror == "bbc") | 239 | assert(r == nil and l == 0 and poserr == 1) |
| 240 | 240 | ||
| 241 | p = m.Rec(#m.P("a") * m.T(22), m.T(15), 22) | 241 | p = m.Rec(#m.P("a") * m.T(22), m.T(15), 22) |
| 242 | r, l, serror = p:match("abc") | 242 | r, l, poserr = p:match("abc") |
| 243 | assert(r == nil and l == 15 and serror == "abc") | 243 | assert(r == nil and l == 15 and poserr == 1) |
| 244 | 244 | ||
| 245 | p = m.Rec(#(m.P("a") * m.T(22)), m.T(15), 22) | 245 | p = m.Rec(#(m.P("a") * m.T(22)), m.T(15), 22) |
| 246 | r, l, serror = p:match("abc") | 246 | r, l, poserr = p:match("abc") |
| 247 | assert(r == nil and l == 15 and serror == "bc") | 247 | assert(r == nil and l == 15 and poserr == 2) |
| 248 | 248 | ||
| 249 | p = m.Lc(#m.T(22), m.P"a", 22) | 249 | p = m.Lc(#m.T(22), m.P"a", 22) |
| 250 | assert(p:match("abc") == 2) | 250 | assert(p:match("abc") == 2) |
| @@ -256,27 +256,27 @@ p = m.Lc(m.T(22), #m.P"a", 22) | |||
| 256 | assert(p:match("abc") == 1) | 256 | assert(p:match("abc") == 1) |
| 257 | 257 | ||
| 258 | p = m.Lc(#m.T(22), m.P"a", 22) | 258 | p = m.Lc(#m.T(22), m.P"a", 22) |
| 259 | r, l, serror = p:match("bbc") | 259 | r, l, poserr = p:match("bbc") |
| 260 | assert(r == nil and l == 0 and serror == "bbc") | 260 | assert(r == nil and l == 0 and poserr == 1) |
| 261 | 261 | ||
| 262 | p = m.Lc(#m.P("a") * m.T(22), m.T(15), 22) | 262 | p = m.Lc(#m.P("a") * m.T(22), m.T(15), 22) |
| 263 | r, l, serror = p:match("abc") | 263 | r, l, poserr = p:match("abc") |
| 264 | assert(r == nil and l == 15 and serror == "abc") | 264 | assert(r == nil and l == 15 and poserr == 1) |
| 265 | 265 | ||
| 266 | p = m.Lc(#(m.P("a") * m.T(22)), m.T(15), 22) | 266 | p = m.Lc(#(m.P("a") * m.T(22)), m.T(15), 22) |
| 267 | r, l, serror = p:match("abc") | 267 | r, l, poserr = p:match("abc") |
| 268 | assert(r == nil and l == 15 and serror == "abc") | 268 | assert(r == nil and l == 15 and poserr == 1) |
| 269 | 269 | ||
| 270 | 270 | ||
| 271 | 271 | ||
| 272 | -- tests related to repetition | 272 | -- tests related to repetition |
| 273 | p = m.T(1)^0 | 273 | p = m.T(1)^0 |
| 274 | r, l, serror = p:match("ab") | 274 | r, l, poserr = p:match("ab") |
| 275 | assert(r == nil and l == 1 and serror == "ab") | 275 | assert(r == nil and l == 1 and poserr == 1) |
| 276 | 276 | ||
| 277 | p = (m.P"a" + m.T(1))^0 | 277 | p = (m.P"a" + m.T(1))^0 |
| 278 | r, l, serror = p:match("aa") | 278 | r, l, poserr = p:match("aa") |
| 279 | assert(r == nil and l == 1 and serror == "") | 279 | assert(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 | } |
| 313 | assert(g:match("ab") == 2) | 313 | assert(g:match("ab") == 2) |
| 314 | r, l, serror = g:match("bc") | 314 | r, l, poserr = g:match("bc") |
| 315 | assert(r == nil and l == 0 and serror == "bc") | 315 | assert(r == nil and l == 0 and poserr == 1) |
| 316 | 316 | ||
| 317 | 317 | ||
| 318 | --[[ | 318 | --[[ |
| @@ -328,22 +328,22 @@ g = m.P{ | |||
| 328 | } | 328 | } |
| 329 | assert(g:match("a;a;") == 5) | 329 | assert(g:match("a;a;") == 5) |
| 330 | 330 | ||
| 331 | r, l, serror = g:match("a;a") | 331 | r, l, poserr = g:match("a;a") |
| 332 | assert(r == nil and l == 1 and serror == "") | 332 | assert(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' |
| 336 | p = m.Rec(m.Rec(m.T(1), m.T(2), 1, 3), m.P"a", 2) | 336 | p = m.Rec(m.Rec(m.T(1), m.T(2), 1, 3), m.P"a", 2) |
| 337 | assert(p:match("abc") == 2) | 337 | assert(p:match("abc") == 2) |
| 338 | 338 | ||
| 339 | r, l, serror = p:match("") | 339 | r, l, poserr = p:match("") |
| 340 | assert(r == nil and l == 0 and serror == "") | 340 | assert(r == nil and l == 0 and poserr == 1) |
| 341 | 341 | ||
| 342 | p = m.Rec(m.T(1), m.Rec(m.T(2), m.P"a", 2), 1, 3) | 342 | p = m.Rec(m.T(1), m.Rec(m.T(2), m.P"a", 2), 1, 3) |
| 343 | assert(p:match("abc") == 2) | 343 | assert(p:match("abc") == 2) |
| 344 | 344 | ||
| 345 | r, l, serror = p:match("") | 345 | r, l, poserr = p:match("") |
| 346 | assert(r == nil and l == 0 and serror == "") | 346 | assert(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 | } |
| 360 | assert(g:match("ab") == 2) | 360 | assert(g:match("ab") == 2) |
| 361 | r, l, serror = g:match("bc") | 361 | r, l, poserr = g:match("bc") |
| 362 | assert(r == nil and l == 0 and serror == "bc") | 362 | assert(r == nil and l == 0 and poserr == 1) |
| 363 | 363 | ||
| 364 | 364 | ||
| 365 | --[[ | 365 | --[[ |
| @@ -375,37 +375,37 @@ g = m.P{ | |||
| 375 | } | 375 | } |
| 376 | assert(g:match("a;a;") == 5) | 376 | assert(g:match("a;a;") == 5) |
| 377 | 377 | ||
| 378 | r, l, serror = g:match("a;a") | 378 | r, l, poserr = g:match("a;a") |
| 379 | assert(r == nil and l == 1 and serror == "") | 379 | assert(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' |
| 383 | p = m.Lc(m.Lc(m.T(1), m.T(2), 1, 3), m.P"a", 2) | 383 | p = m.Lc(m.Lc(m.T(1), m.T(2), 1, 3), m.P"a", 2) |
| 384 | assert(p:match("abc") == 2) | 384 | assert(p:match("abc") == 2) |
| 385 | 385 | ||
| 386 | r, l, serror = p:match("") | 386 | r, l, poserr = p:match("") |
| 387 | assert(r == nil and l == 0 and serror == "") | 387 | assert(r == nil and l == 0 and poserr == 1) |
| 388 | 388 | ||
| 389 | p = m.Lc(m.T(1), m.Lc(m.T(2), m.P"a", 2), 1, 3) | 389 | p = m.Lc(m.T(1), m.Lc(m.T(2), m.P"a", 2), 1, 3) |
| 390 | assert(p:match("abc") == 2) | 390 | assert(p:match("abc") == 2) |
| 391 | 391 | ||
| 392 | r, l, serror = p:match("") | 392 | r, l, poserr = p:match("") |
| 393 | assert(r == nil and l == 0 and serror == "") | 393 | assert(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 |
| 398 | p = m.Rec(m.T(1), m.T(1), 1) | 398 | p = 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) |
| 403 | p = m.Rec(m.T(1), m.P"a" * (-m.P(1) + m.T(1)), 1) | 403 | p = m.Rec(m.T(1), m.P"a" * (-m.P(1) + m.T(1)), 1) |
| 404 | r, l, serror = p:match("ab") | 404 | r, l, poserr = p:match("ab") |
| 405 | assert(r == nil and l == 0 and serror == "b") | 405 | assert(r == nil and l == 0 and poserr == 2) |
| 406 | 406 | ||
| 407 | r, l, serror = p:match("cd") | 407 | r, l, poserr = p:match("cd") |
| 408 | assert(r == nil and l == 0 and serror == "cd") | 408 | assert(r == nil and l == 0 and poserr == 1) |
| 409 | 409 | ||
| 410 | -- %1 //{1} . (!. / %1) | 410 | -- %1 //{1} . (!. / %1) |
| 411 | p = m.Rec(m.T(1), m.P(1) * (-m.P(1) + m.T(1)), 1) | 411 | p = 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 | ||
| 448 | p = m.T(255) | 448 | p = m.T(255) |
| 449 | s = "abc" | 449 | s = "abc" |
| 450 | r, l, serror = p:match(s) | 450 | r, l, poserr = p:match(s) |
| 451 | assert(r == nil and l == 255 and serror == "abc") | 451 | assert(r == nil and l == 255 and poserr == 1) |
| 452 | 452 | ||
| 453 | 453 | ||
| 454 | 454 | ||
| @@ -496,20 +496,20 @@ s = "unsigned a a" | |||
| 496 | assert(g:match(s) == #s + 1) --4 | 496 | assert(g:match(s) == #s + 1) --4 |
| 497 | 497 | ||
| 498 | s = "b" | 498 | s = "b" |
| 499 | r, l, serror = g:match(s) | 499 | r, l, poserr = g:match(s) |
| 500 | assert(r == nil and l == 5 and serror == "b") | 500 | assert(r == nil and l == 5 and poserr == 1) |
| 501 | 501 | ||
| 502 | s = "unsigned" | 502 | s = "unsigned" |
| 503 | r, l, serror = g:match(s) | 503 | r, l, poserr = g:match(s) |
| 504 | assert(r == nil and l == 5 and serror == s) | 504 | assert(r == nil and l == 5 and poserr == 1) |
| 505 | 505 | ||
| 506 | s = "unsigned a" | 506 | s = "unsigned a" |
| 507 | r, l, serror = g:match(s) | 507 | r, l, poserr = g:match(s) |
| 508 | assert(r == nil and l == 5 and serror == s) | 508 | assert(r == nil and l == 5 and poserr == 1) |
| 509 | 509 | ||
| 510 | s = "unsigned int" | 510 | s = "unsigned int" |
| 511 | r, l, serror = g:match(s) | 511 | r, l, poserr = g:match(s) |
| 512 | assert(r == nil and l == 5 and serror == s) | 512 | assert(r == nil and l == 5 and poserr == 1) |
| 513 | 513 | ||
| 514 | 514 | ||
| 515 | print("+") | 515 | print("+") |
| @@ -520,39 +520,39 @@ local re = require 'relabel' | |||
| 520 | g = re.compile[['a' //{4,9} [a-z] | 520 | g = re.compile[['a' //{4,9} [a-z] |
| 521 | ]] | 521 | ]] |
| 522 | assert(g:match("a") == 2) | 522 | assert(g:match("a") == 2) |
| 523 | r, l, serror = g:match("b") | 523 | r, l, poserr = g:match("b") |
| 524 | assert(r == nil and l == 0 and serror == "b") | 524 | assert(r == nil and l == 0 and poserr == 1) |
| 525 | 525 | ||
| 526 | g = re.compile[['a' //{4,9} [a-f] //{5, 7} [a-z] | 526 | g = re.compile[['a' //{4,9} [a-f] //{5, 7} [a-z] |
| 527 | ]] | 527 | ]] |
| 528 | assert(g:match("a") == 2) | 528 | assert(g:match("a") == 2) |
| 529 | r, l, serror = g:match("b") | 529 | r, l, poserr = g:match("b") |
| 530 | assert(r == nil and l == 0 and serror == "b") | 530 | assert(r == nil and l == 0 and poserr == 1) |
| 531 | 531 | ||
| 532 | g = re.compile[[%{1} //{4,9} [a-z] | 532 | g = re.compile[[%{1} //{4,9} [a-z] |
| 533 | ]] | 533 | ]] |
| 534 | r, l, serror = g:match("a") | 534 | r, l, poserr = g:match("a") |
| 535 | assert(r == nil and l == 1 and serror == "a") | 535 | assert(r == nil and l == 1 and poserr == 1) |
| 536 | 536 | ||
| 537 | 537 | ||
| 538 | g = re.compile[[%{1} //{4,1} [a-f] | 538 | g = re.compile[[%{1} //{4,1} [a-f] |
| 539 | ]] | 539 | ]] |
| 540 | assert(g:match("a") == 2) | 540 | assert(g:match("a") == 2) |
| 541 | r, l, serror = g:match("h") | 541 | r, l, poserr = g:match("h") |
| 542 | assert(r == nil and l == 0 and serror == "h") | 542 | assert(r == nil and l == 0 and poserr == 1) |
| 543 | 543 | ||
| 544 | g = re.compile[[[a-f]%{9} //{4,9} [a-c]%{7} //{5, 7} [a-z] ]] | 544 | g = re.compile[[[a-f]%{9} //{4,9} [a-c]%{7} //{5, 7} [a-z] ]] |
| 545 | r, l, serror = g:match("a") | 545 | r, l, poserr = g:match("a") |
| 546 | assert(r == nil and l == 0 and serror == "") | 546 | assert(r == nil and l == 0 and poserr == 2) |
| 547 | r, l, serror = g:match("aa") | 547 | r, l, poserr = g:match("aa") |
| 548 | assert(r == nil and l == 0 and serror == "") | 548 | assert(r == nil and l == 0 and poserr == 3) |
| 549 | assert(g:match("aaa") == 4) | 549 | assert(g:match("aaa") == 4) |
| 550 | 550 | ||
| 551 | r, l, serror = g:match("ad") | 551 | r, l, poserr = g:match("ad") |
| 552 | assert(r == nil and l == 0 and serror == "d") | 552 | assert(r == nil and l == 0 and poserr == 2) |
| 553 | 553 | ||
| 554 | r, l, serror = g:match("g") | 554 | r, l, poserr = g:match("g") |
| 555 | assert(r == nil and l == 0 and serror == "g") | 555 | assert(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 | |||
| 584 | s = "unsigned a a" | 584 | s = "unsigned a a" |
| 585 | assert(g:match(s) == #s + 1) --4 | 585 | assert(g:match(s) == #s + 1) --4 |
| 586 | s = "b" | 586 | s = "b" |
| 587 | r, l, serror = g:match(s) | 587 | r, l, poserr = g:match(s) |
| 588 | assert(r == nil and l == 5 and serror == s) | 588 | assert(r == nil and l == 5 and poserr == 1) |
| 589 | s = "unsigned" | 589 | s = "unsigned" |
| 590 | r, l, serror = g:match(s) | 590 | r, l, poserr = g:match(s) |
| 591 | assert(r == nil and l == 5 and serror == s) | 591 | assert(r == nil and l == 5 and poserr == 1) |
| 592 | s = "unsigned a" | 592 | s = "unsigned a" |
| 593 | r, l, serror = g:match(s) | 593 | r, l, poserr = g:match(s) |
| 594 | assert(r == nil and l == 5 and serror == s) | 594 | assert(r == nil and l == 5 and poserr == 1) |
| 595 | s = "unsigned int" | 595 | s = "unsigned int" |
| 596 | r, l, serror = g:match(s) | 596 | r, l, poserr = g:match(s) |
| 597 | assert(r == nil and l == 5 and serror == s) | 597 | assert(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 |
| 605 | s = "abc" | 605 | s = "abc" |
| 606 | p = m.Lc(m.T(2), m.P"a", 1, 3) | 606 | p = m.Lc(m.T(2), m.P"a", 1, 3) |
| 607 | r, l, serror = p:match(s) | 607 | r, l, poserr = p:match(s) |
| 608 | assert(r == nil and l == 2 and serror == "abc") | 608 | assert(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 |
| 621 | s = "bola" | 621 | s = "bola" |
| 622 | r, l, serror = p:match("bola") | 622 | r, l, poserr = p:match("bola") |
| 623 | assert(r == nil and l == 0 and serror == "bola") | 623 | assert(r == nil and l == 0 and poserr == 1) |
| 624 | 624 | ||
| 625 | -- labeled choice does not catch "fail" | 625 | -- labeled choice does not catch "fail" |
| 626 | p = m.Lc(m.P"b", m.P"a", 1) | 626 | p = m.Lc(m.P"b", m.P"a", 1) |
| 627 | 627 | ||
| 628 | r, l, serror = p:match("abc") | 628 | r, l, poserr = p:match("abc") |
| 629 | assert(r == nil and l == 0 and serror == "abc") | 629 | assert(r == nil and l == 0 and poserr == 1) |
| 630 | assert(p:match("bola") == 2) | 630 | assert(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) | |||
| 642 | assert(p:match("abc") == 2) | 642 | assert(p:match("abc") == 2) |
| 643 | assert(p:match("bac") == 2) | 643 | assert(p:match("bac") == 2) |
| 644 | assert(p:match("cab") == 2) | 644 | assert(p:match("cab") == 2) |
| 645 | r, l, serror = p:match("dab") | 645 | r, l, poserr = p:match("dab") |
| 646 | assert(r == nil and l == 0 and serror == "dab") | 646 | assert(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) | |||
| 652 | assert(p:match("abc") == 2) | 652 | assert(p:match("abc") == 2) |
| 653 | assert(p:match("bac") == 2) | 653 | assert(p:match("bac") == 2) |
| 654 | assert(p:match("cab") == 2) | 654 | assert(p:match("cab") == 2) |
| 655 | r, l, serror = p:match("dab") | 655 | r, l, poserr = p:match("dab") |
| 656 | assert(r == nil and l == 0 and serror == "dab") | 656 | assert(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" |
| 664 | p = m.Lc(m.Lc(m.P"a" + m.T(2), m.P"b" + m.T(2), 1), m.P"c", 2) | 664 | p = m.Lc(m.Lc(m.P"a" + m.T(2), m.P"b" + m.T(2), 1), m.P"c", 2) |
| 665 | assert(p:match("abc") == 2) | 665 | assert(p:match("abc") == 2) |
| 666 | r, l, serror = p:match("bac") | 666 | r, l, poserr = p:match("bac") |
| 667 | assert(r == nil and l == 0 and serror == "bac") | 667 | assert(r == nil and l == 0 and poserr == 1) |
| 668 | assert(p:match("cab") == 2) | 668 | assert(p:match("cab") == 2) |
| 669 | r, l, serror = p:match("dab") | 669 | r, l, poserr = p:match("dab") |
| 670 | assert(r == nil and l == 0 and serror == "dab") | 670 | assert(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") |
| 675 | p = m.Lc(m.P"a" + m.T(2), m.Lc(m.P"b" + m.T(2), m.P"c", 2), 1) | 675 | p = m.Lc(m.P"a" + m.T(2), m.Lc(m.P"b" + m.T(2), m.P"c", 2), 1) |
| 676 | assert(p:match("abc") == 2) | 676 | assert(p:match("abc") == 2) |
| 677 | r, l, serror = p:match("bac") | 677 | r, l, poserr = p:match("bac") |
| 678 | assert(r == nil and l == 2 and serror == "bac") | 678 | assert(r == nil and l == 2 and poserr == 1) |
| 679 | r, l, serror = p:match("cab") | 679 | r, l, poserr = p:match("cab") |
| 680 | assert(r == nil and l == 2 and serror == "cab") | 680 | assert(r == nil and l == 2 and poserr == 1) |
| 681 | r, l, serror = p:match("dab") | 681 | r, l, poserr = p:match("dab") |
| 682 | assert(r == nil and l == 2 and serror == "dab") | 682 | assert(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 | |||
| 713 | s = "unsigned a a" | 713 | s = "unsigned a a" |
| 714 | assert(g:match(s) == #s + 1) --4 | 714 | assert(g:match(s) == #s + 1) --4 |
| 715 | s = "b" | 715 | s = "b" |
| 716 | r, l, serror = g:match(s) | 716 | r, l, poserr = g:match(s) |
| 717 | assert(r == nil and l == 5 and serror == s) | 717 | assert(r == nil and l == 5 and poserr == 1) |
| 718 | s = "unsigned" | 718 | s = "unsigned" |
| 719 | r, l, serror = g:match(s) | 719 | r, l, poserr = g:match(s) |
| 720 | assert(r == nil and l == 5 and serror == s) | 720 | assert(r == nil and l == 5 and poserr == 1) |
| 721 | s = "unsigned a" | 721 | s = "unsigned a" |
| 722 | r, l, serror = g:match(s) | 722 | r, l, poserr = g:match(s) |
| 723 | assert(r == nil and l == 5 and serror == s) | 723 | assert(r == nil and l == 5 and poserr == 1) |
| 724 | s = "unsigned int" | 724 | s = "unsigned int" |
| 725 | r, l, serror = g:match(s) | 725 | r, l, poserr = g:match(s) |
| 726 | assert(r == nil and l == 5 and serror == s) | 726 | assert(r == nil and l == 5 and poserr == 1) |
| 727 | 727 | ||
| 728 | 728 | ||
| 729 | 729 | ||
| @@ -933,15 +933,15 @@ print("+") | |||
| 933 | 933 | ||
| 934 | p = m.Rec("a", "b", 3) | 934 | p = m.Rec("a", "b", 3) |
| 935 | assert(p:match("a") == 2) | 935 | assert(p:match("a") == 2) |
| 936 | checklabeq({nil, 0, "b"}, p:match("b")) | 936 | checklabeq({nil, 0, 1}, p:match("b")) |
| 937 | checklabeq({nil, 0, "c"}, p:match("c")) | 937 | checklabeq({nil, 0, 1}, p:match("c")) |
| 938 | 938 | ||
| 939 | p = m.Rec(m.T(3), "b", 1) | 939 | p = m.Rec(m.T(3), "b", 1) |
| 940 | checklabeq({nil, 3, "a"}, p:match("a")) | 940 | checklabeq({nil, 3, 1}, p:match("a")) |
| 941 | checklabeq({nil, 3, "b"}, p:match("b")) | 941 | checklabeq({nil, 3, 1}, p:match("b")) |
| 942 | 942 | ||
| 943 | p = m.Rec(m.T(3), "b", 3) | 943 | p = m.Rec(m.T(3), "b", 3) |
| 944 | checklabeq({nil, 0, "a"}, p:match("a")) | 944 | checklabeq({nil, 0, 1}, p:match("a")) |
| 945 | assert(p:match("b") == 2) | 945 | assert(p:match("b") == 2) |
| 946 | 946 | ||
| 947 | --[[ | 947 | --[[ |
| @@ -960,11 +960,11 @@ assert(g:match("abc") == 4) | |||
| 960 | assert(g:match("aabc") == 5) | 960 | assert(g:match("aabc") == 5) |
| 961 | assert(g:match("aadc") == 5) | 961 | assert(g:match("aadc") == 5) |
| 962 | assert(g:match("dc") == 3) | 962 | assert(g:match("dc") == 3) |
| 963 | checklabeq({nil, 0, "bc"}, g:match("bbc")) | 963 | checklabeq({nil, 0, 2}, g:match("bbc")) |
| 964 | assert(g:match("xxc") == 4) | 964 | assert(g:match("xxc") == 4) |
| 965 | assert(g:match("c") == 2) | 965 | assert(g:match("c") == 2) |
| 966 | checklabeq({nil, 0, ""}, g:match("fail")) | 966 | checklabeq({nil, 0, 5}, g:match("fail")) |
| 967 | checklabeq({nil, 0, ""}, g:match("aaxx")) | 967 | checklabeq({nil, 0, 5}, g:match("aaxx")) |
| 968 | 968 | ||
| 969 | 969 | ||
| 970 | --[[ | 970 | --[[ |
| @@ -982,14 +982,14 @@ g = m.P{ | |||
| 982 | assert(g:match("abc") == 4) | 982 | assert(g:match("abc") == 4) |
| 983 | assert(g:match("aabc") == 5) | 983 | assert(g:match("aabc") == 5) |
| 984 | assert(g:match("aadc") == 5) | 984 | assert(g:match("aadc") == 5) |
| 985 | checklabeq({nil, 0, "bc"}, g:match("bc")) | 985 | checklabeq({nil, 0, 1}, g:match("bc")) |
| 986 | checklabeq({nil, 0, "bbc"}, g:match("bbc")) | 986 | checklabeq({nil, 0, 1}, g:match("bbc")) |
| 987 | checklabeq({nil, 0, "b"}, g:match("abb")) | 987 | checklabeq({nil, 0, 3}, g:match("abb")) |
| 988 | checklabeq({nil, 0, ""}, g:match("axx")) | 988 | checklabeq({nil, 0, 4}, g:match("axx")) |
| 989 | assert(g:match("accc") == 5) | 989 | assert(g:match("accc") == 5) |
| 990 | assert(g:match("axxc") == 5) | 990 | assert(g:match("axxc") == 5) |
| 991 | checklabeq({nil, 0, "c"}, g:match("c")) | 991 | checklabeq({nil, 0, 1}, g:match("c")) |
| 992 | checklabeq({nil, 0, "fail"}, g:match("fail")) | 992 | checklabeq({nil, 0, 1}, g:match("fail")) |
| 993 | 993 | ||
| 994 | 994 | ||
| 995 | 995 | ||
| @@ -1114,10 +1114,10 @@ local g = m.P{ | |||
| 1114 | assert(g:match("a.") == 3) | 1114 | assert(g:match("a.") == 3) |
| 1115 | assert(g:match("aa.") == 4) | 1115 | assert(g:match("aa.") == 4) |
| 1116 | assert(g:match(".") == 2) | 1116 | assert(g:match(".") == 2) |
| 1117 | checklabeq({nil, 1, "ba."}, g:match("ba.")) | 1117 | checklabeq({nil, 1, 1}, g:match("ba.")) |
| 1118 | checklabeq({nil, 1, "ba."}, g:match("aba.")) | 1118 | checklabeq({nil, 1, 2}, g:match("aba.")) |
| 1119 | checklabeq({nil, 1, "cba."}, g:match("cba.")) | 1119 | checklabeq({nil, 1, 1}, g:match("cba.")) |
| 1120 | checklabeq({nil, 2, "a"}, g:match("a.a")) | 1120 | checklabeq({nil, 2, 3}, g:match("a.a")) |
| 1121 | 1121 | ||
| 1122 | 1122 | ||
| 1123 | local g2 = m.P{ | 1123 | local g2 = m.P{ |
| @@ -1131,8 +1131,8 @@ assert(g2:match("aa.") == 4) | |||
| 1131 | assert(g2:match(".") == 2) | 1131 | assert(g2:match(".") == 2) |
| 1132 | assert(g2:match("ba.") == 4) | 1132 | assert(g2:match("ba.") == 4) |
| 1133 | assert(g2:match("aba.") == 5) | 1133 | assert(g2:match("aba.") == 5) |
| 1134 | checklabeq({nil, 3, "cba."}, g2:match("cba.")) | 1134 | checklabeq({nil, 3, 1}, g2:match("cba.")) |
| 1135 | checklabeq({nil, 2, "a"}, g2:match("a.a")) | 1135 | checklabeq({nil, 2, 3}, g2:match("a.a")) |
| 1136 | 1136 | ||
| 1137 | local g3 = m.P{ | 1137 | local g3 = m.P{ |
| 1138 | "S", | 1138 | "S", |
| @@ -1146,9 +1146,9 @@ assert(g3:match(".") == 2) | |||
| 1146 | assert(g3:match("ba.") == 4) | 1146 | assert(g3:match("ba.") == 4) |
| 1147 | assert(g3:match("aba.") == 5) | 1147 | assert(g3:match("aba.") == 5) |
| 1148 | assert(g3:match("cba.") == 5) | 1148 | assert(g3:match("cba.") == 5) |
| 1149 | checklabeq({nil, 4, "a"}, g3:match("a.a")) | 1149 | checklabeq({nil, 4, 3}, g3:match("a.a")) |
| 1150 | checklabeq({nil, 4, "dc"}, g3:match("dc")) | 1150 | checklabeq({nil, 4, 1}, g3:match("dc")) |
| 1151 | checklabeq({nil, 4, "d"}, g3:match(".d")) | 1151 | checklabeq({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 | ||
| 1160 | checkeq({"523", "624", "346", "888"} , {g:match("523 624 346\n888")}) | 1160 | checkeq({"523", "624", "346", "888"} , {g:match("523 624 346\n888")}) |
| 1161 | checkeq({nil, 5, "a 123"}, {g:match("44 a 123")}) | 1161 | checkeq({nil, 5, 4}, {g:match("44 a 123")}) |
| 1162 | 1162 | ||
| 1163 | local g2 = m.Rec(g, ((-m.R("09") * m.P(1))^0) / "58", 5) | 1163 | local 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 | ||
| 1174 | checkeq({"523", "624", "346", "888"} , {g:match("523 624 346\n888")}) | 1174 | checkeq({"523", "624", "346", "888"} , {g:match("523 624 346\n888")}) |
| 1175 | checkeq({nil, 5, "a 123"}, {g:match("44 a 123")}) | 1175 | checkeq({nil, 5, 4}, {g:match("44 a 123")}) |
| 1176 | 1176 | ||
| 1177 | local g2 = m.Rec(g, ((-m.R("09") * m.P(1))^0) / "58", 5) | 1177 | local g2 = m.Rec(g, ((-m.R("09") * m.P(1))^0) / "58", 5) |
| 1178 | 1178 | ||
