/jaspyon/rlib/parsing/regexparse.py
Python | 2004 lines | 1993 code | 4 blank | 7 comment | 11 complexity | 31fa14b51b3b976826549006db2bd5a2 MD5 | raw file
1import py 2from jaspyon.rlib.parsing.parsing import PackratParser, Rule 3from jaspyon.rlib.parsing.tree import Nonterminal 4from jaspyon.rlib.parsing.regex import StringExpression, RangeExpression 5from jaspyon.rlib.parsing.lexer import Lexer, DummyLexer 6from jaspyon.rlib.parsing.deterministic import compress_char_set, DFA 7import string 8 9set = py.builtin.set 10 11ESCAPES = { 12 "a": "\a", 13 "b": "\b", 14 "e": "\x1b", 15 "f": "\f", 16 "n": "\n", 17 "r": "\r", 18 "t": "\t", 19 "v": "\v", 20} 21 22for i in range(256): 23 # Add the ctrl-x types: 24 # Rule, according to PCRE: 25 # if x is a lower case letter, it is converted to upper case. 26 # Then bit 6 of the character (hex 40) is inverted. 27 # Thus, \cz => 0x1A, \c{ => 0x3B, \c; => 0x7B. 28 escaped = "c%s" % chr(i) 29 ESCAPES[escaped] = chr(ord(chr(i).upper()) ^ 0x40) 30 31def unescape_muncher(string): 32 """Return a tuple, representing the first character of the string 33 (appropriately unescaped) and the rest of the string that wasn't 34 handled.""" 35 if string[0] != '\\': 36 # Not an escape character 37 return string[0], string[1:] 38 if string[1] == 'x': 39 # Hex char, must have two hex digits 40 char = chr(int(string[2:4], 16)) 41 return char, string[4:] 42 if string[1] in '01234567': 43 # Octal number, up to three digits long 44 span = 2 45 span += (span < len(string)) and (string[span] in '01234567') 46 span += (span < len(string)) and (string[span] in '01234567') 47 char = chr(int(string[1:span], 8)) 48 return char, string[span:] 49 if string[1] == 'c': 50 # Special \cx types 51 return ESCAPES['c'+string[2]], string[3:] 52 if string[1] in ESCAPES: 53 # Special escapes are in ESCAPE 54 return ESCAPES[string[1]], string[2:] 55 # Otherwise, it's just the character it's meant to be (e.g., '\.') 56 return string[1], string[2:] 57 58 59def unescape(s): 60 """Unescape a whole string.""" 61 result = [] 62 while s: 63 char, s = unescape_muncher(s) 64 result.append(char) 65 return "".join(result) 66 67syntax = r""" 68EOF: 69 !__any__; 70 71parse: 72 regex 73 [EOF]; 74 75regex: 76 r1 = concatenation 77 '|' 78 r2 = regex 79 return {r1 | r2} 80 | concatenation; 81 82concatenation: 83 l = repetition* 84 return {reduce(operator.add, l, regex.StringExpression(""))}; 85 86repetition: 87 r1 = primary 88 '*' 89 return {r1.kleene()} 90 | r1 = primary 91 '+' 92 return {r1 + r1.kleene()} 93 | r1 = primary 94 '?' 95 return {regex.StringExpression("") | r1} 96 | r1 = primary 97 '{' 98 n = clippednumrange 99 '}' 100 return {r1 * n + r1.kleene()} 101 | r1 = primary 102 '{' 103 n = numrange 104 '}' 105 return {r1 * n[0] + reduce(operator.or_, [r1 * i for i in range(n[1] - n[0] + 1)], regex.StringExpression(""))} 106 | '{' 107 return {regex.StringExpression("{")} 108 | primary; 109 110primary: 111 ['('] regex [')'] 112 | range 113 | cc = charclass 114 return {reduce(operator.or_, [regex.RangeExpression(a, chr(ord(a) + b - 1)) for a, b in compress_char_set(cc)])} 115 | c = char 116 return {regex.StringExpression(c)} 117 | '.' 118 return {regex.RangeExpression(chr(0), chr(255))} 119 | '-' 120 return {regex.StringExpression('-')} 121 | '\' 122 return {regex.StringExpression('\\')} 123 | ']' 124 return {regex.StringExpression(']')}; 125 126char: 127 c = QUOTEDCHAR 128 return {unescape(c)} 129 | c = CHAR 130 return {c}; 131 132QUOTEDCHAR: 133 `(\\x[0-9a-fA-F]{2})|(\\[0-3]?[0-7][0-7])|(\\c.)|(\\[^dswDSW])`; 134 135CHAR: 136 `[^\*\+\(\)\[\]\{\|\.\-\?\^\\]`; 137 138range: 139 '[' 140 s = rangeinner 141 ']' 142 return {reduce(operator.or_, [regex.RangeExpression(a, chr(ord(a) + b - 1)) for a, b in compress_char_set(s)])}; 143 144rangeinner: 145 '^' 146 s = subrange 147 return {set([chr(c) for c in range(256)]) - s} 148 | subrange; 149 150subrange: 151 ']' 152 l = rangeelement* 153 return {reduce(operator.or_, [set(["]"])] + l)} 154 | l = rangeelement+ 155 return {reduce(operator.or_, l)}; 156 157rangeelement: 158 charclass 159 | c1 = char 160 '-' 161 c2 = char 162 return {set([chr(i) for i in range(ord(c1), ord(c2) + 1)])} 163 | '.' 164 return { set(['.']) } 165 | '*' 166 return { set(['*']) } 167 | '+' 168 return { set(['+']) } 169 | '?' 170 return { set(['?']) } 171 | '-' 172 return { set(['-']) } 173 | '[' 174 return { set(['[']) } 175 | c = char 176 return { set([c]) }; 177 178numrange: 179 n1 = NUM 180 ',' 181 n2 = NUM 182 return {n1, n2} 183 | n1 = NUM 184 return {n1, n1}; 185 186clippednumrange: 187 n1 = NUM 188 ',' 189 return {n1}; 190 191charclass: 192 '\' 'd' 193 return { set([chr(c) for c in range(ord('0'), ord('9')+1)]) } 194 | '\' 's' 195 return { set(['\t', '\n', '\f', '\r', ' ']) } 196 | '\' 'w' 197 return { set([chr(c) for c in range(ord('a'), ord('z')+1)] + [chr(c) for c in range(ord('A'), ord('Z')+1)] + [chr(c) for c in range(ord('0'), ord('9')+1)] + ['_']) } 198 | '\' 'D' 199 return { set([chr(c) for c in range(256)]) - set([chr(c) for c in range(ord('0'), ord('9')+1)]) } 200 | '\' 'S' 201 return { set([chr(c) for c in range(256)]) - set(['\t', '\n', '\f', '\r', ' ']) } 202 | '\' 'W' 203 return { set([chr(c) for c in range(256)]) - set([chr(c) for c in range(ord('a'), ord('z')+1)] + [chr(c) for c in range(ord('A'), ord('Z')+1)] + [chr(c) for c in range(ord('0'), ord('9')+1)] + ['_'])}; 204 205NUM: 206 c = `0|([1-9][0-9]*)` 207 return {int(c)}; 208""" 209 210 211def parse_regex(s): 212 p = RegexParser(s) 213 r = p.parse() 214 return r 215 216def make_runner(regex, view=False): 217 r = parse_regex(regex) 218 nfa = r.make_automaton() 219 dfa = nfa.make_deterministic() 220 if view: 221 dfa.view() 222 dfa.optimize() 223 if view: 224 dfa.view() 225 r = dfa.get_runner() 226 return r 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241# generated code between this line and its other occurence 242 243from jaspyon.rlib.parsing.pypackrat import PackratParser, Status 244from jaspyon.rlib.parsing.pypackrat import BacktrackException 245from jaspyon.rlib.parsing import regex 246import operator 247class Parser(object): 248 def EOF(self): 249 return self._EOF().result 250 def _EOF(self): 251 _key = self._pos 252 _status = self._dict_EOF.get(_key, None) 253 if _status is None: 254 _status = self._dict_EOF[_key] = Status() 255 else: 256 _statusstatus = _status.status 257 if _statusstatus == _status.NORMAL: 258 self._pos = _status.pos 259 return _status 260 elif _statusstatus == _status.ERROR: 261 raise BacktrackException(_status.error) 262 elif (_statusstatus == _status.INPROGRESS or 263 _statusstatus == _status.LEFTRECURSION): 264 _status.status = _status.LEFTRECURSION 265 if _status.result is not None: 266 self._pos = _status.pos 267 return _status 268 else: 269 raise BacktrackException(None) 270 elif _statusstatus == _status.SOMESOLUTIONS: 271 _status.status = _status.INPROGRESS 272 _startingpos = self._pos 273 try: 274 _result = None 275 _error = None 276 _choice0 = self._pos 277 _stored_result1 = _result 278 try: 279 _result = self.__any__() 280 except BacktrackException: 281 self._pos = _choice0 282 _result = _stored_result1 283 else: 284 raise BacktrackException(None) 285 if _status.status == _status.LEFTRECURSION: 286 if _status.result is not None: 287 if _status.pos >= self._pos: 288 _status.status = _status.NORMAL 289 self._pos = _status.pos 290 return _status 291 _status.pos = self._pos 292 _status.status = _status.SOMESOLUTIONS 293 _status.result = _result 294 _status.error = _error 295 self._pos = _startingpos 296 return self._EOF() 297 _status.status = _status.NORMAL 298 _status.pos = self._pos 299 _status.result = _result 300 _status.error = _error 301 return _status 302 except BacktrackException, _exc: 303 _status.pos = -1 304 _status.result = None 305 _error = _exc.error 306 _status.error = _error 307 _status.status = _status.ERROR 308 raise BacktrackException(_error) 309 def parse(self): 310 return self._parse().result 311 def _parse(self): 312 _key = self._pos 313 _status = self._dict_parse.get(_key, None) 314 if _status is None: 315 _status = self._dict_parse[_key] = Status() 316 else: 317 _statusstatus = _status.status 318 if _statusstatus == _status.NORMAL: 319 self._pos = _status.pos 320 return _status 321 elif _statusstatus == _status.ERROR: 322 raise BacktrackException(_status.error) 323 elif (_statusstatus == _status.INPROGRESS or 324 _statusstatus == _status.LEFTRECURSION): 325 _status.status = _status.LEFTRECURSION 326 if _status.result is not None: 327 self._pos = _status.pos 328 return _status 329 else: 330 raise BacktrackException(None) 331 elif _statusstatus == _status.SOMESOLUTIONS: 332 _status.status = _status.INPROGRESS 333 _startingpos = self._pos 334 try: 335 _result = None 336 _error = None 337 _call_status = self._regex() 338 _result = _call_status.result 339 _error = _call_status.error 340 _before_discard0 = _result 341 _call_status = self._EOF() 342 _result = _call_status.result 343 _error = self._combine_errors(_error, _call_status.error) 344 _result = _before_discard0 345 if _status.status == _status.LEFTRECURSION: 346 if _status.result is not None: 347 if _status.pos >= self._pos: 348 _status.status = _status.NORMAL 349 self._pos = _status.pos 350 return _status 351 _status.pos = self._pos 352 _status.status = _status.SOMESOLUTIONS 353 _status.result = _result 354 _status.error = _error 355 self._pos = _startingpos 356 return self._parse() 357 _status.status = _status.NORMAL 358 _status.pos = self._pos 359 _status.result = _result 360 _status.error = _error 361 return _status 362 except BacktrackException, _exc: 363 _status.pos = -1 364 _status.result = None 365 _error = self._combine_errors(_error, _exc.error) 366 _status.error = _error 367 _status.status = _status.ERROR 368 raise BacktrackException(_error) 369 def regex(self): 370 return self._regex().result 371 def _regex(self): 372 _key = self._pos 373 _status = self._dict_regex.get(_key, None) 374 if _status is None: 375 _status = self._dict_regex[_key] = Status() 376 else: 377 _statusstatus = _status.status 378 if _statusstatus == _status.NORMAL: 379 self._pos = _status.pos 380 return _status 381 elif _statusstatus == _status.ERROR: 382 raise BacktrackException(_status.error) 383 elif (_statusstatus == _status.INPROGRESS or 384 _statusstatus == _status.LEFTRECURSION): 385 _status.status = _status.LEFTRECURSION 386 if _status.result is not None: 387 self._pos = _status.pos 388 return _status 389 else: 390 raise BacktrackException(None) 391 elif _statusstatus == _status.SOMESOLUTIONS: 392 _status.status = _status.INPROGRESS 393 _startingpos = self._pos 394 try: 395 _result = None 396 _error = None 397 while 1: 398 _choice0 = self._pos 399 try: 400 _call_status = self._concatenation() 401 _result = _call_status.result 402 _error = _call_status.error 403 r1 = _result 404 _result = self.__chars__('|') 405 _call_status = self._regex() 406 _result = _call_status.result 407 _error = self._combine_errors(_error, _call_status.error) 408 r2 = _result 409 _result = (r1 | r2) 410 break 411 except BacktrackException, _exc: 412 _error = self._combine_errors(_error, _exc.error) 413 self._pos = _choice0 414 _choice1 = self._pos 415 try: 416 _call_status = self._concatenation() 417 _result = _call_status.result 418 _error = self._combine_errors(_error, _call_status.error) 419 break 420 except BacktrackException, _exc: 421 _error = self._combine_errors(_error, _exc.error) 422 self._pos = _choice1 423 raise BacktrackException(_error) 424 _call_status = self._concatenation() 425 _result = _call_status.result 426 _error = self._combine_errors(_error, _call_status.error) 427 break 428 if _status.status == _status.LEFTRECURSION: 429 if _status.result is not None: 430 if _status.pos >= self._pos: 431 _status.status = _status.NORMAL 432 self._pos = _status.pos 433 return _status 434 _status.pos = self._pos 435 _status.status = _status.SOMESOLUTIONS 436 _status.result = _result 437 _status.error = _error 438 self._pos = _startingpos 439 return self._regex() 440 _status.status = _status.NORMAL 441 _status.pos = self._pos 442 _status.result = _result 443 _status.error = _error 444 return _status 445 except BacktrackException, _exc: 446 _status.pos = -1 447 _status.result = None 448 _error = self._combine_errors(_error, _exc.error) 449 _status.error = _error 450 _status.status = _status.ERROR 451 raise BacktrackException(_error) 452 def concatenation(self): 453 return self._concatenation().result 454 def _concatenation(self): 455 _key = self._pos 456 _status = self._dict_concatenation.get(_key, None) 457 if _status is None: 458 _status = self._dict_concatenation[_key] = Status() 459 else: 460 _statusstatus = _status.status 461 if _statusstatus == _status.NORMAL: 462 self._pos = _status.pos 463 return _status 464 elif _statusstatus == _status.ERROR: 465 raise BacktrackException(_status.error) 466 elif (_statusstatus == _status.INPROGRESS or 467 _statusstatus == _status.LEFTRECURSION): 468 _status.status = _status.LEFTRECURSION 469 if _status.result is not None: 470 self._pos = _status.pos 471 return _status 472 else: 473 raise BacktrackException(None) 474 elif _statusstatus == _status.SOMESOLUTIONS: 475 _status.status = _status.INPROGRESS 476 _startingpos = self._pos 477 try: 478 _result = None 479 _error = None 480 _all0 = [] 481 while 1: 482 _choice1 = self._pos 483 try: 484 _call_status = self._repetition() 485 _result = _call_status.result 486 _error = _call_status.error 487 _all0.append(_result) 488 except BacktrackException, _exc: 489 _error = self._combine_errors(_error, _exc.error) 490 self._pos = _choice1 491 break 492 _result = _all0 493 l = _result 494 _result = (reduce(operator.add, l, regex.StringExpression(""))) 495 if _status.status == _status.LEFTRECURSION: 496 if _status.result is not None: 497 if _status.pos >= self._pos: 498 _status.status = _status.NORMAL 499 self._pos = _status.pos 500 return _status 501 _status.pos = self._pos 502 _status.status = _status.SOMESOLUTIONS 503 _status.result = _result 504 _status.error = _error 505 self._pos = _startingpos 506 return self._concatenation() 507 _status.status = _status.NORMAL 508 _status.pos = self._pos 509 _status.result = _result 510 _status.error = _error 511 return _status 512 except BacktrackException, _exc: 513 _status.pos = -1 514 _status.result = None 515 _error = self._combine_errors(_error, _exc.error) 516 _status.error = _error 517 _status.status = _status.ERROR 518 raise BacktrackException(_error) 519 def repetition(self): 520 return self._repetition().result 521 def _repetition(self): 522 _key = self._pos 523 _status = self._dict_repetition.get(_key, None) 524 if _status is None: 525 _status = self._dict_repetition[_key] = Status() 526 else: 527 _statusstatus = _status.status 528 if _statusstatus == _status.NORMAL: 529 self._pos = _status.pos 530 return _status 531 elif _statusstatus == _status.ERROR: 532 raise BacktrackException(_status.error) 533 elif (_statusstatus == _status.INPROGRESS or 534 _statusstatus == _status.LEFTRECURSION): 535 _status.status = _status.LEFTRECURSION 536 if _status.result is not None: 537 self._pos = _status.pos 538 return _status 539 else: 540 raise BacktrackException(None) 541 elif _statusstatus == _status.SOMESOLUTIONS: 542 _status.status = _status.INPROGRESS 543 _startingpos = self._pos 544 try: 545 _result = None 546 _error = None 547 while 1: 548 _choice0 = self._pos 549 try: 550 _call_status = self._primary() 551 _result = _call_status.result 552 _error = _call_status.error 553 r1 = _result 554 _result = self.__chars__('*') 555 _result = (r1.kleene()) 556 break 557 except BacktrackException, _exc: 558 _error = self._combine_errors(_error, _exc.error) 559 self._pos = _choice0 560 _choice1 = self._pos 561 try: 562 _call_status = self._primary() 563 _result = _call_status.result 564 _error = self._combine_errors(_error, _call_status.error) 565 r1 = _result 566 _result = self.__chars__('+') 567 _result = (r1 + r1.kleene()) 568 break 569 except BacktrackException, _exc: 570 _error = self._combine_errors(_error, _exc.error) 571 self._pos = _choice1 572 _choice2 = self._pos 573 try: 574 _call_status = self._primary() 575 _result = _call_status.result 576 _error = self._combine_errors(_error, _call_status.error) 577 r1 = _result 578 _result = self.__chars__('?') 579 _result = (regex.StringExpression("") | r1) 580 break 581 except BacktrackException, _exc: 582 _error = self._combine_errors(_error, _exc.error) 583 self._pos = _choice2 584 _choice3 = self._pos 585 try: 586 _call_status = self._primary() 587 _result = _call_status.result 588 _error = self._combine_errors(_error, _call_status.error) 589 r1 = _result 590 _result = self.__chars__('{') 591 _call_status = self._clippednumrange() 592 _result = _call_status.result 593 _error = self._combine_errors(_error, _call_status.error) 594 n = _result 595 _result = self.__chars__('}') 596 _result = (r1 * n + r1.kleene()) 597 break 598 except BacktrackException, _exc: 599 _error = self._combine_errors(_error, _exc.error) 600 self._pos = _choice3 601 _choice4 = self._pos 602 try: 603 _call_status = self._primary() 604 _result = _call_status.result 605 _error = self._combine_errors(_error, _call_status.error) 606 r1 = _result 607 _result = self.__chars__('{') 608 _call_status = self._numrange() 609 _result = _call_status.result 610 _error = self._combine_errors(_error, _call_status.error) 611 n = _result 612 _result = self.__chars__('}') 613 _result = (r1 * n[0] + reduce(operator.or_, [r1 * i for i in range(n[1] - n[0] + 1)], regex.StringExpression(""))) 614 break 615 except BacktrackException, _exc: 616 _error = self._combine_errors(_error, _exc.error) 617 self._pos = _choice4 618 _choice5 = self._pos 619 try: 620 _result = self.__chars__('{') 621 _result = (regex.StringExpression("{")) 622 break 623 except BacktrackException, _exc: 624 _error = self._combine_errors(_error, _exc.error) 625 self._pos = _choice5 626 _choice6 = self._pos 627 try: 628 _call_status = self._primary() 629 _result = _call_status.result 630 _error = self._combine_errors(_error, _call_status.error) 631 break 632 except BacktrackException, _exc: 633 _error = self._combine_errors(_error, _exc.error) 634 self._pos = _choice6 635 raise BacktrackException(_error) 636 _call_status = self._primary() 637 _result = _call_status.result 638 _error = self._combine_errors(_error, _call_status.error) 639 break 640 if _status.status == _status.LEFTRECURSION: 641 if _status.result is not None: 642 if _status.pos >= self._pos: 643 _status.status = _status.NORMAL 644 self._pos = _status.pos 645 return _status 646 _status.pos = self._pos 647 _status.status = _status.SOMESOLUTIONS 648 _status.result = _result 649 _status.error = _error 650 self._pos = _startingpos 651 return self._repetition() 652 _status.status = _status.NORMAL 653 _status.pos = self._pos 654 _status.result = _result 655 _status.error = _error 656 return _status 657 except BacktrackException, _exc: 658 _status.pos = -1 659 _status.result = None 660 _error = self._combine_errors(_error, _exc.error) 661 _status.error = _error 662 _status.status = _status.ERROR 663 raise BacktrackException(_error) 664 def primary(self): 665 return self._primary().result 666 def _primary(self): 667 _key = self._pos 668 _status = self._dict_primary.get(_key, None) 669 if _status is None: 670 _status = self._dict_primary[_key] = Status() 671 else: 672 _statusstatus = _status.status 673 if _statusstatus == _status.NORMAL: 674 self._pos = _status.pos 675 return _status 676 elif _statusstatus == _status.ERROR: 677 raise BacktrackException(_status.error) 678 elif (_statusstatus == _status.INPROGRESS or 679 _statusstatus == _status.LEFTRECURSION): 680 _status.status = _status.LEFTRECURSION 681 if _status.result is not None: 682 self._pos = _status.pos 683 return _status 684 else: 685 raise BacktrackException(None) 686 elif _statusstatus == _status.SOMESOLUTIONS: 687 _status.status = _status.INPROGRESS 688 _startingpos = self._pos 689 try: 690 _result = None 691 _error = None 692 while 1: 693 _choice0 = self._pos 694 try: 695 _before_discard1 = _result 696 _result = self.__chars__('(') 697 _result = _before_discard1 698 _call_status = self._regex() 699 _result = _call_status.result 700 _error = _call_status.error 701 _before_discard2 = _result 702 _result = self.__chars__(')') 703 _result = _before_discard2 704 break 705 except BacktrackException, _exc: 706 _error = self._combine_errors(_error, _exc.error) 707 self._pos = _choice0 708 _choice3 = self._pos 709 try: 710 _call_status = self._range() 711 _result = _call_status.result 712 _error = self._combine_errors(_error, _call_status.error) 713 break 714 except BacktrackException, _exc: 715 _error = self._combine_errors(_error, _exc.error) 716 self._pos = _choice3 717 _choice4 = self._pos 718 try: 719 _call_status = self._charclass() 720 _result = _call_status.result 721 _error = self._combine_errors(_error, _call_status.error) 722 cc = _result 723 _result = (reduce(operator.or_, [regex.RangeExpression(a, chr(ord(a) + b - 1)) for a, b in compress_char_set(cc)])) 724 break 725 except BacktrackException, _exc: 726 _error = self._combine_errors(_error, _exc.error) 727 self._pos = _choice4 728 _choice5 = self._pos 729 try: 730 _call_status = self._char() 731 _result = _call_status.result 732 _error = self._combine_errors(_error, _call_status.error) 733 c = _result 734 _result = (regex.StringExpression(c)) 735 break 736 except BacktrackException, _exc: 737 _error = self._combine_errors(_error, _exc.error) 738 self._pos = _choice5 739 _choice6 = self._pos 740 try: 741 _result = self.__chars__('.') 742 _result = (regex.RangeExpression(chr(0), chr(255))) 743 break 744 except BacktrackException, _exc: 745 _error = self._combine_errors(_error, _exc.error) 746 self._pos = _choice6 747 _choice7 = self._pos 748 try: 749 _result = self.__chars__('-') 750 _result = (regex.StringExpression('-')) 751 break 752 except BacktrackException, _exc: 753 _error = self._combine_errors(_error, _exc.error) 754 self._pos = _choice7 755 _choice8 = self._pos 756 try: 757 _result = self.__chars__('\\') 758 _result = (regex.StringExpression('\\')) 759 break 760 except BacktrackException, _exc: 761 _error = self._combine_errors(_error, _exc.error) 762 self._pos = _choice8 763 _choice9 = self._pos 764 try: 765 _result = self.__chars__(']') 766 _result = (regex.StringExpression(']')) 767 break 768 except BacktrackException, _exc: 769 _error = self._combine_errors(_error, _exc.error) 770 self._pos = _choice9 771 raise BacktrackException(_error) 772 _result = self.__chars__(']') 773 _result = (regex.StringExpression(']')) 774 break 775 if _status.status == _status.LEFTRECURSION: 776 if _status.result is not None: 777 if _status.pos >= self._pos: 778 _status.status = _status.NORMAL 779 self._pos = _status.pos 780 return _status 781 _status.pos = self._pos 782 _status.status = _status.SOMESOLUTIONS 783 _status.result = _result 784 _status.error = _error 785 self._pos = _startingpos 786 return self._primary() 787 _status.status = _status.NORMAL 788 _status.pos = self._pos 789 _status.result = _result 790 _status.error = _error 791 return _status 792 except BacktrackException, _exc: 793 _status.pos = -1 794 _status.result = None 795 _error = self._combine_errors(_error, _exc.error) 796 _status.error = _error 797 _status.status = _status.ERROR 798 raise BacktrackException(_error) 799 def char(self): 800 return self._char().result 801 def _char(self): 802 _key = self._pos 803 _status = self._dict_char.get(_key, None) 804 if _status is None: 805 _status = self._dict_char[_key] = Status() 806 else: 807 _statusstatus = _status.status 808 if _statusstatus == _status.NORMAL: 809 self._pos = _status.pos 810 return _status 811 elif _statusstatus == _status.ERROR: 812 raise BacktrackException(_status.error) 813 elif (_statusstatus == _status.INPROGRESS or 814 _statusstatus == _status.LEFTRECURSION): 815 _status.status = _status.LEFTRECURSION 816 if _status.result is not None: 817 self._pos = _status.pos 818 return _status 819 else: 820 raise BacktrackException(None) 821 elif _statusstatus == _status.SOMESOLUTIONS: 822 _status.status = _status.INPROGRESS 823 _startingpos = self._pos 824 try: 825 _result = None 826 _error = None 827 while 1: 828 _choice0 = self._pos 829 try: 830 _call_status = self._QUOTEDCHAR() 831 _result = _call_status.result 832 _error = _call_status.error 833 c = _result 834 _result = (unescape(c)) 835 break 836 except BacktrackException, _exc: 837 _error = self._combine_errors(_error, _exc.error) 838 self._pos = _choice0 839 _choice1 = self._pos 840 try: 841 _call_status = self._CHAR() 842 _result = _call_status.result 843 _error = self._combine_errors(_error, _call_status.error) 844 c = _result 845 _result = (c) 846 break 847 except BacktrackException, _exc: 848 _error = self._combine_errors(_error, _exc.error) 849 self._pos = _choice1 850 raise BacktrackException(_error) 851 _call_status = self._CHAR() 852 _result = _call_status.result 853 _error = self._combine_errors(_error, _call_status.error) 854 c = _result 855 _result = (c) 856 break 857 if _status.status == _status.LEFTRECURSION: 858 if _status.result is not None: 859 if _status.pos >= self._pos: 860 _status.status = _status.NORMAL 861 self._pos = _status.pos 862 return _status 863 _status.pos = self._pos 864 _status.status = _status.SOMESOLUTIONS 865 _status.result = _result 866 _status.error = _error 867 self._pos = _startingpos 868 return self._char() 869 _status.status = _status.NORMAL 870 _status.pos = self._pos 871 _status.result = _result 872 _status.error = _error 873 return _status 874 except BacktrackException, _exc: 875 _status.pos = -1 876 _status.result = None 877 _error = self._combine_errors(_error, _exc.error) 878 _status.error = _error 879 _status.status = _status.ERROR 880 raise BacktrackException(_error) 881 def QUOTEDCHAR(self): 882 return self._QUOTEDCHAR().result 883 def _QUOTEDCHAR(self): 884 _key = self._pos 885 _status = self._dict_QUOTEDCHAR.get(_key, None) 886 if _status is None: 887 _status = self._dict_QUOTEDCHAR[_key] = Status() 888 else: 889 _statusstatus = _status.status 890 if _statusstatus == _status.NORMAL: 891 self._pos = _status.pos 892 return _status 893 elif _statusstatus == _status.ERROR: 894 raise BacktrackException(_status.error) 895 _startingpos = self._pos 896 try: 897 _result = None 898 _error = None 899 _result = self._regex1423754537() 900 assert _status.status != _status.LEFTRECURSION 901 _status.status = _status.NORMAL 902 _status.pos = self._pos 903 _status.result = _result 904 _status.error = _error 905 return _status 906 except BacktrackException, _exc: 907 _status.pos = -1 908 _status.result = None 909 _error = _exc.error 910 _status.error = _error 911 _status.status = _status.ERROR 912 raise BacktrackException(_error) 913 def CHAR(self): 914 return self._CHAR().result 915 def _CHAR(self): 916 _key = self._pos 917 _status = self._dict_CHAR.get(_key, None) 918 if _status is None: 919 _status = self._dict_CHAR[_key] = Status() 920 else: 921 _statusstatus = _status.status 922 if _statusstatus == _status.NORMAL: 923 self._pos = _status.pos 924 return _status 925 elif _statusstatus == _status.ERROR: 926 raise BacktrackException(_status.error) 927 _startingpos = self._pos 928 try: 929 _result = None 930 _error = None 931 _result = self._regex2132196932() 932 assert _status.status != _status.LEFTRECURSION 933 _status.status = _status.NORMAL 934 _status.pos = self._pos 935 _status.result = _result 936 _status.error = _error 937 return _status 938 except BacktrackException, _exc: 939 _status.pos = -1 940 _status.result = None 941 _error = _exc.error 942 _status.error = _error 943 _status.status = _status.ERROR 944 raise BacktrackException(_error) 945 def range(self): 946 return self._range().result 947 def _range(self): 948 _key = self._pos 949 _status = self._dict_range.get(_key, None) 950 if _status is None: 951 _status = self._dict_range[_key] = Status() 952 else: 953 _statusstatus = _status.status 954 if _statusstatus == _status.NORMAL: 955 self._pos = _status.pos 956 return _status 957 elif _statusstatus == _status.ERROR: 958 raise BacktrackException(_status.error) 959 elif (_statusstatus == _status.INPROGRESS or 960 _statusstatus == _status.LEFTRECURSION): 961 _status.status = _status.LEFTRECURSION 962 if _status.result is not None: 963 self._pos = _status.pos 964 return _status 965 else: 966 raise BacktrackException(None) 967 elif _statusstatus == _status.SOMESOLUTIONS: 968 _status.status = _status.INPROGRESS 969 _startingpos = self._pos 970 try: 971 _result = None 972 _error = None 973 _result = self.__chars__('[') 974 _call_status = self._rangeinner() 975 _result = _call_status.result 976 _error = _call_status.error 977 s = _result 978 _result = self.__chars__(']') 979 _result = (reduce(operator.or_, [regex.RangeExpression(a, chr(ord(a) + b - 1)) for a, b in compress_char_set(s)])) 980 if _status.status == _status.LEFTRECURSION: 981 if _status.result is not None: 982 if _status.pos >= self._pos: 983 _status.status = _status.NORMAL 984 self._pos = _status.pos 985 return _status 986 _status.pos = self._pos 987 _status.status = _status.SOMESOLUTIONS 988 _status.result = _result 989 _status.error = _error 990 self._pos = _startingpos 991 return self._range() 992 _status.status = _status.NORMAL 993 _status.pos = self._pos 994 _status.result = _result 995 _status.error = _error 996 return _status 997 except BacktrackException, _exc: 998 _status.pos = -1 999 _status.result = None 1000 _error = self._combine_errors(_error, _exc.error) 1001 _status.error = _error 1002 _status.status = _status.ERROR 1003 raise BacktrackException(_error) 1004 def rangeinner(self): 1005 return self._rangeinner().result 1006 def _rangeinner(self): 1007 _key = self._pos 1008 _status = self._dict_rangeinner.get(_key, None) 1009 if _status is None: 1010 _status = self._dict_rangeinner[_key] = Status() 1011 else: 1012 _statusstatus = _status.status 1013 if _statusstatus == _status.NORMAL: 1014 self._pos = _status.pos 1015 return _status 1016 elif _statusstatus == _status.ERROR: 1017 raise BacktrackException(_status.error) 1018 elif (_statusstatus == _status.INPROGRESS or 1019 _statusstatus == _status.LEFTRECURSION): 1020 _status.status = _status.LEFTRECURSION 1021 if _status.result is not None: 1022 self._pos = _status.pos 1023 return _status 1024 else: 1025 raise BacktrackException(None) 1026 elif _statusstatus == _status.SOMESOLUTIONS: 1027 _status.status = _status.INPROGRESS 1028 _startingpos = self._pos 1029 try: 1030 _result = None 1031 _error = None 1032 while 1: 1033 _choice0 = self._pos 1034 try: 1035 _result = self.__chars__('^') 1036 _call_status = self._subrange() 1037 _result = _call_status.result 1038 _error = _call_status.error 1039 s = _result 1040 _result = (set([chr(c) for c in range(256)]) - s) 1041 break 1042 except BacktrackException, _exc: 1043 _error = self._combine_errors(_error, _exc.error) 1044 self._pos = _choice0 1045 _choice1 = self._pos 1046 try: 1047 _call_status = self._subrange() 1048 _result = _call_status.result 1049 _error = self._combine_errors(_error, _call_status.error) 1050 break 1051 except BacktrackException, _exc: 1052 _error = self._combine_errors(_error, _exc.error) 1053 self._pos = _choice1 1054 raise BacktrackException(_error) 1055 _call_status = self._subrange() 1056 _result = _call_status.result 1057 _error = self._combine_errors(_error, _call_status.error) 1058 break 1059 if _status.status == _status.LEFTRECURSION: 1060 if _status.result is not None: 1061 if _status.pos >= self._pos: 1062 _status.status = _status.NORMAL 1063 self._pos = _status.pos 1064 return _status 1065 _status.pos = self._pos 1066 _status.status = _status.SOMESOLUTIONS 1067 _status.result = _result 1068 _status.error = _error 1069 self._pos = _startingpos 1070 return self._rangeinner() 1071 _status.status = _status.NORMAL 1072 _status.pos = self._pos 1073 _status.result = _result 1074 _status.error = _error 1075 return _status 1076 except BacktrackException, _exc: 1077 _status.pos = -1 1078 _status.result = None 1079 _error = self._combine_errors(_error, _exc.error) 1080 _status.error = _error 1081 _status.status = _status.ERROR 1082 raise BacktrackException(_error) 1083 def subrange(self): 1084 return self._subrange().result 1085 def _subrange(self): 1086 _key = self._pos 1087 _status = self._dict_subrange.get(_key, None) 1088 if _status is None: 1089 _status = self._dict_subrange[_key] = Status() 1090 else: 1091 _statusstatus = _status.status 1092 if _statusstatus == _status.NORMAL: 1093 self._pos = _status.pos 1094 return _status 1095 elif _statusstatus == _status.ERROR: 1096 raise BacktrackException(_status.error) 1097 elif (_statusstatus == _status.INPROGRESS or 1098 _statusstatus == _status.LEFTRECURSION): 1099 _status.status = _status.LEFTRECURSION 1100 if _status.result is not None: 1101 self._pos = _status.pos 1102 return _status 1103 else: 1104 raise BacktrackException(None) 1105 elif _statusstatus == _status.SOMESOLUTIONS: 1106 _status.status = _status.INPROGRESS 1107 _startingpos = self._pos 1108 try: 1109 _result = None 1110 _error = None 1111 while 1: 1112 _choice0 = self._pos 1113 try: 1114 _result = self.__chars__(']') 1115 _all1 = [] 1116 while 1: 1117 _choice2 = self._pos 1118 try: 1119 _call_status = self._rangeelement() 1120 _result = _call_status.result 1121 _error = _call_status.error 1122 _all1.append(_result) 1123 except BacktrackException, _exc: 1124 _error = self._combine_errors(_error, _exc.error) 1125 self._pos = _choice2 1126 break 1127 _result = _all1 1128 l = _result 1129 _result = (reduce(operator.or_, [set(["]"])] + l)) 1130 break 1131 except BacktrackException, _exc: 1132 _error = self._combine_errors(_error, _exc.error) 1133 self._pos = _choice0 1134 _choice3 = self._pos 1135 try: 1136 _all4 = [] 1137 _call_status = self._rangeelement() 1138 _result = _call_status.result 1139 _error = self._combine_errors(_error, _call_status.error) 1140 _all4.append(_result) 1141 while 1: 1142 _choice5 = self._pos 1143 try: 1144 _call_status = self._rangeelement() 1145 _result = _call_status.result 1146 _error = self._combine_errors(_error, _call_status.error) 1147 _all4.append(_result) 1148 except BacktrackException, _exc: 1149 _error = self._combine_errors(_error, _exc.error) 1150 self._pos = _choice5 1151 break 1152 _result = _all4 1153 l = _result 1154 _result = (reduce(operator.or_, l)) 1155 break 1156 except BacktrackException, _exc: 1157 _error = self._combine_errors(_error, _exc.error) 1158 self._pos = _choice3 1159 raise BacktrackException(_error) 1160 _all6 = [] 1161 _call_status = self._rangeelement() 1162 _result = _call_status.result 1163 _error = self._combine_errors(_error, _call_status.error) 1164 _all6.append(_result) 1165 while 1: 1166 _choice7 = self._pos 1167 try: 1168 _call_status = self._rangeelement() 1169 _result = _call_status.result 1170 _error = self._combine_errors(_error, _call_status.error) 1171 _all6.append(_result) 1172 except BacktrackException, _exc: 1173 _error = self._combine_errors(_error, _exc.error) 1174 self._pos = _choice7 1175 break 1176 _result = _all6 1177 l = _result 1178 _result = (reduce(operator.or_, l)) 1179 break 1180 if _status.status == _status.LEFTRECURSION: 1181 if _status.result is not None: 1182 if _status.pos >= self._pos: 1183 _status.status = _status.NORMAL 1184 self._pos = _status.pos 1185 return _status 1186 _status.pos = self._pos 1187 _status.status = _status.SOMESOLUTIONS 1188 _status.result = _result 1189 _status.error = _error 1190 self._pos = _startingpos 1191 return self._subrange() 1192 _status.status = _status.NORMAL 1193 _status.pos = self._pos 1194 _status.result = _result 1195 _status.error = _error 1196 return _status 1197 except BacktrackException, _exc: 1198 _status.pos = -1 1199 _status.result = None 1200 _error = self._combine_errors(_error, _exc.error) 1201 _status.error = _error 1202 _status.status = _status.ERROR 1203 raise BacktrackException(_error) 1204 def rangeelement(self): 1205 return self._rangeelement().result 1206 def _rangeelement(self): 1207 _key = self._pos 1208 _status = self._dict_rangeelement.get(_key, None) 1209 if _status is None: 1210 _status = self._dict_rangeelement[_key] = Status() 1211 else: 1212 _statusstatus = _status.status 1213 if _statusstatus == _status.NORMAL: 1214 self._pos = _status.pos 1215 return _status 1216 elif _statusstatus == _status.ERROR: 1217 raise BacktrackException(_status.error) 1218 elif (_statusstatus == _status.INPROGRESS or 1219 _statusstatus == _status.LEFTRECURSION): 1220 _status.status = _status.LEFTRECURSION 1221 if _status.result is not None: 1222 self._pos = _status.pos 1223 return _status 1224 else: 1225 raise BacktrackException(None) 1226 elif _statusstatus == _status.SOMESOLUTIONS: 1227 _status.status = _status.INPROGRESS 1228 _startingpos = self._pos 1229 try: 1230 _result = None 1231 _error = None 1232 while 1: 1233 _choice0 = self._pos 1234 try: 1235 _call_status = self._charclass() 1236 _result = _call_status.result 1237 _error = _call_status.error 1238 break 1239 except BacktrackException, _exc: 1240 _error = self._combine_errors(_error, _exc.error) 1241 self._pos = _choice0 1242 _choice1 = self._pos 1243 try: 1244 _call_status = self._char() 1245 _result = _call_status.result 1246 _error = self._combine_errors(_error, _call_status.error) 1247 c1 = _result 1248 _result = self.__chars__('-') 1249 _call_status = self._char() 1250 _result = _call_status.result 1251 _error = self._combine_errors(_error, _call_status.error) 1252 c2 = _result 1253 _result = (set([chr(i) for i in range(ord(c1), ord(c2) + 1)])) 1254 break 1255 except BacktrackException, _exc: 1256 _error = self._combine_errors(_error, _exc.error) 1257 self._pos = _choice1 1258 _choice2 = self._pos 1259 try: 1260 _result = self.__chars__('.') 1261 _result = ( set(['.']) ) 1262 break 1263 except BacktrackException, _exc: 1264 _error = self._combine_errors(_error, _exc.error) 1265 self._pos = _choice2 1266 _choice3 = self._pos 1267 try: 1268 _result = self.__chars__('*') 1269 _result = ( set(['*']) ) 1270 break 1271 except BacktrackException, _exc: 1272 _error = self._combine_errors(_error, _exc.error) 1273 self._pos = _choice3 1274 _choice4 = self._pos 1275 try: 1276 _result = self.__chars__('+') 1277 _result = ( set(['+']) ) 1278 break 1279 except BacktrackException, _exc: 1280 _error = self._combine_errors(_error, _exc.error) 1281 self._pos = _choice4 1282 _choice5 = self._pos 1283 try: 1284 _result = self.__chars__('?') 1285 _result = ( set(['?']) ) 1286 break 1287 except BacktrackException, _exc: 1288 _error = self._combine_errors(_error, _exc.error) 1289 self._pos = _choice5 1290 _choice6 = self._pos 1291 try: 1292 _result = self.__chars__('-') 1293 _result = ( set(['-']) ) 1294 break 1295 except BacktrackException, _exc: 1296 _error = self._combine_errors(_error, _exc.error) 1297 self._pos = _choice6 1298 _choice7 = self._pos 1299 try: 1300 _result = self.__chars__('[') 1301 _result = ( set(['[']) ) 1302 break 1303 except BacktrackException, _exc: 1304 _error = self._combine_errors(_error, _exc.error) 1305 self._pos = _choice7 1306 _choice8 = self._pos 1307 try: 1308 _call_status = self._char() 1309 _result = _call_status.result 1310 _error = self._combine_errors(_error, _call_status.error) 1311 c = _result 1312 _result = ( set([c]) ) 1313 break 1314 except BacktrackException, _exc: 1315 _error = self._combine_errors(_error, _exc.error) 1316 self._pos = _choice8 1317 raise BacktrackException(_error) 1318 _call_status = self._char() 1319 _result = _call_status.result 1320 _error = self._combine_errors(_error, _call_status.error) 1321 c = _result 1322 _result = ( set([c]) ) 1323 break 1324 if _status.status == _status.LEFTRECURSION: 1325 if _status.result is not None: 1326 if _status.pos >= self._pos: 1327 _status.status = _status.NORMAL 1328 self._pos = _status.pos 1329 return _status 1330 _status.pos = self._pos 1331 _status.status = _status.SOMESOLUTIONS 1332 _status.result = _result 1333 _status.error = _error 1334 self._pos = _startingpos 1335 return self._rangeelement() 1336 _status.status = _status.NORMAL 1337 _status.pos = self._pos 1338 _status.result = _result 1339 _status.error = _error 1340 return _status 1341 except BacktrackException, _exc: 1342 _status.pos = -1 1343 _status.result = None 1344 _error = self._combine_errors(_error, _exc.error) 1345 _status.error = _error 1346 _status.status = _status.ERROR 1347 raise BacktrackException(_error) 1348 def numrange(self): 1349 return self._numrange().result 1350 def _numrange(self): 1351 _key = self._pos 1352 _status = self._dict_numrange.get(_key, None) 1353 if _status is None: 1354 _status = self._dict_numrange[_key] = Status() 1355 else: 1356 _statusstatus = _status.status 1357 if _statusstatus == _status.NORMAL: 1358 self._pos = _status.pos 1359 return _status 1360 elif _statusstatus == _status.ERROR: 1361 raise BacktrackException(_status.error) 1362 elif (_statusstatus == _status.INPROGRESS or 1363 _statusstatus == _status.LEFTRECURSION): 1364 _status.status = _status.LEFTRECURSION 1365 if _status.result is not None: 1366 self._pos = _status.pos 1367 return _status 1368 else: 1369 raise BacktrackException(None) 1370 elif _statusstatus == _status.SOMESOLUTIONS: 1371 _status.status = _status.INPROGRESS 1372 _startingpos = self._pos 1373 try: 1374 _result = None 1375 _error = None 1376 while 1: 1377 _choice0 = self._pos 1378 try: 1379 _call_status = self._NUM() 1380 _result = _call_status.result 1381 _error = _call_status.error 1382 n1 = _result 1383 _result = self.__chars__(',') 1384 _call_status = self._NUM() 1385 _result = _call_status.result 1386 _error = self._combine_errors(_error, _call_status.error) 1387 n2 = _result 1388 _result = (n1, n2) 1389 break 1390 except BacktrackException, _exc: 1391 _error = self._combine_errors(_error, _exc.error) 1392 self._pos = _choice0 1393 _choice1 = self._pos 1394 try: 1395 _call_status = self._NUM() 1396 _result = _call_status.result 1397 _error = self._combine_errors(_error, _call_status.error) 1398 n1 = _result 1399 _result = (n1, n1) 1400 break 1401 except BacktrackException, _exc: 1402 _error = self._combine_errors(_error, _exc.error) 1403 self._pos = _choice1 1404 raise BacktrackException(_error) 1405 _call_status = self._NUM() 1406 _result = _call_status.result 1407 _error = self._combine_errors(_error, _call_status.error) 1408 n1 = _result 1409 _result = (n1, n1) 1410 break 1411 if _status.status == _status.LEFTRECURSION: 1412 if _status.result is not None: 1413 if _status.pos >= self._pos: 1414 _status.status = _status.NORMAL 1415 self._pos = _status.pos 1416 return _status 1417 _status.pos = self._pos 1418 _status.status = _status.SOMESOLUTIONS 1419 _status.result = _result 1420 _status.error = _error 1421 self._pos = _startingpos 1422 return self._numrange() 1423 _status.status = _status.NORMAL 1424 _status.pos = self._pos 1425 _status.result = _result 1426 _status.error = _error 1427 return _status 1428 except BacktrackException, _exc: 1429 _status.pos = -1 1430 _status.result = None 1431 _error = self._combine_errors(_error, _exc.error) 1432 _status.error = _error 1433 _status.status = _status.ERROR 1434 raise BacktrackException(_error) 1435 def clippednumrange(self): 1436 return self._clippednumrange().result 1437 def _clippednumrange(self): 1438 _key = self._pos 1439 _status = self._dict_clippednumrange.get(_key, None) 1440 if _status is None: 1441 _status = self._dict_clippednumrange[_key] = Status() 1442 else: 1443 _statusstatus = _status.status 1444 if _statusstatus == _status.NORMAL: 1445 self._pos = _status.pos 1446 return _status 1447 elif _statusstatus == _status.ERROR: 1448 raise BacktrackException(_status.error) 1449 elif (_statusstatus == _status.INPROGRESS or 1450 _statusstatus == _status.LEFTRECURSION): 1451 _status.status = _status.LEFTRECURSION 1452 if _status.result is not None: 1453 self._pos = _status.pos 1454 return _status 1455 else: 1456 raise BacktrackException(None) 1457 elif _statusstatus == _status.SOMESOLUTIONS: 1458 _status.status = _status.INPROGRESS 1459 _startingpos = self._pos 1460 try: 1461 _result = None 1462 _error = None 1463 _call_status = self._NUM() 1464 _result = _call_status.result 1465 _error = _call_status.error 1466 n1 = _result 1467 _result = self.__chars__(',') 1468 _result = (n1) 1469 if _status.status == _status.LEFTRECURSION: 1470 if _status.result is not None: 1471 if _status.pos >= self._pos: 1472 _status.status = _status.NORMAL 1473 self._pos = _status.pos 1474 return _status 1475 _status.pos = self._pos 1476 _status.status = _status.SOMESOLUTIONS 1477 _status.result = _result 1478 _status.error = _error 1479 self._pos = _startingpos 1480 return self._clippednumrange() 1481 _status.status = _status.NORMAL 1482 _status.pos = self._pos 1483 _status.result = _result 1484 _status.error = _error 1485 return _status 1486 except BacktrackException, _exc: 1487 _status.pos = -1 1488 _status.result = None 1489 _error = self._combine_errors(_error, _exc.error) 1490 _status.error = _error 1491 _status.status = _status.ERROR 1492 raise BacktrackException(_error) 1493 def charclass(self): 1494 return self._charclass().result 1495 def _charclass(self): 1496 _key = self._pos 1497 _status = self._dict_charclass.get(_key, None) 1498 if _status is None: 1499 _status = self._dict_charclass[_key] = Status() 1500 else: 1501 _statusstatus = _status.status 1502 if _statusstatus == _status.NORMAL: 1503 self._pos = _status.pos 1504 return _status 1505 elif _statusstatus == _status.ERROR: 1506 raise BacktrackException(_status.error) 1507 _startingpos = self._pos 1508 try: 1509 _result = None 1510 _error = None 1511 while 1: 1512 _choice0 = self._pos 1513 try: 1514 _result = self.__chars__('\\') 1515 _result = self.__chars__('d') 1516 _result = ( set([chr(c) for c in range(ord('0'), ord('9')+1)]) ) 1517 break 1518 except BacktrackException, _exc: 1519 _error = _exc.error 1520 self._pos = _choice0 1521 _choice1 = self._pos 1522 try: 1523 _result = self.__chars__('\\') 1524 _result = self.__chars__('s') 1525 _result = ( set(['\t', '\n', '\f', '\r', ' ']) ) 1526 break 1527 except BacktrackException, _exc: 1528 _error = self._combine_errors(_error, _exc.error) 1529 self._pos = _choice1 1530 _choice2 = self._pos 1531 try: 1532 _result = self.__chars__('\\') 1533 _result = self.__chars__('w') 1534 _result = ( set([chr(c) for c in range(ord('a'), ord('z')+1)] + [chr(c) for c in range(ord('A'), ord('Z')+1)] + [chr(c) for c in range(ord('0'), ord('9')+1)] + ['_']) ) 1535 break 1536 except BacktrackException, _exc: 1537 _error = self._combine_errors(_error, _exc.error) 1538 self._pos = _choice2 1539 _choice3 = self._pos 1540 try: 1541 _result = self.__chars__('\\') 1542 _result = self.__chars__('D') 1543 _result = ( set([chr(c) for c in range(256)]) - set([chr(c) for c in range(ord('0'), ord('9')+1)]) ) 1544 break 1545 except BacktrackException, _exc: 1546 _error = self._combine_errors(_error, _exc.error) 1547 self._pos = _choice3 1548 _choice4 = self._pos 1549 try: 1550 _result = self.__chars__('\\') 1551 _result = self.__chars__('S') 1552 _result = ( set([chr(c) for c in range(256)]) - set(['\t', '\n', '\f', '\r', ' ']) ) 1553 break 1554 except BacktrackException, _exc: 1555 _error = self._combine_errors(_error, _exc.error) 1556 self._pos = _choice4 1557 _choice5 = self._pos 1558 try: 1559 _result = self.__chars__('\\') 1560 _result = self.__chars__('W') 1561 _result = ( set([chr(c) for c in range(256)]) - set([chr(c) for c in range(ord('a'), ord('z')+1)] + [chr(c) for c in range(ord('A'), ord('Z')+1)] + [chr(c) for c in range(ord('0'), ord('9')+1)] + ['_'])) 1562 break 1563 except BacktrackException, _exc: 1564 _error = self._combine_errors(_error, _exc.error) 1565 self._pos = _choice5 1566 raise BacktrackException(_error) 1567 _result = self.__chars__('\\') 1568 _result = self.__chars__('W') 1569 _result = ( set([chr(c) for c in range(256)]) - set([chr(c) for c in range(ord('a'), ord('z')+1)] + [chr(c) for c in range(ord('A'), ord('Z')+1)] + [chr(c) for c in range(ord('0'), ord('9')+1)] + ['_'])) 1570 break 1571 assert _status.status != _status.LEFTRECURSION 1572 _status.status = _status.NORMAL 1573 _status.pos = self._pos 1574 _status.result = _result 1575 _status.error = _error 1576 return _status 1577 except BacktrackException, _exc: 1578 _status.pos = -1 1579 _status.result = None 1580 _error = self._combine_errors(_error, _exc.error) 1581 _status.error = _error 1582 _status.status = _status.ERROR 1583 raise BacktrackException(_error) 1584 def NUM(self): 1585 return self._NUM().result 1586 def _NUM(self): 1587 _key = self._pos 1588 _status = self._dict_NUM.get(_key, None) 1589 if _status is None: 1590 _status = self._dict_NUM[_key] = Status() 1591 else: 1592 _statusstatus = _status.status 1593 if _statusstatus == _status.NORMAL: 1594 self._pos = _status.pos 1595 return _status 1596 elif _statusstatus == _status.ERROR: 1597 raise BacktrackException(_status.error) 1598 _startingpos = self._pos 1599 try: 1600 _result = None 1601 _error = None 1602 _result = self._regex1166214427() 1603 c = _result 1604 _result = (int(c)) 1605 assert _status.status != _status.LEFTRECURSION 1606 _status.status = _status.NORMAL 1607 _status.pos = self._pos 1608 _status.result = _result 1609 _status.error = _error 1610 return _status 1611 except BacktrackException, _exc: 1612 _status.pos = -1 1613 _status.result = None 1614 _error = _exc.error 1615 _status.error = _error 1616 _status.status = _status.ERROR 1617 raise BacktrackException(_error) 1618 def __init__(self, inputstream): 1619 self._dict_EOF = {} 1620 self._dict_parse = {} 1621 self._dict_regex = {} 1622 self._dict_concatenation = {} 1623 self._dict_repetition = {} 1624 self._dict_primary = {} 1625 self._dict_char = {} 1626 self._dict_QUOTEDCHAR = {} 1627 self._dict_CHAR = {} 1628 self._dict_range = {} 1629 self._dict_rangeinner = {} 1630 self._dict_subrange = {} 1631 self._dict_rangeelement = {} 1632 self._dict_numrange = {} 1633 self._dict_clippednumrange = {} 1634 self._dict_charclass = {} 1635 self._dict_NUM = {} 1636 self._pos = 0 1637 self._inputstream = inputstream 1638 def _regex2132196932(self): 1639 _choice0 = self._pos 1640 _runner = self._Runner(self._inputstream, self._pos) 1641 _i = _runner.recognize_2132196932(self._pos) 1642 if _runner.last_matched_state == -1: 1643 self._pos = _choice0 1644 raise BacktrackException 1645 _upto = _runner.last_matched_index + 1 1646 _pos = self._pos 1647 assert _pos >= 0 1648 assert _upto >= 0 1649 _result = self._inputstream[_pos: _upto] 1650 self._pos = _upto 1651 return _result 1652 def _regex1166214427(self): 1653 _choice1 = self._pos 1654 _runner = self._Runner(self._inputstream, self._pos) 1655 _i = _runner.recognize_1166214427(self._pos) 1656 if _runner.last_matched_state == -1: 1657 self._pos = _choice1 1658 raise BacktrackException 1659 _upto = _runner.last_matched_index + 1 1660 _pos = self._pos 1661 assert _pos >= 0 1662 assert _upto >= 0 1663 _result = self._inputstream[_pos: _upto] 1664 self._pos = _upto 1665 return _result 1666 def _regex1423754537(self): 1667 _choice2 = self._pos 1668 _runner = self._Runner(self._inputstream, self._pos) 1669 _i = _runner.recognize_1423754537(self._pos) 1670 if _runner.last_matched_state == -1: 1671 self._pos = _choice2 1672 raise BacktrackException 1673 _upto = _runner.last_matched_index + 1 1674 _pos = self._pos 1675 assert _pos >= 0 1676 assert _upto >= 0 1677 _result = self._inputstream[_pos: _upto] 1678 self._pos = _upto 1679 return _result 1680 class _Runner(object): 1681 def __init__(self, text, pos): 1682 self.text = text 1683 self.pos = pos 1684 self.last_matched_state = -1 1685 self.last_matched_index = -1 1686 self.state = -1 1687 def recognize_2132196932(runner, i): 1688 #auto-generated code, don't edit 1689 assert i >= 0 1690 input = runner.text 1691 state = 0 1692 while 1: 1693 if state == 0: 1694 try: 1695 char = input[i] 1696 i += 1 1697 except IndexError: 1698 runner.state = 0 1699 return ~i 1700 if '}' <= char <= '\xff': 1701 state = 1 1702 elif '\x00' <= char <= "'": 1703 state = 1 1704 elif '_' <= char <= 'z': 1705 state = 1 1706 elif '@' <= char <= 'Z': 1707 state = 1 1708 elif '/' <= char <= '>': 1709 state = 1 1710 elif char == ',': 1711 state = 1 1712 else: 1713 break 1714 runner.last_matched_state = state 1715 runner.last_matched_index = i - 1 1716 runner.state = state 1717 if i == len(input): 1718 return i 1719 else: 1720 return ~i 1721 break 1722 runner.state = state 1723 return ~i 1724 def recognize_1166214427(runner, i): 1725 #auto-generated code, don't edit 1726 assert i >= 0 1727 input = runner.text 1728 state = 0 1729 while 1: 1730 if state == 0: 1731 try: 1732 char = input[i] 1733 i += 1 1734 except IndexError: 1735 runner.state = 0 1736 return ~i 1737 if char == '0': 1738 state = 1 1739 elif '1' <= char <= '9': 1740 state = 2 1741 else: 1742 break 1743 if state == 2: 1744 runner.last_matched_index = i - 1 1745 runner.last_matched_state = state 1746 try: 1747 char = input[i] 1748 i += 1 1749 except IndexError: 1750 runner.state = 2 1751 return i 1752 if '0' <= char <= '9': 1753 state = 2 1754 continue 1755 else: 1756 break 1757 runner.last_matched_state = state 1758 runner.last_matched_index = i - 1 1759 runner.state = state 1760 if i == len(input): 1761 return i 1762 else: 1763 return ~i 1764 break 1765 runner.state = state 1766 return ~i 1767 def recognize_1423754537(runner, i): 1768 #auto-generated code, don't edit 1769 assert i >= 0 1770 input = runner.text 1771 state = 0 1772 while 1: 1773 if state == 0: 1774 try: 1775 char = input[i] 1776 i += 1 1777 except IndexError: 1778 runner.state = 0 1779 return ~i 1780 if char == '\\': 1781 state = 5 1782 else: 1783 break 1784 if state == 1: 1785 runner.last_matched_index = i - 1 1786 runner.last_matched_state = state 1787 try: 1788 char = input[i] 1789 i += 1 1790 except IndexError: 1791 runner.state = 1 1792 return i 1793 if '0' <= char <= '9': 1794 state = 6 1795 elif 'A' <= char <= 'F': 1796 state = 6 1797 elif 'a' <= char <= 'f': 1798 state = 6 1799 else: 1800 break 1801 if state == 2: 1802 runner.last_matched_index = i - 1 1803 runner.last_matched_state = state 1804 try: 1805 char = input[i] 1806 i += 1 1807 except IndexError: 1808 runner.state = 2 1809 return i 1810 if '0' <= char <= '7': 1811 state = 4 1812 else: 1813 break 1814 if state == 3: 1815 runner.last_matched_index = i - 1 1816 runner.last_matched_state = state 1817 try: 1818 char = input[i] 1819 i += 1 1820 except IndexError: 1821 runner.state = 3 1822 return i 1823 if '0' <= char <= '7': 1824 state = 2 1825 continue 1826 else: 1827 break 1828 if state == 5: 1829 try: 1830 char = input[i] 1831 i += 1 1832 except IndexError: 1833 runner.state = 5 1834 return ~i 1835 if char == 'x': 1836 state = 1 1837 continue 1838 elif '4' <= char <= '7': 1839 state = 2 1840 continue 1841 elif '0' <= char <= '3': 1842 state = 3 1843 continue 1844 elif 'y' <= char <= '\xff': 1845 state = 4 1846 elif '\x00' <= char <= '/': 1847 state = 4 1848 elif 'E' <= char <= 'R': 1849 state = 4 1850 elif 'e' <= char <= 'r': 1851 state = 4 1852 elif '8' <= char <= 'C': 1853 state = 4 1854 elif 'X' <= char <= 'b': 1855 state = 4 1856 elif 'T' <= char <= 'V': 1857 state = 4 1858 elif 't' <= char <= 'v': 1859 state = 4 1860 elif char == 'c': 1861 state = 7 1862 else: 1863 break 1864 if state == 6: 1865 try: 1866 char = input[i] 1867 i += 1 1868 except IndexError: 1869 runner.state = 6 1870 return ~i 1871 if '0' <= char <= '9': 1872 state = 4 1873 elif 'A' <= char <= 'F': 1874 state = 4 1875 elif 'a' <= char <= 'f': 1876 state = 4 1877 else: 1878 break 1879 if state == 7: 1880 runner.last_matched_index = i - 1 1881 runner.last_matched_state = state 1882 try: 1883 char = input[i] 1884 i += 1 1885 except IndexError: 1886 runner.state = 7 1887 return i 1888 if '\x00' <= char <= '\xff': 1889 state = 4 1890 else: 1891 break 1892 runner.last_matched_state = state 1893 runner.last_matched_index = i - 1 1894 runner.state = state 1895 if i == len(input): 1896 return i 1897 else: 1898 return ~i 1899 break 1900 runner.state = state 1901 return ~i 1902class RegexParser(PackratParser): 1903 def __init__(self, stream): 1904 self.init_parser(stream) 1905forbidden = dict.fromkeys(("__weakref__ __doc__ " 1906 "__dict__ __module__").split()) 1907initthere = "__init__" in RegexParser.__dict__ 1908for key, value in Parser.__dict__.iteritems(): 1909 if key not in RegexParser.__dict__ and key not in forbidden: 1910 setattr(RegexParser, key, value) 1911RegexParser.init_parser = Parser.__init__.im_func 1912# generated code between this line and its other occurence 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968def test_generate(): 1969 f = py.magic.autopath() 1970 oldcontent = f.read() 1971 s = "# GENERATED CODE BETWEEN THIS LINE AND ITS OTHER OCCURENCE\n".lower() 1972 pre, gen, after = oldcontent.split(s) 1973 1974 from pypackrat import PyPackratSyntaxParser 1975 from makepackrat import TreeOptimizer, ParserBuilder 1976 p = PyPackratSyntaxParser(syntax) 1977 t = p.file() 1978 t = t.visit(TreeOptimizer()) 1979 visitor = ParserBuilder() 1980 t.visit(visitor) 1981 code = visitor.get_code() 1982 content = """\ 1983%s\ 1984%s 1985from jaspyon.rlib.parsing.pypackrat import PackratParser, Status 1986from jaspyon.rlib.parsing.pypackrat import BacktrackException 1987from jaspyon.rlib.parsing import regex 1988import operator 1989%s 1990class RegexParser(PackratParser): 1991 def __init__(self, stream): 1992 self.init_parser(stream) 1993forbidden = dict.fromkeys(("__weakref__ __doc__ " 1994 "__dict__ __module__").split()) 1995initthere = "__init__" in RegexParser.__dict__ 1996for key, value in Parser.__dict__.iteritems(): 1997 if key not in RegexParser.__dict__ and key not in forbidden: 1998 setattr(RegexParser, key, value) 1999RegexParser.init_parser = Parser.__init__.im_func 2000%s 2001%s\ 2002""" % (pre, s, code, s, after) 2003 print content 2004 f.write(content)