1 # 2 # Secret Labs' Regular Expression Engine 3 # 4 # convert re-style regular expression to sre pattern 5 # 6 # Copyright (c) 1998-2001 by Secret Labs AB. All rights reserved. 7 # 8 # See the sre.py file for information on usage and redistribution. 9 # 10 11 """Internal support module for sre""" 12 13 # XXX: show string offset and offending character for all errors 14 15 import sys 16 17 from sre_constants import * 18 from _sre import MAXREPEAT 19 20 SPECIAL_CHARS = ".\\[{()*+?^$|" 21 REPEAT_CHARS = "*+?{" 22 23 DIGITS = set("0123456789") 24 25 OCTDIGITS = set("01234567") 26 HEXDIGITS = set("0123456789abcdefABCDEF") 27 28 WHITESPACE = set(" \t\n\r\v\f") 29 30 ESCAPES = { 31 r"\a": (LITERAL, ord("\a")), 32 r"\b": (LITERAL, ord("\b")), 33 r"\f": (LITERAL, ord("\f")), 34 r"\n": (LITERAL, ord("\n")), 35 r"\r": (LITERAL, ord("\r")), 36 r"\t": (LITERAL, ord("\t")), 37 r"\v": (LITERAL, ord("\v")), 38 r"\\": (LITERAL, ord("\\")) 39 } 40 41 CATEGORIES = { 42 r"\A": (AT, AT_BEGINNING_STRING), # start of string 43 r"\b": (AT, AT_BOUNDARY), 44 r"\B": (AT, AT_NON_BOUNDARY), 45 r"\d": (IN, [(CATEGORY, CATEGORY_DIGIT)]), 46 r"\D": (IN, [(CATEGORY, CATEGORY_NOT_DIGIT)]), 47 r"\s": (IN, [(CATEGORY, CATEGORY_SPACE)]), 48 r"\S": (IN, [(CATEGORY, CATEGORY_NOT_SPACE)]), 49 r"\w": (IN, [(CATEGORY, CATEGORY_WORD)]), 50 r"\W": (IN, [(CATEGORY, CATEGORY_NOT_WORD)]), 51 r"\Z": (AT, AT_END_STRING), # end of string 52 } 53 54 FLAGS = { 55 # standard flags 56 "i": SRE_FLAG_IGNORECASE, 57 "L": SRE_FLAG_LOCALE, 58 "m": SRE_FLAG_MULTILINE, 59 "s": SRE_FLAG_DOTALL, 60 "x": SRE_FLAG_VERBOSE, 61 # extensions 62 "t": SRE_FLAG_TEMPLATE, 63 "u": SRE_FLAG_UNICODE, 64 } 65 66 class Pattern: 67 # master pattern object. keeps track of global attributes 68 def __init__(self): 69 self.flags = 0 70 self.open = [] 71 self.groups = 1 72 self.groupdict = {} 73 def opengroup(self, name=None): 74 gid = self.groups 75 self.groups = gid + 1 76 if name is not None: 77 ogid = self.groupdict.get(name, None) 78 if ogid is not None: 79 raise error, ("redefinition of group name %s as group %d; " 80 "was group %d" % (repr(name), gid, ogid)) 81 self.groupdict[name] = gid 82 self.open.append(gid) 83 return gid 84 def closegroup(self, gid): 85 self.open.remove(gid) 86 def checkgroup(self, gid): 87 return gid < self.groups and gid not in self.open 88 89 class SubPattern: 90 # a subpattern, in intermediate form 91 def __init__(self, pattern, data=None): 92 self.pattern = pattern 93 if data is None: 94 data = [] 95 self.data = data 96 self.width = None 97 def dump(self, level=0): 98 nl = 1 99 seqtypes = type(()), type([]) 100 for op, av in self.data: 101 print level*" " + op,; nl = 0 102 if op == "in": 103 # member sublanguage 104 print; nl = 1 105 for op, a in av: 106 print (level+1)*" " + op, a 107 elif op == "branch": 108 print; nl = 1 109 i = 0 110 for a in av[1]: 111 if i > 0: 112 print level*" " + "or" 113 a.dump(level+1); nl = 1 114 i = i + 1 115 elif type(av) in seqtypes: 116 for a in av: 117 if isinstance(a, SubPattern): 118 if not nl: print 119 a.dump(level+1); nl = 1 120 else: 121 print a, ; nl = 0 122 else: 123 print av, ; nl = 0 124 if not nl: print 125 def __repr__(self): 126 return repr(self.data) 127 def __len__(self): 128 return len(self.data) 129 def __delitem__(self, index): 130 del self.data[index] 131 def __getitem__(self, index): 132 if isinstance(index, slice): 133 return SubPattern(self.pattern, self.data[index]) 134 return self.data[index] 135 def __setitem__(self, index, code): 136 self.data[index] = code 137 def insert(self, index, code): 138 self.data.insert(index, code) 139 def append(self, code): 140 self.data.append(code) 141 def getwidth(self): 142 # determine the width (min, max) for this subpattern 143 if self.width: 144 return self.width 145 lo = hi = 0L 146 UNITCODES = (ANY, RANGE, IN, LITERAL, NOT_LITERAL, CATEGORY) 147 REPEATCODES = (MIN_REPEAT, MAX_REPEAT) 148 for op, av in self.data: 149 if op is BRANCH: 150 i = sys.maxint 151 j = 0 152 for av in av[1]: 153 l, h = av.getwidth() 154 i = min(i, l) 155 j = max(j, h) 156 lo = lo + i 157 hi = hi + j 158 elif op is CALL: 159 i, j = av.getwidth() 160 lo = lo + i 161 hi = hi + j 162 elif op is SUBPATTERN: 163 i, j = av[1].getwidth() 164 lo = lo + i 165 hi = hi + j 166 elif op in REPEATCODES: 167 i, j = av[2].getwidth() 168 lo = lo + long(i) * av[0] 169 hi = hi + long(j) * av[1] 170 elif op in UNITCODES: 171 lo = lo + 1 172 hi = hi + 1 173 elif op == SUCCESS: 174 break 175 self.width = int(min(lo, sys.maxint)), int(min(hi, sys.maxint)) 176 return self.width 177 178 class Tokenizer: 179 def __init__(self, string): 180 self.string = string 181 self.index = 0 182 self.__next() 183 def __next(self): 184 if self.index >= len(self.string): 185 self.next = None 186 return 187 char = self.string[self.index] 188 if char[0] == "\\": 189 try: 190 c = self.string[self.index + 1] 191 except IndexError: 192 raise error, "bogus escape (end of line)" 193 char = char + c 194 self.index = self.index + len(char) 195 self.next = char 196 def match(self, char, skip=1): 197 if char == self.next: 198 if skip: 199 self.__next() 200 return 1 201 return 0 202 def get(self): 203 this = self.next 204 self.__next() 205 return this 206 def tell(self): 207 return self.index, self.next 208 def seek(self, index): 209 self.index, self.next = index 210 211 def isident(char): 212 return "a" <= char <= "z" or "A" <= char <= "Z" or char == "_" 213 214 def isdigit(char): 215 return "0" <= char <= "9" 216 217 def isname(name): 218 # check that group name is a valid string 219 if not isident(name[0]): 220 return False 221 for char in name[1:]: 222 if not isident(char) and not isdigit(char): 223 return False 224 return True 225 226 def _class_escape(source, escape): 227 # handle escape code inside character class 228 code = ESCAPES.get(escape) 229 if code: 230 return code 231 code = CATEGORIES.get(escape) 232 if code and code[0] == IN: 233 return code 234 try: 235 c = escape[1:2] 236 if c == "x": 237 # hexadecimal escape (exactly two digits) 238 while source.next in HEXDIGITS and len(escape) < 4: 239 escape = escape + source.get() 240 escape = escape[2:] 241 if len(escape) != 2: 242 raise error, "bogus escape: %s" % repr("\\" + escape) 243 return LITERAL, int(escape, 16) & 0xff 244 elif c in OCTDIGITS: 245 # octal escape (up to three digits) 246 while source.next in OCTDIGITS and len(escape) < 4: 247 escape = escape + source.get() 248 escape = escape[1:] 249 return LITERAL, int(escape, 8) & 0xff 250 elif c in DIGITS: 251 raise error, "bogus escape: %s" % repr(escape) 252 if len(escape) == 2: 253 return LITERAL, ord(escape[1]) 254 except ValueError: 255 pass 256 raise error, "bogus escape: %s" % repr(escape) 257 258 def _escape(source, escape, state): 259 # handle escape code in expression 260 code = CATEGORIES.get(escape) 261 if code: 262 return code 263 code = ESCAPES.get(escape) 264 if code: 265 return code 266 try: 267 c = escape[1:2] 268 if c == "x": 269 # hexadecimal escape 270 while source.next in HEXDIGITS and len(escape) < 4: 271 escape = escape + source.get() 272 if len(escape) != 4: 273 raise ValueError 274 return LITERAL, int(escape[2:], 16) & 0xff 275 elif c == "0": 276 # octal escape 277 while source.next in OCTDIGITS and len(escape) < 4: 278 escape = escape + source.get() 279 return LITERAL, int(escape[1:], 8) & 0xff 280 elif c in DIGITS: 281 # octal escape *or* decimal group reference (sigh) 282 if source.next in DIGITS: 283 escape = escape + source.get() 284 if (escape[1] in OCTDIGITS and escape[2] in OCTDIGITS and 285 source.next in OCTDIGITS): 286 # got three octal digits; this is an octal escape 287 escape = escape + source.get() 288 return LITERAL, int(escape[1:], 8) & 0xff 289 # not an octal escape, so this is a group reference 290 group = int(escape[1:]) 291 if group < state.groups: 292 if not state.checkgroup(group): 293 raise error, "cannot refer to open group" 294 return GROUPREF, group 295 raise ValueError 296 if len(escape) == 2: 297 return LITERAL, ord(escape[1]) 298 except ValueError: 299 pass 300 raise error, "bogus escape: %s" % repr(escape) 301 302 def _parse_sub(source, state, nested=1): 303 # parse an alternation: a|b|c 304 305 items = [] 306 itemsappend = items.append 307 sourcematch = source.match 308 while 1: 309 itemsappend(_parse(source, state)) 310 if sourcematch("|"): 311 continue 312 if not nested: 313 break 314 if not source.next or sourcematch(")", 0): 315 break 316 else: 317 raise error, "pattern not properly closed" 318 319 if len(items) == 1: 320 return items[0] 321 322 subpattern = SubPattern(state) 323 subpatternappend = subpattern.append 324 325 # check if all items share a common prefix 326 while 1: 327 prefix = None 328 for item in items: 329 if not item: 330 break 331 if prefix is None: 332 prefix = item[0] 333 elif item[0] != prefix: 334 break 335 else: 336 # all subitems start with a common "prefix". 337 # move it out of the branch 338 for item in items: 339 del item[0] 340 subpatternappend(prefix) 341 continue # check next one 342 break 343 344 # check if the branch can be replaced by a character set 345 for item in items: 346 if len(item) != 1 or item[0][0] != LITERAL: 347 break 348 else: 349 # we can store this as a character set instead of a 350 # branch (the compiler may optimize this even more) 351 set = [] 352 setappend = set.append 353 for item in items: 354 setappend(item[0]) 355 subpatternappend((IN, set)) 356 return subpattern 357 358 subpattern.append((BRANCH, (None, items))) 359 return subpattern 360 361 def _parse_sub_cond(source, state, condgroup): 362 item_yes = _parse(source, state) 363 if source.match("|"): 364 item_no = _parse(source, state) 365 if source.match("|"): 366 raise error, "conditional backref with more than two branches" 367 else: 368 item_no = None 369 if source.next and not source.match(")", 0): 370 raise error, "pattern not properly closed" 371 subpattern = SubPattern(state) 372 subpattern.append((GROUPREF_EXISTS, (condgroup, item_yes, item_no))) 373 return subpattern 374 375 _PATTERNENDERS = set("|)") 376 _ASSERTCHARS = set("=!<") 377 _LOOKBEHINDASSERTCHARS = set("=!") 378 _REPEATCODES = set([MIN_REPEAT, MAX_REPEAT]) 379 380 def _parse(source, state): 381 # parse a simple pattern 382 subpattern = SubPattern(state) 383 384 # precompute constants into local variables 385 subpatternappend = subpattern.append 386 sourceget = source.get 387 sourcematch = source.match 388 _len = len 389 PATTERNENDERS = _PATTERNENDERS 390 ASSERTCHARS = _ASSERTCHARS 391 LOOKBEHINDASSERTCHARS = _LOOKBEHINDASSERTCHARS 392 REPEATCODES = _REPEATCODES 393 394 while 1: 395 396 if source.next in PATTERNENDERS: 397 break # end of subpattern 398 this = sourceget() 399 if this is None: 400 break # end of pattern 401 402 if state.flags & SRE_FLAG_VERBOSE: 403 # skip whitespace and comments 404 if this in WHITESPACE: 405 continue 406 if this == "#": 407 while 1: 408 this = sourceget() 409 if this in (None, "\n"): 410 break 411 continue 412 413 if this and this[0] not in SPECIAL_CHARS: 414 subpatternappend((LITERAL, ord(this))) 415 416 elif this == "[": 417 # character set 418 set = [] 419 setappend = set.append 420 ## if sourcematch(":"): 421 ## pass # handle character classes 422 if sourcematch("^"): 423 setappend((NEGATE, None)) 424 # check remaining characters 425 start = set[:] 426 while 1: 427 this = sourceget() 428 if this == "]" and set != start: 429 break 430 elif this and this[0] == "\\": 431 code1 = _class_escape(source, this) 432 elif this: 433 code1 = LITERAL, ord(this) 434 else: 435 raise error, "unexpected end of regular expression" 436 if sourcematch("-"): 437 # potential range 438 this = sourceget() 439 if this == "]": 440 if code1[0] is IN: 441 code1 = code1[1][0] 442 setappend(code1) 443 setappend((LITERAL, ord("-"))) 444 break 445 elif this: 446 if this[0] == "\\": 447 code2 = _class_escape(source, this) 448 else: 449 code2 = LITERAL, ord(this) 450 if code1[0] != LITERAL or code2[0] != LITERAL: 451 raise error, "bad character range" 452 lo = code1[1] 453 hi = code2[1] 454 if hi < lo: 455 raise error, "bad character range" 456 setappend((RANGE, (lo, hi))) 457 else: 458 raise error, "unexpected end of regular expression" 459 else: 460 if code1[0] is IN: 461 code1 = code1[1][0] 462 setappend(code1) 463 464 # XXX: <fl> should move set optimization to compiler! 465 if _len(set)==1 and set[0][0] is LITERAL: 466 subpatternappend(set[0]) # optimization 467 elif _len(set)==2 and set[0][0] is NEGATE and set[1][0] is LITERAL: 468 subpatternappend((NOT_LITERAL, set[1][1])) # optimization 469 else: 470 # XXX: <fl> should add charmap optimization here 471 subpatternappend((IN, set)) 472 473 elif this and this[0] in REPEAT_CHARS: 474 # repeat previous item 475 if this == "?": 476 min, max = 0, 1 477 elif this == "*": 478 min, max = 0, MAXREPEAT 479 480 elif this == "+": 481 min, max = 1, MAXREPEAT 482 elif this == "{": 483 if source.next == "}": 484 subpatternappend((LITERAL, ord(this))) 485 continue 486 here = source.tell() 487 min, max = 0, MAXREPEAT 488 lo = hi = "" 489 while source.next in DIGITS: 490 lo = lo + source.get() 491 if sourcematch(","): 492 while source.next in DIGITS: 493 hi = hi + sourceget() 494 else: 495 hi = lo 496 if not sourcematch("}"): 497 subpatternappend((LITERAL, ord(this))) 498 source.seek(here) 499 continue 500 if lo: 501 min = int(lo) 502 if min >= MAXREPEAT: 503 raise OverflowError("the repetition number is too large") 504 if hi: 505 max = int(hi) 506 if max >= MAXREPEAT: 507 raise OverflowError("the repetition number is too large") 508 if max < min: 509 raise error("bad repeat interval") 510 else: 511 raise error, "not supported" 512 # figure out which item to repeat 513 if subpattern: 514 item = subpattern[-1:] 515 else: 516 item = None 517 if not item or (_len(item) == 1 and item[0][0] == AT): 518 raise error, "nothing to repeat" 519 if item[0][0] in REPEATCODES: 520 raise error, "multiple repeat" 521 if sourcematch("?"): 522 subpattern[-1] = (MIN_REPEAT, (min, max, item)) 523 else: 524 subpattern[-1] = (MAX_REPEAT, (min, max, item)) 525 526 elif this == ".": 527 subpatternappend((ANY, None)) 528 529 elif this == "(": 530 group = 1 531 name = None 532 condgroup = None 533 if sourcematch("?"): 534 group = 0 535 # options 536 if sourcematch("P"): 537 # python extensions 538 if sourcematch("<"): 539 # named group: skip forward to end of name 540 name = "" 541 while 1: 542 char = sourceget() 543 if char is None: 544 raise error, "unterminated name" 545 if char == ">": 546 break 547 name = name + char 548 group = 1 549 if not name: 550 raise error("missing group name") 551 if not isname(name): 552 raise error("bad character in group name %r" % 553 name) 554 elif sourcematch("="): 555 # named backreference 556 name = "" 557 while 1: 558 char = sourceget() 559 if char is None: 560 raise error, "unterminated name" 561 if char == ")": 562 break 563 name = name + char 564 if not name: 565 raise error("missing group name") 566 if not isname(name): 567 raise error("bad character in backref group name " 568 "%r" % name) 569 gid = state.groupdict.get(name) 570 if gid is None: 571 raise error, "unknown group name" 572 subpatternappend((GROUPREF, gid)) 573 continue 574 else: 575 char = sourceget() 576 if char is None: 577 raise error, "unexpected end of pattern" 578 raise error, "unknown specifier: ?P%s" % char 579 elif sourcematch(":"): 580 # non-capturing group 581 group = 2 582 elif sourcematch("#"): 583 # comment 584 while 1: 585 if source.next is None or source.next == ")": 586 break 587 sourceget() 588 if not sourcematch(")"): 589 raise error, "unbalanced parenthesis" 590 continue 591 elif source.next in ASSERTCHARS: 592 # lookahead assertions 593 char = sourceget() 594 dir = 1 595 if char == "<": 596 if source.next not in LOOKBEHINDASSERTCHARS: 597 raise error, "syntax error" 598 dir = -1 # lookbehind 599 char = sourceget() 600 p = _parse_sub(source, state) 601 if not sourcematch(")"): 602 raise error, "unbalanced parenthesis" 603 if char == "=": 604 subpatternappend((ASSERT, (dir, p))) 605 else: 606 subpatternappend((ASSERT_NOT, (dir, p))) 607 continue 608 elif sourcematch("("): 609 # conditional backreference group 610 condname = "" 611 while 1: 612 char = sourceget() 613 if char is None: 614 raise error, "unterminated name" 615 if char == ")": 616 break 617 condname = condname + char 618 group = 2 619 if not condname: 620 raise error("missing group name") 621 if isname(condname): 622 condgroup = state.groupdict.get(condname) 623 if condgroup is None: 624 raise error, "unknown group name" 625 else: 626 try: 627 condgroup = int(condname) 628 except ValueError: 629 raise error, "bad character in group name" 630 else: 631 # flags 632 if not source.next in FLAGS: 633 raise error, "unexpected end of pattern" 634 while source.next in FLAGS: 635 state.flags = state.flags | FLAGS[sourceget()] 636 if group: 637 # parse group contents 638 if group == 2: 639 # anonymous group 640 group = None 641 else: 642 group = state.opengroup(name) 643 if condgroup: 644 p = _parse_sub_cond(source, state, condgroup) 645 else: 646 p = _parse_sub(source, state) 647 if not sourcematch(")"): 648 raise error, "unbalanced parenthesis" 649 if group is not None: 650 state.closegroup(group) 651 subpatternappend((SUBPATTERN, (group, p))) 652 else: 653 while 1: 654 char = sourceget() 655 if char is None: 656 raise error, "unexpected end of pattern" 657 if char == ")": 658 break 659 raise error, "unknown extension" 660 661 elif this == "^": 662 subpatternappend((AT, AT_BEGINNING)) 663 664 elif this == "$": 665 subpattern.append((AT, AT_END)) 666 667 elif this and this[0] == "\\": 668 code = _escape(source, this, state) 669 subpatternappend(code) 670 671 else: 672 raise error, "parser error" 673 674 return subpattern 675 676 def parse(str, flags=0, pattern=None): 677 # parse 're' pattern into list of (opcode, argument) tuples 678 679 source = Tokenizer(str) 680 681 if pattern is None: 682 pattern = Pattern() 683 pattern.flags = flags 684 pattern.str = str 685 686 p = _parse_sub(source, pattern, 0) 687 688 tail = source.get() 689 if tail == ")": 690 raise error, "unbalanced parenthesis" 691 elif tail: 692 raise error, "bogus characters at end of regular expression" 693 694 if flags & SRE_FLAG_DEBUG: 695 p.dump() 696 697 if not (flags & SRE_FLAG_VERBOSE) and p.pattern.flags & SRE_FLAG_VERBOSE: 698 # the VERBOSE flag was switched on inside the pattern. to be 699 # on the safe side, we'll parse the whole thing again... 700 return parse(str, p.pattern.flags) 701 702 return p 703 704 def parse_template(source, pattern): 705 # parse 're' replacement string into list of literals and 706 # group references 707 s = Tokenizer(source) 708 sget = s.get 709 p = [] 710 a = p.append 711 def literal(literal, p=p, pappend=a): 712 if p and p[-1][0] is LITERAL: 713 p[-1] = LITERAL, p[-1][1] + literal 714 else: 715 pappend((LITERAL, literal)) 716 sep = source[:0] 717 if type(sep) is type(""): 718 makechar = chr 719 else: 720 makechar = unichr 721 while 1: 722 this = sget() 723 if this is None: 724 break # end of replacement string 725 if this and this[0] == "\\": 726 # group 727 c = this[1:2] 728 if c == "g": 729 name = "" 730 if s.match("<"): 731 while 1: 732 char = sget() 733 if char is None: 734 raise error, "unterminated group name" 735 if char == ">": 736 break 737 name = name + char 738 if not name: 739 raise error, "missing group name" 740 try: 741 index = int(name) 742 if index < 0: 743 raise error, "negative group number" 744 except ValueError: 745 if not isname(name): 746 raise error, "bad character in group name" 747 try: 748 index = pattern.groupindex[name] 749 except KeyError: 750 raise IndexError, "unknown group name" 751 a((MARK, index)) 752 elif c == "0": 753 if s.next in OCTDIGITS: 754 this = this + sget() 755 if s.next in OCTDIGITS: 756 this = this + sget() 757 literal(makechar(int(this[1:], 8) & 0xff)) 758 elif c in DIGITS: 759 isoctal = False 760 if s.next in DIGITS: 761 this = this + sget() 762 if (c in OCTDIGITS and this[2] in OCTDIGITS and 763 s.next in OCTDIGITS): 764 this = this + sget() 765 isoctal = True 766 literal(makechar(int(this[1:], 8) & 0xff)) 767 if not isoctal: 768 a((MARK, int(this[1:]))) 769 else: 770 try: 771 this = makechar(ESCAPES[this][1]) 772 except KeyError: 773 pass 774 literal(this) 775 else: 776 literal(this) 777 # convert template to groups and literals lists 778 i = 0 779 groups = [] 780 groupsappend = groups.append 781 literals = [None] * len(p) 782 for c, s in p: 783 if c is MARK: 784 groupsappend((i, s)) 785 # literal[i] is already None 786 else: 787 literals[i] = s 788 i = i + 1 789 return groups, literals 790 791 def expand_template(template, match): 792 g = match.group 793 sep = match.string[:0] 794 groups, literals = template 795 literals = literals[:] 796 try: 797 for index, group in groups: 798 literals[index] = s = g(group) 799 if s is None: 800 raise error, "unmatched group" 801 except IndexError: 802 raise error, "invalid group reference" 803 return sep.join(literals) 804