feat: type_fields in diff and tracker, joined with emulator property reads
The tracker lists each scope's original properties (constructor chain, or class fields up the chain for Piklib 6.1 and singletons such as SCENE) and where the emulator reads them: a CNVParser.createVariable case arm, an attribute read in the type's class or Variable.java, or other code. The switch-arm parser is shared with the @ statements. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
9d5a600d6b
commit
aee8f17417
+128
-21
@@ -89,6 +89,63 @@ def scan_signals(root: Path, source_root: Path, variable_root: Path) -> dict:
|
||||
return scopes
|
||||
|
||||
|
||||
def switch_arms(text: str, code: str, head: re.Pattern) -> list[tuple[list[str], int, int]]:
|
||||
"""[(case labels, start, end)] of the first `switch` inside the method `head` matches.
|
||||
Positions index `text`; `code` is its literal/comment mask. Default arms are skipped."""
|
||||
found = head.search(code)
|
||||
if found is None:
|
||||
return []
|
||||
switch = code.index("switch", found.end())
|
||||
body_start = code.index("{", switch)
|
||||
body_end = closing(code, body_start, "{}")
|
||||
arms = list(CASE_ARM.finditer(code, body_start, body_end))
|
||||
out = []
|
||||
for arm, following in zip(arms, arms[1:] + [None]):
|
||||
if arm.group(1) is not None:
|
||||
labels = re.findall(r'"([A-Za-z_]+)"', text[arm.start(1):arm.end(1)])
|
||||
out.append((labels, arm.start(), following.start() if following else body_end))
|
||||
return out
|
||||
|
||||
|
||||
CNV_SOURCE = "pl/genschu/bloomooemulator/loader/CNVParser.java"
|
||||
CNV_CREATE = re.compile(r"\bcreateVariable\s*\([^)]*\)\s*\{")
|
||||
PROPERTY_KEY = re.compile(r'":([A-Z][A-Z0-9_]*)"')
|
||||
ATTRIBUTE_NAME = re.compile(r'"([A-Z][A-Z0-9_]*)"')
|
||||
# context.getAttribute(name, "X"), getAttributeInHierarchy(name(), "X"), attributes().get(name, "X")
|
||||
ATTRIBUTE_CALL = re.compile(r"(?:\bgetAttribute\w*|\battributes\(\)\s*\.\s*get\w*)\s*\([^;]*$")
|
||||
|
||||
|
||||
def scan_properties(root: Path, source_root: Path, variable_root: Path) -> dict:
|
||||
"""Where the emulator reads each CNV property: {scope: {property: [source]}}. A `":NAME"`
|
||||
key inside a `case "TYPE"` arm of CNVParser.createVariable belongs to those types, and
|
||||
an attribute read (`getAttribute`, `getAttributeInHierarchy`, `attributes().get`) in
|
||||
`<Type>Variable.java` to that type, in Variable.java to `*`; anything else is `engine`.
|
||||
Like signals, absence means only that no literal was found."""
|
||||
scopes = {}
|
||||
for path in sorted(source_root.rglob("*.java")):
|
||||
text = path.read_text(encoding="utf-8")
|
||||
code = code_mask(text)
|
||||
scope = "engine"
|
||||
if path.parent == variable_root and path.name == "Variable.java":
|
||||
scope = "*"
|
||||
elif path.parent == variable_root and path.name.endswith("Variable.java"):
|
||||
match = re.search(r"return\s+VariableType\.([A-Z_]+)\s*;", code)
|
||||
scope = canonical(match[1]) if match else "engine"
|
||||
arms = switch_arms(text, code, CNV_CREATE) if path == source_root / CNV_SOURCE else []
|
||||
for token in JAVA_TOKENS.finditer(text):
|
||||
literal = PROPERTY_KEY.fullmatch(token.group())
|
||||
if literal is None:
|
||||
literal = ATTRIBUTE_NAME.fullmatch(token.group())
|
||||
if literal is None or not ATTRIBUTE_CALL.search(code[max(0, token.start() - 160):token.start()]):
|
||||
continue
|
||||
owners = [canonical(label) for labels, start, end in arms if start <= token.start() < end
|
||||
for label in labels] or [scope]
|
||||
source = {"path": str(path.relative_to(root)), "line": text.count("\n", 0, token.start()) + 1}
|
||||
for owner in owners:
|
||||
scopes.setdefault(owner, {}).setdefault(literal[1], []).append(source)
|
||||
return scopes
|
||||
|
||||
|
||||
def scan_builtins(root: Path, source_root: Path) -> dict | None:
|
||||
"""`@IF`, `@LOOP`, ... are parsed by CodeParser.dispatchSpecialCall, not registered as
|
||||
MethodSpec: each `case "NAME", ... ->` arm of its switch is one built-in statement."""
|
||||
@@ -97,27 +154,19 @@ def scan_builtins(root: Path, source_root: Path) -> dict | None:
|
||||
return None
|
||||
text = path.read_text(encoding="utf-8")
|
||||
code = code_mask(text)
|
||||
head = BUILTINS_DISPATCH.search(code)
|
||||
if head is None:
|
||||
if BUILTINS_DISPATCH.search(code) is None:
|
||||
return None
|
||||
switch = code.index("switch", head.end())
|
||||
body_start = code.index("{", switch)
|
||||
body_end = closing(code, body_start, "{}")
|
||||
arms = list(CASE_ARM.finditer(code, body_start, body_end))
|
||||
methods = {}
|
||||
for arm, following in zip(arms, arms[1:] + [None]):
|
||||
if arm.group(1) is None:
|
||||
continue # default arm: unknown statements are a parse error
|
||||
end = following.start() if following else body_end
|
||||
body = text[arm.start():end]
|
||||
for labels, start, end in switch_arms(text, code, BUILTINS_DISPATCH):
|
||||
body = text[start:end]
|
||||
status = "registered"
|
||||
notes = []
|
||||
if re.search(r"not (?:yet )?implemented", body, re.I) or "UnsupportedOperationException" in body:
|
||||
status = "stub_candidate"
|
||||
notes.append("Heurystyka: wyjątek lub komunikat o braku implementacji; wymaga przeglądu.")
|
||||
for name in re.findall(r'"([A-Za-z_]+)"', text[arm.start(1):arm.end(1)]):
|
||||
for name in labels:
|
||||
methods[name.upper()] = {"status": status, "notes": list(notes), "source": {
|
||||
"path": str(path.relative_to(root)), "line": text.count("\n", 0, arm.start()) + 1,
|
||||
"path": str(path.relative_to(root)), "line": text.count("\n", 0, start) + 1,
|
||||
"owner": "@", "body_sha256": digest(body.encode()), "file_sha256": digest(text.encode())}}
|
||||
return {"methods": methods, "mapping_notes": [
|
||||
"Instrukcje @ są rozpoznawane w CodeParser.dispatchSpecialCall, nie przez MethodSpec."]}
|
||||
@@ -178,7 +227,8 @@ def scan_emulator(root: Path) -> dict:
|
||||
builtins = scan_builtins(root, source_root)
|
||||
if builtins is not None:
|
||||
types["@"] = builtins
|
||||
return {"root": str(root), "signals": scan_signals(root, source_root, variable_root), "commit": _git(root, "rev-parse", "HEAD"),
|
||||
return {"root": str(root), "signals": scan_signals(root, source_root, variable_root),
|
||||
"properties": scan_properties(root, source_root, variable_root), "commit": _git(root, "rev-parse", "HEAD"),
|
||||
"dirty": bool(_git(root, "status", "--porcelain")),
|
||||
"source_tree_sha256": tree.hexdigest(), "types": types, "warnings": warnings}
|
||||
|
||||
@@ -225,10 +275,11 @@ def _constructor_runner(name, cpp_class, class_runner, bases, known_classes):
|
||||
return None
|
||||
|
||||
|
||||
def script_events(snap: Snapshot) -> dict[str, list[str]]:
|
||||
def script_events(snap: Snapshot, scopes: dict | None = None) -> dict[str, list[str]]:
|
||||
"""Script type -> ordered event names for one binary. `type_events` (each type's own
|
||||
vftable, schema 6) wins; otherwise the class list of the nearest class up this snapshot's
|
||||
constructor chain that has one (INTEGER -> CMC_Variable)."""
|
||||
constructor chain that has one (INTEGER -> CMC_Variable). `scopes` (script_owners) adds
|
||||
singletons such as SCENE, whose class is their runner's owner."""
|
||||
out = {}
|
||||
for e in sorted(snap.type_events, key=lambda e: e["order"]):
|
||||
out.setdefault(canonical(e["type"]), []).append(e["name"].upper())
|
||||
@@ -251,6 +302,43 @@ def script_events(snap: Snapshot) -> dict[str, list[str]]:
|
||||
cls = bases.get(cls)
|
||||
if cls in by_class:
|
||||
out[name] = by_class[cls]
|
||||
for name, (owner, _) in (scopes or {}).items():
|
||||
if name not in out and owner in by_class:
|
||||
out[name] = by_class[owner]
|
||||
return out
|
||||
|
||||
|
||||
def script_fields(snap: Snapshot, scopes: dict | None = None) -> dict[str, list[tuple[str, list[str]]]]:
|
||||
"""Script type -> [(property, value types)] for one binary. `type_fields` (constructor
|
||||
chain, schema 7) wins; otherwise the class fields of the type's class and every base up
|
||||
this snapshot's constructor chain, base first - properties accumulate, unlike events.
|
||||
`scopes` (script_owners) adds singletons such as SCENE."""
|
||||
out = {}
|
||||
for f in sorted(snap.type_fields, key=lambda f: f["order"]):
|
||||
out.setdefault(canonical(f["type"]), []).append((f["name"].upper(), f.get("value_types", [])))
|
||||
by_class = {}
|
||||
for f in sorted(snap.fields, key=lambda f: f.get("order") or 0):
|
||||
by_class.setdefault(f["owner"], []).append((f["name"].upper(), [f.get("type")]))
|
||||
bases = {e["class"]: e["base_class"] for e in snap.field_inheritance}
|
||||
known = set(by_class) | set(bases) | set(bases.values())
|
||||
for t in snap.types:
|
||||
name = canonical(t["script_name"])
|
||||
if name in out:
|
||||
continue
|
||||
cls = t.get("cpp_class") if t.get("cpp_class") != "CMC_Behaviour" or name == "BEHAVIOUR" else None
|
||||
if not cls:
|
||||
candidates = [c for c in known if owner_name(c) == name]
|
||||
cls = candidates[0] if len(candidates) == 1 else None
|
||||
chain = []
|
||||
while cls and cls not in chain:
|
||||
chain.append(cls)
|
||||
cls = bases.get(cls)
|
||||
fields = [f for c in reversed(chain) for f in by_class.get(c, [])]
|
||||
if fields:
|
||||
out[name] = fields
|
||||
for name, (owner, _) in (scopes or {}).items():
|
||||
if name not in out and owner in by_class:
|
||||
out[name] = by_class[owner]
|
||||
return out
|
||||
|
||||
|
||||
@@ -346,12 +434,20 @@ def build_tracker(snapshots: list[Snapshot], emulator: dict, reviews: list[dict]
|
||||
parents = defaultdict(list)
|
||||
for edge in snap.method_inheritance:
|
||||
parents[edge["runner"].removesuffix("_Runner")].append(edge["base_runner"].removesuffix("_Runner"))
|
||||
for name, names in script_events(snap).items():
|
||||
mappings = script_owners(snap, owners)
|
||||
for name, names in script_events(snap, mappings).items():
|
||||
events = type_row(name).setdefault("events", {})
|
||||
for order, event in enumerate(names):
|
||||
events.setdefault(event, {"name": event, "order": order, "original": [], "emulator": [],
|
||||
"status": "not_found"})["original"].append(bid)
|
||||
mappings = script_owners(snap, owners)
|
||||
for name, props in script_fields(snap, mappings).items():
|
||||
fields = type_row(name).setdefault("fields", {})
|
||||
for order, (prop, value_types) in enumerate(props):
|
||||
row = fields.setdefault(prop, {"name": prop, "order": order, "value_types": [],
|
||||
"original": [], "emulator": [], "status": "not_found"})
|
||||
if bid not in row["original"]:
|
||||
row["original"].append(bid)
|
||||
row["value_types"] = sorted(set(row["value_types"]) | {v for v in value_types if v})
|
||||
for t in snap.types:
|
||||
row = type_row(canonical(t["script_name"]))
|
||||
if t.get("cpp_class_provenance"):
|
||||
@@ -436,6 +532,13 @@ def build_tracker(snapshots: list[Snapshot], emulator: dict, reviews: list[dict]
|
||||
row["emulator"] = own + base + engine
|
||||
row["status"] = ("emitted" if own else "emitted_base" if base
|
||||
else "engine_literal" if engine else "not_found")
|
||||
properties = emulator.get("properties", {})
|
||||
for name, t in rows.items():
|
||||
for prop, row in t.get("fields", {}).items():
|
||||
own, base, engine = (properties.get(scope, {}).get(prop, []) for scope in (name, "*", "engine"))
|
||||
row["emulator"] = own + base + engine
|
||||
row["status"] = ("read" if own else "read_base" if base
|
||||
else "engine_literal" if engine else "not_found")
|
||||
base_verbs = {m for m, row in rows.get(BASE_SCOPE, {}).get("methods", {}).items() if row["original"]}
|
||||
for name, t in rows.items():
|
||||
for method, row in t["methods"].items():
|
||||
@@ -475,20 +578,24 @@ def build_tracker(snapshots: list[Snapshot], emulator: dict, reviews: list[dict]
|
||||
if "inherits" in t:
|
||||
t["inherits"] = sorted(set(t["inherits"]))
|
||||
t["events"] = sorted(t.get("events", {}).values(), key=lambda e: (e["order"], e["name"]))
|
||||
t["fields"] = sorted(t.get("fields", {}).values(), key=lambda f: (f["order"], f["name"]))
|
||||
all_rows = [m for t in rows.values() for m in t["methods"]]
|
||||
all_events = [e for t in rows.values() for e in t["events"]]
|
||||
return {"schema": "tracker/1", "emulator": {k: v for k, v in emulator.items() if k not in ("types", "warnings", "signals")},
|
||||
all_fields = [f for t in rows.values() for f in t["fields"]]
|
||||
return {"schema": "tracker/1", "emulator": {k: v for k, v in emulator.items() if k not in ("types", "warnings", "signals", "properties")},
|
||||
"binaries": sorted(binaries, key=lambda b: (b["name"].lower(), b["id"])),
|
||||
"types": sorted(rows.values(), key=lambda t: t["name"]),
|
||||
"summary": {"types": len(rows), "methods": len(all_rows), "status": dict(Counter(m["status"] for m in all_rows)),
|
||||
"verification": dict(Counter(m["verification"] for m in all_rows)),
|
||||
"events": len(all_events), "event_status": dict(Counter(e["status"] for e in all_events))},
|
||||
"events": len(all_events), "event_status": dict(Counter(e["status"] for e in all_events)),
|
||||
"fields": len(all_fields), "field_status": dict(Counter(f["status"] for f in all_fields))},
|
||||
"warnings": warnings + ["Brak obserwacji w wariancie oznacza nieustalone, nie brak metody.",
|
||||
"Rejestracja Java nie dowodzi poprawności. Liczniki obejmują pary typ/metoda, także odziedziczone.",
|
||||
"Typ → runner pochodzi z CMC_RunnerFactory, gdy snapshot ma runner_map; inaczej z nazwy klasy (wnioskowane).",
|
||||
"Zakres * to metody bazowe CMC_Runner, wspólne dla każdego typu (pole inherits); nie są powielane przy typach.",
|
||||
"Zakres @ to wbudowane instrukcje @IF/@LOOP/... (runner typu 0).",
|
||||
"Eventy: status emulatora to literał nazwy w kodzie (klasa typu / Variable / silnik); nazwy budowane dynamicznie nie są widoczne."]}
|
||||
"Eventy: status emulatora to literał nazwy w kodzie (klasa typu / Variable / silnik); nazwy budowane dynamicznie nie są widoczne.",
|
||||
"Pola: status emulatora to literał klucza (`:NAME` w gałęzi typu w CNVParser, odczyt atrybutu w klasie typu lub w Variable.java, albo w innym kodzie)."]}
|
||||
|
||||
|
||||
def validate_review(review: dict) -> None:
|
||||
|
||||
Reference in New Issue
Block a user