Skip to content

Commit dfaa089

Browse files
committed
Adjust scripts/generate_opcode_metadata.py
1 parent 58419b1 commit dfaa089

File tree

1 file changed

+33
-37
lines changed

1 file changed

+33
-37
lines changed

scripts/generate_opcode_metadata.py

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -85,48 +85,44 @@ def extract_enum_body(text: str, name: str) -> str:
8585
return text[start + 1 : i]
8686

8787

88-
def build_deopts(contents: str) -> dict[str, list[str]]:
89-
raw_body = re.search(
90-
r"fn deopt\(self\) -> Option<Self>(.*)", contents, re.DOTALL
91-
).group(1)
92-
body = "\n".join(
93-
itertools.takewhile(
94-
lambda l: not l.startswith("_ =>"), # Take until reaching fallback
95-
filter(
96-
lambda l: (
97-
not l.startswith(
98-
("//", "Some(match")
99-
) # Skip comments or start of match
100-
),
101-
map(str.strip, raw_body.splitlines()),
102-
),
103-
)
104-
).removeprefix("{")
88+
def build_deopts(text: str) -> dict[str, list[str]]:
89+
raw_body = re.search(r"fn deopt\(self\)(.*)", text, re.DOTALL).group(1)
90+
match_start = raw_body.find("match self")
91+
if match_start == -1:
92+
raise ValueError("Could not detect a match statement in deopt method")
10593

106-
depth = 0
107-
arms = []
108-
buf = []
109-
for char in body:
110-
if char == "{":
111-
depth += 1
112-
elif char == "}":
113-
depth -= 1
94+
brace_depth = 0
95+
block_start = None
96+
block_end = None
11497

115-
if depth == 0 and (char in ("}", ",")):
116-
arm = "".join(buf).strip()
117-
arms.append(arm)
118-
buf = []
119-
else:
120-
buf.append(char)
98+
for i, ch in enumerate(raw_body[match_start:], match_start):
99+
if ch == "{":
100+
brace_depth += 1
101+
if block_start is None:
102+
block_start = i + 1
103+
elif ch == "}":
104+
brace_depth -= 1
105+
if brace_depth == 0:
106+
block_end = i
107+
break
108+
109+
match_body = raw_body[block_start:block_end]
121110

122-
# last arm
123-
arms.append("".join(buf))
124-
arms = [arm for arm in arms if arm]
111+
arm_pattern = re.compile(
112+
r"((?:Self::\w+\s*\|\s*)*Self::\w+)\s*=>\s*(?:\{\s*)?Opcode::(\w+)", re.DOTALL
113+
)
114+
variants_pattern = re.compile(r"Self::(\w+)")
125115

126116
deopts = {}
127-
for arm in arms:
128-
*specialized, deopt = map(to_snake_case, re.findall(r"Self::(\w*)\b", arm))
129-
deopts[deopt] = specialized
117+
for hit in arm_pattern.finditer(match_body):
118+
raw_variants = hit.group(1)
119+
opcode = hit.group(2)
120+
121+
variants = variants_pattern.findall(raw_variants)
122+
123+
key = to_snake_case(opcode)
124+
value = [to_snake_case(variant) for variant in variants]
125+
deopts[key] = value
130126

131127
return deopts
132128

0 commit comments

Comments
 (0)