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