blob: 6c24797ffd851be40ac1b5dee6a143ec425d5d70 [file] [log] [blame]
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001#!/usr/bin/env python
2# -*- mode: python -*-
3# $Id$
4
5import string
6import reop
7
Guido van Rossum6af4abd1997-08-13 03:25:34 +00008# reop.error and re.error should be the same, since exceptions can be
9# raised from either module.
10error = reop.error # 're error'
11
12from reop import NORMAL, CHARCLASS, REPLACEMENT
13from reop import CHAR, MEMORY_REFERENCE, SYNTAX, NOT_SYNTAX, SET
14from reop import WORD_BOUNDARY, NOT_WORD_BOUNDARY, BEGINNING_OF_BUFFER, END_OF_BUFFER
Guido van Rossum5ca1b711997-07-10 21:00:31 +000015
16# compilation flags
17
18IGNORECASE = I = 0x01
19
20MULTILINE = M = 0x02
21DOTALL = S = 0x04
22VERBOSE = X = 0x08
23
Guido van Rossuma4f1a781997-07-17 22:38:10 +000024repetition_operators = ['*', '*?', '+', '+?', '?', '??', '{n}', '{n}?',
25 '{n,}', '{n,}?', '{n,m}', '{n,m}?']
Guido van Rossum5ca1b711997-07-10 21:00:31 +000026
27#
28#
29#
30
Guido van Rossum09bcfd61997-07-15 15:38:20 +000031def valid_identifier(id):
32 if len(id) == 0:
33 return 0
Guido van Rossuma4f1a781997-07-17 22:38:10 +000034 if (not reop.syntax_table[id[0]] & reop.word) or \
35 (reop.syntax_table[id[0]] & reop.digit):
Guido van Rossum09bcfd61997-07-15 15:38:20 +000036 return 0
37 for char in id[1:]:
Guido van Rossuma4f1a781997-07-17 22:38:10 +000038 if not reop.syntax_table[char] & reop.word:
Guido van Rossum09bcfd61997-07-15 15:38:20 +000039 return 0
40 return 1
41
42#
43#
44#
45
Guido van Rossum26d80e61997-07-15 18:59:04 +000046_cache = {}
47_MAXCACHE = 20
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +000048
Guido van Rossum9e18ec71997-07-17 22:39:13 +000049def _cachecompile(pattern, flags=0):
Guido van Rossum26d80e61997-07-15 18:59:04 +000050 key = (pattern, flags)
51 try:
52 return _cache[key]
53 except KeyError:
54 pass
55 value = compile(pattern, flags)
56 if len(_cache) >= _MAXCACHE:
57 _cache.clear()
58 _cache[key] = value
59 return value
60
Guido van Rossum5ca1b711997-07-10 21:00:31 +000061def match(pattern, string, flags=0):
Guido van Rossum26d80e61997-07-15 18:59:04 +000062 return _cachecompile(pattern, flags).match(string)
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +000063
Guido van Rossum5ca1b711997-07-10 21:00:31 +000064def search(pattern, string, flags=0):
Guido van Rossum26d80e61997-07-15 18:59:04 +000065 return _cachecompile(pattern, flags).search(string)
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +000066
Guido van Rossum5ca1b711997-07-10 21:00:31 +000067def sub(pattern, repl, string, count=0):
Guido van Rossum9e18ec71997-07-17 22:39:13 +000068 if type(pattern) == type(''):
69 pattern = _cachecompile(pattern)
70 return pattern.sub(repl, string, count)
Guido van Rossum5ca1b711997-07-10 21:00:31 +000071
72def subn(pattern, repl, string, count=0):
Guido van Rossum9e18ec71997-07-17 22:39:13 +000073 if type(pattern) == type(''):
74 pattern = _cachecompile(pattern)
75 return pattern.subn(repl, string, count)
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +000076
Guido van Rossum8a9a4a21997-07-11 20:48:25 +000077def split(pattern, string, maxsplit=0):
Guido van Rossum9e18ec71997-07-17 22:39:13 +000078 if type(pattern) == type(''):
79 pattern = _cachecompile(pattern)
80 return pattern.split(string, maxsplit)
Guido van Rossum5ca1b711997-07-10 21:00:31 +000081
82#
83#
84#
85
Guido van Rossum71fa97c1997-07-18 04:26:03 +000086def _expand(m, repl):
87 results = []
88 index = 0
89 size = len(repl)
90 while index < size:
91 found = string.find(repl, '\\', index)
92 if found < 0:
93 results.append(repl[index:])
94 break
95 if found > index:
96 results.append(repl[index:found])
97 escape_type, value, index = expand_escape(repl, found+1, REPLACEMENT)
98 if escape_type == CHAR:
99 results.append(value)
100 elif escape_type == MEMORY_REFERENCE:
101 r = m.group(value)
102 if r is None:
103 raise error, ('group "' + str(value) + '" did not contribute '
104 'to the match')
105 results.append(m.group(value))
106 else:
107 raise error, "bad escape in replacement"
108 return string.join(results, '')
109
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000110class RegexObject:
Guido van Rossum71fa97c1997-07-18 04:26:03 +0000111 def __init__(self, pattern, flags, code, num_regs, groupindex):
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000112 self.code = code
113 self.num_regs = num_regs
114 self.flags = flags
115 self.pattern = pattern
116 self.groupindex = groupindex
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000117 self.fastmap = build_fastmap(code)
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +0000118
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000119 if code[0].name == 'bol':
120 self.anchor = 1
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +0000121
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000122 elif code[0].name == 'begbuf':
123 self.anchor = 2
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +0000124
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000125 else:
126 self.anchor = 0
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +0000127
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000128 self.buffer = assemble(code)
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000129 def search(self, string, pos=0):
130 regs = reop.search(self.buffer,
131 self.num_regs,
132 self.flags,
133 self.fastmap.can_be_null,
134 self.fastmap.fastmap(),
135 self.anchor,
136 string,
137 pos)
138 if regs is None:
139 return None
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +0000140
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000141 return MatchObject(self,
142 string,
143 pos,
144 regs)
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +0000145
Guido van Rossum8a9a4a21997-07-11 20:48:25 +0000146 def match(self, string, pos=0):
147 regs = reop.match(self.buffer,
148 self.num_regs,
149 self.flags,
150 self.fastmap.can_be_null,
151 self.fastmap.fastmap(),
152 self.anchor,
153 string,
154 pos)
155 if regs is None:
156 return None
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +0000157
Guido van Rossum8a9a4a21997-07-11 20:48:25 +0000158 return MatchObject(self,
159 string,
160 pos,
161 regs)
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +0000162
Guido van Rossum8a9a4a21997-07-11 20:48:25 +0000163 def sub(self, repl, string, count=0):
Guido van Rossum71fa97c1997-07-18 04:26:03 +0000164 return self.subn(repl, string, count)[0]
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +0000165
Guido van Rossum9e18ec71997-07-17 22:39:13 +0000166 def subn(self, repl, source, count=0):
Guido van Rossum71fa97c1997-07-18 04:26:03 +0000167 if count < 0:
168 raise ValueError, "negative substibution count"
169 if count == 0:
170 import sys
171 count = sys.maxint
Guido van Rossum9e18ec71997-07-17 22:39:13 +0000172 if type(repl) == type(''):
173 if '\\' in repl:
174 repl = lambda m, r=repl: _expand(m, r)
175 else:
176 repl = lambda m, r=repl: r
Guido van Rossum71fa97c1997-07-18 04:26:03 +0000177 n = 0 # Number of matches
178 pos = 0 # Where to start searching
179 lastmatch = -1 # End of last match
180 results = [] # Substrings making up the result
Guido van Rossum9e18ec71997-07-17 22:39:13 +0000181 end = len(source)
182 while n < count and pos <= end:
183 m = self.search(source, pos)
Guido van Rossum71fa97c1997-07-18 04:26:03 +0000184 if not m:
185 break
Guido van Rossum9e18ec71997-07-17 22:39:13 +0000186 i, j = m.span(0)
187 if i == j == lastmatch:
188 # Empty match adjacent to previous match
Guido van Rossum71fa97c1997-07-18 04:26:03 +0000189 pos = pos + 1
Guido van Rossum9e18ec71997-07-17 22:39:13 +0000190 results.append(source[lastmatch:pos])
191 continue
Guido van Rossum71fa97c1997-07-18 04:26:03 +0000192 if pos < i:
193 results.append(source[pos:i])
Guido van Rossum9e18ec71997-07-17 22:39:13 +0000194 results.append(repl(m))
195 pos = lastmatch = j
196 if i == j:
197 # Last match was empty; don't try here again
Guido van Rossum71fa97c1997-07-18 04:26:03 +0000198 pos = pos + 1
Guido van Rossum9e18ec71997-07-17 22:39:13 +0000199 results.append(source[lastmatch:pos])
Guido van Rossum71fa97c1997-07-18 04:26:03 +0000200 n = n + 1
Guido van Rossum9e18ec71997-07-17 22:39:13 +0000201 results.append(source[pos:])
202 return (string.join(results, ''), n)
Guido van Rossum71fa97c1997-07-18 04:26:03 +0000203
Guido van Rossum9e18ec71997-07-17 22:39:13 +0000204 def split(self, source, maxsplit=0):
Guido van Rossum71fa97c1997-07-18 04:26:03 +0000205 if maxsplit < 0:
206 raise error, "negative split count"
207 if maxsplit == 0:
208 import sys
209 maxsplit = sys.maxint
Guido van Rossum9e18ec71997-07-17 22:39:13 +0000210 n = 0
211 pos = 0
212 lastmatch = 0
213 results = []
214 end = len(source)
215 while n < maxsplit:
216 m = self.search(source, pos)
Guido van Rossum71fa97c1997-07-18 04:26:03 +0000217 if not m:
218 break
Guido van Rossum9e18ec71997-07-17 22:39:13 +0000219 i, j = m.span(0)
220 if i == j:
221 # Empty match
Guido van Rossum71fa97c1997-07-18 04:26:03 +0000222 if pos >= end:
223 break
Guido van Rossum9e18ec71997-07-17 22:39:13 +0000224 pos = pos+1
225 continue
226 results.append(source[lastmatch:i])
227 g = m.group()
228 if g:
229 results[len(results):] = list(g)
230 pos = lastmatch = j
231 results.append(source[lastmatch:])
232 return results
233
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000234class MatchObject:
235 def __init__(self, re, string, pos, regs):
236 self.re = re
237 self.string = string
238 self.pos = pos
239 self.regs = regs
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +0000240
Guido van Rossum8a9a4a21997-07-11 20:48:25 +0000241 def start(self, g):
242 if type(g) == type(''):
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000243 try:
Guido van Rossum8a9a4a21997-07-11 20:48:25 +0000244 g = self.re.groupindex[g]
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000245 except (KeyError, TypeError):
Guido van Rossum8a9a4a21997-07-11 20:48:25 +0000246 raise IndexError, ('group "' + g + '" is undefined')
247 return self.regs[g][0]
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +0000248
Guido van Rossum8a9a4a21997-07-11 20:48:25 +0000249 def end(self, g):
250 if type(g) == type(''):
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000251 try:
Guido van Rossum8a9a4a21997-07-11 20:48:25 +0000252 g = self.re.groupindex[g]
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000253 except (KeyError, TypeError):
Guido van Rossum8a9a4a21997-07-11 20:48:25 +0000254 raise IndexError, ('group "' + g + '" is undefined')
255 return self.regs[g][1]
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +0000256
Guido van Rossum8a9a4a21997-07-11 20:48:25 +0000257 def span(self, g):
258 if type(g) == type(''):
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000259 try:
Guido van Rossum8a9a4a21997-07-11 20:48:25 +0000260 g = self.re.groupindex[g]
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000261 except (KeyError, TypeError):
Guido van Rossum8a9a4a21997-07-11 20:48:25 +0000262 raise IndexError, ('group "' + g + '" is undefined')
263 return self.regs[g]
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +0000264
Guido van Rossum8a9a4a21997-07-11 20:48:25 +0000265 def group(self, *groups):
266 if len(groups) == 0:
267 groups = range(1, self.re.num_regs)
Guido van Rossum53109751997-07-15 15:40:29 +0000268 use_all = 1
269 else:
270 use_all = 0
Guido van Rossum8a9a4a21997-07-11 20:48:25 +0000271 result = []
272 for g in groups:
273 if type(g) == type(''):
274 try:
275 g = self.re.groupindex[g]
276 except (KeyError, TypeError):
277 raise IndexError, ('group "' + g + '" is undefined')
Guido van Rossum04a1d741997-07-15 14:38:13 +0000278 if (self.regs[g][0] == -1) or (self.regs[g][1] == -1):
Guido van Rossum8a9a4a21997-07-11 20:48:25 +0000279 result.append(None)
280 else:
281 result.append(self.string[self.regs[g][0]:self.regs[g][1]])
Guido van Rossum53109751997-07-15 15:40:29 +0000282 if use_all or len(result) > 1:
Guido van Rossum8a9a4a21997-07-11 20:48:25 +0000283 return tuple(result)
284 elif len(result) == 1:
285 return result[0]
286 else:
287 return ()
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000288
289#
290# A set of classes to make assembly a bit easier, if a bit verbose.
291#
292
293class Instruction:
294 def __init__(self, opcode, size=1):
295 self.opcode = opcode
296 self.size = size
297 def assemble(self, position, labels):
298 return self.opcode
299 def __repr__(self):
300 return '%-15s' % (self.name)
301
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000302class End(Instruction):
303 name = 'end'
304 def __init__(self):
305 Instruction.__init__(self, chr(0))
306
307class Bol(Instruction):
308 name = 'bol'
309 def __init__(self):
310 self.name = 'bol'
311 Instruction.__init__(self, chr(1))
312
313class Eol(Instruction):
314 name = 'eol'
315 def __init__(self):
316 Instruction.__init__(self, chr(2))
317
318class Set(Instruction):
319 name = 'set'
Guido van Rossum95e80531997-08-13 22:34:14 +0000320 def __init__(self, set, flags=0):
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000321 self.set = set
Guido van Rossum95e80531997-08-13 22:34:14 +0000322 if flags & IGNORECASE: self.set=map(string.lower, self.set)
323 if len(set)==1:
324 # If only one element, use the "exact" opcode (it'll be faster)
325 Instruction.__init__(self, chr(4), 2)
326 else:
327 # Use the "set" opcode
328 Instruction.__init__(self, chr(3), 33)
Guido van Rossum63e18191997-07-11 11:08:38 +0000329 def assemble(self, position, labels):
Guido van Rossum95e80531997-08-13 22:34:14 +0000330 if len(self.set)==1:
331 # If only one character in set, generate an "exact" opcode
332 return self.opcode + self.set[0]
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000333 result = self.opcode
334 temp = 0
335 for i, c in map(lambda x: (x, chr(x)), range(256)):
Guido van Rossum63e18191997-07-11 11:08:38 +0000336 if c in self.set:
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000337 temp = temp | (1 << (i & 7))
338 if (i % 8) == 7:
339 result = result + chr(temp)
340 temp = 0
341 return result
342 def __repr__(self):
343 result = '%-15s' % (self.name)
344 self.set.sort()
Guido van Rossum95e80531997-08-13 22:34:14 +0000345 # XXX this should print more intelligently
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000346 for char in self.set:
Guido van Rossum63e18191997-07-11 11:08:38 +0000347 result = result + char
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000348 return result
349
350class Exact(Instruction):
351 name = 'exact'
Guido van Rossum95e80531997-08-13 22:34:14 +0000352 def __init__(self, char, flags):
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000353 self.char = char
Guido van Rossum95e80531997-08-13 22:34:14 +0000354 if flags & IGNORECASE: self.char=string.lower(self.char)
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000355 Instruction.__init__(self, chr(4), 2)
356 def assemble(self, position, labels):
357 return self.opcode + self.char
358 def __repr__(self):
359 return '%-15s %s' % (self.name, `self.char`)
360
361class AnyChar(Instruction):
362 name = 'anychar'
363 def __init__(self):
364 Instruction.__init__(self, chr(5))
365 def assemble(self, position, labels):
366 return self.opcode
367
368class MemoryInstruction(Instruction):
369 def __init__(self, opcode, register):
370 self.register = register
371 Instruction.__init__(self, opcode, 2)
372 def assemble(self, position, labels):
373 return self.opcode + chr(self.register)
374 def __repr__(self):
375 return '%-15s %i' % (self.name, self.register)
376
377class StartMemory(MemoryInstruction):
378 name = 'start_memory'
379 def __init__(self, register):
380 MemoryInstruction.__init__(self, chr(6), register)
381
382class EndMemory(MemoryInstruction):
383 name = 'end_memory'
384 def __init__(self, register):
385 MemoryInstruction.__init__(self, chr(7), register)
386
387class MatchMemory(MemoryInstruction):
388 name = 'match_memory'
389 def __init__(self, register):
390 MemoryInstruction.__init__(self, chr(8), register)
391
392class JumpInstruction(Instruction):
393 def __init__(self, opcode, label):
394 self.label = label
395 Instruction.__init__(self, opcode, 3)
396 def compute_offset(self, start, dest):
397 return dest - (start + 3)
398 def pack_offset(self, offset):
399 if offset > 32767:
400 raise error, 'offset out of range (pos)'
401 elif offset < -32768:
402 raise error, 'offset out of range (neg)'
403 elif offset < 0:
404 offset = offset + 65536
405 return chr(offset & 0xff) + chr((offset >> 8) & 0xff)
406 def assemble(self, position, labels):
407 return self.opcode + \
408 self.pack_offset(self.compute_offset(position,
409 labels[self.label]))
410 def __repr__(self):
411 return '%-15s %i' % (self.name, self.label)
412
413class Jump(JumpInstruction):
414 name = 'jump'
415 def __init__(self, label):
416 JumpInstruction.__init__(self, chr(9), label)
417
418class StarJump(JumpInstruction):
419 name = 'star_jump'
420 def __init__(self, label):
421 JumpInstruction.__init__(self, chr(10), label)
422
423class FailureJump(JumpInstruction):
424 name = 'failure_jump'
425 def __init__(self, label):
426 JumpInstruction.__init__(self, chr(11), label)
427
428class UpdateFailureJump(JumpInstruction):
429 name = 'update_failure_jump'
430 def __init__(self, label):
431 JumpInstruction.__init__(self, chr(12), label)
432
433class DummyFailureJump(JumpInstruction):
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +0000434 name = 'dummy_failure_jump'
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000435 def __init__(self, label):
436 JumpInstruction.__init__(self, chr(13), label)
437
438class BegBuf(Instruction):
439 name = 'begbuf'
440 def __init__(self):
441 Instruction.__init__(self, chr(14))
442
443class EndBuf(Instruction):
444 name = 'endbuf'
445 def __init__(self):
446 Instruction.__init__(self, chr(15))
447
448class WordBeg(Instruction):
449 name = 'wordbeg'
450 def __init__(self):
451 Instruction.__init__(self, chr(16))
452
453class WordEnd(Instruction):
454 name = 'wordend'
455 def __init__(self):
456 Instruction.__init__(self, chr(17))
457
458class WordBound(Instruction):
459 name = 'wordbound'
460 def __init__(self):
461 Instruction.__init__(self, chr(18))
462
463class NotWordBound(Instruction):
464 name = 'notwordbound'
465 def __init__(self):
Guido van Rossum827ea7c1997-08-15 15:44:23 +0000466 Instruction.__init__(self, chr(19))
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000467
468class SyntaxSpec(Instruction):
469 name = 'syntaxspec'
470 def __init__(self, syntax):
471 self.syntax = syntax
472 Instruction.__init__(self, chr(20), 2)
473 def assemble(self, postition, labels):
474 return self.opcode + chr(self.syntax)
475
Guido van Rossum8a9a4a21997-07-11 20:48:25 +0000476class NotSyntaxSpec(Instruction):
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000477 name = 'notsyntaxspec'
478 def __init__(self, syntax):
479 self.syntax = syntax
480 Instruction.__init__(self, chr(21), 2)
481 def assemble(self, postition, labels):
482 return self.opcode + chr(self.syntax)
483
484class Label(Instruction):
485 name = 'label'
486 def __init__(self, label):
487 self.label = label
488 Instruction.__init__(self, '', 0)
489 def __repr__(self):
490 return '%-15s %i' % (self.name, self.label)
491
492class OpenParen(Instruction):
493 name = '('
494 def __init__(self, register):
495 self.register = register
496 Instruction.__init__(self, '', 0)
Guido van Rossum5d6de251997-07-11 21:10:17 +0000497 def assemble(self, position, labels):
498 raise error, 'unmatched open parenthesis'
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000499
500class Alternation(Instruction):
501 name = '|'
502 def __init__(self):
503 Instruction.__init__(self, '', 0)
Guido van Rossum5d6de251997-07-11 21:10:17 +0000504 def assemble(self, position, labels):
505 raise error, 'an alternation was not taken care of'
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000506
507#
508#
509#
510
511def assemble(instructions):
512 labels = {}
513 position = 0
514 pass1 = []
515 for instruction in instructions:
516 if instruction.name == 'label':
517 labels[instruction.label] = position
518 else:
519 pass1.append((position, instruction))
520 position = position + instruction.size
521 pass2 = ''
522 for position, instruction in pass1:
523 pass2 = pass2 + instruction.assemble(position, labels)
524 return pass2
525
526#
527#
528#
529
530def escape(pattern):
Guido van Rossum8a9a4a21997-07-11 20:48:25 +0000531 result = []
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000532 for char in pattern:
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000533 if not reop.syntax_table[char] & reop.word:
Guido van Rossum8a9a4a21997-07-11 20:48:25 +0000534 result.append('\\')
535 result.append(char)
536 return string.join(result, '')
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000537
538#
539#
540#
541
542def registers_used(instructions):
543 result = []
544 for instruction in instructions:
545 if (instruction.name in ['set_memory', 'end_memory']) and \
546 (instruction.register not in result):
547 result.append(instruction.register)
548 return result
549
550#
551#
552#
553
554class Fastmap:
555 def __init__(self):
556 self.map = ['\000']*256
557 self.can_be_null = 0
558 def add(self, char):
559 self.map[ord(char)] = '\001'
560 def fastmap(self):
561 return string.join(self.map, '')
562 def __getitem__(self, char):
563 return ord(self.map[ord(char)])
564 def __repr__(self):
565 self.map.sort()
566 return 'Fastmap(' + `self.can_be_null` + ', ' + `self.map` + ')'
567
568#
569#
570#
571
572def find_label(code, label):
573 line = 0
574 for instruction in code:
575 if (instruction.name == 'label') and (instruction.label == label):
576 return line + 1
577 line = line + 1
578
579def build_fastmap_aux(code, pos, visited, fastmap):
580 if visited[pos]:
581 return
582 while 1:
583 instruction = code[pos]
584 visited[pos] = 1
585 pos = pos + 1
586 if instruction.name == 'end':
587 fastmap.can_be_null = 1
588 return
589 elif instruction.name == 'syntaxspec':
590 for char in map(chr, range(256)):
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000591 if reop.syntax_table[char] & instruction.syntax:
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000592 fastmap.add(char)
593 return
594 elif instruction.name == 'notsyntaxspec':
595 for char in map(chr, range(256)):
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000596 if not reop.syntax_table[char] & instruction.syntax:
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000597 fastmap.add(char)
598 return
599 elif instruction.name == 'eol':
600 fastmap.add('\n')
601 if fastmap.can_be_null == 0:
602 fastmap.can_be_null = 2
603 return
604 elif instruction.name == 'set':
605 for char in instruction.set:
606 fastmap.add(char)
607 return
608 elif instruction.name == 'exact':
609 fastmap.add(instruction.char)
610 elif instruction.name == 'anychar':
611 for char in map(chr, range(256)):
612 if char != '\n':
613 fastmap.add(char)
614 return
615 elif instruction.name == 'match_memory':
616 for char in map(chr, range(256)):
617 fastmap.add(char)
618 fastmap.can_be_null = 1
619 return
620 elif instruction.name in ['jump', 'dummy_failure_jump', \
621 'update_failure_jump', 'star_jump']:
622 pos = find_label(code, instruction.label)
623 if visited[pos]:
624 return
625 visited[pos] = 1
626 elif instruction.name == 'failure_jump':
627 build_fastmap_aux(code,
628 find_label(code, instruction.label),
629 visited,
630 fastmap)
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000631
632def build_fastmap(code, pos=0):
633 visited = [0] * len(code)
634 fastmap = Fastmap()
635 build_fastmap_aux(code, pos, visited, fastmap)
636 return fastmap
637
638#
639#
640#
641
Guido van Rossum6af4abd1997-08-13 03:25:34 +0000642#[NORMAL, CHARCLASS, REPLACEMENT] = range(3)
643#[CHAR, MEMORY_REFERENCE, SYNTAX, NOT_SYNTAX, SET, WORD_BOUNDARY,
644# NOT_WORD_BOUNDARY, BEGINNING_OF_BUFFER, END_OF_BUFFER] = range(9)
Guido van Rossum04a1d741997-07-15 14:38:13 +0000645
646def expand_escape(pattern, index, context=NORMAL):
647 if index >= len(pattern):
648 raise error, 'escape ends too soon'
649
650 elif pattern[index] == 't':
651 return CHAR, chr(9), index + 1
652
653 elif pattern[index] == 'n':
654 return CHAR, chr(10), index + 1
655
Guido van Rossum6af4abd1997-08-13 03:25:34 +0000656 elif pattern[index] == 'v':
657 return CHAR, chr(11), index + 1
658
Guido van Rossum04a1d741997-07-15 14:38:13 +0000659 elif pattern[index] == 'r':
660 return CHAR, chr(13), index + 1
661
662 elif pattern[index] == 'f':
663 return CHAR, chr(12), index + 1
664
665 elif pattern[index] == 'a':
666 return CHAR, chr(7), index + 1
667
Guido van Rossum04a1d741997-07-15 14:38:13 +0000668 elif pattern[index] == 'x':
669 # CAUTION: this is the Python rule, not the Perl rule!
Guido van Rossum6af4abd1997-08-13 03:25:34 +0000670 end = index + 1 # Skip over the 'x' character
Guido van Rossum04a1d741997-07-15 14:38:13 +0000671 while (end < len(pattern)) and (pattern[end] in string.hexdigits):
672 end = end + 1
673 if end == index:
674 raise error, "\\x must be followed by hex digit(s)"
675 # let Python evaluate it, so we don't incorrectly 2nd-guess
676 # what it's doing (and Python in turn passes it on to sscanf,
677 # so that *it* doesn't incorrectly 2nd-guess what C does!)
Guido van Rossum6af4abd1997-08-13 03:25:34 +0000678 char = eval ('"' + pattern[index-1:end] + '"')
Guido van Rossum04a1d741997-07-15 14:38:13 +0000679 assert len(char) == 1
680 return CHAR, char, end
681
682 elif pattern[index] == 'b':
683 if context != NORMAL:
684 return CHAR, chr(8), index + 1
685 else:
686 return WORD_BOUNDARY, '', index + 1
687
688 elif pattern[index] == 'B':
689 if context != NORMAL:
690 return CHAR, 'B', index + 1
691 else:
692 return NOT_WORD_BOUNDARY, '', index + 1
693
694 elif pattern[index] == 'A':
695 if context != NORMAL:
696 return CHAR, 'A', index + 1
697 else:
698 return BEGINNING_OF_BUFFER, '', index + 1
699
700 elif pattern[index] == 'Z':
701 if context != NORMAL:
Guido van Rossum6af4abd1997-08-13 03:25:34 +0000702 return CHAR, 'Z', index + 1
Guido van Rossum04a1d741997-07-15 14:38:13 +0000703 else:
704 return END_OF_BUFFER, '', index + 1
705
706 elif pattern[index] in 'GluLUQE':
Guido van Rossum6af4abd1997-08-13 03:25:34 +0000707 raise error, ('\\' + pattern[index] + ' is not allowed')
Guido van Rossum04a1d741997-07-15 14:38:13 +0000708
709 elif pattern[index] == 'w':
710 if context == NORMAL:
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000711 return SYNTAX, reop.word, index + 1
Guido van Rossum04a1d741997-07-15 14:38:13 +0000712 elif context == CHARCLASS:
713 set = []
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000714 for char in reop.syntax_table.keys():
715 if reop.syntax_table[char] & reop.word:
Guido van Rossum04a1d741997-07-15 14:38:13 +0000716 set.append(char)
717 return SET, set, index + 1
718 else:
719 return CHAR, 'w', index + 1
720
721 elif pattern[index] == 'W':
722 if context == NORMAL:
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000723 return NOT_SYNTAX, reop.word, index + 1
Guido van Rossum04a1d741997-07-15 14:38:13 +0000724 elif context == CHARCLASS:
725 set = []
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000726 for char in reop.syntax_table.keys():
727 if not reop.syntax_table[char] & reop.word:
Guido van Rossum04a1d741997-07-15 14:38:13 +0000728 set.append(char)
729 return SET, set, index + 1
730 else:
731 return CHAR, 'W', index + 1
732
733 elif pattern[index] == 's':
734 if context == NORMAL:
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000735 return SYNTAX, reop.whitespace, index + 1
Guido van Rossum04a1d741997-07-15 14:38:13 +0000736 elif context == CHARCLASS:
737 set = []
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000738 for char in reop.syntax_table.keys():
739 if reop.syntax_table[char] & reop.whitespace:
Guido van Rossum04a1d741997-07-15 14:38:13 +0000740 set.append(char)
741 return SET, set, index + 1
742 else:
743 return CHAR, 's', index + 1
744
745 elif pattern[index] == 'S':
746 if context == NORMAL:
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000747 return NOT_SYNTAX, reop.whitespace, index + 1
Guido van Rossum04a1d741997-07-15 14:38:13 +0000748 elif context == CHARCLASS:
749 set = []
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000750 for char in reop.syntax_table.keys():
Guido van Rossum827ea7c1997-08-15 15:44:23 +0000751 if not reop.syntax_table[char] & reop.whitespace:
Guido van Rossum04a1d741997-07-15 14:38:13 +0000752 set.append(char)
753 return SET, set, index + 1
754 else:
755 return CHAR, 'S', index + 1
756
757 elif pattern[index] == 'd':
758 if context == NORMAL:
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000759 return SYNTAX, reop.digit, index + 1
Guido van Rossum04a1d741997-07-15 14:38:13 +0000760 elif context == CHARCLASS:
761 set = []
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000762 for char in reop.syntax_table.keys():
763 if reop.syntax_table[char] & reop.digit:
Guido van Rossum04a1d741997-07-15 14:38:13 +0000764 set.append(char)
765 return SET, set, index + 1
766 else:
767 return CHAR, 'd', index + 1
768
769 elif pattern[index] == 'D':
770 if context == NORMAL:
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000771 return NOT_SYNTAX, reop.digit, index + 1
Guido van Rossum04a1d741997-07-15 14:38:13 +0000772 elif context == CHARCLASS:
773 set = []
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000774 for char in reop.syntax_table.keys():
775 if not reop.syntax_table[char] & reop.digit:
Guido van Rossum04a1d741997-07-15 14:38:13 +0000776 set.append(char)
777 return SET, set, index + 1
778 else:
779 return CHAR, 'D', index + 1
780
781 elif pattern[index] in '0123456789':
Guido van Rossum04a1d741997-07-15 14:38:13 +0000782
Guido van Rossum9f845ec1997-07-15 18:11:42 +0000783 if pattern[index] == '0':
784 if (index + 1 < len(pattern)) and \
785 (pattern[index + 1] in string.octdigits):
786 if (index + 2 < len(pattern)) and \
787 (pattern[index + 2] in string.octdigits):
788 value = string.atoi(pattern[index:index + 3], 8)
789 index = index + 3
790
791 else:
792 value = string.atoi(pattern[index:index + 2], 8)
793 index = index + 2
794
795 else:
796 value = 0
797 index = index + 1
798
Guido van Rossum04a1d741997-07-15 14:38:13 +0000799 if value > 255:
Guido van Rossum9f845ec1997-07-15 18:11:42 +0000800 raise error, 'octal value out of range'
Guido van Rossum04a1d741997-07-15 14:38:13 +0000801
Guido van Rossum9f845ec1997-07-15 18:11:42 +0000802 return CHAR, chr(value), index
803
Guido van Rossum04a1d741997-07-15 14:38:13 +0000804 else:
Guido van Rossum9f845ec1997-07-15 18:11:42 +0000805 if (index + 1 < len(pattern)) and \
806 (pattern[index + 1] in string.digits):
807 if (index + 2 < len(pattern)) and \
808 (pattern[index + 2] in string.octdigits) and \
809 (pattern[index + 1] in string.octdigits) and \
810 (pattern[index] in string.octdigits):
811 value = string.atoi(pattern[index:index + 3], 8)
812 if value > 255:
813 raise error, 'octal value out of range'
814
815 return CHAR, chr(value), index + 3
816
817 else:
818 value = string.atoi(pattern[index:index + 2])
819 if (value < 1) or (value > 99):
820 raise error, 'memory reference out of range'
821
822 if context == CHARCLASS:
823 raise error, ('cannot reference a register from '
824 'inside a character class')
825 return MEMORY_REFERENCE, value, index + 2
826
827 else:
828 if context == CHARCLASS:
829 raise error, ('cannot reference a register from '
830 'inside a character class')
831
832 value = string.atoi(pattern[index])
833 return MEMORY_REFERENCE, value, index + 1
834
Guido van Rossum71fa97c1997-07-18 04:26:03 +0000835 elif pattern[index] == 'g':
836 if context != REPLACEMENT:
837 return CHAR, 'g', index + 1
Guido van Rossum04a1d741997-07-15 14:38:13 +0000838
Guido van Rossum71fa97c1997-07-18 04:26:03 +0000839 index = index + 1
840 if index >= len(pattern):
841 raise error, 'unfinished symbolic reference'
842 if pattern[index] != '<':
843 raise error, 'missing < in symbolic reference'
844
845 index = index + 1
846 end = string.find(pattern, '>', index)
847 if end == -1:
848 raise error, 'unfinished symbolic reference'
849 value = pattern[index:end]
850 if not valid_identifier(value):
851 raise error, 'illegal symbolic reference'
852 return MEMORY_REFERENCE, value, end + 1
853
Guido van Rossum04a1d741997-07-15 14:38:13 +0000854 else:
855 return CHAR, pattern[index], index + 1
856
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000857def compile(pattern, flags=0):
858 stack = []
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000859 label = 0
860 register = 1
861 groupindex = {}
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000862 lastop = ''
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +0000863
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000864 # look for embedded pattern modifiers at the beginning of the pattern
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +0000865
866 index = 0
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +0000867
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000868 if len(pattern) >= 3 and \
869 (pattern[:2] == '(?') and \
870 (pattern[2] in 'iImMsSxX'):
871 index = 2
872 while (index < len(pattern)) and (pattern[index] != ')'):
873 if pattern[index] in 'iI':
874 flags = flags | IGNORECASE
875 elif pattern[index] in 'mM':
876 flags = flags | MULTILINE
877 elif pattern[index] in 'sS':
878 flags = flags | DOTALL
879 elif pattern[index] in 'xX':
880 flags = flags | VERBOSE
881 else:
882 raise error, 'unknown modifier'
883 index = index + 1
884 index = index + 1
885
886 # compile the rest of the pattern
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +0000887
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000888 while (index < len(pattern)):
889 char = pattern[index]
890 index = index + 1
891 if char == '\\':
Guido van Rossum04a1d741997-07-15 14:38:13 +0000892 escape_type, value, index = expand_escape(pattern, index)
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000893
Guido van Rossum04a1d741997-07-15 14:38:13 +0000894 if escape_type == CHAR:
Guido van Rossum95e80531997-08-13 22:34:14 +0000895 stack.append([Exact(value, flags)])
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000896 lastop = '\\' + value
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000897
Guido van Rossum04a1d741997-07-15 14:38:13 +0000898 elif escape_type == MEMORY_REFERENCE:
899 if value >= register:
900 raise error, ('cannot reference a register '
901 'not yet used')
902 stack.append([MatchMemory(value)])
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000903 lastop = '\\1'
Guido van Rossum04a1d741997-07-15 14:38:13 +0000904
905 elif escape_type == BEGINNING_OF_BUFFER:
906 stack.append([BegBuf()])
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000907 lastop = '\\A'
Guido van Rossum04a1d741997-07-15 14:38:13 +0000908
909 elif escape_type == END_OF_BUFFER:
910 stack.append([EndBuf()])
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000911 lastop = '\\Z'
Guido van Rossum04a1d741997-07-15 14:38:13 +0000912
913 elif escape_type == WORD_BOUNDARY:
914 stack.append([WordBound()])
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000915 lastop = '\\b'
Guido van Rossum04a1d741997-07-15 14:38:13 +0000916
917 elif escape_type == NOT_WORD_BOUNDARY:
918 stack.append([NotWordBound()])
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000919 lastop = '\\B'
Guido van Rossum04a1d741997-07-15 14:38:13 +0000920
921 elif escape_type == SYNTAX:
922 stack.append([SyntaxSpec(value)])
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000923 if value == reop.word:
924 lastop = '\\w'
925 elif value == reop.whitespace:
926 lastop = '\\s'
927 elif value == reop.digit:
928 lastop = '\\d'
929 else:
930 lastop = '\\?'
931
Guido van Rossum04a1d741997-07-15 14:38:13 +0000932 elif escape_type == NOT_SYNTAX:
933 stack.append([NotSyntaxSpec(value)])
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000934 if value == reop.word:
935 lastop = '\\W'
936 elif value == reop.whitespace:
937 lastop = '\\S'
938 elif value == reop.digit:
939 lastop = '\\D'
940 else:
941 lastop = '\\?'
Guido van Rossum04a1d741997-07-15 14:38:13 +0000942
943 elif escape_type == SET:
944 raise error, 'cannot use set escape type here'
945
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000946 else:
Guido van Rossum04a1d741997-07-15 14:38:13 +0000947 raise error, 'unknown escape type'
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000948
949 elif char == '|':
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000950 expr = []
Guido van Rossum04a1d741997-07-15 14:38:13 +0000951
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000952 while (len(stack) != 0) and \
953 (stack[-1][0].name != '(') and \
954 (stack[-1][0].name != '|'):
955 expr = stack[-1] + expr
956 del stack[-1]
957 stack.append([FailureJump(label)] + \
958 expr + \
959 [Jump(-1),
960 Label(label)])
Guido van Rossum8a9a4a21997-07-11 20:48:25 +0000961 stack.append([Alternation()])
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000962 label = label + 1
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000963 lastop = '|'
964
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000965 elif char == '(':
966 if index >= len(pattern):
967 raise error, 'no matching close paren'
968
969 elif pattern[index] == '?':
970 # Perl style (?...) extensions
971 index = index + 1
972 if index >= len(pattern):
973 raise error, 'extension ends prematurely'
974
975 elif pattern[index] == 'P':
976 # Python extensions
977 index = index + 1
978 if index >= len(pattern):
979 raise error, 'extension ends prematurely'
980
981 elif pattern[index] == '<':
Guido van Rossum09bcfd61997-07-15 15:38:20 +0000982 # Handle Python symbolic group names (?P<...>...)
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000983 index = index + 1
984 end = string.find(pattern, '>', index)
985 if end == -1:
986 raise error, 'no end to symbolic group name'
987 name = pattern[index:end]
Guido van Rossum09bcfd61997-07-15 15:38:20 +0000988 if not valid_identifier(name):
989 raise error, ('symbolic group name must be a '
990 'valid identifier')
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000991 index = end + 1
992 groupindex[name] = register
993 stack.append([OpenParen(register)])
994 register = register + 1
Guido van Rossuma4f1a781997-07-17 22:38:10 +0000995 lastop = '('
Guido van Rossum5ca1b711997-07-10 21:00:31 +0000996
997 elif pattern[index] == '=':
998 # backreference to symbolic group name
999 if index >= len(pattern):
1000 raise error, '(?P= at the end of the pattern'
1001 start = index + 1
1002 end = string.find(pattern, ')', start)
1003 if end == -1:
1004 raise error, 'no ) to end symbolic group name'
1005 name = pattern[start:end]
Guido van Rossum09bcfd61997-07-15 15:38:20 +00001006 if name not in groupindex.keys():
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001007 raise error, ('symbolic group name ' + name + \
1008 ' has not been used yet')
1009 stack.append([MatchMemory(groupindex[name])])
1010 index = end + 1
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001011 lastop = '(?P=)'
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001012
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001013 else:
Guido van Rossum8a9a4a21997-07-11 20:48:25 +00001014 raise error, ('unknown Python extension: ' + \
1015 pattern[index])
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001016
1017 elif pattern[index] == ':':
1018 # grouping, but no registers
1019 index = index + 1
Guido van Rossum8a9a4a21997-07-11 20:48:25 +00001020 stack.append([OpenParen(-1)])
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001021 lastop = '('
1022
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001023 elif pattern[index] == '#':
1024 # comment
1025 index = index + 1
1026 end = string.find(pattern, ')', index)
1027 if end == -1:
1028 raise error, 'no end to comment'
1029 index = end + 1
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001030 # do not change lastop
1031
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001032 elif pattern[index] == '=':
1033 raise error, ('zero-width positive lookahead '
1034 'assertion is unsupported')
1035
1036 elif pattern[index] == '!':
1037 raise error, ('zero-width negative lookahead '
1038 'assertion is unsupported')
1039
1040 elif pattern[index] in 'iImMsSxX':
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001041 raise error, ('embedded pattern modifiers are only '
1042 'allowed at the beginning of the pattern')
1043
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001044 else:
1045 raise error, 'unknown extension'
1046
1047 else:
1048 stack.append([OpenParen(register)])
1049 register = register + 1
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001050 lastop = '('
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001051
1052 elif char == ')':
1053 # make one expression out of everything on the stack up to
1054 # the marker left by the last parenthesis
1055 expr = []
1056 while (len(stack) > 0) and (stack[-1][0].name != '('):
1057 expr = stack[-1] + expr
1058 del stack[-1]
1059
1060 if len(stack) == 0:
1061 raise error, 'too many close parens'
Guido van Rossum04a1d741997-07-15 14:38:13 +00001062
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001063 # remove markers left by alternation
1064 expr = filter(lambda x: x.name != '|', expr)
1065
1066 # clean up jumps inserted by alternation
1067 need_label = 0
1068 for i in range(len(expr)):
1069 if (expr[i].name == 'jump') and (expr[i].label == -1):
Guido van Rossum04a1d741997-07-15 14:38:13 +00001070 expr[i] = Jump(label)
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001071 need_label = 1
1072 if need_label:
1073 expr.append(Label(label))
1074 label = label + 1
1075
Guido van Rossum63e18191997-07-11 11:08:38 +00001076 if stack[-1][0].register > 0:
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001077 expr = [StartMemory(stack[-1][0].register)] + \
1078 expr + \
1079 [EndMemory(stack[-1][0].register)]
1080 del stack[-1]
1081 stack.append(expr)
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001082 lastop = ')'
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001083
1084 elif char == '{':
1085 if len(stack) == 0:
1086 raise error, 'no expression to repeat'
1087 end = string.find(pattern, '}', index)
1088 if end == -1:
1089 raise error, ('no close curly bracket to match'
1090 ' open curly bracket')
1091
1092 fields = map(string.strip,
1093 string.split(pattern[index:end], ','))
1094 index = end + 1
1095
1096 minimal = 0
1097 if (index < len(pattern)) and (pattern[index] == '?'):
1098 minimal = 1
1099 index = index + 1
1100
1101 if len(fields) == 1:
1102 # {n} or {n}? (there's really no difference)
1103 try:
1104 count = string.atoi(fields[0])
1105 except ValueError:
1106 raise error, ('count must be an integer '
1107 'inside curly braces')
1108 if count > 65535:
1109 raise error, 'repeat count out of range'
1110 expr = []
1111 while count > 0:
1112 expr = expr + stack[-1]
1113 count = count - 1
1114 del stack[-1]
1115 stack.append(expr)
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001116 if minimal:
1117 lastop = '{n}?'
1118 else:
1119 lastop = '{n}'
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001120
1121 elif len(fields) == 2:
1122 # {n,} or {n,m}
1123 if fields[1] == '':
1124 # {n,}
1125 try:
1126 min = string.atoi(fields[0])
1127 except ValueError:
1128 raise error, ('minimum must be an integer '
1129 'inside curly braces')
1130 if min > 65535:
1131 raise error, 'minimum repeat count out of range'
1132
1133 expr = []
1134 while min > 0:
1135 expr = expr + stack[-1]
1136 min = min - 1
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001137 if minimal:
1138 expr = expr + \
1139 ([Jump(label + 1),
1140 Label(label)] + \
1141 stack[-1] + \
1142 [Label(label + 1),
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +00001143 FailureJump(label)])
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001144 lastop = '{n,}?'
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001145 else:
1146 expr = expr + \
1147 ([Label(label),
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +00001148 FailureJump(label + 1)] +
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001149 stack[-1] +
1150 [StarJump(label),
1151 Label(label + 1)])
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001152 lastop = '{n,}'
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001153
1154 del stack[-1]
1155 stack.append(expr)
1156 label = label + 2
1157
1158 else:
1159 # {n,m}
1160 try:
1161 min = string.atoi(fields[0])
1162 except ValueError:
1163 raise error, ('minimum must be an integer '
1164 'inside curly braces')
1165 try:
1166 max = string.atoi(fields[1])
1167 except ValueError:
1168 raise error, ('maximum must be an integer '
1169 'inside curly braces')
1170 if min > 65535:
1171 raise error, ('minumim repeat count out '
1172 'of range')
1173 if max > 65535:
1174 raise error, ('maximum repeat count out '
1175 'of range')
1176 if min > max:
1177 raise error, ('minimum repeat count must be '
1178 'less than the maximum '
1179 'repeat count')
1180 expr = []
1181 while min > 0:
1182 expr = expr + stack[-1]
1183 min = min - 1
1184 max = max - 1
1185 if minimal:
1186 while max > 0:
1187 expr = expr + \
1188 [FailureJump(label),
1189 Jump(label + 1),
1190 Label(label)] + \
1191 stack[-1] + \
1192 [Label(label + 1)]
Guido van Rossum26d80e61997-07-15 18:59:04 +00001193 max = max - 1
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001194 label = label + 2
1195 del stack[-1]
1196 stack.append(expr)
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001197 lastop = '{n,m}?'
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001198 else:
1199 while max > 0:
1200 expr = expr + \
1201 [FailureJump(label)] + \
1202 stack[-1]
1203 max = max - 1
1204 del stack[-1]
1205 stack.append(expr + [Label(label)])
1206 label = label + 1
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001207 lastop = '{n,m}'
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001208
1209 else:
1210 raise error, ('there need to be one or two fields '
1211 'in a {} expression')
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001212
1213 elif char == '}':
1214 raise error, 'unbalanced close curly brace'
1215
1216 elif char == '*':
1217 # Kleene closure
1218 if len(stack) == 0:
Guido van Rossum5d6de251997-07-11 21:10:17 +00001219 raise error, '* needs something to repeat'
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001220
1221 if lastop in ['(', '|']:
Guido van Rossum5d6de251997-07-11 21:10:17 +00001222 raise error, '* needs something to repeat'
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001223
1224 if lastop in repetition_operators:
1225 raise error, 'nested repetition operators'
1226
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001227 if (index < len(pattern)) and (pattern[index] == '?'):
1228 # non-greedy matching
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +00001229 expr = [Jump(label + 1),
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001230 Label(label)] + \
1231 stack[-1] + \
1232 [Label(label + 1),
1233 FailureJump(label)]
1234 index = index + 1
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001235 lastop = '*?'
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001236 else:
1237 # greedy matching
1238 expr = [Label(label),
1239 FailureJump(label + 1)] + \
1240 stack[-1] + \
1241 [StarJump(label),
1242 Label(label + 1)]
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001243 lastop = '*'
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001244 del stack[-1]
1245 stack.append(expr)
1246 label = label + 2
1247
1248 elif char == '+':
1249 # positive closure
1250 if len(stack) == 0:
Guido van Rossum5d6de251997-07-11 21:10:17 +00001251 raise error, '+ needs something to repeat'
Guido van Rossuma0e4c1b1997-07-17 14:52:48 +00001252
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001253 if lastop in ['(', '|']:
Guido van Rossum5d6de251997-07-11 21:10:17 +00001254 raise error, '+ needs something to repeat'
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001255
1256 if lastop in repetition_operators:
1257 raise error, 'nested repetition operators'
1258
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001259 if (index < len(pattern)) and (pattern[index] == '?'):
1260 # non-greedy
1261 expr = [Label(label)] + \
1262 stack[-1] + \
1263 [FailureJump(label)]
1264 label = label + 1
1265 index = index + 1
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001266 lastop = '+?'
1267
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001268 else:
1269 # greedy
1270 expr = [DummyFailureJump(label + 1),
1271 Label(label),
1272 FailureJump(label + 2),
1273 Label(label + 1)] + \
1274 stack[-1] + \
1275 [StarJump(label),
1276 Label(label + 2)]
1277 label = label + 3
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001278 lastop = '+'
1279
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001280 del stack[-1]
1281 stack.append(expr)
1282
1283 elif char == '?':
1284 if len(stack) == 0:
1285 raise error, 'need something to be optional'
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001286
1287 if len(stack) == 0:
1288 raise error, '? needs something to repeat'
1289
1290 if lastop in ['(', '|']:
1291 raise error, '? needs something to repeat'
1292
1293 if lastop in repetition_operators:
1294 raise error, 'nested repetition operators'
1295
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001296 if (index < len(pattern)) and (pattern[index] == '?'):
1297 # non-greedy matching
1298 expr = [FailureJump(label),
1299 Jump(label + 1),
1300 Label(label)] + \
1301 stack[-1] + \
1302 [Label(label + 1)]
1303 label = label + 2
1304 index = index + 1
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001305 lastop = '??'
1306
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001307 else:
1308 # greedy matching
1309 expr = [FailureJump(label)] + \
1310 stack[-1] + \
1311 [Label(label)]
1312 label = label + 1
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001313 lastop = '?'
1314
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001315 del stack[-1]
1316 stack.append(expr)
1317
1318 elif char == '.':
1319 if flags & DOTALL:
Guido van Rossum95e80531997-08-13 22:34:14 +00001320 stack.append([Set(map(chr, range(256)), flags)])
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001321 else:
1322 stack.append([AnyChar()])
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001323 lastop = '.'
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001324
1325 elif char == '^':
1326 if flags & MULTILINE:
1327 stack.append([Bol()])
1328 else:
1329 stack.append([BegBuf()])
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001330 lastop = '^'
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001331
1332 elif char == '$':
1333 if flags & MULTILINE:
1334 stack.append([Eol()])
1335 else:
1336 stack.append([EndBuf()])
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001337 lastop = '$'
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001338
1339 elif char == '#':
1340 if flags & VERBOSE:
1341 # comment
1342 index = index + 1
1343 end = string.find(pattern, '\n', index)
1344 if end == -1:
1345 index = len(pattern)
1346 else:
1347 index = end + 1
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001348 # do not change lastop
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001349 else:
Guido van Rossum95e80531997-08-13 22:34:14 +00001350 stack.append([Exact(char, flags)])
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001351 lastop = '#'
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001352
1353 elif char in string.whitespace:
Guido van Rossum04a1d741997-07-15 14:38:13 +00001354 if not (flags & VERBOSE):
Guido van Rossum95e80531997-08-13 22:34:14 +00001355 stack.append([Exact(char, flags)])
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001356 lastop = char
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001357
1358 elif char == '[':
Guido van Rossum09bcfd61997-07-15 15:38:20 +00001359 # compile character class
1360
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001361 if index >= len(pattern):
Guido van Rossum09bcfd61997-07-15 15:38:20 +00001362 raise error, 'unclosed character class'
1363
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001364 negate = 0
1365 last = ''
1366 set = []
Guido van Rossum04a1d741997-07-15 14:38:13 +00001367
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001368 if pattern[index] == '^':
1369 negate = 1
1370 index = index + 1
Guido van Rossum09bcfd61997-07-15 15:38:20 +00001371 if index >= len(pattern):
1372 raise error, 'unclosed character class'
Guido van Rossum04a1d741997-07-15 14:38:13 +00001373
Guido van Rossum09bcfd61997-07-15 15:38:20 +00001374 if pattern[index] == ']':
1375 set.append(']')
1376 index = index + 1
1377 if index >= len(pattern):
1378 raise error, 'unclosed character class'
1379
1380 elif pattern[index] == '-':
1381 set.append('-')
1382 index = index + 1
1383 if index >= len(pattern):
1384 raise error, 'unclosed character class'
1385
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001386 while (index < len(pattern)) and (pattern[index] != ']'):
1387 next = pattern[index]
1388 index = index + 1
1389 if next == '-':
Guido van Rossum09bcfd61997-07-15 15:38:20 +00001390 if index >= len(pattern):
1391 raise error, 'incomplete range in character class'
Guido van Rossum04a1d741997-07-15 14:38:13 +00001392
Guido van Rossum09bcfd61997-07-15 15:38:20 +00001393 elif pattern[index] == ']':
1394 set.append('-')
Guido van Rossum04a1d741997-07-15 14:38:13 +00001395
Guido van Rossum09bcfd61997-07-15 15:38:20 +00001396 else:
1397 if last == '':
1398 raise error, ('improper use of range in '
1399 'character class')
1400
1401 start = last
1402
1403 if pattern[index] == '\\':
1404 escape_type,
1405 value,
1406 index = expand_escape(pattern,
1407 index + 1,
1408 CHARCLASS)
1409
1410 if escape_type == CHAR:
1411 end = value
1412
1413 else:
1414 raise error, ('illegal escape in character '
1415 'class range')
1416 else:
1417 end = pattern[index]
1418 index = index + 1
1419
1420 if start > end:
1421 raise error, ('range arguments out of order '
1422 'in character class')
1423
1424 for char in map(chr, range(ord(start), ord(end) + 1)):
1425 if char not in set:
1426 set.append(char)
Guido van Rossum04a1d741997-07-15 14:38:13 +00001427
Guido van Rossum09bcfd61997-07-15 15:38:20 +00001428 last = ''
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001429
1430 elif next == '\\':
1431 # expand syntax meta-characters and add to set
1432 if index >= len(pattern):
1433 raise error, 'incomplete set'
Guido van Rossum04a1d741997-07-15 14:38:13 +00001434
1435 escape_type, value, index = expand_escape(pattern,
1436 index,
1437 CHARCLASS)
1438
1439 if escape_type == CHAR:
1440 set.append(value)
1441 last = value
1442
1443 elif escape_type == SET:
1444 for char in value:
1445 if char not in set:
1446 set.append(char)
1447 last = ''
1448
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001449 else:
Guido van Rossum04a1d741997-07-15 14:38:13 +00001450 raise error, 'illegal escape type in character class'
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001451
1452 else:
1453 if next not in set:
1454 set.append(next)
1455 last = next
Guido van Rossum04a1d741997-07-15 14:38:13 +00001456
1457 if (index >= len(pattern)) or ( pattern[index] != ']'):
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001458 raise error, 'incomplete set'
1459
1460 index = index + 1
1461
1462 if negate:
Guido van Rossum95e80531997-08-13 22:34:14 +00001463 # If case is being ignored, then both upper- and lowercase
1464 # versions of the letters must be excluded.
1465 if flags & IGNORECASE: set=set+map(string.upper, set)
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001466 notset = []
1467 for char in map(chr, range(256)):
1468 if char not in set:
1469 notset.append(char)
Guido van Rossum04a1d741997-07-15 14:38:13 +00001470 if len(notset) == 0:
1471 raise error, 'empty negated set'
Guido van Rossum95e80531997-08-13 22:34:14 +00001472 stack.append([Set(notset, flags)])
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001473 else:
Guido van Rossum04a1d741997-07-15 14:38:13 +00001474 if len(set) == 0:
1475 raise error, 'empty set'
Guido van Rossum95e80531997-08-13 22:34:14 +00001476 stack.append([Set(set, flags)])
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001477
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001478 lastop = '[]'
1479
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001480 else:
Guido van Rossum95e80531997-08-13 22:34:14 +00001481 stack.append([Exact(char, flags)])
Guido van Rossuma4f1a781997-07-17 22:38:10 +00001482 lastop = char
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001483
1484 code = []
1485 while len(stack) > 0:
1486 if stack[-1][0].name == '(':
1487 raise error, 'too many open parens'
1488 code = stack[-1] + code
1489 del stack[-1]
1490 if len(code) == 0:
1491 raise error, 'no code generated'
Guido van Rossum5ca1b711997-07-10 21:00:31 +00001492 code = filter(lambda x: x.name != '|', code)
1493 need_label = 0
1494 for i in range(len(code)):
1495 if (code[i].name == 'jump') and (code[i].label == -1):
1496 code[i] = Jump(label)
1497 need_label = 1
1498 if need_label:
1499 code.append(Label(label))
1500 label = label + 1
1501 code.append(End())
Guido van Rossum95e80531997-08-13 22:34:14 +00001502# print code
Guido van Rossum71fa97c1997-07-18 04:26:03 +00001503 return RegexObject(pattern, flags, code, register, groupindex)
Guido van Rossum6af4abd1997-08-13 03:25:34 +00001504
1505# Replace expand_escape and _expand functions with their C equivalents.
1506# If you suspect bugs in the C versions, comment out the next two lines
Guido van Rossumc4d6c4d1997-08-14 21:23:07 +00001507expand_escape = reop.expand_escape
1508_expand = reop._expand