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