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