mirror of
https://github.com/python/cpython.git
synced 2025-08-30 13:38:43 +00:00
Issue #22434: Constants in sre_constants are now named constants (enum-like).
This commit is contained in:
parent
bf764a1912
commit
c7f7d3897e
4 changed files with 127 additions and 186 deletions
|
@ -13,7 +13,6 @@
|
|||
import _sre
|
||||
import sre_parse
|
||||
from sre_constants import *
|
||||
from _sre import MAXREPEAT
|
||||
|
||||
assert _sre.MAGIC == MAGIC, "SRE module mismatch"
|
||||
|
||||
|
@ -38,65 +37,65 @@ def _compile(code, pattern, flags):
|
|||
for op, av in pattern:
|
||||
if op in LITERAL_CODES:
|
||||
if flags & SRE_FLAG_IGNORECASE:
|
||||
emit(OPCODES[OP_IGNORE[op]])
|
||||
emit(OP_IGNORE[op])
|
||||
emit(_sre.getlower(av, flags))
|
||||
else:
|
||||
emit(OPCODES[op])
|
||||
emit(op)
|
||||
emit(av)
|
||||
elif op is IN:
|
||||
if flags & SRE_FLAG_IGNORECASE:
|
||||
emit(OPCODES[OP_IGNORE[op]])
|
||||
emit(OP_IGNORE[op])
|
||||
def fixup(literal, flags=flags):
|
||||
return _sre.getlower(literal, flags)
|
||||
else:
|
||||
emit(OPCODES[op])
|
||||
emit(op)
|
||||
fixup = None
|
||||
skip = _len(code); emit(0)
|
||||
_compile_charset(av, flags, code, fixup)
|
||||
code[skip] = _len(code) - skip
|
||||
elif op is ANY:
|
||||
if flags & SRE_FLAG_DOTALL:
|
||||
emit(OPCODES[ANY_ALL])
|
||||
emit(ANY_ALL)
|
||||
else:
|
||||
emit(OPCODES[ANY])
|
||||
emit(ANY)
|
||||
elif op in REPEATING_CODES:
|
||||
if flags & SRE_FLAG_TEMPLATE:
|
||||
raise error("internal: unsupported template operator")
|
||||
elif _simple(av) and op is not REPEAT:
|
||||
if op is MAX_REPEAT:
|
||||
emit(OPCODES[REPEAT_ONE])
|
||||
emit(REPEAT_ONE)
|
||||
else:
|
||||
emit(OPCODES[MIN_REPEAT_ONE])
|
||||
emit(MIN_REPEAT_ONE)
|
||||
skip = _len(code); emit(0)
|
||||
emit(av[0])
|
||||
emit(av[1])
|
||||
_compile(code, av[2], flags)
|
||||
emit(OPCODES[SUCCESS])
|
||||
emit(SUCCESS)
|
||||
code[skip] = _len(code) - skip
|
||||
else:
|
||||
emit(OPCODES[REPEAT])
|
||||
emit(REPEAT)
|
||||
skip = _len(code); emit(0)
|
||||
emit(av[0])
|
||||
emit(av[1])
|
||||
_compile(code, av[2], flags)
|
||||
code[skip] = _len(code) - skip
|
||||
if op is MAX_REPEAT:
|
||||
emit(OPCODES[MAX_UNTIL])
|
||||
emit(MAX_UNTIL)
|
||||
else:
|
||||
emit(OPCODES[MIN_UNTIL])
|
||||
emit(MIN_UNTIL)
|
||||
elif op is SUBPATTERN:
|
||||
if av[0]:
|
||||
emit(OPCODES[MARK])
|
||||
emit(MARK)
|
||||
emit((av[0]-1)*2)
|
||||
# _compile_info(code, av[1], flags)
|
||||
_compile(code, av[1], flags)
|
||||
if av[0]:
|
||||
emit(OPCODES[MARK])
|
||||
emit(MARK)
|
||||
emit((av[0]-1)*2+1)
|
||||
elif op in SUCCESS_CODES:
|
||||
emit(OPCODES[op])
|
||||
emit(op)
|
||||
elif op in ASSERT_CODES:
|
||||
emit(OPCODES[op])
|
||||
emit(op)
|
||||
skip = _len(code); emit(0)
|
||||
if av[0] >= 0:
|
||||
emit(0) # look ahead
|
||||
|
@ -106,57 +105,57 @@ def _compile(code, pattern, flags):
|
|||
raise error("look-behind requires fixed-width pattern")
|
||||
emit(lo) # look behind
|
||||
_compile(code, av[1], flags)
|
||||
emit(OPCODES[SUCCESS])
|
||||
emit(SUCCESS)
|
||||
code[skip] = _len(code) - skip
|
||||
elif op is CALL:
|
||||
emit(OPCODES[op])
|
||||
emit(op)
|
||||
skip = _len(code); emit(0)
|
||||
_compile(code, av, flags)
|
||||
emit(OPCODES[SUCCESS])
|
||||
emit(SUCCESS)
|
||||
code[skip] = _len(code) - skip
|
||||
elif op is AT:
|
||||
emit(OPCODES[op])
|
||||
emit(op)
|
||||
if flags & SRE_FLAG_MULTILINE:
|
||||
av = AT_MULTILINE.get(av, av)
|
||||
if flags & SRE_FLAG_LOCALE:
|
||||
av = AT_LOCALE.get(av, av)
|
||||
elif flags & SRE_FLAG_UNICODE:
|
||||
av = AT_UNICODE.get(av, av)
|
||||
emit(ATCODES[av])
|
||||
emit(av)
|
||||
elif op is BRANCH:
|
||||
emit(OPCODES[op])
|
||||
emit(op)
|
||||
tail = []
|
||||
tailappend = tail.append
|
||||
for av in av[1]:
|
||||
skip = _len(code); emit(0)
|
||||
# _compile_info(code, av, flags)
|
||||
_compile(code, av, flags)
|
||||
emit(OPCODES[JUMP])
|
||||
emit(JUMP)
|
||||
tailappend(_len(code)); emit(0)
|
||||
code[skip] = _len(code) - skip
|
||||
emit(0) # end of branch
|
||||
for tail in tail:
|
||||
code[tail] = _len(code) - tail
|
||||
elif op is CATEGORY:
|
||||
emit(OPCODES[op])
|
||||
emit(op)
|
||||
if flags & SRE_FLAG_LOCALE:
|
||||
av = CH_LOCALE[av]
|
||||
elif flags & SRE_FLAG_UNICODE:
|
||||
av = CH_UNICODE[av]
|
||||
emit(CHCODES[av])
|
||||
emit(av)
|
||||
elif op is GROUPREF:
|
||||
if flags & SRE_FLAG_IGNORECASE:
|
||||
emit(OPCODES[OP_IGNORE[op]])
|
||||
emit(OP_IGNORE[op])
|
||||
else:
|
||||
emit(OPCODES[op])
|
||||
emit(op)
|
||||
emit(av-1)
|
||||
elif op is GROUPREF_EXISTS:
|
||||
emit(OPCODES[op])
|
||||
emit(op)
|
||||
emit(av[0]-1)
|
||||
skipyes = _len(code); emit(0)
|
||||
_compile(code, av[1], flags)
|
||||
if av[2]:
|
||||
emit(OPCODES[JUMP])
|
||||
emit(JUMP)
|
||||
skipno = _len(code); emit(0)
|
||||
code[skipyes] = _len(code) - skipyes + 1
|
||||
_compile(code, av[2], flags)
|
||||
|
@ -170,7 +169,7 @@ def _compile_charset(charset, flags, code, fixup=None):
|
|||
# compile charset subprogram
|
||||
emit = code.append
|
||||
for op, av in _optimize_charset(charset, fixup):
|
||||
emit(OPCODES[op])
|
||||
emit(op)
|
||||
if op is NEGATE:
|
||||
pass
|
||||
elif op is LITERAL:
|
||||
|
@ -184,14 +183,14 @@ def _compile_charset(charset, flags, code, fixup=None):
|
|||
code.extend(av)
|
||||
elif op is CATEGORY:
|
||||
if flags & SRE_FLAG_LOCALE:
|
||||
emit(CHCODES[CH_LOCALE[av]])
|
||||
emit(CH_LOCALE[av])
|
||||
elif flags & SRE_FLAG_UNICODE:
|
||||
emit(CHCODES[CH_UNICODE[av]])
|
||||
emit(CH_UNICODE[av])
|
||||
else:
|
||||
emit(CHCODES[av])
|
||||
emit(av)
|
||||
else:
|
||||
raise error("internal: unsupported set operator")
|
||||
emit(OPCODES[FAILURE])
|
||||
emit(FAILURE)
|
||||
|
||||
def _optimize_charset(charset, fixup):
|
||||
# internal: optimize character set
|
||||
|
@ -414,7 +413,7 @@ def _compile_info(code, pattern, flags):
|
|||
## print "*** CHARSET", charset
|
||||
# add an info block
|
||||
emit = code.append
|
||||
emit(OPCODES[INFO])
|
||||
emit(INFO)
|
||||
skip = len(code); emit(0)
|
||||
# literal flag
|
||||
mask = 0
|
||||
|
@ -460,7 +459,7 @@ def _code(p, flags):
|
|||
# compile the pattern
|
||||
_compile(code, p.data, flags)
|
||||
|
||||
code.append(OPCODES[SUCCESS])
|
||||
code.append(SUCCESS)
|
||||
|
||||
return code
|
||||
|
||||
|
@ -475,7 +474,7 @@ def compile(p, flags=0):
|
|||
|
||||
code = _code(p, flags)
|
||||
|
||||
# print code
|
||||
# print(code)
|
||||
|
||||
# map in either direction
|
||||
groupindex = p.pattern.groupdict
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue