| 1 |
import py |
| 2 |
from rpython.rlib.parsing import regex |
| 3 |
from rpython.rlib.parsing.pypackrat import * |
| 4 |
import operator |
| 5 |
|
| 6 |
class TestPackrat(object): |
| 7 |
def test_simple(self): |
| 8 |
class parser(PackratParser): |
| 9 |
""" |
| 10 |
a: 'a'*; |
| 11 |
b: 'a'+; |
| 12 |
c: ('a' | 'b')+; |
| 13 |
""" |
| 14 |
print parser._code |
| 15 |
p = parser("ababababa") |
| 16 |
assert p.c() == list("ababababa") |
| 17 |
p = parser("aaaaaaaa") |
| 18 |
assert p.a() == list("aaaaaaaa") |
| 19 |
p = parser("") |
| 20 |
assert p.a() == [] |
| 21 |
p = parser("") |
| 22 |
py.test.raises(BacktrackException, p.b) |
| 23 |
|
| 24 |
def test_questionmark(self): |
| 25 |
class parser(PackratParser): |
| 26 |
""" |
| 27 |
a: 'a'? 'b'; |
| 28 |
""" |
| 29 |
print parser._code |
| 30 |
p = parser("ab") |
| 31 |
assert p.a() == 'b' |
| 32 |
p = parser("b") |
| 33 |
assert p.a() == 'b' |
| 34 |
|
| 35 |
def test_call(self): |
| 36 |
class parser(PackratParser): |
| 37 |
""" |
| 38 |
a: 'a'? 'b'; |
| 39 |
b: a 'c'; |
| 40 |
""" |
| 41 |
print parser._code |
| 42 |
p = parser("abc") |
| 43 |
res = p.b() |
| 44 |
assert res == 'c' |
| 45 |
p = parser("bc") |
| 46 |
res = p.b() |
| 47 |
assert res == 'c' |
| 48 |
|
| 49 |
def test_memoize(self): |
| 50 |
class parser(PackratParser): |
| 51 |
""" |
| 52 |
x: a 'end'; |
| 53 |
a: b c | b; |
| 54 |
b: 'b'; |
| 55 |
c: 'c'; |
| 56 |
""" |
| 57 |
print parser._code |
| 58 |
p = parser("bend") |
| 59 |
res = p.x() |
| 60 |
assert res == 'end' |
| 61 |
|
| 62 |
def test_enclose(self): |
| 63 |
class parser(PackratParser): |
| 64 |
""" |
| 65 |
a: 'a' <'b'> 'c'+; |
| 66 |
""" |
| 67 |
print parser._code |
| 68 |
p = parser("abcccccc") |
| 69 |
p.a() == 'b' |
| 70 |
|
| 71 |
def test_not(self): |
| 72 |
class parser(PackratParser): |
| 73 |
""" |
| 74 |
a: 'bh' !'a'; |
| 75 |
""" |
| 76 |
print parser._code |
| 77 |
p = parser('bhc') |
| 78 |
assert p.a() == 'bh' |
| 79 |
p.__chars__('c') == 'c' |
| 80 |
p = parser('bh') |
| 81 |
p.a() == 'bh' |
| 82 |
py.test.raises(BacktrackException, p.__any__) |
| 83 |
|
| 84 |
def test_lookahead(self): |
| 85 |
class parser(PackratParser): |
| 86 |
""" |
| 87 |
a: 'b' !!'a'; |
| 88 |
""" |
| 89 |
print parser._code |
| 90 |
p = parser('ba') |
| 91 |
res = p.a() |
| 92 |
assert res == 'b' |
| 93 |
assert p.__any__() == 'a' |
| 94 |
|
| 95 |
def test_regex1(self): |
| 96 |
class parser(PackratParser): |
| 97 |
""" |
| 98 |
a: 'b' `a|b`; |
| 99 |
""" |
| 100 |
print parser._code |
| 101 |
p = parser('ba') |
| 102 |
res = p.a() |
| 103 |
assert res == 'a' |
| 104 |
py.test.raises(BacktrackException, p.__any__) |
| 105 |
p = parser('bb') |
| 106 |
res = p.a() |
| 107 |
assert res == 'b' |
| 108 |
py.test.raises(BacktrackException, p.__any__) |
| 109 |
|
| 110 |
|
| 111 |
def test_regex2(self): |
| 112 |
class parser(PackratParser): |
| 113 |
""" |
| 114 |
a: 'b' `[^\n]*`; |
| 115 |
""" |
| 116 |
print parser._code |
| 117 |
p = parser('ba#$@@$%\nbc') |
| 118 |
res = p.a() |
| 119 |
assert res == 'a#$@@$%' |
| 120 |
assert p.__any__() == '\n' |
| 121 |
|
| 122 |
def test_name(self): |
| 123 |
class parser(PackratParser): |
| 124 |
""" |
| 125 |
a: c = 'b' |
| 126 |
r = `[^\n]*` |
| 127 |
return {c + r}; |
| 128 |
""" |
| 129 |
print parser._code |
| 130 |
p = parser('ba#$@@$%\nbc') |
| 131 |
res = p.a() |
| 132 |
assert res == 'ba#$@@$%' |
| 133 |
assert p.__any__() == '\n' |
| 134 |
|
| 135 |
def test_name2(self): |
| 136 |
class parser(PackratParser): |
| 137 |
""" |
| 138 |
a: c = 'b'* |
| 139 |
r = `[^\n]*` |
| 140 |
return {(len(c), r)}; |
| 141 |
""" |
| 142 |
print parser._code |
| 143 |
p = parser('bbbbbba#$@@$%\nbc') |
| 144 |
res = p.a() |
| 145 |
assert res == (6, "a#$@@$%") |
| 146 |
assert p.__any__() == '\n' |
| 147 |
|
| 148 |
def test_name3(self): |
| 149 |
class parser(PackratParser): |
| 150 |
""" |
| 151 |
a: c = 'd'+ |
| 152 |
r = 'f'+ |
| 153 |
return {"".join(c) + "".join(r)} |
| 154 |
| c = 'b'* |
| 155 |
r = `[^\n]*` |
| 156 |
return {(len(c), r)}; |
| 157 |
""" |
| 158 |
print parser._code |
| 159 |
p = parser('bbbbbba#$@@$%\nbc') |
| 160 |
res = p.a() |
| 161 |
assert res == (6, "a#$@@$%") |
| 162 |
assert p.__any__() == '\n' |
| 163 |
p = parser('dddffffx') |
| 164 |
res = p.a() |
| 165 |
assert res == "dddffff" |
| 166 |
assert p.__any__() == 'x' |
| 167 |
|
| 168 |
def test_nested_repetition(self): |
| 169 |
class parser(PackratParser): |
| 170 |
""" |
| 171 |
a: ('a' 'b'*)+; |
| 172 |
""" |
| 173 |
print parser._code |
| 174 |
p = parser('aaabbbab') |
| 175 |
res = p.a() |
| 176 |
assert res == [[], [], ['b', 'b', 'b'], ['b']] |
| 177 |
|
| 178 |
|
| 179 |
def test_ignore(self): |
| 180 |
class parser(PackratParser): |
| 181 |
""" |
| 182 |
a: ('a' ['b'])+; |
| 183 |
""" |
| 184 |
print parser._code |
| 185 |
p = parser('abababababab') |
| 186 |
res = p.a() |
| 187 |
assert res == list('aaaaaa') |
| 188 |
|
| 189 |
|
| 190 |
def test_regex(self): |
| 191 |
class parser(PackratParser): |
| 192 |
r""" |
| 193 |
a: `\"`; |
| 194 |
""" |
| 195 |
print parser._code |
| 196 |
p = parser('"') |
| 197 |
res = p.a() |
| 198 |
assert res == '"' |
| 199 |
|
| 200 |
|
| 201 |
def test_memoize_exceptions(self): |
| 202 |
class parser(PackratParser): |
| 203 |
""" |
| 204 |
b: 'a'; |
| 205 |
""" |
| 206 |
print parser._code |
| 207 |
p = parser("c") |
| 208 |
excinfo = py.test.raises(BacktrackException, p.b) |
| 209 |
excinfo = py.test.raises(BacktrackException, p.b) |
| 210 |
excinfo = py.test.raises(BacktrackException, p.b) |
| 211 |
|
| 212 |
def test_error_character(self): |
| 213 |
class parser(PackratParser): |
| 214 |
""" |
| 215 |
b: 'a'; |
| 216 |
""" |
| 217 |
print parser._code |
| 218 |
p = parser("c") |
| 219 |
excinfo = py.test.raises(BacktrackException, p.b) |
| 220 |
assert excinfo.value.error.pos == 0 |
| 221 |
assert excinfo.value.error.expected == ['a'] |
| 222 |
|
| 223 |
def test_error_or(self): |
| 224 |
class parser(PackratParser): |
| 225 |
""" |
| 226 |
b: 'a' | 'b'; |
| 227 |
""" |
| 228 |
print parser._code |
| 229 |
p = parser("c") |
| 230 |
excinfo = py.test.raises(BacktrackException, p.b) |
| 231 |
assert excinfo.value.error.pos == 0 |
| 232 |
assert excinfo.value.error.expected == ['a', 'b'] |
| 233 |
|
| 234 |
def test_error_not(self): |
| 235 |
class parser(PackratParser): |
| 236 |
""" |
| 237 |
b: |
| 238 |
'b' !'a'; |
| 239 |
""" |
| 240 |
p = parser("ba") |
| 241 |
excinfo = py.test.raises(BacktrackException, p.b) |
| 242 |
assert excinfo.value.error.pos == 1 |
| 243 |
assert excinfo.value.error.expected == ['NOT a'] |
| 244 |
print parser._code |
| 245 |
|
| 246 |
def test_error_lookahead(self): |
| 247 |
class parser(PackratParser): |
| 248 |
""" |
| 249 |
b: |
| 250 |
'b' !!'a'; |
| 251 |
""" |
| 252 |
p = parser("bc") |
| 253 |
print parser._code |
| 254 |
excinfo = py.test.raises(BacktrackException, p.b) |
| 255 |
assert excinfo.value.error.pos == 1 |
| 256 |
assert excinfo.value.error.expected == ['a'] |
| 257 |
|
| 258 |
def test_error_star(self): |
| 259 |
class parser(PackratParser): |
| 260 |
""" |
| 261 |
b: |
| 262 |
'b'* !__any__; |
| 263 |
""" |
| 264 |
print parser._code |
| 265 |
p = parser("bbc") |
| 266 |
print parser._code |
| 267 |
excinfo = py.test.raises(BacktrackException, p.b) |
| 268 |
assert excinfo.value.error.pos == 2 |
| 269 |
assert excinfo.value.error.expected == ['b'] |
| 270 |
|
| 271 |
def test_error_success(self): |
| 272 |
class parser(PackratParser): |
| 273 |
""" |
| 274 |
b: |
| 275 |
bstar !__any__; |
| 276 |
bstar: |
| 277 |
'b'*; |
| 278 |
""" |
| 279 |
print parser._code |
| 280 |
p = parser("bbc") |
| 281 |
print parser._code |
| 282 |
excinfo = py.test.raises(BacktrackException, p.b) |
| 283 |
assert excinfo.value.error.pos == 2 |
| 284 |
assert excinfo.value.error.expected == ['b'] |
| 285 |
|
| 286 |
def test_leftrecursion(self): |
| 287 |
class parser(PackratParser): |
| 288 |
""" |
| 289 |
b: b 'a' | 'b'; |
| 290 |
""" |
| 291 |
print parser._code |
| 292 |
p = parser("b") |
| 293 |
res = p.b() |
| 294 |
assert res == "b" |
| 295 |
p = parser("bac") |
| 296 |
res = p.b() |
| 297 |
assert p._pos == 2 |
| 298 |
assert res == "a" |
| 299 |
p = parser("baaaaaaaaaaaaaac") |
| 300 |
res = p.b() |
| 301 |
assert p._pos == 15 |
| 302 |
assert res == "a" |
| 303 |
|
| 304 |
def test_leftrecursion_arithmetic(self): |
| 305 |
class parser(PackratParser): |
| 306 |
""" |
| 307 |
additive: |
| 308 |
a = additive |
| 309 |
'-' |
| 310 |
b = multitive |
| 311 |
return {a - b} |
| 312 |
| multitive; |
| 313 |
multitive: |
| 314 |
a = multitive |
| 315 |
'*' |
| 316 |
b = simple |
| 317 |
return {a * b} |
| 318 |
| simple; |
| 319 |
simple: |
| 320 |
x = `0|([1-9][0-9]*)` |
| 321 |
return {int(x)}; |
| 322 |
""" |
| 323 |
print parser._code |
| 324 |
p = parser("5") |
| 325 |
res = p.multitive() |
| 326 |
assert res == 5 |
| 327 |
p._pos = 0 |
| 328 |
res = p.multitive() |
| 329 |
assert res == 5 |
| 330 |
p = parser("5-5-5") |
| 331 |
res = p.additive() |
| 332 |
assert res == -5 |
| 333 |
assert p._pos == 5 |
| 334 |
|
| 335 |
def test_leftrecursion_more_choices(self): |
| 336 |
class parser(PackratParser): |
| 337 |
""" |
| 338 |
b: |
| 339 |
b 'a' |
| 340 |
| b 'c' |
| 341 |
| 'b'; |
| 342 |
""" |
| 343 |
print parser._code |
| 344 |
p = parser("b") |
| 345 |
res = p.b() |
| 346 |
assert res == "b" |
| 347 |
p = parser("bcx") |
| 348 |
res = p.b() |
| 349 |
assert p._pos == 2 |
| 350 |
assert res == "c" |
| 351 |
|
| 352 |
def test_leftrecursion_argument(self): |
| 353 |
class parser(PackratParser): |
| 354 |
""" |
| 355 |
additive(op): |
| 356 |
a = additive({op}) |
| 357 |
__chars__({op}) |
| 358 |
b = multitive |
| 359 |
return {eval('a %s b' % (op, ))} |
| 360 |
| multitive; |
| 361 |
multitive: |
| 362 |
a = multitive |
| 363 |
'*' |
| 364 |
b = simple |
| 365 |
return {a * b} |
| 366 |
| simple; |
| 367 |
simple: |
| 368 |
x = `0|([1-9][0-9]*)` |
| 369 |
return {int(x)}; |
| 370 |
""" |
| 371 |
p = parser('4-4-4-4') |
| 372 |
res = p.additive('-') |
| 373 |
assert res == -8 |
| 374 |
|
| 375 |
|
| 376 |
def test_doif(self): |
| 377 |
class parser(PackratParser): |
| 378 |
""" |
| 379 |
INT: |
| 380 |
c = `[1-9][0-9]*` |
| 381 |
return {int(c)}; |
| 382 |
b: |
| 383 |
do |
| 384 |
c = INT |
| 385 |
if {c > 42}; |
| 386 |
""" |
| 387 |
print parser._code |
| 388 |
p = parser("54") |
| 389 |
res = p.b() |
| 390 |
assert res == 54 |
| 391 |
p = parser("12") |
| 392 |
excinfo = py.test.raises(BacktrackException, p.b) |
| 393 |
assert excinfo.value.error.pos == 0 |
| 394 |
assert excinfo.value.error.expected == ['condition not met'] |
| 395 |
|
| 396 |
def test_if(self): |
| 397 |
class parser(PackratParser): |
| 398 |
""" |
| 399 |
b(c): |
| 400 |
if {c > 42} |
| 401 |
c = __chars__({str(c)}) |
| 402 |
return {int(c)} |
| 403 |
| 'xyz'; |
| 404 |
""" |
| 405 |
print parser._code |
| 406 |
p = parser("54") |
| 407 |
res = p.b(54) |
| 408 |
assert res == 54 |
| 409 |
p = parser("xyz") |
| 410 |
res = p.b(21) |
| 411 |
assert res == 'xyz' |
| 412 |
|
| 413 |
def test_parse_arguments(self): |
| 414 |
class parser(PackratParser): |
| 415 |
""" |
| 416 |
between(a, b): |
| 417 |
do |
| 418 |
c = __any__ |
| 419 |
if {ord(a) <= ord(c) <= ord(b)} |
| 420 |
return {c}; |
| 421 |
|
| 422 |
small_big_small: |
| 423 |
x = between({'a'}, {'z'})+ |
| 424 |
y = between({'A'}, {'Z'})+ |
| 425 |
z = between({'a'}, {'z'})+ |
| 426 |
return {"".join(x) + "".join(y) + "".join(z)}; |
| 427 |
""" |
| 428 |
p = parser('abc') |
| 429 |
c = p.between('a', 'z') |
| 430 |
assert c == 'a' |
| 431 |
p._pos = 0 |
| 432 |
c = p.between('a', 'z') |
| 433 |
assert c == 'a' |
| 434 |
excinfo = py.test.raises(BacktrackException, p.between, 'A', 'Z') |
| 435 |
assert excinfo.value.error.pos == 1 |
| 436 |
assert excinfo.value.error.expected == ['condition not met'] |
| 437 |
p = parser('aBc') |
| 438 |
res = p.small_big_small() |
| 439 |
assert res == 'aBc' |
| 440 |
p = parser('aaaaarstawfpacawBAAAFPAcccfafp') |
| 441 |
res = p.small_big_small() |
| 442 |
assert res == 'aaaaarstawfpacawBAAAFPAcccfafp' |
| 443 |
|
| 444 |
def test_choose(self): |
| 445 |
|
| 446 |
class parser(PackratParser): |
| 447 |
""" |
| 448 |
choice: |
| 449 |
choose a in {self.possibilities} |
| 450 |
__chars__({a})+ |
| 451 |
return {a}; |
| 452 |
""" |
| 453 |
possibilities = ['a', 'x', 'y'] |
| 454 |
|
| 455 |
p = parser('aaaaaaa') |
| 456 |
c = p.choice() |
| 457 |
assert c == 'a' |
| 458 |
p = parser('xxxxxxxxxxxx') |
| 459 |
c = p.choice() |
| 460 |
assert c == 'x' |
| 461 |
p = parser('y') |
| 462 |
c = p.choice() |
| 463 |
assert c == 'y' |
| 464 |
p = parser('y') |
| 465 |
c = p.choice() |
| 466 |
assert c == 'y' |
| 467 |
p = parser('vvvvvv') |
| 468 |
excinfo = py.test.raises(BacktrackException, p.choice) |
| 469 |
assert excinfo.value.error.pos == 0 |
| 470 |
expected = excinfo.value.error.expected |
| 471 |
expected.sort() |
| 472 |
assert expected == ['a', 'x', 'y'] |
| 473 |
|
| 474 |
|
| 475 |
def test_python_future(self): |
| 476 |
class parser(PackratParser): |
| 477 |
r""" |
| 478 |
comment: |
| 479 |
`#[^\r\n]*` lineend; |
| 480 |
lineend: |
| 481 |
`(\r|\n)+`; |
| 482 |
docstring: |
| 483 |
`(\"\"\"[^\\]*(\\[^\\]+)*\"\"\")|(\'\'\'[^\\]*(\\[^\\]+)*\'\'\')` |
| 484 |
ignore* |
| 485 |
| `(\"[^\\]*(\\[^\\]+)*\")|(\'[^\\]*(\\[^\\]+)*\')` |
| 486 |
ignore*; |
| 487 |
ignore: |
| 488 |
`[ \t]+`; |
| 489 |
ignoreline: |
| 490 |
`[ \t]*[\r\n]+`; |
| 491 |
fromimport: |
| 492 |
'from' ignore+ |
| 493 |
'__future__' ignore+ |
| 494 |
'import' ignore+ |
| 495 |
what; |
| 496 |
identifier: |
| 497 |
`[a-zA-Z0-9_]+`; |
| 498 |
what: |
| 499 |
'(' ignoreline* |
| 500 |
g = group |
| 501 |
ignoreline* |
| 502 |
rest = ([',' ignoreline*] group)* |
| 503 |
')' |
| 504 |
return {[g] + rest} |
| 505 |
| g = group |
| 506 |
rest = ([',' ignore*] group)* |
| 507 |
return {[g] + rest}; |
| 508 |
group: |
| 509 |
name = identifier ignore+ 'as' ignore+ identifier ignore* |
| 510 |
return {name} |
| 511 |
| name = identifier ignore* |
| 512 |
return {name}; |
| 513 |
line: |
| 514 |
comment |
| 515 |
return {None} |
| 516 |
| docstring lineend |
| 517 |
return {None} |
| 518 |
| ignore lineend |
| 519 |
return {None} |
| 520 |
| t = fromimport |
| 521 |
ignore* |
| 522 |
lineend |
| 523 |
return {t}; |
| 524 |
header: |
| 525 |
l = line* |
| 526 |
return {[elt for sublist in l if sublist is not None for elt in sublist]}; |
| 527 |
""" |
| 528 |
p = parser("#\n") |
| 529 |
lines = p.header() |
| 530 |
assert lines == [] |
| 531 |
p = parser('''"abc"\n''') |
| 532 |
lines = p.header() |
| 533 |
assert lines == [] |
| 534 |
p = parser(''''abc'\n''') |
| 535 |
lines = p.header() |
| 536 |
assert lines == [] |
| 537 |
p = parser(''''abc'\n''') |
| 538 |
lines = p.header() |
| 539 |
assert lines == [] |
| 540 |
p = parser('''from __future__ import division\n''') |
| 541 |
lines = p.header() |
| 542 |
assert lines == ['division'] |
| 543 |
p = parser('''from __future__ import division, generators\n''') |
| 544 |
lines = p.fromimport() |
| 545 |
assert lines == ['division', 'generators'] |
| 546 |
p = parser('''from __future__ import (division, \ngenerators)\n''') |
| 547 |
lines = p.fromimport() |
| 548 |
assert lines == ['division', 'generators'] |
| 549 |
p = parser('''from __future__ import (division as d, \ngenerators)\n''') |
| 550 |
lines = p.fromimport() |
| 551 |
assert lines == ['division', 'generators'] |