My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones  
Changes to /src/lepl/rxpy/parser/_test/parser.py
263822c3252c vs. 09a59c3a482e Compare: vs.  Format:
Revision 09a59c3a482e
Go to: 
Project members, sign in to write a code review
/src/lepl/rxpy/parser/_test/parser.py   263822c3252c /src/lepl/rxpy/parser/_test/parser.py   09a59c3a482e
1 #LICENCE 1 #LICENCE
2 2
3 3
4 from lepl.rxpy.support import RxpyError, _CHARS 4 from lepl.rxpy.support import RxpyError, _CHARS
5 from lepl.rxpy.graph._test.lib import GraphTest 5 from lepl.rxpy.graph._test.lib import GraphTest
6 from lepl.rxpy.parser.pattern import parse_pattern 6 from lepl.rxpy.parser.pattern import parse_pattern
7 from lepl.rxpy.parser.support import ParserState 7 from lepl.rxpy.parser.support import ParserState
8 from lepl.rxpy.engine.base import BaseMatchEngine 8 from lepl.rxpy.engine.base import BaseMatchEngine
9 from lepl.rxpy.parser.error import SimpleGroupError 9 from lepl.rxpy.parser.error import SimpleGroupError
10 10
11 11
12 class DummyEngine(BaseMatchEngine): 12 class DummyEngine(BaseMatchEngine):
13 REQUIRE = _CHARS 13 REQUIRE = _CHARS
14 14
15 15
16 def parse(pattern, engine=BaseMatchEngine, flags=0): 16 def parse(pattern, engine=BaseMatchEngine, flags=0):
17 return parse_pattern(pattern, engine, flags=flags) 17 return parse_pattern(pattern, engine, flags=flags)
18 18
19 19
20 class ParserTest(GraphTest): 20 class ParserTest(GraphTest):
21 21
22 def test_sequence(self): 22 def test_sequence(self):
23 self.assert_graphs(parse('abc', engine=DummyEngine), 23 self.assert_graphs(parse('abc', engine=DummyEngine),
24 """digraph { 24 """digraph {
25 0 [label="a"] 25 0 [label="a"]
26 1 [label="b"] 26 1 [label="b"]
27 2 [label="c"] 27 2 [label="c"]
28 3 [label="Match"] 28 3 [label="Match"]
29 0 -> 1 29 0 -> 1
30 1 -> 2 30 1 -> 2
31 2 -> 3 31 2 -> 3
32 }""") 32 }""")
33 self.assert_graphs(parse('(?_c)abc'), 33 self.assert_graphs(parse('(?_c)abc'),
34 """digraph { 34 """digraph {
35 0 [label="a"] 35 0 [label="a"]
36 1 [label="b"] 36 1 [label="b"]
37 2 [label="c"] 37 2 [label="c"]
38 3 [label="Match"] 38 3 [label="Match"]
39 0 -> 1 39 0 -> 1
40 1 -> 2 40 1 -> 2
41 2 -> 3 41 2 -> 3
42 }""") 42 }""")
43 self.assert_graphs(parse('abc'), 43 self.assert_graphs(parse('abc'),
44 """digraph { 44 """digraph {
45 0 [label="abc"] 45 0 [label="abc"]
46 1 [label="Match"] 46 1 [label="Match"]
47 0 -> 1 47 0 -> 1
48 }""") 48 }""")
49 49
50 def test_matching_group(self): 50 def test_matching_group(self):
51 self.assert_graphs(parse('a(b)c'), 51 self.assert_graphs(parse('a(b)c'),
52 """digraph { 52 """digraph {
53 0 [label="a"] 53 0 [label="a"]
54 1 [label="(?P<1>"] 54 1 [label="(?P<1>"]
55 2 [label="b"] 55 2 [label="b"]
56 3 [label=")"] 56 3 [label=")"]
57 4 [label="c"] 57 4 [label="c"]
58 5 [label="Match"] 58 5 [label="Match"]
59 0 -> 1 59 0 -> 1
60 1 -> 2 60 1 -> 2
61 2 -> 3 61 2 -> 3
62 3 -> 4 62 3 -> 4
63 4 -> 5 63 4 -> 5
64 }""") 64 }""")
65 65
66 def test_nested_matching_group(self): 66 def test_nested_matching_group(self):
67 self.assert_graphs(parse('a(b(c)d)e'), 67 self.assert_graphs(parse('a(b(c)d)e'),
68 """digraph { 68 """digraph {
69 0 [label="a"] 69 0 [label="a"]
70 1 [label="(?P<1>"] 70 1 [label="(?P<1>"]
71 2 [label="b"] 71 2 [label="b"]
72 3 [label="(?P<2>"] 72 3 [label="(?P<2>"]
73 4 [label="c"] 73 4 [label="c"]
74 5 [label=")"] 74 5 [label=")"]
75 6 [label="d"] 75 6 [label="d"]
76 7 [label=")"] 76 7 [label=")"]
77 8 [label="e"] 77 8 [label="e"]
78 9 [label="Match"] 78 9 [label="Match"]
79 0 -> 1 79 0 -> 1
80 1 -> 2 80 1 -> 2
81 2 -> 3 81 2 -> 3
82 3 -> 4 82 3 -> 4
83 4 -> 5 83 4 -> 5
84 5 -> 6 84 5 -> 6
85 6 -> 7 85 6 -> 7
86 7 -> 8 86 7 -> 8
87 8 -> 9 87 8 -> 9
88 }""") 88 }""")
89 89
90 def test_nested_matching_close(self): 90 def test_nested_matching_close(self):
91 self.assert_graphs(parse('a((b))c'), 91 self.assert_graphs(parse('a((b))c'),
92 """digraph { 92 """digraph {
93 0 [label="a"] 93 0 [label="a"]
94 1 [label="(?P<1>"] 94 1 [label="(?P<1>"]
95 2 [label="(?P<2>"] 95 2 [label="(?P<2>"]
96 3 [label="b"] 96 3 [label="b"]
97 4 [label=")"] 97 4 [label=")"]
98 5 [label=")"] 98 5 [label=")"]
99 6 [label="c"] 99 6 [label="c"]
100 7 [label="Match"] 100 7 [label="Match"]
101 0 -> 1 101 0 -> 1
102 1 -> 2 102 1 -> 2
103 2 -> 3 103 2 -> 3
104 3 -> 4 104 3 -> 4
105 4 -> 5 105 4 -> 5
106 5 -> 6 106 5 -> 6
107 6 -> 7 107 6 -> 7
108 }""") 108 }""")
109 109
110 def test_matching_group_late_close(self): 110 def test_matching_group_late_close(self):
111 self.assert_graphs(parse('a(b)'), 111 self.assert_graphs(parse('a(b)'),
112 """digraph { 112 """digraph {
113 0 [label="a"] 113 0 [label="a"]
114 1 [label="(?P<1>"] 114 1 [label="(?P<1>"]
115 2 [label="b"] 115 2 [label="b"]
116 3 [label=")"] 116 3 [label=")"]
117 4 [label="Match"] 117 4 [label="Match"]
118 0 -> 1 118 0 -> 1
119 1 -> 2 119 1 -> 2
120 2 -> 3 120 2 -> 3
121 3 -> 4 121 3 -> 4
122 }""") 122 }""")
123 123
124 def test_matching_group_early_open(self): 124 def test_matching_group_early_open(self):
125 self.assert_graphs(parse('(a)b'), 125 self.assert_graphs(parse('(a)b'),
126 """digraph { 126 """digraph {
127 0 [label="(?P<1>"] 127 0 [label="(?P<1>"]
128 1 [label="a"] 128 1 [label="a"]
129 2 [label=")"] 129 2 [label=")"]
130 3 [label="b"] 130 3 [label="b"]
131 4 [label="Match"] 131 4 [label="Match"]
132 0 -> 1 132 0 -> 1
133 1 -> 2 133 1 -> 2
134 2 -> 3 134 2 -> 3
135 3 -> 4 135 3 -> 4
136 }""") 136 }""")
137 137
138 def test_empty_matching_group(self): 138 def test_empty_matching_group(self):
139 self.assert_graphs(parse('a()b'), 139 self.assert_graphs(parse('a()b'),
140 """digraph { 140 """digraph {
141 0 [label="a"] 141 0 [label="a"]
142 1 [label="(?P<1>"] 142 1 [label="(?P<1>"]
143 2 [label=")"] 143 2 [label=")"]
144 3 [label="b"] 144 3 [label="b"]
145 4 [label="Match"] 145 4 [label="Match"]
146 0 -> 1 146 0 -> 1
147 1 -> 2 147 1 -> 2
148 2 -> 3 148 2 -> 3
149 3 -> 4 149 3 -> 4
150 }""") 150 }""")
151 self.assert_graphs(parse('()a'), 151 self.assert_graphs(parse('()a'),
152 """digraph { 152 """digraph {
153 0 [label="(?P<1>"] 153 0 [label="(?P<1>"]
154 1 [label=")"] 154 1 [label=")"]
155 2 [label="a"] 155 2 [label="a"]
156 3 [label="Match"] 156 3 [label="Match"]
157 0 -> 1 157 0 -> 1
158 1 -> 2 158 1 -> 2
159 2 -> 3 159 2 -> 3
160 }""") 160 }""")
161 161
162 def test_non_matching_group(self): 162 def test_non_matching_group(self):
163 self.assert_graphs(parse('a(?:b)c'), 163 self.assert_graphs(parse('a(?:b)c'),
164 """digraph { 164 """digraph {
165 0 [label="abc"] 165 0 [label="abc"]
166 1 [label="Match"] 166 1 [label="Match"]
167 0 -> 1 167 0 -> 1
168 }""") 168 }""")
169 169
170 def test_non_matching_complex_group(self): 170 def test_non_matching_complex_group(self):
171 self.assert_graphs(parse('a(?:p+q|r)c'), 171 self.assert_graphs(parse('a(?:p+q|r)c'),
172 """digraph { 172 """digraph {
173 0 [label="a"] 173 0 [label="a"]
174 1 [label="...|..."] 174 1 [label="...|..."]
175 2 [label="p"] 175 2 [label="p"]
176 3 [label="r"] 176 3 [label="r"]
177 4 [label="c"] 177 4 [label="c"]
178 5 [label="Match"] 178 5 [label="Match"]
179 6 [label="...+"] 179 6 [label="...+"]
180 7 [label="q"] 180 7 [label="q"]
181 0 -> 1 181 0 -> 1
182 1 -> 2 182 1 -> 2
183 1 -> 3 183 1 -> 3
184 3 -> 4 184 3 -> 4
185 4 -> 5 185 4 -> 5
186 2 -> 6 186 2 -> 6
187 6 -> 2 187 6 -> 2
188 6 -> 7 188 6 -> 7
189 7 -> 4 189 7 -> 4
190 }""") 190 }""")
191 191
192 def test_character_plus(self): 192 def test_character_plus(self):
193 self.assert_graphs(parse('ab+c'), 193 self.assert_graphs(parse('ab+c'),
194 """digraph { 194 """digraph {
195 0 [label="a"] 195 0 [label="a"]
196 1 [label="b"] 196 1 [label="b"]
197 2 [label="...+"] 197 2 [label="...+"]
198 3 [label="c"] 198 3 [label="c"]
199 4 [label="Match"] 199 4 [label="Match"]
200 0 -> 1 200 0 -> 1
201 1 -> 2 201 1 -> 2
202 2 -> 1 202 2 -> 1
203 2 -> 3 203 2 -> 3
204 3 -> 4 204 3 -> 4
205 }""") 205 }""")
206 206
207 def test_character_star(self): 207 def test_character_star(self):
208 self.assert_graphs(parse('ab*c'), 208 self.assert_graphs(parse('ab*c'),
209 """digraph { 209 """digraph {
210 0 [label="a"] 210 0 [label="a"]
211 1 [label="...*"] 211 1 [label="...*"]
212 2 [label="b"] 212 2 [label="b"]
213 3 [label="c"] 213 3 [label="c"]
214 4 [label="Match"] 214 4 [label="Match"]
215 0 -> 1 215 0 -> 1
216 1 -> 2 216 1 -> 2
217 1 -> 3 217 1 -> 3
218 3 -> 4 218 3 -> 4
219 2 -> 1 219 2 -> 1
220 }""") 220 }""")
221 221
222 def test_character_question(self): 222 def test_character_question(self):
223 self.assert_graphs(parse('ab?c'), 223 self.assert_graphs(parse('ab?c'),
224 """digraph { 224 """digraph {
225 0 [label="a"] 225 0 [label="a"]
226 1 [label="...?"] 226 1 [label="...?"]
227 2 [label="b"] 227 2 [label="b"]
228 3 [label="c"] 228 3 [label="c"]
229 4 [label="Match"] 229 4 [label="Match"]
230 0 -> 1 230 0 -> 1
231 1 -> 2 231 1 -> 2
232 1 -> 3 232 1 -> 3
233 3 -> 4 233 3 -> 4
234 2 -> 3 234 2 -> 3
235 }""") 235 }""")
236 # this was a bug 236 # this was a bug
237 self.assert_graphs(parse('x|a?'), 237 self.assert_graphs(parse('x|a?'),
238 """digraph { 238 """digraph {
239 0 [label="...|..."] 239 0 [label="...|..."]
240 1 [label="x"] 240 1 [label="x"]
241 2 [label="...?"] 241 2 [label="...?"]
242 3 [label="a"] 242 3 [label="a"]
243 4 [label="Match"] 243 4 [label="Match"]
244 0 -> 1 244 0 -> 1
245 0 -> 2 245 0 -> 2
246 2 -> 3 246 2 -> 3
247 2 -> 4 247 2 -> 4
248 3 -> 4 248 3 -> 4
249 1 -> 4 249 1 -> 4
250 }""") 250 }""")
251 251
252 def test_multiple_character_question(self): 252 def test_multiple_character_question(self):
253 self.assert_graphs(parse('ab?c?de?'), 253 self.assert_graphs(parse('ab?c?de?'),
254 """digraph { 254 """digraph {
255 0 [label="a"] 255 0 [label="a"]
256 1 [label="...?"] 256 1 [label="...?"]
257 2 [label="b"] 257 2 [label="b"]
258 3 [label="...?"] 258 3 [label="...?"]
259 4 [label="c"] 259 4 [label="c"]
260 5 [label="d"] 260 5 [label="d"]
261 6 [label="...?"] 261 6 [label="...?"]
262 7 [label="e"] 262 7 [label="e"]
263 8 [label="Match"] 263 8 [label="Match"]
264 0 -> 1 264 0 -> 1
265 1 -> 2 265 1 -> 2
266 1 -> 3 266 1 -> 3
267 3 -> 4 267 3 -> 4
268 3 -> 5 268 3 -> 5
269 5 -> 6 269 5 -> 6
270 6 -> 7 270 6 -> 7
271 6 -> 8 271 6 -> 8
272 7 -> 8 272 7 -> 8
273 4 -> 5 273 4 -> 5
274 2 -> 3 274 2 -> 3
275 }""") 275 }""")
276 276
277 def test_group_plus(self): 277 def test_group_plus(self):
278 self.assert_graphs(parse('a(bc)+d'), 278 self.assert_graphs(parse('a(bc)+d'),
279 """digraph { 279 """digraph {
280 0 [label="a"] 280 0 [label="a"]
281 1 [label="(?P<1>"] 281 1 [label="(?P<1>"]
282 2 [label="bc"] 282 2 [label="bc"]
283 3 [label=")"] 283 3 [label=")"]
284 4 [label="...+"] 284 4 [label="...+"]
285 5 [label="d"] 285 5 [label="d"]
286 6 [label="Match"] 286 6 [label="Match"]
287 0 -> 1 287 0 -> 1
288 1 -> 2 288 1 -> 2
289 2 -> 3 289 2 -> 3
290 3 -> 4 290 3 -> 4
291 4 -> 1 291 4 -> 1
292 4 -> 5 292 4 -> 5
293 5 -> 6 293 5 -> 6
294 }""") 294 }""")
295 295
296 def test_group_star(self): 296 def test_group_star(self):
297 self.assert_graphs(parse('a(bc)*d'), 297 self.assert_graphs(parse('a(bc)*d'),
298 """digraph { 298 """digraph {
299 0 [label="a"] 299 0 [label="a"]
300 1 [label="...*"] 300 1 [label="...*"]
301 2 [label="(?P<1>"] 301 2 [label="(?P<1>"]
302 3 [label="d"] 302 3 [label="d"]
303 4 [label="Match"] 303 4 [label="Match"]
304 5 [label="bc"] 304 5 [label="bc"]
305 6 [label=")"] 305 6 [label=")"]
306 0 -> 1 306 0 -> 1
307 1 -> 2 307 1 -> 2
308 1 -> 3 308 1 -> 3
309 3 -> 4 309 3 -> 4
310 2 -> 5 310 2 -> 5
311 5 -> 6 311 5 -> 6
312 6 -> 1 312 6 -> 1
313 }""") 313 }""")
314 314
315 def test_group_question(self): 315 def test_group_question(self):
316 self.assert_graphs(parse('a(bc)?d'), 316 self.assert_graphs(parse('a(bc)?d'),
317 """digraph { 317 """digraph {
318 0 [label="a"] 318 0 [label="a"]
319 1 [label="...?"] 319 1 [label="...?"]
320 2 [label="(?P<1>"] 320 2 [label="(?P<1>"]
321 3 [label="d"] 321 3 [label="d"]
322 4 [label="Match"] 322 4 [label="Match"]
323 5 [label="bc"] 323 5 [label="bc"]
324 6 [label=")"] 324 6 [label=")"]
325 0 -> 1 325 0 -> 1
326 1 -> 2 326 1 -> 2
327 1 -> 3 327 1 -> 3
328 3 -> 4 328 3 -> 4
329 2 -> 5 329 2 -> 5
330 5 -> 6 330 5 -> 6
331 6 -> 3 331 6 -> 3
332 }""") 332 }""")
333 333
334 def test_simple_range(self): 334 def test_simple_range(self):
335 self.assert_graphs(parse('[a-z]'), 335 self.assert_graphs(parse('[a-z]'),
336 """digraph { 336 """digraph {
337 0 [label="[a-z]"] 337 0 [label="[a-z]"]
338 1 [label="Match"] 338 1 [label="Match"]
339 0 -> 1 339 0 -> 1
340 }""") 340 }""")
341 341
342 def test_double_range(self): 342 def test_double_range(self):
343 self.assert_graphs(parse('[a-c][p-q]'), 343 self.assert_graphs(parse('[a-c][p-q]'),
344 """digraph { 344 """digraph {
345 0 [label="[a-c]"] 345 0 [label="[a-c]"]
346 1 [label="[pq]"] 346 1 [label="[pq]"]
347 2 [label="Match"] 347 2 [label="Match"]
348 0 -> 1 348 0 -> 1
349 1 -> 2 349 1 -> 2
350 }""") 350 }""")
351 351
352 def test_single_range(self): 352 def test_single_range(self):
353 self.assert_graphs(parse('[a]'), 353 self.assert_graphs(parse('[a]'),
354 """digraph { 354 """digraph {
355 0 [label="a"] 355 0 [label="a"]
356 1 [label="Match"] 356 1 [label="Match"]
357 0 -> 1 357 0 -> 1
358 }""") 358 }""")
359 359
360 def test_auto_escaped_range(self): 360 def test_auto_escaped_range(self):
361 self.assert_graphs(parse('[]]'), 361 self.assert_graphs(parse('[]]'),
362 """digraph { 362 """digraph {
363 0 [label="]"] 363 0 [label="]"]
364 1 [label="Match"] 364 1 [label="Match"]
365 0 -> 1 365 0 -> 1
366 }""") 366 }""")
367 367
368 def test_inverted_range(self): 368 def test_inverted_range(self):
369 self.assert_graphs(parse('[^apz]'), 369 self.assert_graphs(parse('[^apz]'),
370 r"""digraph { 370 r"""digraph {
371 0 [label="[^apz]"] 371 0 [label="[^apz]"]
372 1 [label="Match"] 372 1 [label="Match"]
373 0 -> 1 373 0 -> 1
374 }""") 374 }""")
375 375
376 def test_escaped_range(self): 376 def test_escaped_range(self):
377 self.assert_graphs(parse(r'[\x00-`b-oq-y{-\U0010ffff]'), 377 self.assert_graphs(parse(r'[\x00-`b-oq-y{-\U0010ffff]'),
378 r"""digraph { 378 r"""digraph {
379 0 [label="[\\x00-`b-oq-y{-\\U0010ffff]"] 379 0 [label="[\\x00-`b-oq-y{-\\U0010ffff]"]
380 1 [label="Match"] 380 1 [label="Match"]
381 0 -> 1 381 0 -> 1
382 }""") 382 }""")
383 383
384 def test_x_escape(self): 384 def test_x_escape(self):
385 self.assert_graphs(parse('a\\x62c'), 385 self.assert_graphs(parse('a\\x62c'),
386 """digraph { 386 """digraph {
387 0 [label="abc"] 387 0 [label="abc"]
388 1 [label="Match"] 388 1 [label="Match"]
389 0 -> 1 389 0 -> 1
390 }""") 390 }""")
391 391
392 def test_u_escape(self): 392 def test_u_escape(self):
393 self.assert_graphs(parse('a\\u0062c'), 393 self.assert_graphs(parse('a\\u0062c'),
394 """digraph { 394 """digraph {
395 0 [label="abc"] 395 0 [label="abc"]
396 1 [label="Match"] 396 1 [label="Match"]
397 0 -> 1 397 0 -> 1
398 }""") 398 }""")
399 399
400 def test_U_escape(self): 400 def test_U_escape(self):
401 self.assert_graphs(parse('a\\U00000062c'), 401 self.assert_graphs(parse('a\\U00000062c'),
402 """digraph { 402 """digraph {
403 0 [label="abc"] 403 0 [label="abc"]
404 1 [label="Match"] 404 1 [label="Match"]
405 0 -> 1 405 0 -> 1
406 }""") 406 }""")
407 407
408 def test_escaped_escape(self): 408 def test_escaped_escape(self):
409 self.assert_graphs(parse('\\\\'), 409 self.assert_graphs(parse('\\\\'),
410 # unsure about this... 410 # unsure about this...
411 """digraph { 411 """digraph {
412 0 [label="\\\\"] 412 0 [label="\\\\"]
413 1 [label="Match"] 413 1 [label="Match"]
414 0 -> 1 414 0 -> 1
415 }""") 415 }""")
416 416
417 def test_dot(self): 417 def test_dot(self):
418 self.assert_graphs(parse('.'), 418 self.assert_graphs(parse('.'),
419 """digraph { 419 """digraph {
420 0 [label="."] 420 0 [label="."]
421 1 [label="Match"] 421 1 [label="Match"]
422 0 -> 1 422 0 -> 1
423 }""") 423 }""")
424 424
425 def test_lazy_character_plus(self): 425 def test_lazy_character_plus(self):
426 self.assert_graphs(parse('ab+?c'), 426 self.assert_graphs(parse('ab+?c'),
427 """digraph { 427 """digraph {
428 0 [label="a"] 428 0 [label="a"]
429 1 [label="b"] 429 1 [label="b"]
430 2 [label="...+?"] 430 2 [label="...+?"]
431 3 [label="c"] 431 3 [label="c"]
432 4 [label="Match"] 432 4 [label="Match"]
433 0 -> 1 433 0 -> 1
434 1 -> 2 434 1 -> 2
435 2 -> 3 435 2 -> 3
436 2 -> 1 436 2 -> 1
437 3 -> 4 437 3 -> 4
438 }""") 438 }""")
439 439
440 440
441 def test_lazy_character_star(self): 441 def test_lazy_character_star(self):
442 self.assert_graphs(parse('ab*?c'), 442 self.assert_graphs(parse('ab*?c'),
443 """digraph { 443 """digraph {
444 0 [label="a"] 444 0 [label="a"]
445 1 [label="...*?"] 445 1 [label="...*?"]
446 2 [label="c"] 446 2 [label="c"]
447 3 [label="b"] 447 3 [label="b"]
448 4 [label="Match"] 448 4 [label="Match"]
449 0 -> 1 449 0 -> 1
450 1 -> 2 450 1 -> 2
451 1 -> 3 451 1 -> 3
452 3 -> 1 452 3 -> 1
453 2 -> 4 453 2 -> 4
454 }""") 454 }""")
455 455
456 def test_lazy_character_question(self): 456 def test_lazy_character_question(self):
457 self.assert_graphs(parse('ab??c'), 457 self.assert_graphs(parse('ab??c'),
458 """digraph { 458 """digraph {
459 0 [label="a"] 459 0 [label="a"]
460 1 [label="...??"] 460 1 [label="...??"]
461 2 [label="c"] 461 2 [label="c"]
462 3 [label="b"] 462 3 [label="b"]
463 4 [label="Match"] 463 4 [label="Match"]
464 0 -> 1 464 0 -> 1
465 1 -> 2 465 1 -> 2
466 1 -> 3 466 1 -> 3
467 3 -> 2 467 3 -> 2
468 2 -> 4 468 2 -> 4
469 }""") 469 }""")
470 470
471 def test_lazy_group_plus(self): 471 def test_lazy_group_plus(self):
472 self.assert_graphs(parse('a(bc)+?d'), 472 self.assert_graphs(parse('a(bc)+?d'),
473 """digraph { 473 """digraph {
474 0 [label="a"] 474 0 [label="a"]
475 1 [label="(?P<1>"] 475 1 [label="(?P<1>"]
476 2 [label="bc"] 476 2 [label="bc"]
477 3 [label=")"] 477 3 [label=")"]
478 4 [label="...+?"] 478 4 [label="...+?"]
479 5 [label="d"] 479 5 [label="d"]
480 6 [label="Match"] 480 6 [label="Match"]
481 0 -> 1 481 0 -> 1
482 1 -> 2 482 1 -> 2
483 2 -> 3 483 2 -> 3
484 3 -> 4 484 3 -> 4
485 4 -> 5 485 4 -> 5
486 4 -> 1 486 4 -> 1
487 5 -> 6 487 5 -> 6
488 }""") 488 }""")
489 489
490 def test_lazy_group_star(self): 490 def test_lazy_group_star(self):
491 self.assert_graphs(parse('a(bc)*?d'), 491 self.assert_graphs(parse('a(bc)*?d'),
492 """digraph { 492 """digraph {
493 0 [label="a"] 493 0 [label="a"]
494 1 [label="...*?"] 494 1 [label="...*?"]
495 2 [label="d"] 495 2 [label="d"]
496 3 [label="(?P<1>"] 496 3 [label="(?P<1>"]
497 4 [label="bc"] 497 4 [label="bc"]
498 5 [label=")"] 498 5 [label=")"]
499 6 [label="Match"] 499 6 [label="Match"]
500 0 -> 1 500 0 -> 1
501 1 -> 2 501 1 -> 2
502 1 -> 3 502 1 -> 3
503 3 -> 4 503 3 -> 4
504 4 -> 5 504 4 -> 5
505 5 -> 1 505 5 -> 1
506 2 -> 6 506 2 -> 6
507 }""") 507 }""")
508 508
509 def test_alternatives(self): 509 def test_alternatives(self):
510 self.assert_graphs(parse('a|'), 510 self.assert_graphs(parse('a|'),
511 """digraph { 511 """digraph {
512 0 [label="...|..."] 512 0 [label="...|..."]
513 1 [label="a"] 513 1 [label="a"]
514 2 [label="Match"] 514 2 [label="Match"]
515 0 -> 1 515 0 -> 1
516 0 -> 2 516 0 -> 2
517 1 -> 2 517 1 -> 2
518 }""") 518 }""")
519 self.assert_graphs(parse('a|b|cd'), 519 self.assert_graphs(parse('a|b|cd'),
520 """digraph { 520 """digraph {
521 0 [label="...|..."] 521 0 [label="...|..."]
522 1 [label="a"] 522 1 [label="a"]
523 2 [label="b"] 523 2 [label="b"]
524 3 [label="cd"] 524 3 [label="cd"]
525 4 [label="Match"] 525 4 [label="Match"]
526 0 -> 1 526 0 -> 1
527 0 -> 2 527 0 -> 2
528 0 -> 3 528 0 -> 3
529 3 -> 4 529 3 -> 4
530 2 -> 4 530 2 -> 4
531 1 -> 4 531 1 -> 4
532 }""") 532 }""")
533 533
534 def test_group_alternatives(self): 534 def test_group_alternatives(self):
535 self.assert_graphs(parse('(a|b|cd)'), 535 self.assert_graphs(parse('(a|b|cd)'),
536 """digraph { 536 """digraph {
537 0 [label="(?P<1>"] 537 0 [label="(?P<1>"]
538 1 [label="...|..."] 538 1 [label="...|..."]
539 2 [label="a"] 539 2 [label="a"]
540 3 [label="b"] 540 3 [label="b"]
541 4 [label="cd"] 541 4 [label="cd"]
542 5 [label=")"] 542 5 [label=")"]
543 6 [label="Match"] 543 6 [label="Match"]
544 0 -> 1 544 0 -> 1
545 1 -> 2 545 1 -> 2
546 1 -> 3 546 1 -> 3
547 1 -> 4 547 1 -> 4
548 4 -> 5 548 4 -> 5
549 5 -> 6 549 5 -> 6
550 3 -> 5 550 3 -> 5
551 2 -> 5 551 2 -> 5
552 }""") 552 }""")
553 self.assert_graphs(parse('(a|)'), 553 self.assert_graphs(parse('(a|)'),
554 """digraph { 554 """digraph {
555 0 [label="(?P<1>"] 555 0 [label="(?P<1>"]
556 1 [label="...|..."] 556 1 [label="...|..."]
557 2 [label="a"] 557 2 [label="a"]
558 3 [label=")"] 558 3 [label=")"]
559 4 [label="Match"] 559 4 [label="Match"]
560 0 -> 1 560 0 -> 1
561 1 -> 2 561 1 -> 2
562 1 -> 3 562 1 -> 3
563 3 -> 4 563 3 -> 4
564 2 -> 3 564 2 -> 3
565 }""") 565 }""")
566 566
567 def test_nested_groups(self): 567 def test_nested_groups(self):
568 self.assert_graphs(parse('a|(b|cd)'), 568 self.assert_graphs(parse('a|(b|cd)'),
569 """digraph { 569 """digraph {
570 0 [label="...|..."] 570 0 [label="...|..."]
571 1 [label="a"] 571 1 [label="a"]
572 2 [label="(?P<1>"] 572 2 [label="(?P<1>"]
573 3 [label="...|..."] 573 3 [label="...|..."]
574 4 [label="b"] 574 4 [label="b"]
575 5 [label="cd"] 575 5 [label="cd"]
576 6 [label=")"] 576 6 [label=")"]
577 7 [label="Match"] 577 7 [label="Match"]
578 0 -> 1 578 0 -> 1
579 0 -> 2 579 0 -> 2
580 2 -> 3 580 2 -> 3
581 3 -> 4 581 3 -> 4
582 3 -> 5 582 3 -> 5
583 5 -> 6 583 5 -> 6
584 6 -> 7 584 6 -> 7
585 4 -> 6 585 4 -> 6
586 1 -> 7 586 1 -> 7
587 }""") 587 }""")
588 588
589 def test_named_groups(self): 589 def test_named_groups(self):
590 self.assert_graphs(parse('a(?P<foo>b)c(?P=foo)d'), 590 self.assert_graphs(parse('a(?P<foo>b)c(?P=foo)d'),
591 """digraph { 591 """digraph {
592 0 [label="a"] 592 0 [label="a"]
593 1 [label="(?P<1>"] 593 1 [label="(?P<1>"]
594 2 [label="b"] 594 2 [label="b"]
595 3 [label=")"] 595 3 [label=")"]
596 4 [label="c"] 596 4 [label="c"]
597 5 [label="\\\\1"] 597 5 [label="\\\\1"]
598 6 [label="d"] 598 6 [label="d"]
599 7 [label="Match"] 599 7 [label="Match"]
600 0 -> 1 600 0 -> 1
601 1 -> 2 601 1 -> 2
602 2 -> 3 602 2 -> 3
603 3 -> 4 603 3 -> 4
604 4 -> 5 604 4 -> 5
605 5 -> 6 605 5 -> 6
606 6 -> 7 606 6 -> 7
607 }""") 607 }""")
608 608
609 # def test_comment(self): 609 # def test_comment(self):
610 # self.assert_graphs(parse('a(?#hello world)b'), 610 # self.assert_graphs(parse('a(?#hello world)b'),
611 #"""digraph { 611 #"""digraph {
612 # 0 [label="ab"] 612 # 0 [label="ab"]
613 # 1 [label="Match"] 613 # 1 [label="Match"]
614 # 0 -> 1 614 # 0 -> 1
615 #}""") 615 #}""")
616 616
617 def test_lookahead(self): 617 def test_lookahead(self):
618 self.assert_graphs(parse('a(?=b+)c'), 618 self.assert_graphs(parse('a(?=b+)c'),
619 """digraph { 619 """digraph {
620 0 [label="a"] 620 0 [label="a"]
621 1 [label="(?=...)"] 621 1 [label="(?=...)"]
622 2 [label="c"] 622 2 [label="c"]
623 3 [label="b"] 623 3 [label="b"]
624 4 [label="...+"] 624 4 [label="...+"]
625 5 [label="Match"] 625 5 [label="Match"]
626 6 [label="Match"] 626 6 [label="Match"]
627 0 -> 1 627 0 -> 1
628 1 -> 2 628 1 -> 2
629 1 -> 3 629 1 -> 3
630 3 -> 4 630 3 -> 4
631 4 -> 3 631 4 -> 3
632 4 -> 5 632 4 -> 5
633 2 -> 6 633 2 -> 6
634 }""") 634 }""")
635 635
636 def test_lookback(self): 636 def test_lookback(self):
637 self.assert_graphs(parse('a(?<=b+)c'), 637 self.assert_graphs(parse('a(?<=b+)c'),
638 """digraph { 638 """digraph {
639 0 [label="a"] 639 0 [label="a"]
640 1 [label="(?<=...)"] 640 1 [label="(?<=...)"]
641 2 [label="c"] 641 2 [label="c"]
642 3 [label="b"] 642 3 [label="b"]
643 4 [label="...+"] 643 4 [label="...+"]
644 5 [label="$"] 644 5 [label="$"]
645 6 [label="Match"] 645 6 [label="Match"]
646 7 [label="Match"] 646 7 [label="Match"]
647 0 -> 1 647 0 -> 1
648 1 -> 2 648 1 -> 2
649 1 -> 3 649 1 -> 3
650 3 -> 4 650 3 -> 4
651 4 -> 3 651 4 -> 3
652 4 -> 5 652 4 -> 5
653 5 -> 6 653 5 -> 6
654 2 -> 7 654 2 -> 7
655 }""") 655 }""")
656 656
657 def test_stateful_count(self): 657 def test_stateful_count(self):
658 self.assert_graphs(parse('ab{1,2}c'), 658 self.assert_graphs(parse('ab{1,2}c'),
659 """digraph { 659 """digraph {
660 0 [label="a"] 660 0 [label="a"]
661 1 [label="{1,2}"] 661 1 [label="{1,2}"]
662 2 [label="c"] 662 2 [label="c"]
663 3 [label="b"] 663 3 [label="b"]
664 4 [label="Match"] 664 4 [label="Match"]
665 0 -> 1 665 0 -> 1
666 1 -> 2 666 1 -> 2
667 1 -> 3 667 1 -> 3
668 3 -> 1 668 3 -> 1
669 2 -> 4 669 2 -> 4
670 }""") 670 }""")
671 671
672 def test_lazy_stateful_count(self): 672 def test_lazy_stateful_count(self):
673 self.assert_graphs(parse('ab{1,2}?c'), 673 self.assert_graphs(parse('ab{1,2}?c'),
674 """digraph { 674 """digraph {
675 0 [label="a"] 675 0 [label="a"]
676 1 [label="{1,2}?"] 676 1 [label="{1,2}?"]
677 2 [label="c"] 677 2 [label="c"]
678 3 [label="b"] 678 3 [label="b"]
679 4 [label="Match"] 679 4 [label="Match"]
680 0 -> 1 680 0 -> 1
681 1 -> 2 681 1 -> 2
682 1 -> 3 682 1 -> 3
683 3 -> 1 683 3 -> 1
684 2 -> 4 684 2 -> 4
685 }""") 685 }""")
686 686
687 def test_stateful_open_count(self): 687 def test_stateful_open_count(self):
688 self.assert_graphs(parse('ab{1,}c'), 688 self.assert_graphs(parse('ab{1,}c'),
689 """digraph { 689 """digraph {
690 0 [label="a"] 690 0 [label="a"]
691 1 [label="{1,}"] 691 1 [label="{1,}"]
692 2 [label="c"] 692 2 [label="c"]
693 3 [label="b"] 693 3 [label="b"]
694 4 [label="Match"] 694 4 [label="Match"]
695 0 -> 1 695 0 -> 1
696 1 -> 2 696 1 -> 2
697 1 -> 3 697 1 -> 3
698 3 -> 1 698 3 -> 1
699 2 -> 4 699 2 -> 4
700 }""") 700 }""")
701 701
702 def test_stateful_fixed_count(self): 702 def test_stateful_fixed_count(self):
703 self.assert_graphs(parse('ab{2}c'), 703 self.assert_graphs(parse('ab{2}c'),
704 """digraph { 704 """digraph {
705 0 [label="a"] 705 0 [label="a"]
706 1 [label="{2}"] 706 1 [label="{2}"]
707 2 [label="c"] 707 2 [label="c"]
708 3 [label="b"] 708 3 [label="b"]
709 4 [label="Match"] 709 4 [label="Match"]
710 0 -> 1 710 0 -> 1
711 1 -> 2 711 1 -> 2
712 1 -> 3 712 1 -> 3
713 3 -> 1 713 3 -> 1
714 2 -> 4 714 2 -> 4
715 }""") 715 }""")
716 716
717 def test_stateful_group_count(self): 717 def test_stateful_group_count(self):
718 self.assert_graphs(parse('a(?:bc){1,2}d'), 718 self.assert_graphs(parse('a(?:bc){1,2}d'),
719 """digraph { 719 """digraph {
720 0 [label="a"] 720 0 [label="a"]
721 1 [label="{1,2}"] 721 1 [label="{1,2}"]
722 2 [label="d"] 722 2 [label="d"]
723 3 [label="bc"] 723 3 [label="bc"]
724 4 [label="Match"] 724 4 [label="Match"]
725 0 -> 1 725 0 -> 1
726 1 -> 2 726 1 -> 2
727 1 -> 3 727 1 -> 3
728 3 -> 1 728 3 -> 1
729 2 -> 4 729 2 -> 4
730 }""") 730 }""")
731 731
732 def test_stateless_count(self): 732 def test_stateless_count(self):
733 self.assert_graphs(parse('ab{1,2}c', 733 self.assert_graphs(parse('ab{1,2}c',
734 flags=ParserState._LOOP_UNROLL), 734 flags=ParserState._LOOP_UNROLL),
735 """digraph { 735 """digraph {
736 0 [label="ab"] 736 0 [label="ab"]
737 1 [label="...?"] 737 1 [label="...?"]
738 2 [label="b"] 738 2 [label="b"]
739 3 [label="c"] 739 3 [label="c"]
740 4 [label="Match"] 740 4 [label="Match"]
741 0 -> 1 741 0 -> 1
742 1 -> 2 742 1 -> 2
743 1 -> 3 743 1 -> 3
744 3 -> 4 744 3 -> 4
745 2 -> 3 745 2 -> 3
746 }""") 746 }""")
747 self.assert_graphs(parse('ab{1,2}c(?_l)'), 747 self.assert_graphs(parse('ab{1,2}c(?_l)'),
748 """digraph { 748 """digraph {
749 0 [label="ab"] 749 0 [label="ab"]
750 1 [label="...?"] 750 1 [label="...?"]
751 2 [label="b"] 751 2 [label="b"]
752 3 [label="c"] 752 3 [label="c"]
753 4 [label="Match"] 753 4 [label="Match"]
754 0 -> 1 754 0 -> 1
755 1 -> 2 755 1 -> 2
756 1 -> 3 756 1 -> 3
757 3 -> 4 757 3 -> 4
758 2 -> 3 758 2 -> 3
759 }""") 759 }""")
760 760
761 def test_stateless_open_count(self): 761 def test_stateless_open_count(self):
762 self.assert_graphs(parse('ab{3,}c', 762 self.assert_graphs(parse('ab{3,}c',
763 flags=ParserState._LOOP_UNROLL), 763 flags=ParserState._LOOP_UNROLL),
764 """digraph { 764 """digraph {
765 0 [label="abbb"] 765 0 [label="abbb"]
766 1 [label="...*"] 766 1 [label="...*"]
767 2 [label="b"] 767 2 [label="b"]
768 3 [label="c"] 768 3 [label="c"]
769 4 [label="Match"] 769 4 [label="Match"]
770 0 -> 1 770 0 -> 1
771 1 -> 2 771 1 -> 2
772 1 -> 3 772 1 -> 3
773 3 -> 4 773 3 -> 4
774 2 -> 1 774 2 -> 1
775 }""") 775 }""")
776 776
777 def test_stateless_fixed_count(self): 777 def test_stateless_fixed_count(self):
778 self.assert_graphs(parse('ab{2}c', 778 self.assert_graphs(parse('ab{2}c',
779 flags=ParserState._LOOP_UNROLL), 779 flags=ParserState._LOOP_UNROLL),
780 """digraph { 780 """digraph {
781 0 [label="abbc"] 781 0 [label="abbc"]
782 1 [label="Match"] 782 1 [label="Match"]
783 0 -> 1 783 0 -> 1
784 }""") 784 }""")
785 785
786 def test_stateless_group_count(self): 786 def test_stateless_group_count(self):
787 self.assert_graphs(parse('a(?:bc){1,2}d', 787 self.assert_graphs(parse('a(?:bc){1,2}d',
788 flags=ParserState._LOOP_UNROLL), 788 flags=ParserState._LOOP_UNROLL),
789 """digraph { 789 """digraph {
790 0 [label="abc"] 790 0 [label="abc"]
791 1 [label="...?"] 791 1 [label="...?"]
792 2 [label="bc"] 792 2 [label="bc"]
793 3 [label="d"] 793 3 [label="d"]
794 4 [label="Match"] 794 4 [label="Match"]
795 0 -> 1 795 0 -> 1
796 1 -> 2 796 1 -> 2
797 1 -> 3 797 1 -> 3
798 3 -> 4 798 3 -> 4
799 2 -> 3 799 2 -> 3
800 }""") 800 }""")
801 801
802 def test_lazy_stateless_count(self): 802 def test_lazy_stateless_count(self):
803 self.assert_graphs(parse('ab{1,2}?c', 803 self.assert_graphs(parse('ab{1,2}?c',
804 flags=ParserState._LOOP_UNROLL), 804 flags=ParserState._LOOP_UNROLL),
805 """digraph { 805 """digraph {
806 0 [label="ab"] 806 0 [label="ab"]
807 1 [label="...??"] 807 1 [label="...??"]
808 2 [label="c"] 808 2 [label="c"]
809 3 [label="b"] 809 3 [label="b"]
810 4 [label="Match"] 810 4 [label="Match"]
811 0 -> 1 811 0 -> 1
812 1 -> 2 812 1 -> 2
813 1 -> 3 813 1 -> 3
814 3 -> 2 814 3 -> 2
815 2 -> 4 815 2 -> 4
816 }""") 816 }""")
817 817
818 def test_lazy_stateless_open_count(self): 818 def test_lazy_stateless_open_count(self):
819 self.assert_graphs(parse('ab{3,}?c', 819 self.assert_graphs(parse('ab{3,}?c',
820 flags=ParserState._LOOP_UNROLL), 820 flags=ParserState._LOOP_UNROLL),
821 """digraph { 821 """digraph {
822 0 [label="abbb"] 822 0 [label="abbb"]
823 1 [label="...*?"] 823 1 [label="...*?"]
824 2 [label="c"] 824 2 [label="c"]
825 3 [label="b"] 825 3 [label="b"]
826 4 [label="Match"] 826 4 [label="Match"]
827 0 -> 1 827 0 -> 1
828 1 -> 2 828 1 -> 2
829 1 -> 3 829 1 -> 3
830 3 -> 1 830 3 -> 1
831 2 -> 4 831 2 -> 4
832 }""") 832 }""")
833 833
834 def test_lazy_stateless_fixed_count(self): 834 def test_lazy_stateless_fixed_count(self):
835 self.assert_graphs(parse('ab{2}?c', 835 self.assert_graphs(parse('ab{2}?c',
836 flags=ParserState._LOOP_UNROLL), 836 flags=ParserState._LOOP_UNROLL),
837 """digraph { 837 """digraph {
838 0 [label="abbc"] 838 0 [label="abbc"]
839 1 [label="Match"] 839 1 [label="Match"]
840 0 -> 1 840 0 -> 1
841 }""") 841 }""")
842 842
843 def test_lazy_stateless_group_count(self): 843 def test_lazy_stateless_group_count(self):
844 self.assert_graphs(parse('a(?:bc){1,2}?d', 844 self.assert_graphs(parse('a(?:bc){1,2}?d',
845 flags=ParserState._LOOP_UNROLL), 845 flags=ParserState._LOOP_UNROLL),
846 """digraph { 846 """digraph {
847 0 [label="abc"] 847 0 [label="abc"]
848 1 [label="...??"] 848 1 [label="...??"]
849 2 [label="d"] 849 2 [label="d"]
850 3 [label="bc"] 850 3 [label="bc"]
851 4 [label="Match"] 851 4 [label="Match"]
852 0 -> 1 852 0 -> 1
853 1 -> 2 853 1 -> 2
854 1 -> 3 854 1 -> 3
855 3 -> 2 855 3 -> 2
856 2 -> 4 856 2 -> 4
857 }""") 857 }""")
858 858
859 def test_octal_escape(self): 859 def test_octal_escape(self):
860 self.assert_graphs(parse('a\\075c'), 860 self.assert_graphs(parse('a\\075c'),
861 """digraph { 861 """digraph {
862 0 [label="a=c"] 862 0 [label="a=c"]
863 1 [label="Match"] 863 1 [label="Match"]
864 0 -> 1 864 0 -> 1
865 }""") 865 }""")
866 self.assert_graphs(parse('a\\142c'), 866 self.assert_graphs(parse('a\\142c'),
867 """digraph { 867 """digraph {
868 0 [label="abc"] 868 0 [label="abc"]
869 1 [label="Match"] 869 1 [label="Match"]
870 0 -> 1 870 0 -> 1
871 }""") 871 }""")
872 self.assert_graphs(parse('a\\142c', engine=DummyEngine), 872 self.assert_graphs(parse('a\\142c', engine=DummyEngine),
873 """digraph { 873 """digraph {
874 0 [label="a"] 874 0 [label="a"]
875 1 [label="b"] 875 1 [label="b"]
876 2 [label="c"] 876 2 [label="c"]
877 3 [label="Match"] 877 3 [label="Match"]
878 0 -> 1 878 0 -> 1
879 1 -> 2 879 1 -> 2
880 2 -> 3 880 2 -> 3
881 }""") 881 }""")
882 882
883 def test_numbered_groups(self): 883 def test_numbered_groups(self):
884 self.assert_graphs(parse('a(b)c\\1d'), 884 self.assert_graphs(parse('a(b)c\\1d'),
885 """digraph { 885 """digraph {
886 0 [label="a"] 886 0 [label="a"]
887 1 [label="(?P<1>"] 887 1 [label="(?P<1>"]
888 2 [label="b"] 888 2 [label="b"]
889 3 [label=")"] 889 3 [label=")"]
890 4 [label="c"] 890 4 [label="c"]
891 5 [label="\\\\1"] 891 5 [label="\\\\1"]
892 6 [label="d"] 892 6 [label="d"]
893 7 [label="Match"] 893 7 [label="Match"]
894 0 -> 1 894 0 -> 1
895 1 -> 2 895 1 -> 2
896 2 -> 3 896 2 -> 3
897 3 -> 4 897 3 -> 4
898 4 -> 5 898 4 -> 5
899 5 -> 6 899 5 -> 6
900 6 -> 7 900 6 -> 7
901 }""") 901 }""")
902 902
903 def test_simple_escape(self): 903 def test_simple_escape(self):
904 self.assert_graphs(parse('a\\nc'), 904 self.assert_graphs(parse('a\\nc'),
905 """digraph { 905 """digraph {
906 0 [label="a\\\\nc"] 906 0 [label="a\\\\nc"]
907 1 [label="Match"] 907 1 [label="Match"]
908 0 -> 1 908 0 -> 1
909 }""") 909 }""")
910 910
911 def test_conditional(self): 911 def test_conditional(self):
912 # in all cases, "no" is the first alternative 912 # in all cases, "no" is the first alternative
913 self.assert_graphs(parse('(a)(?(1)b)'), 913 self.assert_graphs(parse('(a)(?(1)b)'),
914 """digraph { 914 """digraph {
915 0 [label="(?P<1>"] 915 0 [label="(?P<1>"]
916 1 [label="a"] 916 1 [label="a"]
917 2 [label=")"] 917 2 [label=")"]
918 3 [label="(?(1)...)"] 918 3 [label="(?(1)...)"]
919 4 [label="Match"] 919 4 [label="Match"]
920 5 [label="b"] 920 5 [label="b"]
921 0 -> 1 921 0 -> 1
922 1 -> 2 922 1 -> 2
923 2 -> 3 923 2 -> 3
924 3 -> 4 924 3 -> 4
925 3 -> 5 925 3 -> 5
926 5 -> 4 926 5 -> 4
927 }""") 927 }""")
928 self.assert_graphs(parse('(a)(?(1)b|cd)'), 928 self.assert_graphs(parse('(a)(?(1)b|cd)'),
929 """digraph { 929 """digraph {
930 0 [label="(?P<1>"] 930 0 [label="(?P<1>"]
931 1 [label="a"] 931 1 [label="a"]
932 2 [label=")"] 932 2 [label=")"]
933 3 [label="(?(1)...|...)"] 933 3 [label="(?(1)...|...)"]
934 4 [label="cd"] 934 4 [label="cd"]
935 5 [label="b"] 935 5 [label="b"]
936 6 [label="Match"] 936 6 [label="Match"]
937 0 -> 1 937 0 -> 1
938 1 -> 2 938 1 -> 2
939 2 -> 3 939 2 -> 3
940 3 -> 4 940 3 -> 4
941 3 -> 5 941 3 -> 5
942 5 -> 6 942 5 -> 6
943 4 -> 6 943 4 -> 6
944 }""") 944 }""")
945 # so here, 'no' goes to b and is before the direct jump to Match 945 # so here, 'no' goes to b and is before the direct jump to Match
946 # (3->4 before 3->5) 946 # (3->4 before 3->5)
947 self.assert_graphs(parse('(a)(?(1)|b)'), 947 self.assert_graphs(parse('(a)(?(1)|b)'),
948 """digraph { 948 """digraph {
949 0 [label="(?P<1>"] 949 0 [label="(?P<1>"]
950 1 [label="a"] 950 1 [label="a"]
951 2 [label=")"] 951 2 [label=")"]
952 3 [label="(?(1)|...)"] 952 3 [label="(?(1)|...)"]
953 4 [label="b"] 953 4 [label="b"]
954 5 [label="Match"] 954 5 [label="Match"]
955 0 -> 1 955 0 -> 1
956 1 -> 2 956 1 -> 2
957 2 -> 3 957 2 -> 3
958 3 -> 4 958 3 -> 4
959 3 -> 5 959 3 -> 5
960 4 -> 5 960 4 -> 5
961 }""") 961 }""")
962 self.assert_graphs(parse('(a)(?(1)|)'), 962 self.assert_graphs(parse('(a)(?(1)|)'),
963 """digraph { 963 """digraph {
964 0 [label="(?P<1>"] 964 0 [label="(?P<1>"]
965 1 [label="a"] 965 1 [label="a"]
966 2 [label=")"] 966 2 [label=")"]
967 3 [label="(?(1)|)"] 967 3 [label="(?(1)|)"]
968 4 [label="Match"] 968 4 [label="Match"]
969 0 -> 1 969 0 -> 1
970 1 -> 2 970 1 -> 2
971 2 -> 3 971 2 -> 3
972 3 -> 4 972 3 -> 4
973 3 -> 4 973 3 -> 4
974 }""") 974 }""")
975 self.assert_graphs(parse('(a)(?(1))'), 975 self.assert_graphs(parse('(a)(?(1))'),
976 """digraph { 976 """digraph {
977 0 [label="(?P<1>"] 977 0 [label="(?P<1>"]
978 1 [label="a"] 978 1 [label="a"]
979 2 [label=")"] 979 2 [label=")"]
980 3 [label="(?(1)|)"] 980 3 [label="(?(1)|)"]
981 4 [label="Match"] 981 4 [label="Match"]
982 0 -> 1 982 0 -> 1
983 1 -> 2 983 1 -> 2
984 2 -> 3 984 2 -> 3
985 3 -> 4 985 3 -> 4
986 3 -> 4 986 3 -> 4
987 }""") 987 }""")
988 988
989 def test_character_escape(self): 989 def test_character_escape(self):
990 self.assert_graphs(parse(r'\A\b\B\d\D\s\S\w\W\Z'), 990 self.assert_graphs(parse(r'\A\b\B\d\D\s\S\w\W\Z'),
991 """digraph { 991 """digraph {
992 0 [label="^"] 992 0 [label="^"]
993 1 [label="\\\\b"] 993 1 [label="\\\\b"]
994 2 [label="\\\\B"] 994 2 [label="\\\\B"]
995 3 [label="\\\\d"] 995 3 [label="\\\\d"]
996 4 [label="\\\\D"] 996 4 [label="\\\\D"]
997 5 [label="\\\\s"] 997 5 [label="\\\\s"]
998 6 [label="\\\\S"] 998 6 [label="\\\\S"]
999 7 [label="\\\\w"] 999 7 [label="\\\\w"]
1000 8 [label="\\\\W"] 1000 8 [label="\\\\W"]
1001 9 [label="$"] 1001 9 [label="$"]
1002 10 [label="Match"] 1002 10 [label="Match"]
1003 0 -> 1 1003 0 -> 1
1004 1 -> 2 1004 1 -> 2
1005 2 -> 3 1005 2 -> 3
1006 3 -> 4 1006 3 -> 4
1007 4 -> 5 1007 4 -> 5
1008 5 -> 6 1008 5 -> 6
1009 6 -> 7 1009 6 -> 7
1010 7 -> 8 1010 7 -> 8
1011 8 -> 9 1011 8 -> 9
1012 9 -> 10 1012 9 -> 10
1013 }""") 1013 }""")
1014 1014
1015 def test_named_group_bug(self): 1015 def test_named_group_bug(self):
1016 self.assert_graphs(parse('(?P<quote>)(?(quote))'), 1016 self.assert_graphs(parse('(?P<quote>)(?(quote))'),
1017 """digraph { 1017 """digraph {
1018 0 [label="(?P<1>"] 1018 0 [label="(?P<1>"]
1019 1 [label=")"] 1019 1 [label=")"]
1020 2 [label="(?(quote)|)"] 1020 2 [label="(?(quote)|)"]
1021 3 [label="Match"] 1021 3 [label="Match"]
1022 0 -> 1 1022 0 -> 1
1023 1 -> 2 1023 1 -> 2
1024 2 -> 3 1024 2 -> 3
1025 2 -> 3 1025 2 -> 3
1026 }""") 1026 }""")
1027 1027
1028 def test_pickle_bug(self): 1028 def test_pickle_bug(self):
1029 self.assert_graphs(parse('(?:b|c)+'), 1029 self.assert_graphs(parse('(?:b|c)+'),
1030 """digraph { 1030 """digraph {
1031 0 [label="...|..."] 1031 0 [label="...|..."]
1032 1 [label="b"] 1032 1 [label="b"]
1033 2 [label="c"] 1033 2 [label="c"]
1034 3 [label="...+"] 1034 3 [label="...+"]
1035 4 [label="Match"] 1035 4 [label="Match"]
1036 0 -> 1 1036 0 -> 1
1037 0 -> 2 1037 0 -> 2
1038 2 -> 3 1038 2 -> 3
1039 3 -> 0 1039 3 -> 0
1040 3 -> 4 1040 3 -> 4
1041 1 -> 3 1041 1 -> 3
1042 }""") 1042 }""")
1043 self.assert_graphs(parse('a(?:b|(c|e){1,2}?|d)+?(.)', 1043 self.assert_graphs(parse('a(?:b|(c|e){1,2}?|d)+?(.)',
1044 flags=ParserState._LOOP_UNROLL), 1044 flags=ParserState._LOOP_UNROLL),
1045 """digraph { 1045 """digraph {
1046 0 [label="a"] 1046 0 [label="a"]
1047 1 [label="...|..."] 1047 1 [label="...|..."]
1048 2 [label="b"] 1048 2 [label="b"]
1049 3 [label="(?P<1>"] 1049 3 [label="(?P<1>"]
1050 4 [label="d"] 1050 4 [label="d"]
1051 5 [label="...+?"] 1051 5 [label="...+?"]
1052 6 [label="(?P<2>"] 1052 6 [label="(?P<2>"]
1053 7 [label="."] 1053 7 [label="."]
1054 8 [label=")"] 1054 8 [label=")"]
1055 9 [label="Match"] 1055 9 [label="Match"]
1056 10 [label="...|..."] 1056 10 [label="...|..."]
1057 11 [label="c"] 1057 11 [label="c"]
1058 12 [label="e"] 1058 12 [label="e"]
1059 13 [label=")"] 1059 13 [label=")"]
1060 14 [label="...??"] 1060 14 [label="...??"]
1061 15 [label="(?P<1>"] 1061 15 [label="(?P<1>"]
1062 16 [label="...|..."] 1062 16 [label="...|..."]
1063 17 [label="c"] 1063 17 [label="c"]
1064 18 [label="e"] 1064 18 [label="e"]
1065 19 [label=")"] 1065 19 [label=")"]
1066 0 -> 1 1066 0 -> 1
1067 1 -> 2 1067 1 -> 2
1068 1 -> 3 1068 1 -> 3
1069 1 -> 4 1069 1 -> 4
1070 4 -> 5 1070 4 -> 5
1071 5 -> 6 1071 5 -> 6
1072 5 -> 1 1072 5 -> 1
1073 6 -> 7 1073 6 -> 7
1074 7 -> 8 1074 7 -> 8
1075 8 -> 9 1075 8 -> 9
1076 3 -> 10 1076 3 -> 10
1077 10 -> 11 1077 10 -> 11
1078 10 -> 12 1078 10 -> 12
1079 12 -> 13 1079 12 -> 13
1080 13 -> 14 1080 13 -> 14
1081 14 -> 5 1081 14 -> 5
1082 14 -> 15 1082 14 -> 15
1083 15 -> 16 1083 15 -> 16
1084 16 -> 17 1084 16 -> 17
1085 16 -> 18 1085 16 -> 18
1086 18 -> 19 1086 18 -> 19
1087 19 -> 5 1087 19 -> 5
1088 17 -> 19 1088 17 -> 19
1089 11 -> 13 1089 11 -> 13
1090 2 -> 5 1090 2 -> 5
1091 }""") 1091 }""")
1092 1092
1093 def test_checkpoint(self): 1093 def test_checkpoint(self):
1094 self.assert_graphs(parse('(a|b)*'), 1094 self.assert_graphs(parse('(a|b)*'),
1095 """digraph { 1095 """digraph {
1096 0 [label="...*"] 1096 0 [label="...*"]
1097 1 [label="(?P<1>"] 1097 1 [label="(?P<1>"]
1098 2 [label="Match"] 1098 2 [label="Match"]
1099 3 [label="...|..."] 1099 3 [label="...|..."]
1100 4 [label="a"] 1100 4 [label="a"]
1101 5 [label="b"] 1101 5 [label="b"]
1102 6 [label=")"] 1102 6 [label=")"]
1103 0 -> 1 1103 0 -> 1
1104 0 -> 2 1104 0 -> 2
1105 1 -> 3 1105 1 -> 3
1106 3 -> 4 1106 3 -> 4
1107 3 -> 5 1107 3 -> 5
1108 5 -> 6 1108 5 -> 6
1109 6 -> 0 1109 6 -> 0
1110 4 -> 6 1110 4 -> 6
1111 }""") 1111 }""")
1112 self.assert_graphs(parse('(?(1)(a))*', flags=ParserState._EMPTY), 1112 self.assert_graphs(parse('(?(1)(a))*', flags=ParserState._EMPTY),
1113 """digraph { 1113 """digraph {
1114 0 [label="...*"] 1114 0 [label="...*"]
1115 1 [label="(?(1)...)"] 1115 1 [label="(?(1)...)"]
1116 2 [label="Match"] 1116 2 [label="Match"]
1117 3 [label="!"] 1117 3 [label="!"]
1118 4 [label="(?P<1>"] 1118 4 [label="(?P<1>"]
1119 5 [label="a"] 1119 5 [label="a"]
1120 6 [label=")"] 1120 6 [label=")"]
1121 0 -> 1 1121 0 -> 1
1122 0 -> 2 1122 0 -> 2
1123 1 -> 3 1123 1 -> 3
1124 1 -> 4 1124 1 -> 4
1125 4 -> 5 1125 4 -> 5
1126 5 -> 6 1126 5 -> 6
1127 6 -> 3 1127 6 -> 3
1128 3 -> 0 1128 3 -> 0
1129 }""") 1129 }""")
1130 self.assert_graphs(parse('(?(1)(a))*', flags=ParserState._EMPTY|ParserState._UNSAFE), 1130 self.assert_graphs(parse('(?(1)(a))*', flags=ParserState._EMPTY|ParserState._UNSAFE),
1131 """digraph { 1131 """digraph {
1132 0 [label="...*"] 1132 0 [label="...*"]
1133 1 [label="(?(1)...)"] 1133 1 [label="(?(1)...)"]
1134 2 [label="Match"] 1134 2 [label="Match"]
1135 3 [label="(?P<1>"] 1135 3 [label="(?P<1>"]
1136 4 [label="a"] 1136 4 [label="a"]
1137 5 [label=")"] 1137 5 [label=")"]
1138 0 -> 1 1138 0 -> 1
1139 0 -> 2 1139 0 -> 2
1140 1 -> 0 1140 1 -> 0
1141 1 -> 3 1141 1 -> 3
1142 3 -> 4 1142 3 -> 4
1143 4 -> 5 1143 4 -> 5
1144 5 -> 0 1144 5 -> 0
1145 }""") 1145 }""")
1146 1146
1147 def test_empty_bug(self): 1147 def test_empty_bug(self):
1148 self.assert_graphs(parse('(?_l_e)a{0,1}*'), 1148 self.assert_graphs(parse('(?_l_e)a{0,1}*'),
1149 """digraph { 1149 """digraph {
1150 0 [label="...*"] 1150 0 [label="...*"]
1151 1 [label="...?"] 1151 1 [label="...?"]
1152 2 [label="Match"] 1152 2 [label="Match"]
1153 3 [label="a"] 1153 3 [label="a"]
1154 4 [label="!"] 1154 4 [label="!"]
1155 0 -> 1 1155 0 -> 1
1156 0 -> 2 1156 0 -> 2
1157 1 -> 3 1157 1 -> 3
1158 1 -> 4 1158 1 -> 4
1159 4 -> 0 1159 4 -> 0
1160 3 -> 4 1160 3 -> 4
1161 }""") 1161 }""")
1162 1162
1163 def test_merge_clone_bug(self): 1163 def test_merge_clone_bug(self):
1164 self.assert_graphs(parse('(?_l)(?:a?){1,2}'), 1164 self.assert_graphs(parse('(?_l)(?:a?){1,2}'),
1165 """digraph { 1165 """digraph {
1166 0 [label="...?"] 1166 0 [label="...?"]
1167 1 [label="a"] 1167 1 [label="a"]
1168 2 [label="...?"] 1168 2 [label="...?"]
1169 3 [label="...?"] 1169 3 [label="...?"]
1170 4 [label="Match"] 1170 4 [label="Match"]
1171 5 [label="a"] 1171 5 [label="a"]
1172 0 -> 1 1172 0 -> 1
1173 0 -> 2 1173 0 -> 2
1174 2 -> 3 1174 2 -> 3
1175 2 -> 4 1175 2 -> 4
1176 3 -> 5 1176 3 -> 5
1177 3 -> 4 1177 3 -> 4
1178 5 -> 4 1178 5 -> 4
1179 1 -> 2 1179 1 -> 2
1180 }""") 1180 }""")
1181 1181
1182 def test_ascii_escapes_bug(self): 1182 def test_ascii_escapes_bug(self):
1183 self.assert_graphs(parse(r'(\s*(\b\w+\b)\s*){3}', 1183 self.assert_graphs(parse(r'(\s*(\b\w+\b)\s*){3}',
1184 flags=ParserState._LOOP_UNROLL|ParserState.ASCII), 1184 flags=ParserState._LOOP_UNROLL|ParserState.ASCII),
1185 """digraph { 1185 """digraph {
1186 0 [label="(?P<1>"] 1186 0 [label="(?P<1>"]
1187 1 [label="...*"] 1187 1 [label="...*"]
1188 2 [label="\\\\s"] 1188 2 [label="\\\\s"]
1189 3 [label="(?P<2>"] 1189 3 [label="(?P<2>"]
1190 4 [label="\\\\b"] 1190 4 [label="\\\\b"]
1191 5 [label="\\\\w"] 1191 5 [label="\\\\w"]
1192 6 [label="...+"] 1192 6 [label="...+"]
1193 7 [label="\\\\b"] 1193 7 [label="\\\\b"]
1194 8 [label=")"] 1194 8 [label=")"]
1195 9 [label="...*"] 1195 9 [label="...*"]
1196 10 [label="\\\\s"] 1196 10 [label="\\\\s"]
1197 11 [label=")"] 1197 11 [label=")"]
1198 12 [label="(?P<1>"] 1198 12 [label="(?P<1>"]
1199 13 [label="...*"] 1199 13 [label="...*"]
1200 14 [label="\\\\s"] 1200 14 [label="\\\\s"]
1201 15 [label="(?P<2>"] 1201 15 [label="(?P<2>"]
1202 16 [label="\\\\b"] 1202 16 [label="\\\\b"]
1203 17 [label="\\\\w"] 1203 17 [label="\\\\w"]
1204 18 [label="...+"] 1204 18 [label="...+"]
1205 19 [label="\\\\b"] 1205 19 [label="\\\\b"]
1206 20 [label=")"] 1206 20 [label=")"]
1207 21 [label="...*"] 1207 21 [label="...*"]
1208 22 [label="\\\\s"] 1208 22 [label="\\\\s"]
1209 23 [label=")"] 1209 23 [label=")"]
1210 24 [label="(?P<1>"] 1210 24 [label="(?P<1>"]
1211 25 [label="...*"] 1211 25 [label="...*"]
1212 26 [label="\\\\s"] 1212 26 [label="\\\\s"]
1213 27 [label="(?P<2>"] 1213 27 [label="(?P<2>"]
1214 28 [label="\\\\b"] 1214 28 [label="\\\\b"]
1215 29 [label="\\\\w"] 1215 29 [label="\\\\w"]
1216 30 [label="...+"] 1216 30 [label="...+"]
1217 31 [label="\\\\b"] 1217 31 [label="\\\\b"]
1218 32 [label=")"] 1218 32 [label=")"]
1219 33 [label="...*"] 1219 33 [label="...*"]
1220 34 [label="\\\\s"] 1220 34 [label="\\\\s"]
1221 35 [label=")"] 1221 35 [label=")"]
1222 36 [label="Match"] 1222 36 [label="Match"]
1223 0 -> 1 1223 0 -> 1
1224 1 -> 2 1224 1 -> 2
1225 1 -> 3 1225 1 -> 3
1226 3 -> 4 1226 3 -> 4
1227 4 -> 5 1227 4 -> 5
1228 5 -> 6 1228 5 -> 6
1229 6 -> 5 1229 6 -> 5
1230 6 -> 7 1230 6 -> 7
1231 7 -> 8 1231 7 -> 8
1232 8 -> 9 1232 8 -> 9
1233 9 -> 10 1233 9 -> 10
1234 9 -> 11 1234 9 -> 11
1235 11 -> 12 1235 11 -> 12
1236 12 -> 13 1236 12 -> 13
1237 13 -> 14 1237 13 -> 14
1238 13 -> 15 1238 13 -> 15
1239 15 -> 16 1239 15 -> 16
1240 16 -> 17 1240 16 -> 17
1241 17 -> 18 1241 17 -> 18
1242 18 -> 17 1242 18 -> 17
1243 18 -> 19 1243 18 -> 19
1244 19 -> 20 1244 19 -> 20
1245 20 -> 21 1245 20 -> 21
1246 21 -> 22 1246 21 -> 22
1247 21 -> 23 1247 21 -> 23
1248 23 -> 24 1248 23 -> 24
1249 24 -> 25 1249 24 -> 25
1250 25 -> 26 1250 25 -> 26
1251 25 -> 27 1251 25 -> 27
1252 27 -> 28 1252 27 -> 28
1253 28 -> 29 1253 28 -> 29
1254 29 -> 30 1254 29 -> 30
1255 30 -> 29 1255 30 -> 29
1256 30 -> 31 1256 30 -> 31
1257 31 -> 32 1257 31 -> 32
1258 32 -> 33 1258 32 -> 33
1259 33 -> 34 1259 33 -> 34
1260 33 -> 35 1260 33 -> 35
1261 35 -> 36 1261 35 -> 36
1262 34 -> 33 1262 34 -> 33
1263 26 -> 25 1263 26 -> 25
1264 22 -> 21 1264 22 -> 21
1265 14 -> 13 1265 14 -> 13
1266 10 -> 9 1266 10 -> 9
1267 2 -> 1 1267 2 -> 1
1268 }""") 1268 }""")
1269 1269
1270 def test_duplicate_group(self): 1270 def test_duplicate_group(self):
1271 self.assert_graphs(parse(r'(?_g)(?P<a>.)(?P<a>.)'), 1271 self.assert_graphs(parse(r'(?_g)(?P<a>.)(?P<a>.)'),
1272 """digraph { 1272 """digraph {
1273 0 [label="(?P<1>"] 1273 0 [label="(?P<1>"]
1274 1 [label="."] 1274 1 [label="."]
1275 2 [label=")"] 1275 2 [label=")"]
1276 3 [label="(?P<1>"] 1276 3 [label="(?P<1>"]
1277 4 [label="."] 1277 4 [label="."]
1278 5 [label=")"] 1278 5 [label=")"]
1279 6 [label="Match"] 1279 6 [label="Match"]
1280 0 -> 1 1280 0 -> 1
1281 1 -> 2 1281 1 -> 2
1282 2 -> 3 1282 2 -> 3
1283 3 -> 4 1283 3 -> 4
1284 4 -> 5 1284 4 -> 5
1285 5 -> 6 1285 5 -> 6
1286 }""") 1286 }""")
1287 try: 1287 try:
1288 parse(r'(?P<a>.)(?P<a>.)') 1288 parse(r'(?P<a>.)(?P<a>.)')
1289 assert False, 'expected error' 1289 assert False, 'expected error'
1290 except SimpleGroupError: 1290 except SimpleGroupError:
1291 pass 1291 pass
1292 1292
1293 def test_star_star_bug(self): 1293 def test_star_star_bug(self):
1294 self.assert_graphs(parse(r'(?_e)a(.*)*c'), 1294 self.assert_graphs(parse(r'(?_e)a(.*)*c'),
1295 """digraph { 1295 """digraph {
1296 0 [label="a"] 1296 0 [label="a"]
1297 1 [label="...*"] 1297 1 [label="...*"]
1298 2 [label="(?P<1>"] 1298 2 [label="(?P<1>"]
1299 3 [label="c"] 1299 3 [label="c"]
1300 4 [label="Match"] 1300 4 [label="Match"]
1301 5 [label="...*"] 1301 5 [label="...*"]
1302 6 [label="."] 1302 6 [label="."]
1303 7 [label=")"] 1303 7 [label=")"]
1304 8 [label="!"] 1304 8 [label="!"]
1305 0 -> 1 1305 0 -> 1
1306 1 -> 2 1306 1 -> 2
1307 1 -> 3 1307 1 -> 3
1308 3 -> 4 1308 3 -> 4
1309 2 -> 5 1309 2 -> 5
1310 5 -> 6 1310 5 -> 6
1311 5 -> 7 1311 5 -> 7
1312 7 -> 8 1312 7 -> 8
1313 8 -> 1 1313 8 -> 1
1314 6 -> 5 1314 6 -> 5
1315 }""") 1315 }""")
1316 1316
1317 def test_b_dot_bug(self): 1317 def test_b_dot_bug(self):
1318 self.assert_graphs(parse(r'(?_c)a.*c'), 1318 self.assert_graphs(parse(r'(?_c)a.*c'),
1319 """digraph { 1319 """digraph {
1320 0 [label="a"] 1320 0 [label="a"]
1321 1 [label="...*"] 1321 1 [label="...*"]
1322 2 [label="."] 1322 2 [label="."]
1323 3 [label="c"] 1323 3 [label="c"]
1324 4 [label="Match"] 1324 4 [label="Match"]
1325 0 -> 1 1325 0 -> 1
1326 1 -> 2 1326 1 -> 2
1327 1 -> 3 1327 1 -> 3
1328 3 -> 4 1328 3 -> 4
1329 2 -> 1 1329 2 -> 1
1330 }""") 1330 }""")
1331 self.assert_graphs(parse(r'(?_c)ab*c'), 1331 self.assert_graphs(parse(r'(?_c)ab*c'),
1332 """digraph { 1332 """digraph {
1333 0 [label="a"] 1333 0 [label="a"]
1334 1 [label="...*"] 1334 1 [label="...*"]
1335 2 [label="b"] 1335 2 [label="b"]
1336 3 [label="c"] 1336 3 [label="c"]
1337 4 [label="Match"] 1337 4 [label="Match"]
1338 0 -> 1 1338 0 -> 1
1339 1 -> 2 1339 1 -> 2
1340 1 -> 3 1340 1 -> 3
1341 3 -> 4 1341 3 -> 4
1342 2 -> 1 1342 2 -> 1
1343 }""") 1343 }""")
1344 1344
1345 def assert_flags(self, regexp, flags): 1345 def assert_flags(self, regexp, flags):
1346 (state, _graph) = parse(regexp) 1346 (state, _graph) = parse(regexp)
1347 assert state.flags == flags, state.flags 1347 assert state.flags == flags, state.flags
1348 1348
1349 def test_flags(self): 1349 def test_flags(self):
1350 self.assert_flags('', ParserState.UNICODE) # default 1350 self.assert_flags('', ParserState.UNICODE) # default
1351 self.assert_flags('(?i)', ParserState.IGNORE_CASE | ParserState.UNICODE) 1351 self.assert_flags('(?i)', ParserState.IGNORECASE | ParserState.UNICODE)
1352 try: 1352 try:
1353 self.assert_flags('(?L)', 0) 1353 self.assert_flags('(?L)', 0)
1354 assert False 1354 assert False
1355 except RxpyError: 1355 except RxpyError:
1356 pass 1356 pass
1357 self.assert_flags('(?m)', ParserState.MULTILINE | ParserState.UNICODE) 1357 self.assert_flags('(?m)', ParserState.MULTILINE | ParserState.UNICODE)
1358 self.assert_flags('(?s)', ParserState.DOT_ALL | ParserState.UNICODE) 1358 self.assert_flags('(?s)', ParserState.DOT_ALL | ParserState.UNICODE)
1359 self.assert_flags('(?u)', ParserState.UNICODE) 1359 self.assert_flags('(?u)', ParserState.UNICODE)
1360 self.assert_flags('(?x)', ParserState.VERBOSE | ParserState.UNICODE) 1360 self.assert_flags('(?x)', ParserState.VERBOSE | ParserState.UNICODE)
1361 self.assert_flags('(?a)', ParserState.ASCII) 1361 self.assert_flags('(?a)', ParserState.ASCII)
1362 self.assert_flags('(?_l)', ParserState._LOOP_UNROLL | ParserState.UNICODE) 1362 self.assert_flags('(?_l)', ParserState._LOOP_UNROLL | ParserState.UNICODE)
1363 try: 1363 try:
1364 self.assert_flags('(?imsuxa_l)', 0) 1364 self.assert_flags('(?imsuxa_l)', 0)
1365 assert False 1365 assert False
1366 except ValueError: 1366 except ValueError:
1367 pass 1367 pass
1368 self.assert_flags('(?imsux_l)', 1368 self.assert_flags('(?imsux_l)',
1369 ParserState.IGNORE_CASE | ParserState.MULTILINE | 1369 ParserState.IGNORECASE | ParserState.MULTILINE |
1370 ParserState.DOT_ALL | ParserState.UNICODE | 1370 ParserState.DOT_ALL | ParserState.UNICODE |
1371 ParserState.VERBOSE | ParserState._LOOP_UNROLL) 1371 ParserState.VERBOSE | ParserState._LOOP_UNROLL)
Powered by Google Project Hosting