8000 Simplify the compliler code. · python/cpython@9d54059 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9d54059

Browse files
Simplify the compliler code.
1 parent 1ff76c3 commit 9d54059

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

Lib/sre_compile.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@
1717
assert _sre.MAGIC == MAGIC, "SRE module mismatch"
1818

1919
_LITERAL_CODES = {LITERAL, NOT_LITERAL}
20-
_REPEATING_CODES = {MIN_REPEAT, MAX_REPEAT, POSSESSIVE_REPEAT}
2120
_SUCCESS_CODES = {SUCCESS, FAILURE}
2221
_ASSERT_CODES = {ASSERT, ASSERT_NOT}
2322
_UNIT_CODES = _LITERAL_CODES | {ANY, IN}
2423

24+
_REPEATING_CODES = {
25+
MIN_REPEAT: (REPEAT, MIN_UNTIL, MIN_REPEAT_ONE),
26+
MAX_REPEAT: (REPEAT, MAX_UNTIL, REPEAT_ONE),
27+
POSSESSIVE_REPEAT: (POSSESSIVE_REPEAT, SUCCESS, POSSESSIVE_REPEAT_ONE),
28+
}
29+
2530
# Sets of lowercase characters which have the same uppercase.
2631
_equivalences = (
2732
# LATIN SMALL LETTER I, LATIN SMALL LETTER DOTLESS I
@@ -138,34 +143,21 @@ def _compile(code, pattern, flags):
138143
if flags & SRE_FLAG_TEMPLATE:
139144
raise error("internal: unsupported template operator %r" % (op,))
140145
if _simple(av[2]):
141-
if op is MAX_REPEAT:
142-
emit(REPEAT_ONE)
143-
elif op is POSSESSIVE_REPEAT:
144-
emit(POSSESSIVE_REPEAT_ONE)
145-
else:
146-
emit(MIN_REPEAT_ONE)
146+
emit(REPEATING_CODES[op][2])
147147
skip = _len(code); emit(0)
148148
emit(av[0])
149149
emit(av[1])
150150
_compile(code, av[2], flags)
151151
emit(SUCCESS)
152152
code[skip] = _len(code) - skip
153153
else:
154-
if op is POSSESSIVE_REPEAT:
155-
emit(POSSESSIVE_REPEAT)
156-
else:
157-
emit(REPEAT)
154+
emit(REPEATING_CODES[op][0])
158155
skip = _len(code); emit(0)
159156
emit(av[0])
160157
emit(av[1])
161158
_compile(code, av[2], flags)
162159
code[skip] = _len(code) - skip
163-
if op is POSSESSIVE_REPEAT:
164-
emit(SUCCESS)
165-
elif op is MAX_REPEAT:
166-
emit(MAX_UNTIL)
167-
else:
168-
emit(MIN_UNTIL)
160+
emit(REPEATING_CODES[op][1])
169161
elif op is SUBPATTERN:
170162
group, add_flags, del_flags, p = av
171163
if group:

0 commit comments

Comments
 (0)
0