blob: fe5d143482fbf31092cea3524fd87dd7e5f66966 [file] [log] [blame]
Guido van Rossum7627c0d2000-03-31 14:58:54 +00001#
2# Secret Labs' Regular Expression Engine
Guido van Rossum7627c0d2000-03-31 14:58:54 +00003#
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +00004# convert re-style regular expression to sre pattern
Guido van Rossum7627c0d2000-03-31 14:58:54 +00005#
Fredrik Lundh770617b2001-01-14 15:06:11 +00006# Copyright (c) 1998-2001 by Secret Labs AB. All rights reserved.
Guido van Rossum7627c0d2000-03-31 14:58:54 +00007#
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00008# See the sre.py file for information on usage and redistribution.
Guido van Rossum7627c0d2000-03-31 14:58:54 +00009#
10
Fred Drakeb8f22742001-09-04 19:10:20 +000011"""Internal support module for sre"""
12
Fredrik Lundh470ea5a2001-01-14 21:00:44 +000013# XXX: show string offset and offending character for all errors
14
Fredrik Lundhf2989b22001-02-18 12:05:16 +000015# this module works under 1.5.2 and later. don't use string methods
16import string, sys
Guido van Rossum7627c0d2000-03-31 14:58:54 +000017
18from sre_constants import *
19
20SPECIAL_CHARS = ".\\[{()*+?^$|"
Fredrik Lundh143328b2000-09-02 11:03:34 +000021REPEAT_CHARS = "*+?{"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000022
Tim Peters17289422000-09-02 07:44:32 +000023DIGITS = tuple("0123456789")
Guido van Rossumb81e70e2000-04-10 17:10:48 +000024
Fredrik Lundh75f2d672000-06-29 11:34:28 +000025OCTDIGITS = tuple("01234567")
26HEXDIGITS = tuple("0123456789abcdefABCDEF")
Guido van Rossum7627c0d2000-03-31 14:58:54 +000027
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +000028WHITESPACE = tuple(" \t\n\r\v\f")
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +000029
Guido van Rossum7627c0d2000-03-31 14:58:54 +000030ESCAPES = {
Fredrik Lundhf2989b22001-02-18 12:05:16 +000031 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 Lundh0640e112000-06-30 13:55:15 +000038 r"\\": (LITERAL, ord("\\"))
Guido van Rossum7627c0d2000-03-31 14:58:54 +000039}
40
41CATEGORIES = {
Fredrik Lundh770617b2001-01-14 15:06:11 +000042 r"\A": (AT, AT_BEGINNING_STRING), # start of string
Fredrik Lundh01016fe2000-06-30 00:27:46 +000043 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 Lundh770617b2001-01-14 15:06:11 +000051 r"\Z": (AT, AT_END_STRING), # end of string
Guido van Rossum7627c0d2000-03-31 14:58:54 +000052}
53
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +000054FLAGS = {
Fredrik Lundh436c3d52000-06-29 08:58:44 +000055 # standard flags
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +000056 "i": SRE_FLAG_IGNORECASE,
57 "L": SRE_FLAG_LOCALE,
58 "m": SRE_FLAG_MULTILINE,
59 "s": SRE_FLAG_DOTALL,
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +000060 "x": SRE_FLAG_VERBOSE,
Fredrik Lundh436c3d52000-06-29 08:58:44 +000061 # extensions
62 "t": SRE_FLAG_TEMPLATE,
63 "u": SRE_FLAG_UNICODE,
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +000064}
65
Fredrik Lundhf2989b22001-02-18 12:05:16 +000066# figure out best way to convert hex/octal numbers to integers
67try:
68 int("10", 8)
69 atoi = int # 2.0 and later
70except TypeError:
71 atoi = string.atoi # 1.5.2
72
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +000073class Pattern:
74 # master pattern object. keeps track of global attributes
Guido van Rossum7627c0d2000-03-31 14:58:54 +000075 def __init__(self):
Fredrik Lundh90a07912000-06-30 07:50:59 +000076 self.flags = 0
Fredrik Lundhebc37b22000-10-28 19:30:41 +000077 self.open = []
Fredrik Lundh90a07912000-06-30 07:50:59 +000078 self.groups = 1
79 self.groupdict = {}
Fredrik Lundhebc37b22000-10-28 19:30:41 +000080 def opengroup(self, name=None):
Fredrik Lundh90a07912000-06-30 07:50:59 +000081 gid = self.groups
82 self.groups = gid + 1
Raymond Hettingerf13eb552002-06-02 00:40:05 +000083 if name is not None:
Tim Peters75335872001-11-03 19:35:43 +000084 ogid = self.groupdict.get(name, None)
85 if ogid is not None:
Fredrik Lundh82b23072001-12-09 16:13:15 +000086 raise error, ("redefinition of group name %s as group %d; "
87 "was group %d" % (repr(name), gid, ogid))
Fredrik Lundh90a07912000-06-30 07:50:59 +000088 self.groupdict[name] = gid
Fredrik Lundhebc37b22000-10-28 19:30:41 +000089 self.open.append(gid)
Fredrik Lundh90a07912000-06-30 07:50:59 +000090 return gid
Fredrik Lundhebc37b22000-10-28 19:30:41 +000091 def closegroup(self, gid):
92 self.open.remove(gid)
93 def checkgroup(self, gid):
94 return gid < self.groups and gid not in self.open
Guido van Rossum7627c0d2000-03-31 14:58:54 +000095
96class SubPattern:
97 # a subpattern, in intermediate form
98 def __init__(self, pattern, data=None):
Fredrik Lundh90a07912000-06-30 07:50:59 +000099 self.pattern = pattern
Raymond Hettingerf13eb552002-06-02 00:40:05 +0000100 if data is None:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000101 data = []
102 self.data = data
103 self.width = None
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000104 def dump(self, level=0):
105 nl = 1
106 for op, av in self.data:
107 print level*" " + op,; nl = 0
108 if op == "in":
109 # member sublanguage
110 print; nl = 1
111 for op, a in av:
112 print (level+1)*" " + op, a
113 elif op == "branch":
114 print; nl = 1
115 i = 0
116 for a in av[1]:
117 if i > 0:
118 print level*" " + "or"
119 a.dump(level+1); nl = 1
120 i = i + 1
121 elif type(av) in (type(()), type([])):
122 for a in av:
123 if isinstance(a, SubPattern):
124 if not nl: print
125 a.dump(level+1); nl = 1
126 else:
127 print a, ; nl = 0
128 else:
129 print av, ; nl = 0
130 if not nl: print
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000131 def __repr__(self):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000132 return repr(self.data)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000133 def __len__(self):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000134 return len(self.data)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000135 def __delitem__(self, index):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000136 del self.data[index]
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000137 def __getitem__(self, index):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000138 return self.data[index]
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000139 def __setitem__(self, index, code):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000140 self.data[index] = code
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000141 def __getslice__(self, start, stop):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000142 return SubPattern(self.pattern, self.data[start:stop])
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000143 def insert(self, index, code):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000144 self.data.insert(index, code)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000145 def append(self, code):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000146 self.data.append(code)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000147 def getwidth(self):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000148 # determine the width (min, max) for this subpattern
149 if self.width:
150 return self.width
151 lo = hi = 0L
152 for op, av in self.data:
153 if op is BRANCH:
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +0000154 i = sys.maxint
155 j = 0
Fredrik Lundh90a07912000-06-30 07:50:59 +0000156 for av in av[1]:
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +0000157 l, h = av.getwidth()
158 i = min(i, l)
Fredrik Lundhe1869832000-08-01 22:47:49 +0000159 j = max(j, h)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000160 lo = lo + i
161 hi = hi + j
162 elif op is CALL:
163 i, j = av.getwidth()
164 lo = lo + i
165 hi = hi + j
166 elif op is SUBPATTERN:
167 i, j = av[1].getwidth()
168 lo = lo + i
169 hi = hi + j
170 elif op in (MIN_REPEAT, MAX_REPEAT):
171 i, j = av[2].getwidth()
172 lo = lo + long(i) * av[0]
173 hi = hi + long(j) * av[1]
174 elif op in (ANY, RANGE, IN, LITERAL, NOT_LITERAL, CATEGORY):
175 lo = lo + 1
176 hi = hi + 1
177 elif op == SUCCESS:
178 break
179 self.width = int(min(lo, sys.maxint)), int(min(hi, sys.maxint))
180 return self.width
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000181
182class Tokenizer:
183 def __init__(self, string):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000184 self.string = string
Fredrik Lundhc13222c2000-07-01 23:49:14 +0000185 self.index = 0
186 self.__next()
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000187 def __next(self):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000188 if self.index >= len(self.string):
Fredrik Lundhc13222c2000-07-01 23:49:14 +0000189 self.next = None
190 return
Fredrik Lundh90a07912000-06-30 07:50:59 +0000191 char = self.string[self.index]
192 if char[0] == "\\":
193 try:
194 c = self.string[self.index + 1]
195 except IndexError:
Fredrik Lundh8a0232d2001-11-02 13:59:51 +0000196 raise error, "bogus escape (end of line)"
Fredrik Lundh90a07912000-06-30 07:50:59 +0000197 char = char + c
198 self.index = self.index + len(char)
Fredrik Lundhc13222c2000-07-01 23:49:14 +0000199 self.next = char
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000200 def match(self, char, skip=1):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000201 if char == self.next:
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000202 if skip:
203 self.__next()
Fredrik Lundh90a07912000-06-30 07:50:59 +0000204 return 1
205 return 0
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000206 def get(self):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000207 this = self.next
Fredrik Lundhc13222c2000-07-01 23:49:14 +0000208 self.__next()
Fredrik Lundh90a07912000-06-30 07:50:59 +0000209 return this
Fredrik Lundhc13222c2000-07-01 23:49:14 +0000210 def tell(self):
211 return self.index, self.next
212 def seek(self, index):
213 self.index, self.next = index
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000214
Fredrik Lundh4781b072000-06-29 12:38:45 +0000215def isident(char):
216 return "a" <= char <= "z" or "A" <= char <= "Z" or char == "_"
217
218def isdigit(char):
219 return "0" <= char <= "9"
220
221def isname(name):
222 # check that group name is a valid string
Fredrik Lundh4781b072000-06-29 12:38:45 +0000223 if not isident(name[0]):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000224 return False
Fredrik Lundh4781b072000-06-29 12:38:45 +0000225 for char in name:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000226 if not isident(char) and not isdigit(char):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000227 return False
228 return True
Fredrik Lundh4781b072000-06-29 12:38:45 +0000229
Fredrik Lundh01016fe2000-06-30 00:27:46 +0000230def _group(escape, groups):
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000231 # check if the escape string represents a valid group
232 try:
Fredrik Lundhf2989b22001-02-18 12:05:16 +0000233 gid = atoi(escape[1:])
Fredrik Lundhb71624e2000-06-30 09:13:06 +0000234 if gid and gid < groups:
235 return gid
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000236 except ValueError:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000237 pass
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000238 return None # not a valid group
239
240def _class_escape(source, escape):
241 # handle escape code inside character class
242 code = ESCAPES.get(escape)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000243 if code:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000244 return code
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000245 code = CATEGORIES.get(escape)
246 if code:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000247 return code
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000248 try:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000249 if escape[1:2] == "x":
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000250 # hexadecimal escape (exactly two digits)
251 while source.next in HEXDIGITS and len(escape) < 4:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000252 escape = escape + source.get()
253 escape = escape[2:]
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000254 if len(escape) != 2:
255 raise error, "bogus escape: %s" % repr("\\" + escape)
Fredrik Lundhf2989b22001-02-18 12:05:16 +0000256 return LITERAL, atoi(escape, 16) & 0xff
Martin v. Löwis53d93ad2003-04-19 08:37:24 +0000257 elif escape[1:2] in OCTDIGITS:
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000258 # octal escape (up to three digits)
259 while source.next in OCTDIGITS and len(escape) < 5:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000260 escape = escape + source.get()
261 escape = escape[1:]
Fredrik Lundhf2989b22001-02-18 12:05:16 +0000262 return LITERAL, atoi(escape, 8) & 0xff
Fredrik Lundh90a07912000-06-30 07:50:59 +0000263 if len(escape) == 2:
Fredrik Lundh0640e112000-06-30 13:55:15 +0000264 return LITERAL, ord(escape[1])
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000265 except ValueError:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000266 pass
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000267 raise error, "bogus escape: %s" % repr(escape)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000268
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000269def _escape(source, escape, state):
270 # handle escape code in expression
271 code = CATEGORIES.get(escape)
272 if code:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000273 return code
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000274 code = ESCAPES.get(escape)
275 if code:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000276 return code
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000277 try:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000278 if escape[1:2] == "x":
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000279 # hexadecimal escape
280 while source.next in HEXDIGITS and len(escape) < 4:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000281 escape = escape + source.get()
Fredrik Lundh143328b2000-09-02 11:03:34 +0000282 if len(escape) != 4:
283 raise ValueError
Fredrik Lundhf2989b22001-02-18 12:05:16 +0000284 return LITERAL, atoi(escape[2:], 16) & 0xff
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000285 elif escape[1:2] == "0":
286 # octal escape
Fredrik Lundh143328b2000-09-02 11:03:34 +0000287 while source.next in OCTDIGITS and len(escape) < 4:
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000288 escape = escape + source.get()
Fredrik Lundhf2989b22001-02-18 12:05:16 +0000289 return LITERAL, atoi(escape[1:], 8) & 0xff
Fredrik Lundh90a07912000-06-30 07:50:59 +0000290 elif escape[1:2] in DIGITS:
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000291 # octal escape *or* decimal group reference (sigh)
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000292 if source.next in DIGITS:
293 escape = escape + source.get()
Fredrik Lundh143328b2000-09-02 11:03:34 +0000294 if (escape[1] in OCTDIGITS and escape[2] in OCTDIGITS and
295 source.next in OCTDIGITS):
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000296 # got three octal digits; this is an octal escape
Fredrik Lundh90a07912000-06-30 07:50:59 +0000297 escape = escape + source.get()
Fredrik Lundhf2989b22001-02-18 12:05:16 +0000298 return LITERAL, atoi(escape[1:], 8) & 0xff
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000299 # got at least one decimal digit; this is a group reference
300 group = _group(escape, state.groups)
301 if group:
Fredrik Lundhebc37b22000-10-28 19:30:41 +0000302 if not state.checkgroup(group):
303 raise error, "cannot refer to open group"
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000304 return GROUPREF, group
Fredrik Lundh143328b2000-09-02 11:03:34 +0000305 raise ValueError
Fredrik Lundh90a07912000-06-30 07:50:59 +0000306 if len(escape) == 2:
Fredrik Lundh0640e112000-06-30 13:55:15 +0000307 return LITERAL, ord(escape[1])
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000308 except ValueError:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000309 pass
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000310 raise error, "bogus escape: %s" % repr(escape)
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000311
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000312def _parse_sub(source, state, nested=1):
313 # parse an alternation: a|b|c
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000314
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000315 items = []
316 while 1:
317 items.append(_parse(source, state))
318 if source.match("|"):
319 continue
320 if not nested:
321 break
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000322 if not source.next or source.match(")", 0):
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000323 break
324 else:
325 raise error, "pattern not properly closed"
326
327 if len(items) == 1:
328 return items[0]
329
330 subpattern = SubPattern(state)
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000331
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000332 # check if all items share a common prefix
333 while 1:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000334 prefix = None
335 for item in items:
336 if not item:
337 break
338 if prefix is None:
339 prefix = item[0]
340 elif item[0] != prefix:
341 break
342 else:
343 # all subitems start with a common "prefix".
344 # move it out of the branch
345 for item in items:
346 del item[0]
347 subpattern.append(prefix)
348 continue # check next one
349 break
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000350
351 # check if the branch can be replaced by a character set
352 for item in items:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000353 if len(item) != 1 or item[0][0] != LITERAL:
354 break
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000355 else:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000356 # we can store this as a character set instead of a
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000357 # branch (the compiler may optimize this even more)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000358 set = []
359 for item in items:
360 set.append(item[0])
361 subpattern.append((IN, set))
362 return subpattern
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000363
364 subpattern.append((BRANCH, (None, items)))
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000365 return subpattern
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000366
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000367def _parse_sub_cond(source, state, condgroup):
368 item_yes = _parse(source, state)
369 if source.match("|"):
370 item_no = _parse(source, state)
371 if source.match("|"):
372 raise error, "conditional backref with more than two branches"
373 else:
374 item_no = None
375 if source.next and not source.match(")", 0):
376 raise error, "pattern not properly closed"
377 subpattern = SubPattern(state)
378 subpattern.append((GROUPREF_EXISTS, (condgroup, item_yes, item_no)))
379 return subpattern
380
Fredrik Lundh55a4f4a2000-06-30 22:37:31 +0000381def _parse(source, state):
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000382 # parse a simple pattern
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000383
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000384 subpattern = SubPattern(state)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000385
386 while 1:
387
Fredrik Lundh90a07912000-06-30 07:50:59 +0000388 if source.next in ("|", ")"):
389 break # end of subpattern
390 this = source.get()
391 if this is None:
392 break # end of pattern
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000393
Fredrik Lundh90a07912000-06-30 07:50:59 +0000394 if state.flags & SRE_FLAG_VERBOSE:
395 # skip whitespace and comments
396 if this in WHITESPACE:
397 continue
398 if this == "#":
399 while 1:
400 this = source.get()
401 if this in (None, "\n"):
402 break
403 continue
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000404
Fredrik Lundh90a07912000-06-30 07:50:59 +0000405 if this and this[0] not in SPECIAL_CHARS:
Fredrik Lundh0640e112000-06-30 13:55:15 +0000406 subpattern.append((LITERAL, ord(this)))
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000407
Fredrik Lundh90a07912000-06-30 07:50:59 +0000408 elif this == "[":
409 # character set
410 set = []
411## if source.match(":"):
412## pass # handle character classes
413 if source.match("^"):
414 set.append((NEGATE, None))
415 # check remaining characters
416 start = set[:]
417 while 1:
418 this = source.get()
419 if this == "]" and set != start:
420 break
421 elif this and this[0] == "\\":
422 code1 = _class_escape(source, this)
423 elif this:
Fredrik Lundh0640e112000-06-30 13:55:15 +0000424 code1 = LITERAL, ord(this)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000425 else:
426 raise error, "unexpected end of regular expression"
427 if source.match("-"):
428 # potential range
429 this = source.get()
430 if this == "]":
Fredrik Lundh025468d2000-10-07 10:16:19 +0000431 if code1[0] is IN:
432 code1 = code1[1][0]
Fredrik Lundh90a07912000-06-30 07:50:59 +0000433 set.append(code1)
Fredrik Lundh0640e112000-06-30 13:55:15 +0000434 set.append((LITERAL, ord("-")))
Fredrik Lundh90a07912000-06-30 07:50:59 +0000435 break
Guido van Rossum41c99e72003-04-14 17:59:34 +0000436 elif this:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000437 if this[0] == "\\":
438 code2 = _class_escape(source, this)
439 else:
Fredrik Lundh0640e112000-06-30 13:55:15 +0000440 code2 = LITERAL, ord(this)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000441 if code1[0] != LITERAL or code2[0] != LITERAL:
Fredrik Lundh470ea5a2001-01-14 21:00:44 +0000442 raise error, "bad character range"
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000443 lo = code1[1]
444 hi = code2[1]
445 if hi < lo:
Fredrik Lundh470ea5a2001-01-14 21:00:44 +0000446 raise error, "bad character range"
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000447 set.append((RANGE, (lo, hi)))
Guido van Rossum41c99e72003-04-14 17:59:34 +0000448 else:
449 raise error, "unexpected end of regular expression"
Fredrik Lundh90a07912000-06-30 07:50:59 +0000450 else:
451 if code1[0] is IN:
452 code1 = code1[1][0]
453 set.append(code1)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000454
Fredrik Lundh770617b2001-01-14 15:06:11 +0000455 # XXX: <fl> should move set optimization to compiler!
Fredrik Lundh90a07912000-06-30 07:50:59 +0000456 if len(set)==1 and set[0][0] is LITERAL:
457 subpattern.append(set[0]) # optimization
458 elif len(set)==2 and set[0][0] is NEGATE and set[1][0] is LITERAL:
459 subpattern.append((NOT_LITERAL, set[1][1])) # optimization
460 else:
Fredrik Lundh770617b2001-01-14 15:06:11 +0000461 # XXX: <fl> should add charmap optimization here
Fredrik Lundh90a07912000-06-30 07:50:59 +0000462 subpattern.append((IN, set))
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000463
Fredrik Lundh90a07912000-06-30 07:50:59 +0000464 elif this and this[0] in REPEAT_CHARS:
465 # repeat previous item
466 if this == "?":
467 min, max = 0, 1
468 elif this == "*":
469 min, max = 0, MAXREPEAT
Fredrik Lundhc0c7ee32001-02-18 21:04:48 +0000470
Fredrik Lundh90a07912000-06-30 07:50:59 +0000471 elif this == "+":
472 min, max = 1, MAXREPEAT
473 elif this == "{":
Fredrik Lundhc13222c2000-07-01 23:49:14 +0000474 here = source.tell()
Fredrik Lundh90a07912000-06-30 07:50:59 +0000475 min, max = 0, MAXREPEAT
476 lo = hi = ""
477 while source.next in DIGITS:
478 lo = lo + source.get()
479 if source.match(","):
480 while source.next in DIGITS:
481 hi = hi + source.get()
482 else:
483 hi = lo
484 if not source.match("}"):
Fredrik Lundhc13222c2000-07-01 23:49:14 +0000485 subpattern.append((LITERAL, ord(this)))
486 source.seek(here)
487 continue
Fredrik Lundh90a07912000-06-30 07:50:59 +0000488 if lo:
Fredrik Lundhf2989b22001-02-18 12:05:16 +0000489 min = atoi(lo)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000490 if hi:
Fredrik Lundhf2989b22001-02-18 12:05:16 +0000491 max = atoi(hi)
Fredrik Lundh470ea5a2001-01-14 21:00:44 +0000492 if max < min:
493 raise error, "bad repeat interval"
Fredrik Lundh90a07912000-06-30 07:50:59 +0000494 else:
495 raise error, "not supported"
496 # figure out which item to repeat
497 if subpattern:
498 item = subpattern[-1:]
499 else:
Fredrik Lundhc0c7ee32001-02-18 21:04:48 +0000500 item = None
501 if not item or (len(item) == 1 and item[0][0] == AT):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000502 raise error, "nothing to repeat"
Fredrik Lundh470ea5a2001-01-14 21:00:44 +0000503 if item[0][0] in (MIN_REPEAT, MAX_REPEAT):
504 raise error, "multiple repeat"
Fredrik Lundh90a07912000-06-30 07:50:59 +0000505 if source.match("?"):
506 subpattern[-1] = (MIN_REPEAT, (min, max, item))
507 else:
508 subpattern[-1] = (MAX_REPEAT, (min, max, item))
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000509
Fredrik Lundh90a07912000-06-30 07:50:59 +0000510 elif this == ".":
511 subpattern.append((ANY, None))
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000512
Fredrik Lundh90a07912000-06-30 07:50:59 +0000513 elif this == "(":
514 group = 1
515 name = None
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000516 condgroup = None
Fredrik Lundh90a07912000-06-30 07:50:59 +0000517 if source.match("?"):
518 group = 0
519 # options
520 if source.match("P"):
521 # python extensions
522 if source.match("<"):
523 # named group: skip forward to end of name
524 name = ""
525 while 1:
526 char = source.get()
527 if char is None:
528 raise error, "unterminated name"
529 if char == ">":
530 break
531 name = name + char
532 group = 1
533 if not isname(name):
Fredrik Lundh470ea5a2001-01-14 21:00:44 +0000534 raise error, "bad character in group name"
Fredrik Lundh90a07912000-06-30 07:50:59 +0000535 elif source.match("="):
536 # named backreference
Fredrik Lundhb71624e2000-06-30 09:13:06 +0000537 name = ""
538 while 1:
539 char = source.get()
540 if char is None:
541 raise error, "unterminated name"
542 if char == ")":
543 break
544 name = name + char
545 if not isname(name):
Fredrik Lundh470ea5a2001-01-14 21:00:44 +0000546 raise error, "bad character in group name"
Fredrik Lundhb71624e2000-06-30 09:13:06 +0000547 gid = state.groupdict.get(name)
548 if gid is None:
549 raise error, "unknown group name"
Fredrik Lundh72b82ba2000-07-03 21:31:48 +0000550 subpattern.append((GROUPREF, gid))
Fredrik Lundh7cafe4d2000-07-02 17:33:27 +0000551 continue
Fredrik Lundh90a07912000-06-30 07:50:59 +0000552 else:
553 char = source.get()
554 if char is None:
555 raise error, "unexpected end of pattern"
556 raise error, "unknown specifier: ?P%s" % char
557 elif source.match(":"):
558 # non-capturing group
559 group = 2
560 elif source.match("#"):
561 # comment
562 while 1:
563 if source.next is None or source.next == ")":
564 break
565 source.get()
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000566 if not source.match(")"):
567 raise error, "unbalanced parenthesis"
568 continue
Fredrik Lundh6f013982000-07-03 18:44:21 +0000569 elif source.next in ("=", "!", "<"):
Fredrik Lundh43b3b492000-06-30 10:41:31 +0000570 # lookahead assertions
571 char = source.get()
Fredrik Lundh6f013982000-07-03 18:44:21 +0000572 dir = 1
573 if char == "<":
574 if source.next not in ("=", "!"):
575 raise error, "syntax error"
576 dir = -1 # lookbehind
577 char = source.get()
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000578 p = _parse_sub(source, state)
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000579 if not source.match(")"):
580 raise error, "unbalanced parenthesis"
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000581 if char == "=":
582 subpattern.append((ASSERT, (dir, p)))
583 else:
584 subpattern.append((ASSERT_NOT, (dir, p)))
585 continue
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000586 elif source.match("("):
587 # conditional backreference group
588 condname = ""
589 while 1:
590 char = source.get()
591 if char is None:
592 raise error, "unterminated name"
593 if char == ")":
594 break
595 condname = condname + char
596 group = 2
597 if isname(condname):
598 condgroup = state.groupdict.get(condname)
599 if condgroup is None:
600 raise error, "unknown group name"
601 else:
602 try:
603 condgroup = atoi(condname)
604 except ValueError:
605 raise error, "bad character in group name"
Fredrik Lundh90a07912000-06-30 07:50:59 +0000606 else:
607 # flags
Raymond Hettinger54f02222002-06-01 14:18:47 +0000608 if not source.next in FLAGS:
Fredrik Lundh470ea5a2001-01-14 21:00:44 +0000609 raise error, "unexpected end of pattern"
Raymond Hettinger54f02222002-06-01 14:18:47 +0000610 while source.next in FLAGS:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000611 state.flags = state.flags | FLAGS[source.get()]
612 if group:
613 # parse group contents
Fredrik Lundh90a07912000-06-30 07:50:59 +0000614 if group == 2:
615 # anonymous group
616 group = None
617 else:
Fredrik Lundhebc37b22000-10-28 19:30:41 +0000618 group = state.opengroup(name)
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000619 if condgroup:
620 p = _parse_sub_cond(source, state, condgroup)
621 else:
622 p = _parse_sub(source, state)
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000623 if not source.match(")"):
624 raise error, "unbalanced parenthesis"
Fredrik Lundhebc37b22000-10-28 19:30:41 +0000625 if group is not None:
626 state.closegroup(group)
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000627 subpattern.append((SUBPATTERN, (group, p)))
Fredrik Lundh90a07912000-06-30 07:50:59 +0000628 else:
629 while 1:
630 char = source.get()
Fredrik Lundh470ea5a2001-01-14 21:00:44 +0000631 if char is None:
632 raise error, "unexpected end of pattern"
633 if char == ")":
Fredrik Lundh90a07912000-06-30 07:50:59 +0000634 break
635 raise error, "unknown extension"
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000636
Fredrik Lundh90a07912000-06-30 07:50:59 +0000637 elif this == "^":
638 subpattern.append((AT, AT_BEGINNING))
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000639
Fredrik Lundh90a07912000-06-30 07:50:59 +0000640 elif this == "$":
641 subpattern.append((AT, AT_END))
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000642
Fredrik Lundh90a07912000-06-30 07:50:59 +0000643 elif this and this[0] == "\\":
644 code = _escape(source, this, state)
645 subpattern.append(code)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000646
Fredrik Lundh90a07912000-06-30 07:50:59 +0000647 else:
648 raise error, "parser error"
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000649
650 return subpattern
651
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000652def parse(str, flags=0, pattern=None):
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000653 # parse 're' pattern into list of (opcode, argument) tuples
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000654
655 source = Tokenizer(str)
656
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000657 if pattern is None:
658 pattern = Pattern()
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000659 pattern.flags = flags
Fredrik Lundh470ea5a2001-01-14 21:00:44 +0000660 pattern.str = str
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000661
662 p = _parse_sub(source, pattern, 0)
663
664 tail = source.get()
665 if tail == ")":
666 raise error, "unbalanced parenthesis"
667 elif tail:
668 raise error, "bogus characters at end of regular expression"
669
Fredrik Lundh770617b2001-01-14 15:06:11 +0000670 if flags & SRE_FLAG_DEBUG:
671 p.dump()
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000672
Fredrik Lundhd11b5e52000-10-03 19:22:26 +0000673 if not (flags & SRE_FLAG_VERBOSE) and p.pattern.flags & SRE_FLAG_VERBOSE:
674 # the VERBOSE flag was switched on inside the pattern. to be
675 # on the safe side, we'll parse the whole thing again...
676 return parse(str, p.pattern.flags)
677
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000678 return p
679
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000680def parse_template(source, pattern):
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000681 # parse 're' replacement string into list of literals and
682 # group references
683 s = Tokenizer(source)
684 p = []
685 a = p.append
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000686 def literal(literal, p=p):
687 if p and p[-1][0] is LITERAL:
688 p[-1] = LITERAL, p[-1][1] + literal
689 else:
690 p.append((LITERAL, literal))
691 sep = source[:0]
692 if type(sep) is type(""):
Fredrik Lundh59b68652001-09-18 20:55:24 +0000693 makechar = chr
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000694 else:
Fredrik Lundh59b68652001-09-18 20:55:24 +0000695 makechar = unichr
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000696 while 1:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000697 this = s.get()
698 if this is None:
699 break # end of replacement string
700 if this and this[0] == "\\":
701 # group
702 if this == "\\g":
703 name = ""
704 if s.match("<"):
705 while 1:
706 char = s.get()
707 if char is None:
708 raise error, "unterminated group name"
709 if char == ">":
710 break
711 name = name + char
712 if not name:
713 raise error, "bad group name"
714 try:
Fredrik Lundhf2989b22001-02-18 12:05:16 +0000715 index = atoi(name)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000716 except ValueError:
717 if not isname(name):
Fredrik Lundh470ea5a2001-01-14 21:00:44 +0000718 raise error, "bad character in group name"
Fredrik Lundh90a07912000-06-30 07:50:59 +0000719 try:
720 index = pattern.groupindex[name]
721 except KeyError:
722 raise IndexError, "unknown group name"
723 a((MARK, index))
724 elif len(this) > 1 and this[1] in DIGITS:
725 code = None
726 while 1:
727 group = _group(this, pattern.groups+1)
728 if group:
Fredrik Lundh19f977b2000-09-24 14:46:23 +0000729 if (s.next not in DIGITS or
Fredrik Lundh90a07912000-06-30 07:50:59 +0000730 not _group(this + s.next, pattern.groups+1)):
Fredrik Lundh1c5aa692001-01-16 07:37:30 +0000731 code = MARK, group
Fredrik Lundh90a07912000-06-30 07:50:59 +0000732 break
733 elif s.next in OCTDIGITS:
734 this = this + s.get()
735 else:
736 break
737 if not code:
738 this = this[1:]
Fredrik Lundh59b68652001-09-18 20:55:24 +0000739 code = LITERAL, makechar(atoi(this[-6:], 8) & 0xff)
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000740 if code[0] is LITERAL:
741 literal(code[1])
742 else:
743 a(code)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000744 else:
745 try:
Fredrik Lundh59b68652001-09-18 20:55:24 +0000746 this = makechar(ESCAPES[this][1])
Fredrik Lundh90a07912000-06-30 07:50:59 +0000747 except KeyError:
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000748 pass
749 literal(this)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000750 else:
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000751 literal(this)
752 # convert template to groups and literals lists
753 i = 0
754 groups = []
755 literals = []
756 for c, s in p:
757 if c is MARK:
758 groups.append((i, s))
759 literals.append(None)
760 else:
761 literals.append(s)
762 i = i + 1
763 return groups, literals
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000764
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000765def expand_template(template, match):
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000766 g = match.group
Fredrik Lundh0640e112000-06-30 13:55:15 +0000767 sep = match.string[:0]
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000768 groups, literals = template
769 literals = literals[:]
770 try:
771 for index, group in groups:
772 literals[index] = s = g(group)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000773 if s is None:
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000774 raise IndexError
775 except IndexError:
776 raise error, "empty group"
777 return string.join(literals, sep)