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