blob: 5c4298af1d65db1b2b7d66c343b54b3289fa3bb9 [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
Barry Warsaw8bee7612004-08-25 02:22:30 +000015import sys
Guido van Rossum7627c0d2000-03-31 14:58:54 +000016
17from sre_constants import *
18
19SPECIAL_CHARS = ".\\[{()*+?^$|"
Fredrik Lundh143328b2000-09-02 11:03:34 +000020REPEAT_CHARS = "*+?{"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000021
Tim Peters17289422000-09-02 07:44:32 +000022DIGITS = tuple("0123456789")
Guido van Rossumb81e70e2000-04-10 17:10:48 +000023
Fredrik Lundh75f2d672000-06-29 11:34:28 +000024OCTDIGITS = tuple("01234567")
25HEXDIGITS = tuple("0123456789abcdefABCDEF")
Guido van Rossum7627c0d2000-03-31 14:58:54 +000026
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +000027WHITESPACE = tuple(" \t\n\r\v\f")
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +000028
Guido van Rossum7627c0d2000-03-31 14:58:54 +000029ESCAPES = {
Fredrik Lundhf2989b22001-02-18 12:05:16 +000030 r"\a": (LITERAL, ord("\a")),
31 r"\b": (LITERAL, ord("\b")),
32 r"\f": (LITERAL, ord("\f")),
33 r"\n": (LITERAL, ord("\n")),
34 r"\r": (LITERAL, ord("\r")),
35 r"\t": (LITERAL, ord("\t")),
36 r"\v": (LITERAL, ord("\v")),
Fredrik Lundh0640e112000-06-30 13:55:15 +000037 r"\\": (LITERAL, ord("\\"))
Guido van Rossum7627c0d2000-03-31 14:58:54 +000038}
39
40CATEGORIES = {
Fredrik Lundh770617b2001-01-14 15:06:11 +000041 r"\A": (AT, AT_BEGINNING_STRING), # start of string
Fredrik Lundh01016fe2000-06-30 00:27:46 +000042 r"\b": (AT, AT_BOUNDARY),
43 r"\B": (AT, AT_NON_BOUNDARY),
44 r"\d": (IN, [(CATEGORY, CATEGORY_DIGIT)]),
45 r"\D": (IN, [(CATEGORY, CATEGORY_NOT_DIGIT)]),
46 r"\s": (IN, [(CATEGORY, CATEGORY_SPACE)]),
47 r"\S": (IN, [(CATEGORY, CATEGORY_NOT_SPACE)]),
48 r"\w": (IN, [(CATEGORY, CATEGORY_WORD)]),
49 r"\W": (IN, [(CATEGORY, CATEGORY_NOT_WORD)]),
Fredrik Lundh770617b2001-01-14 15:06:11 +000050 r"\Z": (AT, AT_END_STRING), # end of string
Guido van Rossum7627c0d2000-03-31 14:58:54 +000051}
52
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +000053FLAGS = {
Fredrik Lundh436c3d52000-06-29 08:58:44 +000054 # standard flags
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +000055 "i": SRE_FLAG_IGNORECASE,
56 "L": SRE_FLAG_LOCALE,
57 "m": SRE_FLAG_MULTILINE,
58 "s": SRE_FLAG_DOTALL,
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +000059 "x": SRE_FLAG_VERBOSE,
Fredrik Lundh436c3d52000-06-29 08:58:44 +000060 # extensions
61 "t": SRE_FLAG_TEMPLATE,
62 "u": SRE_FLAG_UNICODE,
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +000063}
64
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +000065class Pattern:
66 # master pattern object. keeps track of global attributes
Guido van Rossum7627c0d2000-03-31 14:58:54 +000067 def __init__(self):
Fredrik Lundh90a07912000-06-30 07:50:59 +000068 self.flags = 0
Fredrik Lundhebc37b22000-10-28 19:30:41 +000069 self.open = []
Fredrik Lundh90a07912000-06-30 07:50:59 +000070 self.groups = 1
71 self.groupdict = {}
Fredrik Lundhebc37b22000-10-28 19:30:41 +000072 def opengroup(self, name=None):
Fredrik Lundh90a07912000-06-30 07:50:59 +000073 gid = self.groups
74 self.groups = gid + 1
Raymond Hettingerf13eb552002-06-02 00:40:05 +000075 if name is not None:
Tim Peters75335872001-11-03 19:35:43 +000076 ogid = self.groupdict.get(name, None)
77 if ogid is not None:
Fredrik Lundh82b23072001-12-09 16:13:15 +000078 raise error, ("redefinition of group name %s as group %d; "
79 "was group %d" % (repr(name), gid, ogid))
Fredrik Lundh90a07912000-06-30 07:50:59 +000080 self.groupdict[name] = gid
Fredrik Lundhebc37b22000-10-28 19:30:41 +000081 self.open.append(gid)
Fredrik Lundh90a07912000-06-30 07:50:59 +000082 return gid
Fredrik Lundhebc37b22000-10-28 19:30:41 +000083 def closegroup(self, gid):
84 self.open.remove(gid)
85 def checkgroup(self, gid):
86 return gid < self.groups and gid not in self.open
Guido van Rossum7627c0d2000-03-31 14:58:54 +000087
88class SubPattern:
89 # a subpattern, in intermediate form
90 def __init__(self, pattern, data=None):
Fredrik Lundh90a07912000-06-30 07:50:59 +000091 self.pattern = pattern
Raymond Hettingerf13eb552002-06-02 00:40:05 +000092 if data is None:
Fredrik Lundh90a07912000-06-30 07:50:59 +000093 data = []
94 self.data = data
95 self.width = None
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +000096 def dump(self, level=0):
97 nl = 1
Raymond Hettinger968c56a2004-03-26 23:24:00 +000098 seqtypes = type(()), type([])
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +000099 for op, av in self.data:
100 print level*" " + op,; nl = 0
101 if op == "in":
102 # member sublanguage
103 print; nl = 1
104 for op, a in av:
105 print (level+1)*" " + op, a
106 elif op == "branch":
107 print; nl = 1
108 i = 0
109 for a in av[1]:
110 if i > 0:
111 print level*" " + "or"
112 a.dump(level+1); nl = 1
113 i = i + 1
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000114 elif type(av) in seqtypes:
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000115 for a in av:
116 if isinstance(a, SubPattern):
117 if not nl: print
118 a.dump(level+1); nl = 1
119 else:
120 print a, ; nl = 0
121 else:
122 print av, ; nl = 0
123 if not nl: print
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000124 def __repr__(self):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000125 return repr(self.data)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000126 def __len__(self):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000127 return len(self.data)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000128 def __delitem__(self, index):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000129 del self.data[index]
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000130 def __getitem__(self, index):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000131 return self.data[index]
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000132 def __setitem__(self, index, code):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000133 self.data[index] = code
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000134 def __getslice__(self, start, stop):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000135 return SubPattern(self.pattern, self.data[start:stop])
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000136 def insert(self, index, code):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000137 self.data.insert(index, code)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000138 def append(self, code):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000139 self.data.append(code)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000140 def getwidth(self):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000141 # determine the width (min, max) for this subpattern
142 if self.width:
143 return self.width
144 lo = hi = 0L
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000145 UNITCODES = (ANY, RANGE, IN, LITERAL, NOT_LITERAL, CATEGORY)
146 REPEATCODES = (MIN_REPEAT, MAX_REPEAT)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000147 for op, av in self.data:
148 if op is BRANCH:
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +0000149 i = sys.maxint
150 j = 0
Fredrik Lundh90a07912000-06-30 07:50:59 +0000151 for av in av[1]:
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +0000152 l, h = av.getwidth()
153 i = min(i, l)
Fredrik Lundhe1869832000-08-01 22:47:49 +0000154 j = max(j, h)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000155 lo = lo + i
156 hi = hi + j
157 elif op is CALL:
158 i, j = av.getwidth()
159 lo = lo + i
160 hi = hi + j
161 elif op is SUBPATTERN:
162 i, j = av[1].getwidth()
163 lo = lo + i
164 hi = hi + j
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000165 elif op in REPEATCODES:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000166 i, j = av[2].getwidth()
167 lo = lo + long(i) * av[0]
168 hi = hi + long(j) * av[1]
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000169 elif op in UNITCODES:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000170 lo = lo + 1
171 hi = hi + 1
172 elif op == SUCCESS:
173 break
174 self.width = int(min(lo, sys.maxint)), int(min(hi, sys.maxint))
175 return self.width
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000176
177class Tokenizer:
178 def __init__(self, string):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000179 self.string = string
Fredrik Lundhc13222c2000-07-01 23:49:14 +0000180 self.index = 0
181 self.__next()
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000182 def __next(self):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000183 if self.index >= len(self.string):
Fredrik Lundhc13222c2000-07-01 23:49:14 +0000184 self.next = None
185 return
Fredrik Lundh90a07912000-06-30 07:50:59 +0000186 char = self.string[self.index]
187 if char[0] == "\\":
188 try:
189 c = self.string[self.index + 1]
190 except IndexError:
Fredrik Lundh8a0232d2001-11-02 13:59:51 +0000191 raise error, "bogus escape (end of line)"
Fredrik Lundh90a07912000-06-30 07:50:59 +0000192 char = char + c
193 self.index = self.index + len(char)
Fredrik Lundhc13222c2000-07-01 23:49:14 +0000194 self.next = char
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000195 def match(self, char, skip=1):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000196 if char == self.next:
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000197 if skip:
198 self.__next()
Fredrik Lundh90a07912000-06-30 07:50:59 +0000199 return 1
200 return 0
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000201 def get(self):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000202 this = self.next
Fredrik Lundhc13222c2000-07-01 23:49:14 +0000203 self.__next()
Fredrik Lundh90a07912000-06-30 07:50:59 +0000204 return this
Fredrik Lundhc13222c2000-07-01 23:49:14 +0000205 def tell(self):
206 return self.index, self.next
207 def seek(self, index):
208 self.index, self.next = index
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000209
Fredrik Lundh4781b072000-06-29 12:38:45 +0000210def isident(char):
211 return "a" <= char <= "z" or "A" <= char <= "Z" or char == "_"
212
213def isdigit(char):
214 return "0" <= char <= "9"
215
216def isname(name):
217 # check that group name is a valid string
Fredrik Lundh4781b072000-06-29 12:38:45 +0000218 if not isident(name[0]):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000219 return False
Fredrik Lundh4781b072000-06-29 12:38:45 +0000220 for char in name:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000221 if not isident(char) and not isdigit(char):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000222 return False
223 return True
Fredrik Lundh4781b072000-06-29 12:38:45 +0000224
Fredrik Lundh01016fe2000-06-30 00:27:46 +0000225def _group(escape, groups):
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000226 # check if the escape string represents a valid group
227 try:
Barry Warsaw8bee7612004-08-25 02:22:30 +0000228 gid = int(escape[1:])
Fredrik Lundhb71624e2000-06-30 09:13:06 +0000229 if gid and gid < groups:
230 return gid
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000231 except ValueError:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000232 pass
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000233 return None # not a valid group
234
235def _class_escape(source, escape):
236 # handle escape code inside character class
237 code = ESCAPES.get(escape)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000238 if code:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000239 return code
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000240 code = CATEGORIES.get(escape)
241 if code:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000242 return code
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000243 try:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000244 if escape[1:2] == "x":
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000245 # hexadecimal escape (exactly two digits)
246 while source.next in HEXDIGITS and len(escape) < 4:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000247 escape = escape + source.get()
248 escape = escape[2:]
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000249 if len(escape) != 2:
250 raise error, "bogus escape: %s" % repr("\\" + escape)
Barry Warsaw8bee7612004-08-25 02:22:30 +0000251 return LITERAL, int(escape, 16) & 0xff
Martin v. Löwis53d93ad2003-04-19 08:37:24 +0000252 elif escape[1:2] in OCTDIGITS:
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000253 # octal escape (up to three digits)
254 while source.next in OCTDIGITS and len(escape) < 5:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000255 escape = escape + source.get()
256 escape = escape[1:]
Barry Warsaw8bee7612004-08-25 02:22:30 +0000257 return LITERAL, int(escape, 8) & 0xff
Fredrik Lundh90a07912000-06-30 07:50:59 +0000258 if len(escape) == 2:
Fredrik Lundh0640e112000-06-30 13:55:15 +0000259 return LITERAL, ord(escape[1])
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000260 except ValueError:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000261 pass
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000262 raise error, "bogus escape: %s" % repr(escape)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000263
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000264def _escape(source, escape, state):
265 # handle escape code in expression
266 code = CATEGORIES.get(escape)
267 if code:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000268 return code
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000269 code = ESCAPES.get(escape)
270 if code:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000271 return code
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000272 try:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000273 if escape[1:2] == "x":
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000274 # hexadecimal escape
275 while source.next in HEXDIGITS and len(escape) < 4:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000276 escape = escape + source.get()
Fredrik Lundh143328b2000-09-02 11:03:34 +0000277 if len(escape) != 4:
278 raise ValueError
Barry Warsaw8bee7612004-08-25 02:22:30 +0000279 return LITERAL, int(escape[2:], 16) & 0xff
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000280 elif escape[1:2] == "0":
281 # octal escape
Fredrik Lundh143328b2000-09-02 11:03:34 +0000282 while source.next in OCTDIGITS and len(escape) < 4:
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000283 escape = escape + source.get()
Barry Warsaw8bee7612004-08-25 02:22:30 +0000284 return LITERAL, int(escape[1:], 8) & 0xff
Fredrik Lundh90a07912000-06-30 07:50:59 +0000285 elif escape[1:2] in DIGITS:
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000286 # octal escape *or* decimal group reference (sigh)
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000287 if source.next in DIGITS:
288 escape = escape + source.get()
Fredrik Lundh143328b2000-09-02 11:03:34 +0000289 if (escape[1] in OCTDIGITS and escape[2] in OCTDIGITS and
290 source.next in OCTDIGITS):
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000291 # got three octal digits; this is an octal escape
Fredrik Lundh90a07912000-06-30 07:50:59 +0000292 escape = escape + source.get()
Barry Warsaw8bee7612004-08-25 02:22:30 +0000293 return LITERAL, int(escape[1:], 8) & 0xff
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000294 # got at least one decimal digit; this is a group reference
295 group = _group(escape, state.groups)
296 if group:
Fredrik Lundhebc37b22000-10-28 19:30:41 +0000297 if not state.checkgroup(group):
298 raise error, "cannot refer to open group"
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000299 return GROUPREF, group
Fredrik Lundh143328b2000-09-02 11:03:34 +0000300 raise ValueError
Fredrik Lundh90a07912000-06-30 07:50:59 +0000301 if len(escape) == 2:
Fredrik Lundh0640e112000-06-30 13:55:15 +0000302 return LITERAL, ord(escape[1])
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000303 except ValueError:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000304 pass
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000305 raise error, "bogus escape: %s" % repr(escape)
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000306
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000307def _parse_sub(source, state, nested=1):
308 # parse an alternation: a|b|c
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000309
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000310 items = []
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000311 itemsappend = items.append
312 sourcematch = source.match
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000313 while 1:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000314 itemsappend(_parse(source, state))
315 if sourcematch("|"):
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000316 continue
317 if not nested:
318 break
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000319 if not source.next or sourcematch(")", 0):
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000320 break
321 else:
322 raise error, "pattern not properly closed"
323
324 if len(items) == 1:
325 return items[0]
326
327 subpattern = SubPattern(state)
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000328 subpatternappend = subpattern.append
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000329
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000330 # check if all items share a common prefix
331 while 1:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000332 prefix = None
333 for item in items:
334 if not item:
335 break
336 if prefix is None:
337 prefix = item[0]
338 elif item[0] != prefix:
339 break
340 else:
341 # all subitems start with a common "prefix".
342 # move it out of the branch
343 for item in items:
344 del item[0]
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000345 subpatternappend(prefix)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000346 continue # check next one
347 break
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000348
349 # check if the branch can be replaced by a character set
350 for item in items:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000351 if len(item) != 1 or item[0][0] != LITERAL:
352 break
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000353 else:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000354 # we can store this as a character set instead of a
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000355 # branch (the compiler may optimize this even more)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000356 set = []
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000357 setappend = set.append
Fredrik Lundh90a07912000-06-30 07:50:59 +0000358 for item in items:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000359 setappend(item[0])
360 subpatternappend((IN, set))
Fredrik Lundh90a07912000-06-30 07:50:59 +0000361 return subpattern
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000362
363 subpattern.append((BRANCH, (None, items)))
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000364 return subpattern
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000365
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000366def _parse_sub_cond(source, state, condgroup):
Tim Peters58eb11c2004-01-18 20:29:55 +0000367 item_yes = _parse(source, state)
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000368 if source.match("|"):
Tim Peters58eb11c2004-01-18 20:29:55 +0000369 item_no = _parse(source, state)
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000370 if source.match("|"):
371 raise error, "conditional backref with more than two branches"
372 else:
373 item_no = None
374 if source.next and not source.match(")", 0):
375 raise error, "pattern not properly closed"
376 subpattern = SubPattern(state)
377 subpattern.append((GROUPREF_EXISTS, (condgroup, item_yes, item_no)))
378 return subpattern
379
Fredrik Lundh55a4f4a2000-06-30 22:37:31 +0000380def _parse(source, state):
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000381 # parse a simple pattern
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000382 subpattern = SubPattern(state)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000383
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000384 # precompute constants into local variables
385 subpatternappend = subpattern.append
386 sourceget = source.get
387 sourcematch = source.match
388 _len = len
389 PATTERNENDERS = ("|", ")")
390 ASSERTCHARS = ("=", "!", "<")
391 LOOKBEHINDASSERTCHARS = ("=", "!")
392 REPEATCODES = (MIN_REPEAT, MAX_REPEAT)
393
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000394 while 1:
395
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000396 if source.next in PATTERNENDERS:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000397 break # end of subpattern
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000398 this = sourceget()
Fredrik Lundh90a07912000-06-30 07:50:59 +0000399 if this is None:
400 break # end of pattern
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000401
Fredrik Lundh90a07912000-06-30 07:50:59 +0000402 if state.flags & SRE_FLAG_VERBOSE:
403 # skip whitespace and comments
404 if this in WHITESPACE:
405 continue
406 if this == "#":
407 while 1:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000408 this = sourceget()
Fredrik Lundh90a07912000-06-30 07:50:59 +0000409 if this in (None, "\n"):
410 break
411 continue
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000412
Fredrik Lundh90a07912000-06-30 07:50:59 +0000413 if this and this[0] not in SPECIAL_CHARS:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000414 subpatternappend((LITERAL, ord(this)))
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000415
Fredrik Lundh90a07912000-06-30 07:50:59 +0000416 elif this == "[":
417 # character set
418 set = []
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000419 setappend = set.append
420## if sourcematch(":"):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000421## pass # handle character classes
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000422 if sourcematch("^"):
423 setappend((NEGATE, None))
Fredrik Lundh90a07912000-06-30 07:50:59 +0000424 # check remaining characters
425 start = set[:]
426 while 1:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000427 this = sourceget()
Fredrik Lundh90a07912000-06-30 07:50:59 +0000428 if this == "]" and set != start:
429 break
430 elif this and this[0] == "\\":
431 code1 = _class_escape(source, this)
432 elif this:
Fredrik Lundh0640e112000-06-30 13:55:15 +0000433 code1 = LITERAL, ord(this)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000434 else:
435 raise error, "unexpected end of regular expression"
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000436 if sourcematch("-"):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000437 # potential range
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000438 this = sourceget()
Fredrik Lundh90a07912000-06-30 07:50:59 +0000439 if this == "]":
Fredrik Lundh025468d2000-10-07 10:16:19 +0000440 if code1[0] is IN:
441 code1 = code1[1][0]
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000442 setappend(code1)
443 setappend((LITERAL, ord("-")))
Fredrik Lundh90a07912000-06-30 07:50:59 +0000444 break
Guido van Rossum41c99e72003-04-14 17:59:34 +0000445 elif this:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000446 if this[0] == "\\":
447 code2 = _class_escape(source, this)
448 else:
Fredrik Lundh0640e112000-06-30 13:55:15 +0000449 code2 = LITERAL, ord(this)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000450 if code1[0] != LITERAL or code2[0] != LITERAL:
Fredrik Lundh470ea5a2001-01-14 21:00:44 +0000451 raise error, "bad character range"
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000452 lo = code1[1]
453 hi = code2[1]
454 if hi < lo:
Fredrik Lundh470ea5a2001-01-14 21:00:44 +0000455 raise error, "bad character range"
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000456 setappend((RANGE, (lo, hi)))
Guido van Rossum41c99e72003-04-14 17:59:34 +0000457 else:
458 raise error, "unexpected end of regular expression"
Fredrik Lundh90a07912000-06-30 07:50:59 +0000459 else:
460 if code1[0] is IN:
461 code1 = code1[1][0]
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000462 setappend(code1)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000463
Fredrik Lundh770617b2001-01-14 15:06:11 +0000464 # XXX: <fl> should move set optimization to compiler!
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000465 if _len(set)==1 and set[0][0] is LITERAL:
466 subpatternappend(set[0]) # optimization
467 elif _len(set)==2 and set[0][0] is NEGATE and set[1][0] is LITERAL:
468 subpatternappend((NOT_LITERAL, set[1][1])) # optimization
Fredrik Lundh90a07912000-06-30 07:50:59 +0000469 else:
Fredrik Lundh770617b2001-01-14 15:06:11 +0000470 # XXX: <fl> should add charmap optimization here
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000471 subpatternappend((IN, set))
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000472
Fredrik Lundh90a07912000-06-30 07:50:59 +0000473 elif this and this[0] in REPEAT_CHARS:
474 # repeat previous item
475 if this == "?":
476 min, max = 0, 1
477 elif this == "*":
478 min, max = 0, MAXREPEAT
Fredrik Lundhc0c7ee32001-02-18 21:04:48 +0000479
Fredrik Lundh90a07912000-06-30 07:50:59 +0000480 elif this == "+":
481 min, max = 1, MAXREPEAT
482 elif this == "{":
Fredrik Lundhc13222c2000-07-01 23:49:14 +0000483 here = source.tell()
Fredrik Lundh90a07912000-06-30 07:50:59 +0000484 min, max = 0, MAXREPEAT
485 lo = hi = ""
486 while source.next in DIGITS:
487 lo = lo + source.get()
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000488 if sourcematch(","):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000489 while source.next in DIGITS:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000490 hi = hi + sourceget()
Fredrik Lundh90a07912000-06-30 07:50:59 +0000491 else:
492 hi = lo
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000493 if not sourcematch("}"):
494 subpatternappend((LITERAL, ord(this)))
Fredrik Lundhc13222c2000-07-01 23:49:14 +0000495 source.seek(here)
496 continue
Fredrik Lundh90a07912000-06-30 07:50:59 +0000497 if lo:
Barry Warsaw8bee7612004-08-25 02:22:30 +0000498 min = int(lo)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000499 if hi:
Barry Warsaw8bee7612004-08-25 02:22:30 +0000500 max = int(hi)
Fredrik Lundh470ea5a2001-01-14 21:00:44 +0000501 if max < min:
502 raise error, "bad repeat interval"
Fredrik Lundh90a07912000-06-30 07:50:59 +0000503 else:
504 raise error, "not supported"
505 # figure out which item to repeat
506 if subpattern:
507 item = subpattern[-1:]
508 else:
Fredrik Lundhc0c7ee32001-02-18 21:04:48 +0000509 item = None
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000510 if not item or (_len(item) == 1 and item[0][0] == AT):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000511 raise error, "nothing to repeat"
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000512 if item[0][0] in REPEATCODES:
Fredrik Lundh470ea5a2001-01-14 21:00:44 +0000513 raise error, "multiple repeat"
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000514 if sourcematch("?"):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000515 subpattern[-1] = (MIN_REPEAT, (min, max, item))
516 else:
517 subpattern[-1] = (MAX_REPEAT, (min, max, item))
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000518
Fredrik Lundh90a07912000-06-30 07:50:59 +0000519 elif this == ".":
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000520 subpatternappend((ANY, None))
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000521
Fredrik Lundh90a07912000-06-30 07:50:59 +0000522 elif this == "(":
523 group = 1
524 name = None
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000525 condgroup = None
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000526 if sourcematch("?"):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000527 group = 0
528 # options
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000529 if sourcematch("P"):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000530 # python extensions
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000531 if sourcematch("<"):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000532 # named group: skip forward to end of name
533 name = ""
534 while 1:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000535 char = sourceget()
Fredrik Lundh90a07912000-06-30 07:50:59 +0000536 if char is None:
537 raise error, "unterminated name"
538 if char == ">":
539 break
540 name = name + char
541 group = 1
542 if not isname(name):
Fredrik Lundh470ea5a2001-01-14 21:00:44 +0000543 raise error, "bad character in group name"
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000544 elif sourcematch("="):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000545 # named backreference
Fredrik Lundhb71624e2000-06-30 09:13:06 +0000546 name = ""
547 while 1:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000548 char = sourceget()
Fredrik Lundhb71624e2000-06-30 09:13:06 +0000549 if char is None:
550 raise error, "unterminated name"
551 if char == ")":
552 break
553 name = name + char
554 if not isname(name):
Fredrik Lundh470ea5a2001-01-14 21:00:44 +0000555 raise error, "bad character in group name"
Fredrik Lundhb71624e2000-06-30 09:13:06 +0000556 gid = state.groupdict.get(name)
557 if gid is None:
558 raise error, "unknown group name"
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000559 subpatternappend((GROUPREF, gid))
Fredrik Lundh7cafe4d2000-07-02 17:33:27 +0000560 continue
Fredrik Lundh90a07912000-06-30 07:50:59 +0000561 else:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000562 char = sourceget()
Fredrik Lundh90a07912000-06-30 07:50:59 +0000563 if char is None:
564 raise error, "unexpected end of pattern"
565 raise error, "unknown specifier: ?P%s" % char
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000566 elif sourcematch(":"):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000567 # non-capturing group
568 group = 2
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000569 elif sourcematch("#"):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000570 # comment
571 while 1:
572 if source.next is None or source.next == ")":
573 break
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000574 sourceget()
575 if not sourcematch(")"):
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000576 raise error, "unbalanced parenthesis"
577 continue
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000578 elif source.next in ASSERTCHARS:
Fredrik Lundh43b3b492000-06-30 10:41:31 +0000579 # lookahead assertions
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000580 char = sourceget()
Fredrik Lundh6f013982000-07-03 18:44:21 +0000581 dir = 1
582 if char == "<":
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000583 if source.next not in LOOKBEHINDASSERTCHARS:
Fredrik Lundh6f013982000-07-03 18:44:21 +0000584 raise error, "syntax error"
585 dir = -1 # lookbehind
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000586 char = sourceget()
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000587 p = _parse_sub(source, state)
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000588 if not sourcematch(")"):
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000589 raise error, "unbalanced parenthesis"
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000590 if char == "=":
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000591 subpatternappend((ASSERT, (dir, p)))
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000592 else:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000593 subpatternappend((ASSERT_NOT, (dir, p)))
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000594 continue
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000595 elif sourcematch("("):
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000596 # conditional backreference group
597 condname = ""
598 while 1:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000599 char = sourceget()
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000600 if char is None:
601 raise error, "unterminated name"
602 if char == ")":
603 break
604 condname = condname + char
605 group = 2
606 if isname(condname):
607 condgroup = state.groupdict.get(condname)
608 if condgroup is None:
609 raise error, "unknown group name"
610 else:
611 try:
Barry Warsaw8bee7612004-08-25 02:22:30 +0000612 condgroup = int(condname)
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000613 except ValueError:
614 raise error, "bad character in group name"
Fredrik Lundh90a07912000-06-30 07:50:59 +0000615 else:
616 # flags
Raymond Hettinger54f02222002-06-01 14:18:47 +0000617 if not source.next in FLAGS:
Fredrik Lundh470ea5a2001-01-14 21:00:44 +0000618 raise error, "unexpected end of pattern"
Raymond Hettinger54f02222002-06-01 14:18:47 +0000619 while source.next in FLAGS:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000620 state.flags = state.flags | FLAGS[sourceget()]
Fredrik Lundh90a07912000-06-30 07:50:59 +0000621 if group:
622 # parse group contents
Fredrik Lundh90a07912000-06-30 07:50:59 +0000623 if group == 2:
624 # anonymous group
625 group = None
626 else:
Fredrik Lundhebc37b22000-10-28 19:30:41 +0000627 group = state.opengroup(name)
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000628 if condgroup:
629 p = _parse_sub_cond(source, state, condgroup)
630 else:
631 p = _parse_sub(source, state)
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000632 if not sourcematch(")"):
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000633 raise error, "unbalanced parenthesis"
Fredrik Lundhebc37b22000-10-28 19:30:41 +0000634 if group is not None:
635 state.closegroup(group)
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000636 subpatternappend((SUBPATTERN, (group, p)))
Fredrik Lundh90a07912000-06-30 07:50:59 +0000637 else:
638 while 1:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000639 char = sourceget()
Fredrik Lundh470ea5a2001-01-14 21:00:44 +0000640 if char is None:
641 raise error, "unexpected end of pattern"
642 if char == ")":
Fredrik Lundh90a07912000-06-30 07:50:59 +0000643 break
644 raise error, "unknown extension"
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000645
Fredrik Lundh90a07912000-06-30 07:50:59 +0000646 elif this == "^":
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000647 subpatternappend((AT, AT_BEGINNING))
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000648
Fredrik Lundh90a07912000-06-30 07:50:59 +0000649 elif this == "$":
650 subpattern.append((AT, AT_END))
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000651
Fredrik Lundh90a07912000-06-30 07:50:59 +0000652 elif this and this[0] == "\\":
653 code = _escape(source, this, state)
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000654 subpatternappend(code)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000655
Fredrik Lundh90a07912000-06-30 07:50:59 +0000656 else:
657 raise error, "parser error"
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000658
659 return subpattern
660
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000661def parse(str, flags=0, pattern=None):
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000662 # parse 're' pattern into list of (opcode, argument) tuples
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000663
664 source = Tokenizer(str)
665
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000666 if pattern is None:
667 pattern = Pattern()
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000668 pattern.flags = flags
Fredrik Lundh470ea5a2001-01-14 21:00:44 +0000669 pattern.str = str
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000670
671 p = _parse_sub(source, pattern, 0)
672
673 tail = source.get()
674 if tail == ")":
675 raise error, "unbalanced parenthesis"
676 elif tail:
677 raise error, "bogus characters at end of regular expression"
678
Fredrik Lundh770617b2001-01-14 15:06:11 +0000679 if flags & SRE_FLAG_DEBUG:
680 p.dump()
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000681
Fredrik Lundhd11b5e52000-10-03 19:22:26 +0000682 if not (flags & SRE_FLAG_VERBOSE) and p.pattern.flags & SRE_FLAG_VERBOSE:
683 # the VERBOSE flag was switched on inside the pattern. to be
684 # on the safe side, we'll parse the whole thing again...
685 return parse(str, p.pattern.flags)
686
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000687 return p
688
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000689def parse_template(source, pattern):
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000690 # parse 're' replacement string into list of literals and
691 # group references
692 s = Tokenizer(source)
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000693 sget = s.get
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000694 p = []
695 a = p.append
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000696 def literal(literal, p=p, pappend=a):
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000697 if p and p[-1][0] is LITERAL:
698 p[-1] = LITERAL, p[-1][1] + literal
699 else:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000700 pappend((LITERAL, literal))
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000701 sep = source[:0]
702 if type(sep) is type(""):
Fredrik Lundh59b68652001-09-18 20:55:24 +0000703 makechar = chr
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000704 else:
Fredrik Lundh59b68652001-09-18 20:55:24 +0000705 makechar = unichr
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000706 while 1:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000707 this = sget()
Fredrik Lundh90a07912000-06-30 07:50:59 +0000708 if this is None:
709 break # end of replacement string
710 if this and this[0] == "\\":
711 # group
712 if this == "\\g":
713 name = ""
714 if s.match("<"):
715 while 1:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000716 char = sget()
Fredrik Lundh90a07912000-06-30 07:50:59 +0000717 if char is None:
718 raise error, "unterminated group name"
719 if char == ">":
720 break
721 name = name + char
722 if not name:
723 raise error, "bad group name"
724 try:
Barry Warsaw8bee7612004-08-25 02:22:30 +0000725 index = int(name)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000726 except ValueError:
727 if not isname(name):
Fredrik Lundh470ea5a2001-01-14 21:00:44 +0000728 raise error, "bad character in group name"
Fredrik Lundh90a07912000-06-30 07:50:59 +0000729 try:
730 index = pattern.groupindex[name]
731 except KeyError:
732 raise IndexError, "unknown group name"
733 a((MARK, index))
734 elif len(this) > 1 and this[1] in DIGITS:
735 code = None
736 while 1:
737 group = _group(this, pattern.groups+1)
738 if group:
Fredrik Lundh19f977b2000-09-24 14:46:23 +0000739 if (s.next not in DIGITS or
Fredrik Lundh90a07912000-06-30 07:50:59 +0000740 not _group(this + s.next, pattern.groups+1)):
Fredrik Lundh1c5aa692001-01-16 07:37:30 +0000741 code = MARK, group
Fredrik Lundh90a07912000-06-30 07:50:59 +0000742 break
743 elif s.next in OCTDIGITS:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000744 this = this + sget()
Fredrik Lundh90a07912000-06-30 07:50:59 +0000745 else:
746 break
747 if not code:
748 this = this[1:]
Barry Warsaw8bee7612004-08-25 02:22:30 +0000749 code = LITERAL, makechar(int(this[-6:], 8) & 0xff)
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000750 if code[0] is LITERAL:
751 literal(code[1])
752 else:
753 a(code)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000754 else:
755 try:
Fredrik Lundh59b68652001-09-18 20:55:24 +0000756 this = makechar(ESCAPES[this][1])
Fredrik Lundh90a07912000-06-30 07:50:59 +0000757 except KeyError:
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000758 pass
759 literal(this)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000760 else:
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000761 literal(this)
762 # convert template to groups and literals lists
763 i = 0
764 groups = []
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000765 groupsappend = groups.append
766 literals = [None] * len(p)
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000767 for c, s in p:
768 if c is MARK:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000769 groupsappend((i, s))
770 # literal[i] is already None
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000771 else:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000772 literals[i] = s
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000773 i = i + 1
774 return groups, literals
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000775
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000776def expand_template(template, match):
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000777 g = match.group
Fredrik Lundh0640e112000-06-30 13:55:15 +0000778 sep = match.string[:0]
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000779 groups, literals = template
780 literals = literals[:]
781 try:
782 for index, group in groups:
783 literals[index] = s = g(group)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000784 if s is None:
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000785 raise IndexError
786 except IndexError:
787 raise error, "empty group"
Barry Warsaw8bee7612004-08-25 02:22:30 +0000788 return sep.join(literals)