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