| 1 |
""" |
| 2 |
Test for cases that cannot be produced using the Python 2.7 sre_compile |
| 3 |
module, but can be produced by other means (e.g. Python 3.5) |
| 4 |
""" |
| 5 |
|
| 6 |
from rpython.rlib.rsre import rsre_core, rsre_constants |
| 7 |
from rpython.rlib.rsre.rsre_char import MAXREPEAT |
| 8 |
from rpython.rlib.rsre.test.support import match, Position |
| 9 |
|
| 10 |
|
| 11 |
for name, value in rsre_constants.__dict__.items(): |
| 12 |
if name.startswith('OPCODE_') and isinstance(value, int): |
| 13 |
globals()[name[7:]] = value |
| 14 |
|
| 15 |
ab_plus_plus_bb = [POSSESSIVE_REPEAT, 7, 0, MAXREPEAT, LITERAL, ord('a'), LITERAL, ord('b'), SUCCESS, LITERAL, ord('b'), LITERAL, ord('b'), SUCCESS] |
| 16 |
|
| 17 |
def test_repeat_one_with_backref(): |
| 18 |
|
| 19 |
|
| 20 |
r = [MARK, 0, ANY, MARK, 1, REPEAT_ONE, 6, 0, MAXREPEAT, |
| 21 |
GROUPREF, 0, SUCCESS, SUCCESS] |
| 22 |
assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "aaa").match_end == 3 |
| 23 |
|
| 24 |
def test_min_repeat_one_with_backref(): |
| 25 |
|
| 26 |
r = [MARK, 0, ANY, MARK, 1, MIN_REPEAT_ONE, 6, 0, MAXREPEAT, |
| 27 |
GROUPREF, 0, SUCCESS, LITERAL, 98, SUCCESS] |
| 28 |
assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "aaab").match_end == 4 |
| 29 |
|
| 30 |
def test_possessive_repeat_one(): |
| 31 |
r = [POSSESSIVE_REPEAT_ONE, 6, 0, MAXREPEAT, LITERAL, ord('a'), SUCCESS, LITERAL, ord('b'), SUCCESS] |
| 32 |
assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "aaab").match_end == 4 |
| 33 |
r = [POSSESSIVE_REPEAT_ONE, 6, 0, MAXREPEAT, LITERAL, ord('a'), SUCCESS, LITERAL, ord('a'), SUCCESS] |
| 34 |
assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "aaaa") is None |
| 35 |
|
| 36 |
def test_possessive_repeat_one_min_count(): |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
r = [POSSESSIVE_REPEAT_ONE, 5, 1, MAXREPEAT, LITERAL, ord('x'), SUCCESS, SUCCESS] |
| 41 |
|
| 42 |
assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "axx") is None |
| 43 |
|
| 44 |
ctx = rsre_core.search(rsre_core.CompiledPattern(r, 0), "axx") |
| 45 |
assert ctx is not None |
| 46 |
assert ctx.match_start == 1 |
| 47 |
assert ctx.match_end == 3 |
| 48 |
|
| 49 |
assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "xx").match_end == 2 |
| 50 |
|
| 51 |
def test_possessive_repeat(): |
| 52 |
r = ab_plus_plus_bb |
| 53 |
assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "abababababbb").match_end == 12 |
| 54 |
r = [POSSESSIVE_REPEAT, 7, 0, MAXREPEAT, LITERAL, ord('a'), LITERAL, ord('b'), SUCCESS, LITERAL, ord('a'), LITERAL, ord('b'), SUCCESS] |
| 55 |
assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "abababababababab") is None |
| 56 |
r = [POSSESSIVE_REPEAT, 7, 0, MAXREPEAT, LITERAL, ord('a'), LITERAL, ord('b'), SUCCESS, LITERAL, ord('c'), SUCCESS] |
| 57 |
assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "ababababababababc") is not None |
| 58 |
assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "ababababababababc", fullmatch=True) is not None |
| 59 |
|
| 60 |
def test_atomic_group(): |
| 61 |
r = [ATOMIC_GROUP, 11, LITERAL, ord('a'), REPEAT_ONE, 6, 0, 1, LITERAL, ord('b'), SUCCESS, SUCCESS, LITERAL, ord('b'), SUCCESS] |
| 62 |
assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "abb").match_end == 3 |
| 63 |
assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "ab") is None |
| 64 |
|
| 65 |
def test_atomic_group_fullmatch_bug(): |
| 66 |
r = [ATOMIC_GROUP, 4, LITERAL, ord('t'), SUCCESS, LITERAL, ord('a'), SUCCESS] |
| 67 |
assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "ta") is not None |
| 68 |
assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "ta", fullmatch=True) is not None |
| 69 |
|
| 70 |
def test_possessive_repeat_of_atomic_group(): |
| 71 |
|
| 72 |
r = [POSSESSIVE_REPEAT, 8, 1, MAXREPEAT, ATOMIC_GROUP, 4, LITERAL, ord('x'), SUCCESS, SUCCESS, LITERAL, ord('x'), SUCCESS] |
| 73 |
assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "xxx") is None |
| 74 |
|
| 75 |
r = [ATOMIC_GROUP, 9, POSSESSIVE_REPEAT, 6, 1, MAXREPEAT, LITERAL, ord('x'), SUCCESS, SUCCESS, LITERAL, ord('x'), SUCCESS] |
| 76 |
assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "xxx") is None |
| 77 |
|
| 78 |
def test_possessive_repeat_mark(): |
| 79 |
|
| 80 |
r = [POSSESSIVE_REPEAT, 8, 1, MAXREPEAT, MARK, 0, ANY, MARK, 1, SUCCESS, ANY, SUCCESS] |
| 81 |
assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "xxx") is None |
| 82 |
|
| 83 |
def test_possesive_repeat_groups(): |
| 84 |
|
| 85 |
r = [POSSESSIVE_REPEAT, 8, 3, 3, MARK, 0, ANY, MARK, 1, SUCCESS, SUCCESS] |
| 86 |
match = rsre_core.match(rsre_core.CompiledPattern(r, 0), "abc") |
| 87 |
assert match.match_marks is not None |
| 88 |
|
| 89 |
def test_possessive_repeat_zero_width(): |
| 90 |
|
| 91 |
r = [POSSESSIVE_REPEAT, 14, 2, 4, MARK, 0, REPEAT_ONE, 6, 0, 1, LITERAL, ord('e'), SUCCESS, MARK, 1, SUCCESS, LITERAL, ord('a'), SUCCESS] |
| 92 |
match = rsre_core.match(rsre_core.CompiledPattern(r, 0), "eeea") |
| 93 |
assert match.match_marks is not None |