2000-03-31 10:58:54 -04:00
|
|
|
#
|
|
|
|
# Secret Labs' Regular Expression Engine
|
|
|
|
#
|
|
|
|
# convert template to internal format
|
|
|
|
#
|
|
|
|
# Copyright (c) 1997-2000 by Secret Labs AB. All rights reserved.
|
|
|
|
#
|
|
|
|
# Portions of this engine have been developed in cooperation with
|
|
|
|
# CNRI. Hewlett-Packard provided funding for 1.6 integration and
|
|
|
|
# other compatibility work.
|
|
|
|
#
|
|
|
|
|
2000-06-29 13:57:40 -03:00
|
|
|
import array
|
2000-03-31 10:58:54 -04:00
|
|
|
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-10 14:10:48 -03:00
|
|
|
break
|
2000-03-31 10:58:54 -04:00
|
|
|
else:
|
|
|
|
raise RuntimeError, "cannot find a useable array type"
|
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
def _compile(code, pattern, flags):
|
2000-06-29 13:57:40 -03:00
|
|
|
emit = code.append
|
2000-03-31 10:58:54 -04:00
|
|
|
for op, av in pattern:
|
2000-04-10 14:10:48 -03:00
|
|
|
if op is ANY:
|
2000-06-01 14:39:12 -03:00
|
|
|
if flags & SRE_FLAG_DOTALL:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(OPCODES[op])
|
2000-04-10 14:10:48 -03:00
|
|
|
else:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(OPCODES[CATEGORY])
|
|
|
|
emit(CHCODES[CATEGORY_NOT_LINEBREAK])
|
2000-04-10 14:10:48 -03:00
|
|
|
elif op in (SUCCESS, FAILURE):
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(OPCODES[op])
|
2000-04-10 14:10:48 -03:00
|
|
|
elif op is AT:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(OPCODES[op])
|
2000-06-01 14:39:12 -03:00
|
|
|
if flags & SRE_FLAG_MULTILINE:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(ATCODES[AT_MULTILINE[av]])
|
2000-06-01 14:39:12 -03:00
|
|
|
else:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(ATCODES[av])
|
2000-04-10 14:10:48 -03:00
|
|
|
elif op is BRANCH:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(OPCODES[op])
|
2000-04-10 14:10:48 -03:00
|
|
|
tail = []
|
|
|
|
for av in av[1]:
|
2000-06-29 13:57:40 -03:00
|
|
|
skip = len(code); emit(0)
|
2000-06-29 05:58:44 -03:00
|
|
|
_compile(code, av, flags)
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(OPCODES[JUMP])
|
|
|
|
tail.append(len(code)); emit(0)
|
2000-04-10 14:10:48 -03:00
|
|
|
code[skip] = len(code) - skip
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(0) # end of branch
|
2000-06-29 05:58:44 -03:00
|
|
|
for tail in tail:
|
2000-04-10 14:10:48 -03:00
|
|
|
code[tail] = len(code) - tail
|
|
|
|
elif op is CALL:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(OPCODES[op])
|
|
|
|
skip = len(code); emit(0)
|
2000-06-29 05:58:44 -03:00
|
|
|
_compile(code, av, flags)
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(OPCODES[SUCCESS])
|
2000-04-10 14:10:48 -03:00
|
|
|
code[skip] = len(code) - skip
|
2000-06-29 05:58:44 -03:00
|
|
|
elif op is CATEGORY:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(OPCODES[op])
|
2000-06-01 14:39:12 -03:00
|
|
|
if flags & SRE_FLAG_LOCALE:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(CH_LOCALE[CHCODES[av]])
|
2000-06-29 05:58:44 -03:00
|
|
|
elif flags & SRE_FLAG_UNICODE:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(CH_UNICODE[CHCODES[av]])
|
2000-06-01 14:39:12 -03:00
|
|
|
else:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(CHCODES[av])
|
2000-04-10 14:10:48 -03:00
|
|
|
elif op is GROUP:
|
2000-06-01 14:39:12 -03:00
|
|
|
if flags & SRE_FLAG_IGNORECASE:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(OPCODES[OP_IGNORE[op]])
|
2000-04-10 14:10:48 -03:00
|
|
|
else:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(OPCODES[op])
|
|
|
|
emit(av-1)
|
2000-04-10 14:10:48 -03:00
|
|
|
elif op is IN:
|
2000-06-01 14:39:12 -03:00
|
|
|
if flags & SRE_FLAG_IGNORECASE:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(OPCODES[OP_IGNORE[op]])
|
2000-06-29 05:58:44 -03:00
|
|
|
def fixup(literal, flags=flags):
|
|
|
|
return _sre.getlower(ord(literal), flags)
|
2000-04-10 14:10:48 -03:00
|
|
|
else:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(OPCODES[op])
|
2000-04-10 14:10:48 -03:00
|
|
|
fixup = ord
|
2000-06-29 13:57:40 -03:00
|
|
|
skip = len(code); emit(0)
|
2000-04-10 14:10:48 -03:00
|
|
|
for op, av in av:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(OPCODES[op])
|
2000-04-10 14:10:48 -03:00
|
|
|
if op is NEGATE:
|
|
|
|
pass
|
|
|
|
elif op is LITERAL:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(fixup(av))
|
2000-04-10 14:10:48 -03:00
|
|
|
elif op is RANGE:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(fixup(av[0]))
|
|
|
|
emit(fixup(av[1]))
|
2000-04-10 14:10:48 -03:00
|
|
|
elif op is CATEGORY:
|
2000-06-01 14:39:12 -03:00
|
|
|
if flags & SRE_FLAG_LOCALE:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(CH_LOCALE[CHCODES[av]])
|
2000-06-29 05:58:44 -03:00
|
|
|
elif flags & SRE_FLAG_UNICODE:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(CH_UNICODE[CHCODES[av]])
|
2000-06-01 14:39:12 -03:00
|
|
|
else:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(CHCODES[av])
|
2000-04-10 14:10:48 -03:00
|
|
|
else:
|
2000-06-29 13:57:40 -03:00
|
|
|
raise error, "internal: unsupported set operator"
|
|
|
|
emit(OPCODES[FAILURE])
|
2000-04-10 14:10:48 -03:00
|
|
|
code[skip] = len(code) - skip
|
|
|
|
elif op in (LITERAL, NOT_LITERAL):
|
2000-06-01 14:39:12 -03:00
|
|
|
if flags & SRE_FLAG_IGNORECASE:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(OPCODES[OP_IGNORE[op]])
|
2000-04-10 14:10:48 -03:00
|
|
|
else:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(OPCODES[op])
|
|
|
|
emit(ord(av))
|
2000-04-10 14:10:48 -03:00
|
|
|
elif op is MARK:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(OPCODES[op])
|
|
|
|
emit(av)
|
2000-04-10 14:10:48 -03:00
|
|
|
elif op in (REPEAT, MIN_REPEAT, MAX_REPEAT):
|
2000-06-29 05:58:44 -03:00
|
|
|
if flags & SRE_FLAG_TEMPLATE:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(OPCODES[REPEAT])
|
|
|
|
skip = len(code); emit(0)
|
|
|
|
emit(av[0])
|
|
|
|
emit(av[1])
|
2000-06-29 05:58:44 -03:00
|
|
|
_compile(code, av[2], flags)
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(OPCODES[SUCCESS])
|
2000-04-10 14:10:48 -03:00
|
|
|
code[skip] = len(code) - skip
|
|
|
|
else:
|
2000-06-29 05:58:44 -03:00
|
|
|
lo, hi = av[2].getwidth()
|
|
|
|
if lo == 0:
|
|
|
|
raise error, "nothing to repeat"
|
|
|
|
if 0 and lo == hi == 1 and op is MAX_REPEAT:
|
|
|
|
# FIXME: <fl> need a better way to figure out when
|
|
|
|
# it's safe to use this one (in the parser, probably)
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(OPCODES[MAX_REPEAT_ONE])
|
|
|
|
skip = len(code); emit(0)
|
|
|
|
emit(av[0])
|
|
|
|
emit(av[1])
|
2000-06-29 05:58:44 -03:00
|
|
|
_compile(code, av[2], flags)
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(OPCODES[SUCCESS])
|
2000-06-29 05:58:44 -03:00
|
|
|
code[skip] = len(code) - skip
|
2000-04-10 14:10:48 -03:00
|
|
|
else:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(OPCODES[op])
|
|
|
|
skip = len(code); emit(0)
|
|
|
|
emit(av[0])
|
|
|
|
emit(av[1])
|
2000-06-29 05:58:44 -03:00
|
|
|
_compile(code, av[2], flags)
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(OPCODES[SUCCESS])
|
2000-06-29 05:58:44 -03:00
|
|
|
code[skip] = len(code) - skip
|
2000-04-10 14:10:48 -03:00
|
|
|
elif op is SUBPATTERN:
|
2000-06-01 14:39:12 -03:00
|
|
|
group = av[0]
|
|
|
|
if group:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(OPCODES[MARK])
|
|
|
|
emit((group-1)*2)
|
2000-06-29 05:58:44 -03:00
|
|
|
_compile(code, av[1], flags)
|
2000-06-01 14:39:12 -03:00
|
|
|
if group:
|
2000-06-29 13:57:40 -03:00
|
|
|
emit(OPCODES[MARK])
|
|
|
|
emit((group-1)*2+1)
|
2000-04-10 14:10:48 -03:00
|
|
|
else:
|
|
|
|
raise ValueError, ("unsupported operand type", op)
|
2000-03-31 10:58:54 -04:00
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
def compile(p, flags=0):
|
2000-06-29 13:57:40 -03:00
|
|
|
# internal: convert pattern list to internal format
|
2000-04-10 14:10:48 -03:00
|
|
|
if type(p) in (type(""), type(u"")):
|
|
|
|
import sre_parse
|
|
|
|
pattern = p
|
|
|
|
p = sre_parse.parse(p)
|
2000-03-31 10:58:54 -04:00
|
|
|
else:
|
2000-04-10 14:10:48 -03:00
|
|
|
pattern = None
|
2000-06-01 14:39:12 -03:00
|
|
|
flags = p.pattern.flags | flags
|
2000-06-29 13:57:40 -03:00
|
|
|
code = []
|
2000-06-01 14:39:12 -03:00
|
|
|
_compile(code, p.data, flags)
|
|
|
|
code.append(OPCODES[SUCCESS])
|
2000-06-29 13:57:40 -03:00
|
|
|
# FIXME: <fl> get rid of this limitation
|
|
|
|
assert p.pattern.groups <= 100,\
|
|
|
|
"sorry, but this version only supports 100 named groups"
|
2000-06-01 14:39:12 -03:00
|
|
|
return _sre.compile(
|
|
|
|
pattern, flags,
|
2000-06-29 13:57:40 -03:00
|
|
|
array.array(WORDSIZE, code).tostring(),
|
2000-06-01 14:39:12 -03:00
|
|
|
p.pattern.groups-1, p.pattern.groupdict
|
|
|
|
)
|