8000 bpo-433030: Add support of atomic grouping in regular expressions by serhiy-storchaka · Pull Request #31982 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-433030: Add support of atomic grouping in regular expressions #31982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 21, 2022
Prev Previous commit
Next Next commit
Simplify the compliler code.
  • Loading branch information
serhiy-storchaka committed Mar 20, 2022
commit 9d540594481838da577c466405f89ddf3314c84a
26 changes: 9 additions & 17 deletions Lib/sre_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@
assert _sre.MAGIC == MAGIC, "SRE module mismatch"

_LITERAL_CODES = {LITERAL, NOT_LITERAL}
_REPEATING_CODES = {MIN_REPEAT, MAX_REPEAT, POSSESSIVE_REPEAT}
_SUCCESS_CODES = {SUCCESS, FAILURE}
_ASSERT_CODES = {ASSERT, ASSERT_NOT}
_UNIT_CODES = _LITERAL_CODES | {ANY, IN}

_REPEATING_CODES = {
MIN_REPEAT: (REPEAT, MIN_UNTIL, MIN_REPEAT_ONE),
MAX_REPEAT: (REPEAT, MAX_UNTIL, REPEAT_ONE),
POSSESSIVE_REPEAT: (POSSESSIVE_REPEAT, SUCCESS, POSSESSIVE_REPEAT_ONE),
}

# Sets of lowercase characters which have the same uppercase.
_equivalences = (
# LATIN SMALL LETTER I, LATIN SMALL LETTER DOTLESS I
Expand Down Expand Up @@ -138,34 +143,21 @@ def _compile(code, pattern, flags):
if flags & SRE_FLAG_TEMPLATE:
raise error("internal: unsupported template operator %r" % (op,))
if _simple(av[2]):
if op is MAX_REPEAT:
emit(REPEAT_ONE)
elif op is POSSESSIVE_REPEAT:
emit(POSSESSIVE_REPEAT_ONE)
else:
emit(MIN_REPEAT_ONE)
emit(REPEATING_CODES[op][2])
skip = _len(code); emit(0)
emit(av[0])
emit(av[1])
_compile(code, av[2], flags)
emit(SUCCESS)
code[skip] = _len(code) - skip
else:
if op is POSSESSIVE_REPEAT:
emit(POSSESSIVE_REPEAT)
else:
emit(REPEAT)
emit(REPEATING_CODES[op][0])
skip = _len(code); emit(0)
emit(av[0])
emit(av[1])
_compile(code, av[2], flags)
code[skip] = _len(code) - skip
if op is POSSESSIVE_REPEAT:
emit(SUCCESS)
elif op is MAX_REPEAT:
emit(MAX_UNTIL)
else:
emit(MIN_UNTIL)
emit(REPEATING_CODES[op][1])
elif op is SUBPATTERN:
group, add_flags, del_flags, p = av
if group:
Expand Down
0