-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathgenerate_tests.py
More file actions
75 lines (58 loc) · 1.95 KB
/
generate_tests.py
File metadata and controls
75 lines (58 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import json
import os
import re
from itertools import chain
from pathlib import Path
m = re.search(r"const SRE_MAGIC: usize = (\d+);", open("src/constants.rs").read())
sre_engine_magic = int(m.group(1))
del m
assert re._constants.MAGIC == sre_engine_magic
class CompiledPattern:
@classmethod
def compile(cls, pattern, flags=0):
p = re._parser.parse(pattern)
code = re._compiler._code(p, flags)
self = cls()
self.pattern = pattern
self.code = code
self.flags = re.RegexFlag(flags | p.state.flags)
return self
for k, v in re.RegexFlag.__members__.items():
setattr(CompiledPattern, k, v)
class EscapeRustStr:
hardcoded = {
ord("\r"): "\\r",
ord("\t"): "\\t",
ord("\r"): "\\r",
ord("\n"): "\\n",
ord("\\"): "\\\\",
ord("'"): "\\'",
ord('"'): '\\"',
}
@classmethod
def __class_getitem__(cls, ch):
if (rpl := cls.hardcoded.get(ch)) is not None:
return rpl
if ch in range(0x20, 0x7F):
return ch
return f"\\u{{{ch:x}}}"
def rust_str(s):
return '"' + s.translate(EscapeRustStr) + '"'
# 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 {{ pattern: {rust_str(pattern.pattern)}, code: &{json.dumps(pattern.code)} }}"
return f"""{line}
{indent}// START GENERATED by generate_tests.py
{indent}#[rustfmt::skip] let {varname} = {pattern};
{indent}// END GENERATED"""
with os.scandir("tests") as t, os.scandir("benches") as b:
for f in chain(t, b):
path = Path(f.path)
if path.suffix == ".rs":
replaced = pattern_pattern.sub(replace_compiled, path.read_text())
path.write_text(replaced)