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