2000-03-31 22:58:54 +08:00
|
|
|
#
|
|
|
|
# Secret Labs' Regular Expression Engine
|
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
# convert template to internal format
|
|
|
|
#
|
|
|
|
# Copyright (c) 1997-2000 by Secret Labs AB. All rights reserved.
|
|
|
|
#
|
|
|
|
# This code can only be used for 1.6 alpha testing. All other use
|
|
|
|
# require explicit permission from Secret Labs AB.
|
|
|
|
#
|
|
|
|
# Portions of this engine have been developed in cooperation with
|
|
|
|
# CNRI. Hewlett-Packard provided funding for 1.6 integration and
|
|
|
|
# other compatibility work.
|
|
|
|
#
|
|
|
|
|
|
|
|
import array, string, sys
|
|
|
|
|
|
|
|
import _sre
|
|
|
|
|
|
|
|
from sre_constants import *
|
|
|
|
|
|
|
|
# find an array type code that matches the engine's code size
|
|
|
|
for WORDSIZE in "BHil":
|
|
|
|
if len(array.array(WORDSIZE, [0]).tostring()) == _sre.getcodesize():
|
2000-04-11 01:10:48 +08:00
|
|
|
break
|
2000-03-31 22:58:54 +08:00
|
|
|
else:
|
|
|
|
raise RuntimeError, "cannot find a useable array type"
|
|
|
|
|
|
|
|
# FIXME: <fl> should move some optimizations from the parser to here!
|
|
|
|
|
|
|
|
class Code:
|
|
|
|
def __init__(self):
|
2000-04-11 01:10:48 +08:00
|
|
|
self.data = []
|
2000-03-31 22:58:54 +08:00
|
|
|
def __len__(self):
|
2000-04-11 01:10:48 +08:00
|
|
|
return len(self.data)
|
2000-03-31 22:58:54 +08:00
|
|
|
def __getitem__(self, index):
|
2000-04-11 01:10:48 +08:00
|
|
|
return self.data[index]
|
2000-03-31 22:58:54 +08:00
|
|
|
def __setitem__(self, index, code):
|
2000-04-11 01:10:48 +08:00
|
|
|
self.data[index] = code
|
2000-03-31 22:58:54 +08:00
|
|
|
def append(self, code):
|
2000-04-11 01:10:48 +08:00
|
|
|
self.data.append(code)
|
2000-03-31 22:58:54 +08:00
|
|
|
def todata(self):
|
2000-04-11 01:10:48 +08:00
|
|
|
# print self.data
|
2000-06-02 01:39:12 +08:00
|
|
|
try:
|
|
|
|
return array.array(WORDSIZE, self.data).tostring()
|
|
|
|
except OverflowError:
|
|
|
|
print self.data
|
|
|
|
raise
|
2000-03-31 22:58:54 +08:00
|
|
|
|
2000-06-02 01:39:12 +08:00
|
|
|
def _compile(code, pattern, flags, level=0):
|
2000-03-31 22:58:54 +08:00
|
|
|
append = code.append
|
|
|
|
for op, av in pattern:
|
2000-04-11 01:10:48 +08:00
|
|
|
if op is ANY:
|
2000-06-02 01:39:12 +08:00
|
|
|
if flags & SRE_FLAG_DOTALL:
|
|
|
|
append(OPCODES[op]) # any character at all!
|
2000-04-11 01:10:48 +08:00
|
|
|
else:
|
2000-06-02 01:39:12 +08:00
|
|
|
append(OPCODES[CATEGORY])
|
|
|
|
append(CHCODES[CATEGORY_NOT_LINEBREAK])
|
2000-04-11 01:10:48 +08:00
|
|
|
elif op in (SUCCESS, FAILURE):
|
2000-06-02 01:39:12 +08:00
|
|
|
append(OPCODES[op])
|
2000-04-11 01:10:48 +08:00
|
|
|
elif op is AT:
|
2000-06-02 01:39:12 +08:00
|
|
|
append(OPCODES[op])
|
|
|
|
if flags & SRE_FLAG_MULTILINE:
|
|
|
|
append(ATCODES[AT_MULTILINE[av]])
|
|
|
|
else:
|
|
|
|
append(ATCODES[av])
|
2000-04-11 01:10:48 +08:00
|
|
|
elif op is BRANCH:
|
2000-06-02 01:39:12 +08:00
|
|
|
append(OPCODES[op])
|
2000-04-11 01:10:48 +08:00
|
|
|
tail = []
|
|
|
|
for av in av[1]:
|
|
|
|
skip = len(code); append(0)
|
2000-06-02 01:39:12 +08:00
|
|
|
_compile(code, av, flags, level)
|
|
|
|
append(OPCODES[JUMP])
|
2000-04-11 01:10:48 +08:00
|
|
|
tail.append(len(code)); append(0)
|
|
|
|
code[skip] = len(code) - skip
|
|
|
|
append(0) # end of branch
|
|
|
|
for tail in tail:
|
|
|
|
code[tail] = len(code) - tail
|
|
|
|
elif op is CALL:
|
2000-06-02 01:39:12 +08:00
|
|
|
append(OPCODES[op])
|
2000-04-11 01:10:48 +08:00
|
|
|
skip = len(code); append(0)
|
2000-06-02 01:39:12 +08:00
|
|
|
_compile(code, av, flags, level+1)
|
|
|
|
append(OPCODES[SUCCESS])
|
2000-04-11 01:10:48 +08:00
|
|
|
code[skip] = len(code) - skip
|
|
|
|
elif op is CATEGORY: # not used by current parser
|
2000-06-02 01:39:12 +08:00
|
|
|
append(OPCODES[op])
|
|
|
|
if flags & SRE_FLAG_LOCALE:
|
|
|
|
append(CH_LOCALE[CHCODES[av]])
|
|
|
|
else:
|
|
|
|
append(CHCODES[av])
|
2000-04-11 01:10:48 +08:00
|
|
|
elif op is GROUP:
|
2000-06-02 01:39:12 +08:00
|
|
|
if flags & SRE_FLAG_IGNORECASE:
|
|
|
|
append(OPCODES[OP_IGNORE[op]])
|
2000-04-11 01:10:48 +08:00
|
|
|
else:
|
2000-06-02 01:39:12 +08:00
|
|
|
append(OPCODES[op])
|
|
|
|
append(av-1)
|
2000-04-11 01:10:48 +08:00
|
|
|
elif op is IN:
|
2000-06-02 01:39:12 +08:00
|
|
|
if flags & SRE_FLAG_IGNORECASE:
|
|
|
|
append(OPCODES[OP_IGNORE[op]])
|
2000-04-11 01:10:48 +08:00
|
|
|
def fixup(literal):
|
2000-06-02 01:39:12 +08:00
|
|
|
return ord(literal.lower())
|
2000-04-11 01:10:48 +08:00
|
|
|
else:
|
2000-06-02 01:39:12 +08:00
|
|
|
append(OPCODES[op])
|
2000-04-11 01:10:48 +08:00
|
|
|
fixup = ord
|
|
|
|
skip = len(code); append(0)
|
|
|
|
for op, av in av:
|
2000-06-02 01:39:12 +08:00
|
|
|
append(OPCODES[op])
|
2000-04-11 01:10:48 +08:00
|
|
|
if op is NEGATE:
|
|
|
|
pass
|
|
|
|
elif op is LITERAL:
|
|
|
|
append(fixup(av))
|
|
|
|
elif op is RANGE:
|
|
|
|
append(fixup(av[0]))
|
|
|
|
append(fixup(av[1]))
|
|
|
|
elif op is CATEGORY:
|
2000-06-02 01:39:12 +08:00
|
|
|
if flags & SRE_FLAG_LOCALE:
|
|
|
|
append(CH_LOCALE[CHCODES[av]])
|
|
|
|
else:
|
|
|
|
append(CHCODES[av])
|
2000-04-11 01:10:48 +08:00
|
|
|
else:
|
|
|
|
raise ValueError, "unsupported set operator"
|
2000-06-02 01:39:12 +08:00
|
|
|
append(OPCODES[FAILURE])
|
2000-04-11 01:10:48 +08:00
|
|
|
code[skip] = len(code) - skip
|
|
|
|
elif op in (LITERAL, NOT_LITERAL):
|
2000-06-02 01:39:12 +08:00
|
|
|
if flags & SRE_FLAG_IGNORECASE:
|
|
|
|
append(OPCODES[OP_IGNORE[op]])
|
|
|
|
append(ord(av.lower()))
|
2000-04-11 01:10:48 +08:00
|
|
|
else:
|
2000-06-02 01:39:12 +08:00
|
|
|
append(OPCODES[op])
|
2000-04-11 01:10:48 +08:00
|
|
|
append(ord(av))
|
|
|
|
elif op is MARK:
|
2000-06-02 01:39:12 +08:00
|
|
|
append(OPCODES[op])
|
2000-04-11 01:10:48 +08:00
|
|
|
append(av)
|
|
|
|
elif op in (REPEAT, MIN_REPEAT, MAX_REPEAT):
|
|
|
|
lo, hi = av[2].getwidth()
|
|
|
|
if lo == 0:
|
|
|
|
raise SyntaxError, "cannot repeat zero-width items"
|
|
|
|
if lo == hi == 1 and op is MAX_REPEAT:
|
2000-06-02 01:39:12 +08:00
|
|
|
append(OPCODES[MAX_REPEAT_ONE])
|
2000-04-11 01:10:48 +08:00
|
|
|
skip = len(code); append(0)
|
|
|
|
append(av[0])
|
|
|
|
append(av[1])
|
2000-06-02 01:39:12 +08:00
|
|
|
_compile(code, av[2], flags, level+1)
|
|
|
|
append(OPCODES[SUCCESS])
|
2000-04-11 01:10:48 +08:00
|
|
|
code[skip] = len(code) - skip
|
|
|
|
else:
|
2000-06-02 01:39:12 +08:00
|
|
|
append(OPCODES[op])
|
2000-04-11 01:10:48 +08:00
|
|
|
skip = len(code); append(0)
|
|
|
|
append(av[0])
|
|
|
|
append(av[1])
|
2000-06-02 01:39:12 +08:00
|
|
|
_compile(code, av[2], flags, level+1)
|
2000-04-11 01:10:48 +08:00
|
|
|
if op is MIN_REPEAT:
|
2000-06-02 01:39:12 +08:00
|
|
|
append(OPCODES[MIN_UNTIL])
|
2000-04-11 01:10:48 +08:00
|
|
|
else:
|
2000-06-02 01:39:12 +08:00
|
|
|
append(OPCODES[MAX_UNTIL])
|
2000-04-11 01:10:48 +08:00
|
|
|
code[skip] = len(code) - skip
|
|
|
|
elif op is SUBPATTERN:
|
2000-06-02 01:39:12 +08:00
|
|
|
group = av[0]
|
|
|
|
if group:
|
|
|
|
append(OPCODES[MARK])
|
|
|
|
append((group-1)*2)
|
|
|
|
_compile(code, av[1], flags, level+1)
|
|
|
|
if group:
|
|
|
|
append(OPCODES[MARK])
|
|
|
|
append((group-1)*2+1)
|
2000-04-11 01:10:48 +08:00
|
|
|
else:
|
|
|
|
raise ValueError, ("unsupported operand type", op)
|
2000-03-31 22:58:54 +08:00
|
|
|
|
2000-06-02 01:39:12 +08:00
|
|
|
def compile(p, flags=0):
|
2000-03-31 22:58:54 +08:00
|
|
|
# convert pattern list to internal format
|
2000-04-11 01:10:48 +08:00
|
|
|
if type(p) in (type(""), type(u"")):
|
|
|
|
import sre_parse
|
|
|
|
pattern = p
|
|
|
|
p = sre_parse.parse(p)
|
2000-03-31 22:58:54 +08:00
|
|
|
else:
|
2000-04-11 01:10:48 +08:00
|
|
|
pattern = None
|
2000-06-02 01:39:12 +08:00
|
|
|
flags = p.pattern.flags | flags
|
2000-03-31 22:58:54 +08:00
|
|
|
code = Code()
|
2000-06-02 01:39:12 +08:00
|
|
|
_compile(code, p.data, flags)
|
|
|
|
code.append(OPCODES[SUCCESS])
|
2000-03-31 22:58:54 +08:00
|
|
|
data = code.todata()
|
|
|
|
if 0: # debugging
|
2000-04-11 01:10:48 +08:00
|
|
|
print
|
|
|
|
print "-" * 68
|
|
|
|
import sre_disasm
|
|
|
|
sre_disasm.disasm(data)
|
|
|
|
print "-" * 68
|
2000-06-02 01:39:12 +08:00
|
|
|
return _sre.compile(
|
|
|
|
pattern, flags,
|
|
|
|
data,
|
|
|
|
p.pattern.groups-1, p.pattern.groupdict
|
|
|
|
)
|