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