forked from Rust-related/RustPython
Merge pull request #4 from RustPython/codegen-inline
Have generate_tests.py generate Patterns inline in tests.rs
This commit is contained in:
@@ -26,12 +26,21 @@ class CompiledPattern:
|
||||
for k, v in re.RegexFlag.__members__.items():
|
||||
setattr(CompiledPattern, k, v)
|
||||
|
||||
|
||||
# matches `// pattern {varname} = re.compile(...)`
|
||||
pattern_pattern = re.compile(r"^((\s*)\/\/\s*pattern\s+(\w+)\s+=\s+(.+?))$(?:.+?END GENERATED)?", re.M | re.S)
|
||||
def replace_compiled(m):
|
||||
line, indent, varname, pattern = m.groups()
|
||||
pattern = eval(pattern, {"re": CompiledPattern})
|
||||
pattern = f"Pattern {{ code: &{json.dumps(pattern.code)}, flags: SreFlag::from_bits_truncate({int(pattern.flags)}) }}"
|
||||
return f'''{line}
|
||||
{indent}// START GENERATED by generate_tests.py
|
||||
{indent}#[rustfmt::skip] let {varname} = {pattern};
|
||||
{indent}// END GENERATED'''
|
||||
|
||||
with os.scandir("tests") as d:
|
||||
for f in d:
|
||||
path = Path(f.path)
|
||||
if path.suffix == ".py":
|
||||
pattern = eval(path.read_text(), {"re": CompiledPattern})
|
||||
path.with_suffix(".re").write_text(
|
||||
f"// {pattern.pattern!r}, flags={pattern.flags!r}\n"
|
||||
f"Pattern {{ code: &{json.dumps(pattern.code)}, flags: SreFlag::from_bits_truncate({int(pattern.flags)}) }}"
|
||||
)
|
||||
if path.suffix == ".rs":
|
||||
replaced = pattern_pattern.sub(replace_compiled, path.read_text())
|
||||
path.write_text(replaced)
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
re.compile(r'(?<!\.)x\b')
|
||||
@@ -1,2 +0,0 @@
|
||||
// '(?<!\\.)x\\b', flags=re.UNICODE
|
||||
Pattern { code: &[15, 4, 0, 1, 1, 5, 5, 1, 17, 46, 1, 17, 120, 6, 10, 1], flags: SreFlag::from_bits_truncate(32) }
|
||||
@@ -1 +0,0 @@
|
||||
re.compile(r'(?<=abc)def')
|
||||
@@ -1,2 +0,0 @@
|
||||
// '(?<=abc)def', flags=re.UNICODE
|
||||
Pattern { code: &[15, 4, 0, 3, 3, 4, 9, 3, 17, 97, 17, 98, 17, 99, 1, 17, 100, 17, 101, 17, 102, 1], flags: SreFlag::from_bits_truncate(32) }
|
||||
@@ -18,18 +18,22 @@ impl Pattern {
|
||||
|
||||
#[test]
|
||||
fn test_2427() {
|
||||
// r'(?<!\.)x\b'
|
||||
let pattern = include!("lookbehind.re");
|
||||
let mut state = pattern.state("x", 0..usize::MAX);
|
||||
// pattern lookbehind = re.compile(r'(?<!\.)x\b')
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let lookbehind = Pattern { code: &[15, 4, 0, 1, 1, 5, 5, 1, 17, 46, 1, 17, 120, 6, 10, 1], flags: SreFlag::from_bits_truncate(32) };
|
||||
// END GENERATED
|
||||
let mut state = lookbehind.state("x", 0..usize::MAX);
|
||||
state = state.pymatch();
|
||||
assert!(state.has_matched == Some(true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_assert() {
|
||||
// '(?<=abc)def', flags=re.UNICODE
|
||||
let pattern = include!("positive_lookbehind.re");
|
||||
let mut state = pattern.state("abcdef", 0..usize::MAX);
|
||||
// pattern positive_lookbehind = re.compile(r'(?<=abc)def')
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let positive_lookbehind = Pattern { code: &[15, 4, 0, 3, 3, 4, 9, 3, 17, 97, 17, 98, 17, 99, 1, 17, 100, 17, 101, 17, 102, 1], flags: SreFlag::from_bits_truncate(32) };
|
||||
// END GENERATED
|
||||
let mut state = positive_lookbehind.state("abcdef", 0..usize::MAX);
|
||||
state = state.search();
|
||||
assert!(state.has_matched == Some(true));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user