diff options
-rw-r--r-- | testlabel.lua | 123 |
1 files changed, 86 insertions, 37 deletions
diff --git a/testlabel.lua b/testlabel.lua index 1962fbf..f7180a7 100644 --- a/testlabel.lua +++ b/testlabel.lua | |||
@@ -1,11 +1,17 @@ | |||
1 | local m = require 'lpeglabel' | 1 | local m = require 'lpeglabel' |
2 | 2 | ||
3 | local p = m.T(1, 2, 5) | 3 | local p, r, l, s, serror |
4 | assert(p:match("abc") == nil) | 4 | |
5 | -- throws a label | ||
6 | p = m.T(1) | ||
7 | s = "abc" | ||
8 | r, l, serror = p:match(s) | ||
9 | assert(r == nil and l == 1 and serror == "abc") | ||
5 | 10 | ||
6 | -- throws a label that is not caught by ordinary choice | 11 | -- throws a label that is not caught by ordinary choice |
7 | p = m.T(1) + m.P"a" | 12 | p = m.T(1) + m.P"a" |
8 | assert(p:match("abc") == nil) | 13 | r, l, serror = p:match(s) |
14 | assert(r == nil and l == 1 and serror == "abc") | ||
9 | 15 | ||
10 | -- again throws a label that is not caught by ordinary choice | 16 | -- again throws a label that is not caught by ordinary choice |
11 | local g = m.P{ | 17 | local g = m.P{ |
@@ -14,28 +20,37 @@ local g = m.P{ | |||
14 | A = m.T(1), | 20 | A = m.T(1), |
15 | B = m.P"a" | 21 | B = m.P"a" |
16 | } | 22 | } |
17 | assert(g:match("abc") == nil) | 23 | r, l, serror = g:match(s) |
24 | assert(r == nil and l == 1 and serror == "abc") | ||
18 | 25 | ||
19 | -- throws a label that is not caught by labeled choice | 26 | -- throws a label that is not caught by labeled choice |
20 | p = m.Lc(m.T(2), m.P"a", 1, 3) | 27 | p = m.Lc(m.T(2), m.P"a", 1, 3) |
21 | assert(p:match("abc") == nil) | 28 | r, l, serror = p:match(s) |
29 | assert(r == nil and l == 2 and serror == "abc") | ||
22 | 30 | ||
23 | -- modifies previous pattern | 31 | -- modifies previous pattern |
24 | -- adds another labeled choice to catch label "2" | 32 | -- adds another labeled choice to catch label "2" |
25 | p = m.Lc(p, m.P"a", 2) | 33 | p = m.Lc(p, m.P"a", 2) |
26 | assert(p:match("abc") == 2) | 34 | assert(p:match(s) == 2) |
27 | 35 | ||
28 | -- throws a label that is caught by labeled choice | 36 | -- throws a label that is caught by labeled choice |
29 | p = m.Lc(m.T(25), m.P"a", 25) | 37 | p = m.Lc(m.T(25), m.P"a", 25) |
30 | assert(p:match("abc") == 2) | 38 | assert(p:match(s) == 2) |
31 | assert(p:match("bola") == nil) | 39 | |
40 | -- "fail" is label "0" | ||
41 | -- throws the "fail" label that is not caught by the labeled choice | ||
42 | s = "bola" | ||
43 | r, l, serror = p:match("bola") | ||
44 | assert(r == nil and l == 0 and serror == "bola") | ||
32 | 45 | ||
33 | -- labeled choice does not catch "fail" by default | 46 | -- labeled choice does not catch "fail" by default |
34 | p = m.Lc(m.P"b", m.P"a", 1) | 47 | p = m.Lc(m.P"b", m.P"a", 1) |
35 | assert(p:match("abc") == nil) | 48 | |
49 | r, l, serror = p:match("abc") | ||
50 | assert(r == nil and l == 0 and serror == "abc") | ||
51 | |||
36 | assert(p:match("bola") == 2) | 52 | assert(p:match("bola") == 2) |
37 | 53 | ||
38 | -- "fail" is label "0" | ||
39 | -- labeled choice can catch "fail" | 54 | -- labeled choice can catch "fail" |
40 | p = m.Lc(m.P"b", m.P"a", 0) | 55 | p = m.Lc(m.P"b", m.P"a", 0) |
41 | assert(p:match("abc") == 2) | 56 | assert(p:match("abc") == 2) |
@@ -46,14 +61,18 @@ assert(p:match("bola") == 2) | |||
46 | p = m.Lc(m.P"a" * m.T(3), (m.P"a" + m.P"b"), 0, 3) | 61 | p = m.Lc(m.P"a" * m.T(3), (m.P"a" + m.P"b"), 0, 3) |
47 | assert(p:match("abc") == 2) | 62 | assert(p:match("abc") == 2) |
48 | assert(p:match("bac") == 2) | 63 | assert(p:match("bac") == 2) |
49 | assert(p:match("cab") == nil) | 64 | |
65 | r, l, serror = p:match("cab") | ||
66 | assert(r == nil and l == 0 and serror == "cab") | ||
50 | 67 | ||
51 | -- tests related to predicates | 68 | -- tests related to predicates |
52 | p = #m.T(1) + m.P"a" | 69 | p = #m.T(1) + m.P"a" |
53 | assert(p:match("abc") == nil) | 70 | r, l, serror = p:match("abc") |
71 | assert(r == nil and l == 1 and serror == "abc") | ||
54 | 72 | ||
55 | p = ##m.T(1) + m.P"a" | 73 | p = ##m.T(1) + m.P"a" |
56 | assert(p:match("abc") == nil) | 74 | r, l, serror = p:match("abc") |
75 | assert(r == nil and l == 1 and serror == "abc") | ||
57 | 76 | ||
58 | p = #m.T(0) * m.P"a" | 77 | p = #m.T(0) * m.P"a" |
59 | assert(p:match("abc") == fail) | 78 | assert(p:match("abc") == fail) |
@@ -62,10 +81,12 @@ p = #m.T(0) + m.P"a" | |||
62 | assert(p:match("abc") == 2) | 81 | assert(p:match("abc") == 2) |
63 | 82 | ||
64 | p = -m.T(1) * m.P"a" | 83 | p = -m.T(1) * m.P"a" |
65 | assert(p:match("abc") == nil) | 84 | r, l, serror = p:match("abc") |
85 | assert(r == nil and l == 1 and serror == "abc") | ||
66 | 86 | ||
67 | p = -(-m.T(1)) * m.P"a" | 87 | p = -(-m.T(1)) * m.P"a" |
68 | assert(p:match("abc") == nil) | 88 | r, l, serror = p:match("abc") |
89 | assert(r == nil and l == 1 and serror == "abc") | ||
69 | 90 | ||
70 | p = -m.T(0) * m.P"a" | 91 | p = -m.T(0) * m.P"a" |
71 | assert(p:match("abc") == 2) | 92 | assert(p:match("abc") == 2) |
@@ -90,13 +111,15 @@ assert(p:match("abc") == 2) | |||
90 | 111 | ||
91 | -- tests related to repetition | 112 | -- tests related to repetition |
92 | p = m.T(1)^0 | 113 | p = m.T(1)^0 |
93 | assert(p:match("ab") == nil) | 114 | r, l, serror = p:match("ab") |
115 | assert(r == nil and l == 1 and serror == "ab") | ||
94 | 116 | ||
95 | p = m.T(0)^0 | 117 | p = m.T(0)^0 |
96 | assert(p:match("ab") == 1) | 118 | assert(p:match("ab") == 1) |
97 | 119 | ||
98 | p = (m.P"a" + m.T(1))^0 | 120 | p = (m.P"a" + m.T(1))^0 |
99 | assert(p:match("aa") == nil) | 121 | r, l, serror = p:match("aa") |
122 | assert(r == nil and l == 1 and serror == "") | ||
100 | 123 | ||
101 | p = (m.P"a" + m.T(0))^0 | 124 | p = (m.P"a" + m.T(0))^0 |
102 | assert(p:match("aa") == 3) | 125 | assert(p:match("aa") == 3) |
@@ -123,7 +146,8 @@ g = m.P{ | |||
123 | B = m.T(1), | 146 | B = m.T(1), |
124 | } | 147 | } |
125 | assert(g:match("ab") == 2) | 148 | assert(g:match("ab") == 2) |
126 | assert(g:match("bc") == nil) | 149 | r, l, serror = g:match("bc") |
150 | assert(r == nil and l == 0 and serror == "bc") | ||
127 | 151 | ||
128 | 152 | ||
129 | --[[ | 153 | --[[ |
@@ -138,17 +162,23 @@ g = m.P{ | |||
138 | B = m.P'a', | 162 | B = m.P'a', |
139 | } | 163 | } |
140 | assert(g:match("a;a;") == 5) | 164 | assert(g:match("a;a;") == 5) |
141 | assert(g:match("a;a") == nil) | 165 | |
166 | r, l, serror = g:match("a;a") | ||
167 | assert(r == nil and l == 1 and serror == "") | ||
142 | 168 | ||
143 | 169 | ||
144 | -- %1 /{1,3} %2 /{2} 'a' | 170 | -- %1 /{1,3} %2 /{2} 'a' |
145 | p = m.Lc(m.Lc(m.T(1), m.T(2), 1, 3), m.P"a", 2) | 171 | p = m.Lc(m.Lc(m.T(1), m.T(2), 1, 3), m.P"a", 2) |
146 | assert(p:match("abc") == 2) | 172 | assert(p:match("abc") == 2) |
147 | assert(p:match("") == nil) | 173 | |
174 | r, l, serror = p:match("") | ||
175 | assert(r == nil and l == 0 and serror == "") | ||
148 | 176 | ||
149 | p = m.Lc(m.T(1), m.Lc(m.T(2), m.P"a", 2), 1, 3) | 177 | p = m.Lc(m.T(1), m.Lc(m.T(2), m.P"a", 2), 1, 3) |
150 | assert(p:match("abc") == 2) | 178 | assert(p:match("abc") == 2) |
151 | assert(p:match("") == nil) | 179 | |
180 | r, l, serror = p:match("") | ||
181 | assert(r == nil and l == 0 and serror == "") | ||
152 | 182 | ||
153 | print("+") | 183 | print("+") |
154 | 184 | ||
@@ -173,7 +203,7 @@ g = m.P{ | |||
173 | m.V"U"^0 * m.V"I" * m.V"ID", 3 | 203 | m.V"U"^0 * m.V"I" * m.V"ID", 3 |
174 | ), | 204 | ), |
175 | m.V"U"^0 * m.V"ID" * m.V"ID", 4) | 205 | m.V"U"^0 * m.V"ID" * m.V"ID", 4) |
176 | + m.T(5), | 206 | + m.T(5), -- error |
177 | S0 = m.V"ID" * m.V"S1" + m.V"U" * m.V"S2" + m.V"I" * m.T(3), | 207 | S0 = m.V"ID" * m.V"S1" + m.V"U" * m.V"S2" + m.V"I" * m.T(3), |
178 | S1 = eq * m.T(2) + sp * -m.P(1) * m.T(1) + m.V"ID" * m.T(4), | 208 | S1 = eq * m.T(2) + sp * -m.P(1) * m.T(1) + m.V"ID" * m.T(4), |
179 | S2 = m.V"U" * m.V"S2" + m.V"ID" * m.T(4) + m.V"I" * m.T(3), | 209 | S2 = m.V"U" * m.V"S2" + m.V"ID" * m.T(4) + m.V"I" * m.T(3), |
@@ -193,14 +223,22 @@ s = "unsigned int a" | |||
193 | assert(g:match(s) == #s + 1) --3 | 223 | assert(g:match(s) == #s + 1) --3 |
194 | s = "unsigned a a" | 224 | s = "unsigned a a" |
195 | assert(g:match(s) == #s + 1) --4 | 225 | assert(g:match(s) == #s + 1) --4 |
226 | |||
196 | s = "b" | 227 | s = "b" |
197 | assert(g:match(s) == nil) | 228 | r, l, serror = g:match(s) |
229 | assert(r == nil and l == 5 and serror == "b") | ||
230 | |||
198 | s = "unsigned" | 231 | s = "unsigned" |
199 | assert(g:match(s) == nil) | 232 | r, l, serror = g:match(s) |
233 | assert(r == nil and l == 5 and serror == s) | ||
234 | |||
200 | s = "unsigned a" | 235 | s = "unsigned a" |
201 | assert(g:match(s) == nil) | 236 | r, l, serror = g:match(s) |
237 | assert(r == nil and l == 5 and serror == s) | ||
238 | |||
202 | s = "unsigned int" | 239 | s = "unsigned int" |
203 | assert(g:match(s) == nil) | 240 | r, l, serror = g:match(s) |
241 | assert(r == nil and l == 5 and serror == s) | ||
204 | 242 | ||
205 | 243 | ||
206 | print("+") | 244 | print("+") |
@@ -210,27 +248,34 @@ local re = require 'relabel' | |||
210 | g = re.compile[['a' /{4,9} [a-z] | 248 | g = re.compile[['a' /{4,9} [a-z] |
211 | ]] | 249 | ]] |
212 | assert(g:match("a") == 2) | 250 | assert(g:match("a") == 2) |
213 | assert(g:match("b") == nil) | 251 | r, l, serror = g:match("b") |
252 | assert(r == nil and l == 0 and serror == "b") | ||
214 | 253 | ||
215 | g = re.compile[['a' /{4,9} [a-f] /{5, 7} [a-z] | 254 | g = re.compile[['a' /{4,9} [a-f] /{5, 7} [a-z] |
216 | ]] | 255 | ]] |
217 | assert(g:match("a") == 2) | 256 | assert(g:match("a") == 2) |
218 | assert(g:match("b") == nil) | 257 | r, l, serror = g:match("b") |
258 | assert(r == nil and l == 0 and serror == "b") | ||
219 | 259 | ||
220 | g = re.compile[[%{1} /{4,9} [a-z] | 260 | g = re.compile[[%{1} /{4,9} [a-z] |
221 | ]] | 261 | ]] |
222 | assert(g:match("a") == nil) | 262 | r, l, serror = g:match("a") |
263 | assert(r == nil and l == 1 and serror == "a") | ||
264 | |||
223 | 265 | ||
224 | g = re.compile[[%{1} /{4,1} [a-f] | 266 | g = re.compile[[%{1} /{4,1} [a-f] |
225 | ]] | 267 | ]] |
226 | assert(g:match("a") == 2) | 268 | assert(g:match("a") == 2) |
227 | assert(g:match("h") == nil) | 269 | r, l, serror = g:match("h") |
270 | assert(r == nil and l == 0 and serror == "h") | ||
228 | 271 | ||
229 | g = re.compile[[[a-f]%{15, 9} /{4,9} [a-c]%{7} /{5, 7} [a-z] ]] | 272 | g = re.compile[[[a-f]%{9} /{4,9} [a-c]%{7} /{5, 7} [a-z] ]] |
230 | assert(g:match("a") == 2) | 273 | assert(g:match("a") == 2) |
231 | assert(g:match("c") == 2) | 274 | assert(g:match("c") == 2) |
232 | assert(g:match("d") == nil) | 275 | r, l, serror = g:match("d") |
233 | assert(g:match("g") == nil) | 276 | assert(r == nil and l == 0 and serror == "d") |
277 | r, l, serror = g:match("g") | ||
278 | assert(r == nil and l == 0 and serror == "g") | ||
234 | 279 | ||
235 | --[[ grammar based on Figure 8 of paper submitted to SCP | 280 | --[[ grammar based on Figure 8 of paper submitted to SCP |
236 | S -> S0 /{1} ID /{2} ID '=' Exp /{3} 'unsigned'* 'int' ID /{4} 'unsigned'* ID ID / %error | 281 | S -> S0 /{1} ID /{2} ID '=' Exp /{3} 'unsigned'* 'int' ID /{4} 'unsigned'* ID ID / %error |
@@ -262,13 +307,17 @@ assert(g:match(s) == #s + 1) --3 | |||
262 | s = "unsigned a a" | 307 | s = "unsigned a a" |
263 | assert(g:match(s) == #s + 1) --4 | 308 | assert(g:match(s) == #s + 1) --4 |
264 | s = "b" | 309 | s = "b" |
265 | assert(g:match(s) == nil) | 310 | r, l, serror = g:match(s) |
311 | assert(r == nil and l == 5 and serror == s) | ||
266 | s = "unsigned" | 312 | s = "unsigned" |
267 | assert(g:match(s) == nil) | 313 | r, l, serror = g:match(s) |
314 | assert(r == nil and l == 5 and serror == s) | ||
268 | s = "unsigned a" | 315 | s = "unsigned a" |
269 | assert(g:match(s) == nil) | 316 | r, l, serror = g:match(s) |
317 | assert(r == nil and l == 5 and serror == s) | ||
270 | s = "unsigned int" | 318 | s = "unsigned int" |
271 | assert(g:match(s) == nil) | 319 | r, l, serror = g:match(s) |
320 | assert(r == nil and l == 5 and serror == s) | ||
272 | 321 | ||
273 | local terror = { ['cmdSeq'] = "Missing ';' in CmdSeq", | 322 | local terror = { ['cmdSeq'] = "Missing ';' in CmdSeq", |
274 | ['ifExp'] = "Error in expresion of 'if'", | 323 | ['ifExp'] = "Error in expresion of 'if'", |