86 lines
3.8 KiB
Python
86 lines
3.8 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import asdict, dataclass, field
|
|
from typing import Any
|
|
from .featurescript_parser import symbolic_string
|
|
from .ir import Call
|
|
|
|
|
|
@dataclass
|
|
class QueryInfo:
|
|
kind: str | None = None
|
|
owner_feature: str | None = None
|
|
topology_type: str | None = None
|
|
source_sketch: str | None = None
|
|
source_entity: str | None = None
|
|
is_start: bool | None = None
|
|
calls: list[str] = field(default_factory=list)
|
|
# The AST is intentionally lossless for the parser's value model. Query
|
|
# aliases may be resolved for lowering, but their nested query semantics
|
|
# must remain auditable and cannot be collapsed into a geometry hint.
|
|
ast: dict[str, Any] | list[Any] | str | float | bool | None = None
|
|
query_combinators: list[str] = field(default_factory=list)
|
|
filters: list[str] = field(default_factory=list)
|
|
body_scope: list[str] = field(default_factory=list)
|
|
disambiguation: list[str] = field(default_factory=list)
|
|
|
|
def __post_init__(self) -> None:
|
|
if self.ast is None:
|
|
self.ast = {}
|
|
|
|
def as_dict(self) -> dict[str, Any]: return asdict(self)
|
|
|
|
|
|
def walk_calls(value: Any):
|
|
if isinstance(value, Call):
|
|
yield value
|
|
for arg in value.args: yield from walk_calls(arg)
|
|
elif isinstance(value, list):
|
|
for item in value: yield from walk_calls(item)
|
|
elif isinstance(value, dict):
|
|
for item in value.values(): yield from walk_calls(item)
|
|
|
|
|
|
def query_ast(value: Any) -> dict[str, Any] | list[Any] | str | float | bool | None:
|
|
"""Serialize parsed FeatureScript query syntax without evaluating it."""
|
|
if isinstance(value, Call):
|
|
return {"call": value.name, "args": [query_ast(arg) for arg in value.args], "line": value.line}
|
|
if isinstance(value, list):
|
|
return [query_ast(item) for item in value]
|
|
if isinstance(value, dict):
|
|
return {str(key): query_ast(item) for key, item in value.items()}
|
|
if value is None or isinstance(value, (str, float, bool, int)):
|
|
return value
|
|
return str(value)
|
|
|
|
|
|
def parse_query(value: Any) -> QueryInfo:
|
|
info = QueryInfo(ast=query_ast(value))
|
|
for call in walk_calls(value):
|
|
info.calls.append(call.name)
|
|
if call.name in {"qUnion", "qIntersection", "qSubtraction", "qAdjacent"}:
|
|
info.query_combinators.append(call.name)
|
|
if call.name in {"qBodyType", "qOwnerBody"}:
|
|
info.body_scope.append(call.name)
|
|
if call.name in {"TDD", "trueDependencyDisambiguation"}:
|
|
info.disambiguation.append(call.name)
|
|
if call.name in {"qBodyType", "qOwnerBody", "qAdjacent"}:
|
|
info.filters.append(call.name)
|
|
if call.name in {"makeQuery", "qCreatedBy"} and call.args:
|
|
owner = symbolic_string(call.args[0])
|
|
if "F" in owner:
|
|
tail = owner[owner.find("F"):].split(".", 1)[0]
|
|
info.owner_feature = tail
|
|
if call.name == "makeQuery" and len(call.args) > 2:
|
|
info.topology_type = str(call.args[1]); info.kind = str(call.args[2]).lower()
|
|
definition = next((arg for arg in call.args if isinstance(arg, dict)), {})
|
|
if isinstance(definition.get("isStart"), str): info.is_start = definition["isStart"].lower() == "true"
|
|
elif "isStart" in definition: info.is_start = bool(definition["isStart"])
|
|
if call.name in {"sQuery", "sketchEntityQuery"} and len(call.args) >= 3:
|
|
sketch = symbolic_string(call.args[0]); info.source_sketch = sketch.split(".", 1)[0]
|
|
if info.topology_type is None: info.kind = str(call.args[1]).lower()
|
|
info.source_entity = str(call.args[2])
|
|
if call.name == "qSketchRegion" and call.args:
|
|
info.source_sketch = symbolic_string(call.args[0]); info.kind = "face"
|
|
return info
|