diff options
author | Fredrik Lundh <fredrik@pythonware.com> | 2000-08-01 22:47:49 (GMT) |
---|---|---|
committer | Fredrik Lundh <fredrik@pythonware.com> | 2000-08-01 22:47:49 (GMT) |
commit | e186983842f0b27606b141010513fa8e3d0cc5db (patch) | |
tree | 8160cdbd00dc449a79a25cfaa6a16069b4bd74b3 /Lib/sre_compile.py | |
parent | fb06539e999271ea9b07b754d461f2172d65978b (diff) | |
download | cpython-e186983842f0b27606b141010513fa8e3d0cc5db.zip cpython-e186983842f0b27606b141010513fa8e3d0cc5db.tar.gz cpython-e186983842f0b27606b141010513fa8e3d0cc5db.tar.bz2 |
final 0.9.8 updates:
-- added REPEAT_ONE operator
-- added ANY_ALL operator (used to represent "(?s).")
Diffstat (limited to 'Lib/sre_compile.py')
-rw-r--r-- | Lib/sre_compile.py | 51 |
1 files changed, 26 insertions, 25 deletions
diff --git a/Lib/sre_compile.py b/Lib/sre_compile.py index 8fdcecf..abd619e 100644 --- a/Lib/sre_compile.py +++ b/Lib/sre_compile.py @@ -73,6 +73,13 @@ def _charset(charset, fixup=None): return out return charset +def _simple(av): + # check if av is a "simple" operator + lo, hi = av[2].getwidth() + if lo == 0: + raise error, "nothing to repeat" + return lo == hi == 1 and av[2][0][0] != SUBPATTERN + def _compile(code, pattern, flags): # internal: compile a (sub)pattern emit = code.append @@ -116,10 +123,9 @@ def _compile(code, pattern, flags): code[skip] = len(code) - skip elif op is ANY: if flags & SRE_FLAG_DOTALL: - emit(OPCODES[op]) + emit(OPCODES[ANY_ALL]) else: - emit(OPCODES[CATEGORY]) - emit(CHCODES[CATEGORY_NOT_LINEBREAK]) + emit(OPCODES[ANY]) elif op in (REPEAT, MIN_REPEAT, MAX_REPEAT): if flags & SRE_FLAG_TEMPLATE: raise error, "internal: unsupported template operator" @@ -130,30 +136,25 @@ def _compile(code, pattern, flags): _compile(code, av[2], flags) emit(OPCODES[SUCCESS]) code[skip] = len(code) - skip + elif _simple(av) and op == MAX_REPEAT: + emit(OPCODES[REPEAT_ONE]) + skip = len(code); emit(0) + emit(av[0]) + emit(av[1]) + _compile(code, av[2], flags) + emit(OPCODES[SUCCESS]) + code[skip] = len(code) - skip else: - 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> fast and wrong (but we'll fix that) - emit(OPCODES[REPEAT_ONE]) - skip = len(code); emit(0) - emit(av[0]) - emit(av[1]) - _compile(code, av[2], flags) - emit(OPCODES[SUCCESS]) - code[skip] = len(code) - skip + emit(OPCODES[REPEAT]) + skip = len(code); emit(0) + emit(av[0]) + emit(av[1]) + _compile(code, av[2], flags) + code[skip] = len(code) - skip + if op == MAX_REPEAT: + emit(OPCODES[MAX_UNTIL]) else: - emit(OPCODES[REPEAT]) - skip = len(code); emit(0) - emit(av[0]) - emit(av[1]) - _compile(code, av[2], flags) - code[skip] = len(code) - skip - if op == MAX_REPEAT: - emit(OPCODES[MAX_UNTIL]) - else: - emit(OPCODES[MIN_UNTIL]) + emit(OPCODES[MIN_UNTIL]) elif op is SUBPATTERN: if av[0]: emit(OPCODES[MARK]) |