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