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