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