blob: a0cf34414b6335d2747bcc319a410bce1d18a702 [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 *
Serhiy Storchakae18e05c2013-02-16 16:47:15 +020018from _sre import MAXREPEAT
Guido van Rossum7627c0d2000-03-31 14:58:54 +000019
20SPECIAL_CHARS = ".\\[{()*+?^$|"
Fredrik Lundh143328b2000-09-02 11:03:34 +000021REPEAT_CHARS = "*+?{"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000022
Raymond Hettinger049ade22005-02-28 19:27:52 +000023DIGITS = set("0123456789")
Guido van Rossumb81e70e2000-04-10 17:10:48 +000024
Raymond Hettinger049ade22005-02-28 19:27:52 +000025OCTDIGITS = set("01234567")
26HEXDIGITS = set("0123456789abcdefABCDEF")
Guido van Rossum7627c0d2000-03-31 14:58:54 +000027
Raymond Hettinger049ade22005-02-28 19:27:52 +000028WHITESPACE = set(" \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 Lundh8a3ebf82000-07-23 21:46:17 +000066class Pattern:
67 # master pattern object. keeps track of global attributes
Guido van Rossum7627c0d2000-03-31 14:58:54 +000068 def __init__(self):
Fredrik Lundh90a07912000-06-30 07:50:59 +000069 self.flags = 0
Fredrik Lundhebc37b22000-10-28 19:30:41 +000070 self.open = []
Fredrik Lundh90a07912000-06-30 07:50:59 +000071 self.groups = 1
72 self.groupdict = {}
Fredrik Lundhebc37b22000-10-28 19:30:41 +000073 def opengroup(self, name=None):
Fredrik Lundh90a07912000-06-30 07:50:59 +000074 gid = self.groups
75 self.groups = gid + 1
Raymond Hettingerf13eb552002-06-02 00:40:05 +000076 if name is not None:
Tim Peters75335872001-11-03 19:35:43 +000077 ogid = self.groupdict.get(name, None)
78 if ogid is not None:
Fredrik Lundh82b23072001-12-09 16:13:15 +000079 raise error, ("redefinition of group name %s as group %d; "
80 "was group %d" % (repr(name), gid, ogid))
Fredrik Lundh90a07912000-06-30 07:50:59 +000081 self.groupdict[name] = gid
Fredrik Lundhebc37b22000-10-28 19:30:41 +000082 self.open.append(gid)
Fredrik Lundh90a07912000-06-30 07:50:59 +000083 return gid
Fredrik Lundhebc37b22000-10-28 19:30:41 +000084 def closegroup(self, gid):
85 self.open.remove(gid)
86 def checkgroup(self, gid):
87 return gid < self.groups and gid not in self.open
Guido van Rossum7627c0d2000-03-31 14:58:54 +000088
89class SubPattern:
90 # a subpattern, in intermediate form
91 def __init__(self, pattern, data=None):
Fredrik Lundh90a07912000-06-30 07:50:59 +000092 self.pattern = pattern
Raymond Hettingerf13eb552002-06-02 00:40:05 +000093 if data is None:
Fredrik Lundh90a07912000-06-30 07:50:59 +000094 data = []
95 self.data = data
96 self.width = None
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +000097 def dump(self, level=0):
98 nl = 1
Raymond Hettinger968c56a2004-03-26 23:24:00 +000099 seqtypes = type(()), type([])
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000100 for op, av in self.data:
101 print level*" " + op,; nl = 0
102 if op == "in":
103 # member sublanguage
104 print; nl = 1
105 for op, a in av:
106 print (level+1)*" " + op, a
107 elif op == "branch":
108 print; nl = 1
109 i = 0
110 for a in av[1]:
111 if i > 0:
112 print level*" " + "or"
113 a.dump(level+1); nl = 1
114 i = i + 1
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000115 elif type(av) in seqtypes:
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000116 for a in av:
117 if isinstance(a, SubPattern):
118 if not nl: print
119 a.dump(level+1); nl = 1
120 else:
121 print a, ; nl = 0
122 else:
123 print av, ; nl = 0
124 if not nl: print
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000125 def __repr__(self):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000126 return repr(self.data)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000127 def __len__(self):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000128 return len(self.data)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000129 def __delitem__(self, index):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000130 del self.data[index]
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000131 def __getitem__(self, index):
Thomas Wouterse3a985f2006-12-19 08:17:50 +0000132 if isinstance(index, slice):
133 return SubPattern(self.pattern, self.data[index])
Fredrik Lundh90a07912000-06-30 07:50:59 +0000134 return self.data[index]
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000135 def __setitem__(self, index, code):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000136 self.data[index] = code
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000137 def insert(self, index, code):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000138 self.data.insert(index, code)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000139 def append(self, code):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000140 self.data.append(code)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000141 def getwidth(self):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000142 # determine the width (min, max) for this subpattern
143 if self.width:
144 return self.width
145 lo = hi = 0L
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000146 UNITCODES = (ANY, RANGE, IN, LITERAL, NOT_LITERAL, CATEGORY)
147 REPEATCODES = (MIN_REPEAT, MAX_REPEAT)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000148 for op, av in self.data:
149 if op is BRANCH:
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +0000150 i = sys.maxint
151 j = 0
Fredrik Lundh90a07912000-06-30 07:50:59 +0000152 for av in av[1]:
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +0000153 l, h = av.getwidth()
154 i = min(i, l)
Fredrik Lundhe1869832000-08-01 22:47:49 +0000155 j = max(j, h)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000156 lo = lo + i
157 hi = hi + j
158 elif op is CALL:
159 i, j = av.getwidth()
160 lo = lo + i
161 hi = hi + j
162 elif op is SUBPATTERN:
163 i, j = av[1].getwidth()
164 lo = lo + i
165 hi = hi + j
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000166 elif op in REPEATCODES:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000167 i, j = av[2].getwidth()
168 lo = lo + long(i) * av[0]
169 hi = hi + long(j) * av[1]
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000170 elif op in UNITCODES:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000171 lo = lo + 1
172 hi = hi + 1
173 elif op == SUCCESS:
174 break
175 self.width = int(min(lo, sys.maxint)), int(min(hi, sys.maxint))
176 return self.width
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000177
178class Tokenizer:
179 def __init__(self, string):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000180 self.string = string
Fredrik Lundhc13222c2000-07-01 23:49:14 +0000181 self.index = 0
182 self.__next()
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000183 def __next(self):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000184 if self.index >= len(self.string):
Fredrik Lundhc13222c2000-07-01 23:49:14 +0000185 self.next = None
186 return
Fredrik Lundh90a07912000-06-30 07:50:59 +0000187 char = self.string[self.index]
188 if char[0] == "\\":
189 try:
190 c = self.string[self.index + 1]
191 except IndexError:
Fredrik Lundh8a0232d2001-11-02 13:59:51 +0000192 raise error, "bogus escape (end of line)"
Fredrik Lundh90a07912000-06-30 07:50:59 +0000193 char = char + c
194 self.index = self.index + len(char)
Fredrik Lundhc13222c2000-07-01 23:49:14 +0000195 self.next = char
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000196 def match(self, char, skip=1):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000197 if char == self.next:
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000198 if skip:
199 self.__next()
Fredrik Lundh90a07912000-06-30 07:50:59 +0000200 return 1
201 return 0
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000202 def get(self):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000203 this = self.next
Fredrik Lundhc13222c2000-07-01 23:49:14 +0000204 self.__next()
Fredrik Lundh90a07912000-06-30 07:50:59 +0000205 return this
Fredrik Lundhc13222c2000-07-01 23:49:14 +0000206 def tell(self):
207 return self.index, self.next
208 def seek(self, index):
209 self.index, self.next = index
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000210
Fredrik Lundh4781b072000-06-29 12:38:45 +0000211def isident(char):
212 return "a" <= char <= "z" or "A" <= char <= "Z" or char == "_"
213
214def isdigit(char):
215 return "0" <= char <= "9"
216
217def isname(name):
218 # check that group name is a valid string
Fredrik Lundh4781b072000-06-29 12:38:45 +0000219 if not isident(name[0]):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000220 return False
Gustavo Niemeyera01a2ee2004-09-03 17:06:10 +0000221 for char in name[1:]:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000222 if not isident(char) and not isdigit(char):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000223 return False
224 return True
Fredrik Lundh4781b072000-06-29 12:38:45 +0000225
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000226def _class_escape(source, escape):
227 # handle escape code inside character class
228 code = ESCAPES.get(escape)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000229 if code:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000230 return code
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000231 code = CATEGORIES.get(escape)
Ezio Melotti5c4e32b2013-01-11 08:32:01 +0200232 if code and code[0] == IN:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000233 return code
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000234 try:
Gustavo Niemeyera01a2ee2004-09-03 17:06:10 +0000235 c = escape[1:2]
236 if c == "x":
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000237 # hexadecimal escape (exactly two digits)
238 while source.next in HEXDIGITS and len(escape) < 4:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000239 escape = escape + source.get()
240 escape = escape[2:]
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000241 if len(escape) != 2:
242 raise error, "bogus escape: %s" % repr("\\" + escape)
Barry Warsaw8bee7612004-08-25 02:22:30 +0000243 return LITERAL, int(escape, 16) & 0xff
Gustavo Niemeyera01a2ee2004-09-03 17:06:10 +0000244 elif c in OCTDIGITS:
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000245 # octal escape (up to three digits)
Gustavo Niemeyera01a2ee2004-09-03 17:06:10 +0000246 while source.next in OCTDIGITS and len(escape) < 4:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000247 escape = escape + source.get()
248 escape = escape[1:]
Barry Warsaw8bee7612004-08-25 02:22:30 +0000249 return LITERAL, int(escape, 8) & 0xff
Gustavo Niemeyera01a2ee2004-09-03 17:06:10 +0000250 elif c in DIGITS:
251 raise error, "bogus escape: %s" % repr(escape)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000252 if len(escape) == 2:
Fredrik Lundh0640e112000-06-30 13:55:15 +0000253 return LITERAL, ord(escape[1])
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000254 except ValueError:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000255 pass
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000256 raise error, "bogus escape: %s" % repr(escape)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000257
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000258def _escape(source, escape, state):
259 # handle escape code in expression
260 code = CATEGORIES.get(escape)
261 if code:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000262 return code
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000263 code = ESCAPES.get(escape)
264 if code:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000265 return code
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000266 try:
Gustavo Niemeyera01a2ee2004-09-03 17:06:10 +0000267 c = escape[1:2]
268 if c == "x":
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000269 # hexadecimal escape
270 while source.next in HEXDIGITS and len(escape) < 4:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000271 escape = escape + source.get()
Fredrik Lundh143328b2000-09-02 11:03:34 +0000272 if len(escape) != 4:
273 raise ValueError
Barry Warsaw8bee7612004-08-25 02:22:30 +0000274 return LITERAL, int(escape[2:], 16) & 0xff
Gustavo Niemeyera01a2ee2004-09-03 17:06:10 +0000275 elif c == "0":
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000276 # octal escape
Fredrik Lundh143328b2000-09-02 11:03:34 +0000277 while source.next in OCTDIGITS and len(escape) < 4:
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000278 escape = escape + source.get()
Barry Warsaw8bee7612004-08-25 02:22:30 +0000279 return LITERAL, int(escape[1:], 8) & 0xff
Gustavo Niemeyera01a2ee2004-09-03 17:06:10 +0000280 elif c in DIGITS:
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000281 # octal escape *or* decimal group reference (sigh)
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000282 if source.next in DIGITS:
283 escape = escape + source.get()
Fredrik Lundh143328b2000-09-02 11:03:34 +0000284 if (escape[1] in OCTDIGITS and escape[2] in OCTDIGITS and
285 source.next in OCTDIGITS):
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000286 # got three octal digits; this is an octal escape
Fredrik Lundh90a07912000-06-30 07:50:59 +0000287 escape = escape + source.get()
Barry Warsaw8bee7612004-08-25 02:22:30 +0000288 return LITERAL, int(escape[1:], 8) & 0xff
Gustavo Niemeyera01a2ee2004-09-03 17:06:10 +0000289 # not an octal escape, so this is a group reference
290 group = int(escape[1:])
291 if group < state.groups:
Fredrik Lundhebc37b22000-10-28 19:30:41 +0000292 if not state.checkgroup(group):
293 raise error, "cannot refer to open group"
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000294 return GROUPREF, group
Fredrik Lundh143328b2000-09-02 11:03:34 +0000295 raise ValueError
Fredrik Lundh90a07912000-06-30 07:50:59 +0000296 if len(escape) == 2:
Fredrik Lundh0640e112000-06-30 13:55:15 +0000297 return LITERAL, ord(escape[1])
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000298 except ValueError:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000299 pass
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000300 raise error, "bogus escape: %s" % repr(escape)
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000301
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000302def _parse_sub(source, state, nested=1):
303 # parse an alternation: a|b|c
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000304
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000305 items = []
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000306 itemsappend = items.append
307 sourcematch = source.match
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000308 while 1:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000309 itemsappend(_parse(source, state))
310 if sourcematch("|"):
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000311 continue
312 if not nested:
313 break
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000314 if not source.next or sourcematch(")", 0):
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000315 break
316 else:
317 raise error, "pattern not properly closed"
318
319 if len(items) == 1:
320 return items[0]
321
322 subpattern = SubPattern(state)
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000323 subpatternappend = subpattern.append
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000324
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000325 # check if all items share a common prefix
326 while 1:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000327 prefix = None
328 for item in items:
329 if not item:
330 break
331 if prefix is None:
332 prefix = item[0]
333 elif item[0] != prefix:
334 break
335 else:
336 # all subitems start with a common "prefix".
337 # move it out of the branch
338 for item in items:
339 del item[0]
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000340 subpatternappend(prefix)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000341 continue # check next one
342 break
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000343
344 # check if the branch can be replaced by a character set
345 for item in items:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000346 if len(item) != 1 or item[0][0] != LITERAL:
347 break
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000348 else:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000349 # we can store this as a character set instead of a
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000350 # branch (the compiler may optimize this even more)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000351 set = []
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000352 setappend = set.append
Fredrik Lundh90a07912000-06-30 07:50:59 +0000353 for item in items:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000354 setappend(item[0])
355 subpatternappend((IN, set))
Fredrik Lundh90a07912000-06-30 07:50:59 +0000356 return subpattern
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000357
358 subpattern.append((BRANCH, (None, items)))
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000359 return subpattern
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000360
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000361def _parse_sub_cond(source, state, condgroup):
Tim Peters58eb11c2004-01-18 20:29:55 +0000362 item_yes = _parse(source, state)
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000363 if source.match("|"):
Tim Peters58eb11c2004-01-18 20:29:55 +0000364 item_no = _parse(source, state)
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000365 if source.match("|"):
366 raise error, "conditional backref with more than two branches"
367 else:
368 item_no = None
369 if source.next and not source.match(")", 0):
370 raise error, "pattern not properly closed"
371 subpattern = SubPattern(state)
372 subpattern.append((GROUPREF_EXISTS, (condgroup, item_yes, item_no)))
373 return subpattern
374
Raymond Hettinger049ade22005-02-28 19:27:52 +0000375_PATTERNENDERS = set("|)")
376_ASSERTCHARS = set("=!<")
377_LOOKBEHINDASSERTCHARS = set("=!")
378_REPEATCODES = set([MIN_REPEAT, MAX_REPEAT])
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
Raymond Hettinger049ade22005-02-28 19:27:52 +0000389 PATTERNENDERS = _PATTERNENDERS
390 ASSERTCHARS = _ASSERTCHARS
391 LOOKBEHINDASSERTCHARS = _LOOKBEHINDASSERTCHARS
392 REPEATCODES = _REPEATCODES
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000393
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 == "{":
Gustavo Niemeyer6fa0c5a2005-09-14 08:54:39 +0000483 if source.next == "}":
484 subpatternappend((LITERAL, ord(this)))
485 continue
Fredrik Lundhc13222c2000-07-01 23:49:14 +0000486 here = source.tell()
Fredrik Lundh90a07912000-06-30 07:50:59 +0000487 min, max = 0, MAXREPEAT
488 lo = hi = ""
489 while source.next in DIGITS:
490 lo = lo + source.get()
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000491 if sourcematch(","):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000492 while source.next in DIGITS:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000493 hi = hi + sourceget()
Fredrik Lundh90a07912000-06-30 07:50:59 +0000494 else:
495 hi = lo
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000496 if not sourcematch("}"):
497 subpatternappend((LITERAL, ord(this)))
Fredrik Lundhc13222c2000-07-01 23:49:14 +0000498 source.seek(here)
499 continue
Fredrik Lundh90a07912000-06-30 07:50:59 +0000500 if lo:
Barry Warsaw8bee7612004-08-25 02:22:30 +0000501 min = int(lo)
Serhiy Storchakae18e05c2013-02-16 16:47:15 +0200502 if min >= MAXREPEAT:
503 raise OverflowError("the repetition number is too large")
Fredrik Lundh90a07912000-06-30 07:50:59 +0000504 if hi:
Barry Warsaw8bee7612004-08-25 02:22:30 +0000505 max = int(hi)
Serhiy Storchakae18e05c2013-02-16 16:47:15 +0200506 if max >= MAXREPEAT:
507 raise OverflowError("the repetition number is too large")
508 if max < min:
509 raise error("bad repeat interval")
Fredrik Lundh90a07912000-06-30 07:50:59 +0000510 else:
511 raise error, "not supported"
512 # figure out which item to repeat
513 if subpattern:
514 item = subpattern[-1:]
515 else:
Fredrik Lundhc0c7ee32001-02-18 21:04:48 +0000516 item = None
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000517 if not item or (_len(item) == 1 and item[0][0] == AT):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000518 raise error, "nothing to repeat"
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000519 if item[0][0] in REPEATCODES:
Fredrik Lundh470ea5a2001-01-14 21:00:44 +0000520 raise error, "multiple repeat"
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000521 if sourcematch("?"):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000522 subpattern[-1] = (MIN_REPEAT, (min, max, item))
523 else:
524 subpattern[-1] = (MAX_REPEAT, (min, max, item))
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000525
Fredrik Lundh90a07912000-06-30 07:50:59 +0000526 elif this == ".":
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000527 subpatternappend((ANY, None))
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000528
Fredrik Lundh90a07912000-06-30 07:50:59 +0000529 elif this == "(":
530 group = 1
531 name = None
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000532 condgroup = None
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000533 if sourcematch("?"):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000534 group = 0
535 # options
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000536 if sourcematch("P"):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000537 # python extensions
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000538 if sourcematch("<"):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000539 # named group: skip forward to end of name
540 name = ""
541 while 1:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000542 char = sourceget()
Fredrik Lundh90a07912000-06-30 07:50:59 +0000543 if char is None:
544 raise error, "unterminated name"
545 if char == ">":
546 break
547 name = name + char
548 group = 1
Ezio Melottief317382012-11-03 20:31:12 +0200549 if not name:
550 raise error("missing group name")
Fredrik Lundh90a07912000-06-30 07:50:59 +0000551 if not isname(name):
Fredrik Lundh470ea5a2001-01-14 21:00:44 +0000552 raise error, "bad character in group name"
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000553 elif sourcematch("="):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000554 # named backreference
Fredrik Lundhb71624e2000-06-30 09:13:06 +0000555 name = ""
556 while 1:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000557 char = sourceget()
Fredrik Lundhb71624e2000-06-30 09:13:06 +0000558 if char is None:
559 raise error, "unterminated name"
560 if char == ")":
561 break
562 name = name + char
Ezio Melottief317382012-11-03 20:31:12 +0200563 if not name:
564 raise error("missing group name")
Fredrik Lundhb71624e2000-06-30 09:13:06 +0000565 if not isname(name):
Fredrik Lundh470ea5a2001-01-14 21:00:44 +0000566 raise error, "bad character in group name"
Fredrik Lundhb71624e2000-06-30 09:13:06 +0000567 gid = state.groupdict.get(name)
568 if gid is None:
569 raise error, "unknown group name"
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000570 subpatternappend((GROUPREF, gid))
Fredrik Lundh7cafe4d2000-07-02 17:33:27 +0000571 continue
Fredrik Lundh90a07912000-06-30 07:50:59 +0000572 else:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000573 char = sourceget()
Fredrik Lundh90a07912000-06-30 07:50:59 +0000574 if char is None:
575 raise error, "unexpected end of pattern"
576 raise error, "unknown specifier: ?P%s" % char
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000577 elif sourcematch(":"):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000578 # non-capturing group
579 group = 2
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000580 elif sourcematch("#"):
Fredrik Lundh90a07912000-06-30 07:50:59 +0000581 # comment
582 while 1:
583 if source.next is None or source.next == ")":
584 break
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000585 sourceget()
586 if not sourcematch(")"):
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000587 raise error, "unbalanced parenthesis"
588 continue
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000589 elif source.next in ASSERTCHARS:
Fredrik Lundh43b3b492000-06-30 10:41:31 +0000590 # lookahead assertions
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000591 char = sourceget()
Fredrik Lundh6f013982000-07-03 18:44:21 +0000592 dir = 1
593 if char == "<":
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000594 if source.next not in LOOKBEHINDASSERTCHARS:
Fredrik Lundh6f013982000-07-03 18:44:21 +0000595 raise error, "syntax error"
596 dir = -1 # lookbehind
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000597 char = sourceget()
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000598 p = _parse_sub(source, state)
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000599 if not sourcematch(")"):
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000600 raise error, "unbalanced parenthesis"
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000601 if char == "=":
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000602 subpatternappend((ASSERT, (dir, p)))
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000603 else:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000604 subpatternappend((ASSERT_NOT, (dir, p)))
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000605 continue
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000606 elif sourcematch("("):
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000607 # conditional backreference group
608 condname = ""
609 while 1:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000610 char = sourceget()
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000611 if char is None:
612 raise error, "unterminated name"
613 if char == ")":
614 break
615 condname = condname + char
616 group = 2
Ezio Melottief317382012-11-03 20:31:12 +0200617 if not condname:
618 raise error("missing group name")
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000619 if isname(condname):
620 condgroup = state.groupdict.get(condname)
621 if condgroup is None:
622 raise error, "unknown group name"
623 else:
624 try:
Barry Warsaw8bee7612004-08-25 02:22:30 +0000625 condgroup = int(condname)
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000626 except ValueError:
627 raise error, "bad character in group name"
Fredrik Lundh90a07912000-06-30 07:50:59 +0000628 else:
629 # flags
Raymond Hettinger54f02222002-06-01 14:18:47 +0000630 if not source.next in FLAGS:
Fredrik Lundh470ea5a2001-01-14 21:00:44 +0000631 raise error, "unexpected end of pattern"
Raymond Hettinger54f02222002-06-01 14:18:47 +0000632 while source.next in FLAGS:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000633 state.flags = state.flags | FLAGS[sourceget()]
Fredrik Lundh90a07912000-06-30 07:50:59 +0000634 if group:
635 # parse group contents
Fredrik Lundh90a07912000-06-30 07:50:59 +0000636 if group == 2:
637 # anonymous group
638 group = None
639 else:
Fredrik Lundhebc37b22000-10-28 19:30:41 +0000640 group = state.opengroup(name)
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000641 if condgroup:
642 p = _parse_sub_cond(source, state, condgroup)
643 else:
644 p = _parse_sub(source, state)
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000645 if not sourcematch(")"):
Fredrik Lundh0c4fdba2000-08-31 22:57:55 +0000646 raise error, "unbalanced parenthesis"
Fredrik Lundhebc37b22000-10-28 19:30:41 +0000647 if group is not None:
648 state.closegroup(group)
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000649 subpatternappend((SUBPATTERN, (group, p)))
Fredrik Lundh90a07912000-06-30 07:50:59 +0000650 else:
651 while 1:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000652 char = sourceget()
Fredrik Lundh470ea5a2001-01-14 21:00:44 +0000653 if char is None:
654 raise error, "unexpected end of pattern"
655 if char == ")":
Fredrik Lundh90a07912000-06-30 07:50:59 +0000656 break
657 raise error, "unknown extension"
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000658
Fredrik Lundh90a07912000-06-30 07:50:59 +0000659 elif this == "^":
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000660 subpatternappend((AT, AT_BEGINNING))
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000661
Fredrik Lundh90a07912000-06-30 07:50:59 +0000662 elif this == "$":
663 subpattern.append((AT, AT_END))
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000664
Fredrik Lundh90a07912000-06-30 07:50:59 +0000665 elif this and this[0] == "\\":
666 code = _escape(source, this, state)
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000667 subpatternappend(code)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000668
Fredrik Lundh90a07912000-06-30 07:50:59 +0000669 else:
670 raise error, "parser error"
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000671
672 return subpattern
673
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000674def parse(str, flags=0, pattern=None):
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000675 # parse 're' pattern into list of (opcode, argument) tuples
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000676
677 source = Tokenizer(str)
678
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000679 if pattern is None:
680 pattern = Pattern()
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000681 pattern.flags = flags
Fredrik Lundh470ea5a2001-01-14 21:00:44 +0000682 pattern.str = str
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000683
684 p = _parse_sub(source, pattern, 0)
685
686 tail = source.get()
687 if tail == ")":
688 raise error, "unbalanced parenthesis"
689 elif tail:
690 raise error, "bogus characters at end of regular expression"
691
Fredrik Lundh770617b2001-01-14 15:06:11 +0000692 if flags & SRE_FLAG_DEBUG:
693 p.dump()
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000694
Fredrik Lundhd11b5e52000-10-03 19:22:26 +0000695 if not (flags & SRE_FLAG_VERBOSE) and p.pattern.flags & SRE_FLAG_VERBOSE:
696 # the VERBOSE flag was switched on inside the pattern. to be
697 # on the safe side, we'll parse the whole thing again...
698 return parse(str, p.pattern.flags)
699
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000700 return p
701
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000702def parse_template(source, pattern):
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000703 # parse 're' replacement string into list of literals and
704 # group references
705 s = Tokenizer(source)
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000706 sget = s.get
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000707 p = []
708 a = p.append
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000709 def literal(literal, p=p, pappend=a):
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000710 if p and p[-1][0] is LITERAL:
711 p[-1] = LITERAL, p[-1][1] + literal
712 else:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000713 pappend((LITERAL, literal))
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000714 sep = source[:0]
715 if type(sep) is type(""):
Fredrik Lundh59b68652001-09-18 20:55:24 +0000716 makechar = chr
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000717 else:
Fredrik Lundh59b68652001-09-18 20:55:24 +0000718 makechar = unichr
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000719 while 1:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000720 this = sget()
Fredrik Lundh90a07912000-06-30 07:50:59 +0000721 if this is None:
722 break # end of replacement string
723 if this and this[0] == "\\":
724 # group
Gustavo Niemeyera01a2ee2004-09-03 17:06:10 +0000725 c = this[1:2]
726 if c == "g":
Fredrik Lundh90a07912000-06-30 07:50:59 +0000727 name = ""
728 if s.match("<"):
729 while 1:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000730 char = sget()
Fredrik Lundh90a07912000-06-30 07:50:59 +0000731 if char is None:
732 raise error, "unterminated group name"
733 if char == ">":
734 break
735 name = name + char
736 if not name:
Ezio Melottief317382012-11-03 20:31:12 +0200737 raise error, "missing group name"
Fredrik Lundh90a07912000-06-30 07:50:59 +0000738 try:
Barry Warsaw8bee7612004-08-25 02:22:30 +0000739 index = int(name)
Gustavo Niemeyera01a2ee2004-09-03 17:06:10 +0000740 if index < 0:
741 raise error, "negative group number"
Fredrik Lundh90a07912000-06-30 07:50:59 +0000742 except ValueError:
743 if not isname(name):
Fredrik Lundh470ea5a2001-01-14 21:00:44 +0000744 raise error, "bad character in group name"
Fredrik Lundh90a07912000-06-30 07:50:59 +0000745 try:
746 index = pattern.groupindex[name]
747 except KeyError:
748 raise IndexError, "unknown group name"
749 a((MARK, index))
Gustavo Niemeyera01a2ee2004-09-03 17:06:10 +0000750 elif c == "0":
751 if s.next in OCTDIGITS:
752 this = this + sget()
753 if s.next in OCTDIGITS:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000754 this = this + sget()
Gustavo Niemeyera01a2ee2004-09-03 17:06:10 +0000755 literal(makechar(int(this[1:], 8) & 0xff))
756 elif c in DIGITS:
757 isoctal = False
758 if s.next in DIGITS:
759 this = this + sget()
Gustavo Niemeyerf5a15992004-09-03 20:15:56 +0000760 if (c in OCTDIGITS and this[2] in OCTDIGITS and
761 s.next in OCTDIGITS):
Gustavo Niemeyera01a2ee2004-09-03 17:06:10 +0000762 this = this + sget()
763 isoctal = True
764 literal(makechar(int(this[1:], 8) & 0xff))
765 if not isoctal:
766 a((MARK, int(this[1:])))
Fredrik Lundh90a07912000-06-30 07:50:59 +0000767 else:
768 try:
Fredrik Lundh59b68652001-09-18 20:55:24 +0000769 this = makechar(ESCAPES[this][1])
Fredrik Lundh90a07912000-06-30 07:50:59 +0000770 except KeyError:
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000771 pass
772 literal(this)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000773 else:
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000774 literal(this)
775 # convert template to groups and literals lists
776 i = 0
777 groups = []
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000778 groupsappend = groups.append
779 literals = [None] * len(p)
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000780 for c, s in p:
781 if c is MARK:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000782 groupsappend((i, s))
783 # literal[i] is already None
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000784 else:
Raymond Hettinger968c56a2004-03-26 23:24:00 +0000785 literals[i] = s
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000786 i = i + 1
787 return groups, literals
Andrew M. Kuchling815d5b92000-06-09 14:08:07 +0000788
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000789def expand_template(template, match):
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000790 g = match.group
Fredrik Lundh0640e112000-06-30 13:55:15 +0000791 sep = match.string[:0]
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000792 groups, literals = template
793 literals = literals[:]
794 try:
795 for index, group in groups:
796 literals[index] = s = g(group)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000797 if s is None:
Gustavo Niemeyera01a2ee2004-09-03 17:06:10 +0000798 raise error, "unmatched group"
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000799 except IndexError:
Gustavo Niemeyera01a2ee2004-09-03 17:06:10 +0000800 raise error, "invalid group reference"
Barry Warsaw8bee7612004-08-25 02:22:30 +0000801 return sep.join(literals)