Skip to content

Commit 05c37de

Browse files
committed
feat: Pass conformance test
1 parent 2623022 commit 05c37de

7 files changed

Lines changed: 580 additions & 9 deletions

File tree

‎src/openjd/model/_format_strings/_expr_support.py‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,64 @@ def static_validate_symbol_free(expr: str) -> None:
174174
def map_eval_error(exc: BaseException) -> _ModelExpressionError:
175175
"""Wrap a Rust evaluation error as the model's ``ExpressionError``."""
176176
return _ModelExpressionError(str(exc))
177+
178+
179+
def expr_type_for_openjd_type(openjd_type: str) -> Optional[str]:
180+
"""Map an OpenJD parameter type name (e.g. ``PATH``, ``LIST[INT]``) to the
181+
EXPR engine's spec-form type string (``path``, ``list[int]``), or ``None``
182+
if there is no confident mapping (so callers fall back to name-only
183+
validation rather than risk a wrong type).
184+
"""
185+
t = openjd_type.strip().upper()
186+
scalar = _OPENJD_TYPE_TO_EXPR_TYPE.get(t)
187+
if scalar is not None:
188+
return scalar
189+
if t.startswith("LIST[") and t.endswith("]"):
190+
inner = expr_type_for_openjd_type(t[len("LIST[") : -1])
191+
if inner is not None:
192+
return f"list[{inner}]"
193+
return None
194+
195+
196+
def longest_defined_prefix(name: str, defined: set) -> Optional[str]:
197+
"""Return the longest dotted prefix of ``name`` that is a defined symbol,
198+
treating any remaining segments as method/property access. E.g. for
199+
``"Param.File.name"`` with ``Param.File`` defined, returns ``"Param.File"``.
200+
"""
201+
segments = name.split(".")
202+
for end in range(len(segments), 0, -1):
203+
prefix = ".".join(segments[:end])
204+
if prefix in defined:
205+
return prefix
206+
return None
207+
208+
209+
def validate_typed_expression(parsed: Any, *, typed_symbols: dict[str, str]) -> None:
210+
"""Statically validate a parsed expression against symbols of known EXPR
211+
type (provided as ``{dotted_name: expr_type_string}``). Catches type
212+
mismatches and invalid method/property access. Raises the model's
213+
``ExpressionError`` on failure.
214+
215+
Symbols are supplied as ``ExprValue.unresolved(<type>)`` placeholders so the
216+
engine type-checks without concrete values.
217+
"""
218+
values: dict[str, Any] = {}
219+
for dotted, type_str in typed_symbols.items():
220+
ev = ExprValue.unresolved(ExprType(type_str))
221+
cursor = values
222+
segs = dotted.split(".")
223+
for seg in segs[:-1]:
224+
cursor = cursor.setdefault(seg, {})
225+
cursor[segs[-1]] = ev
226+
try:
227+
parsed.evaluate(
228+
values=values,
229+
profile=ExprProfile.current().with_host_context(HostContext.unresolved()),
230+
)
231+
except _RUST_EXPR_ERRORS as exc:
232+
msg = str(exc)
233+
# A valid expression that merely depends on a runtime value surfaces as
234+
# an "unresolved" extraction error — that is success, not a failure.
235+
if "unresolved" in msg and "Cannot extract value" in msg:
236+
return
237+
raise _ModelExpressionError(msg)

‎src/openjd/model/_format_strings/_expression.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,24 @@ def __init__(self, expr: str, *, context: ModelParsingContextInterface) -> None:
2929
# Raises: ExpressionError, TokenError
3030
self._expresion_tree = parse_format_string_expr(expr, context=context)
3131

32-
def validate_symbol_refs(self, *, symbols: set[str]) -> None:
32+
def validate_symbol_refs(self, *, symbols: set[str], symbol_types: Any = None) -> None:
3333
"""Check whether this expression can be evaluated correctly given a set of symbol names.
3434
3535
Args:
3636
symbols (set[str]): The names of symbols visible to this expression.
37+
symbol_types: Optional mapping of symbol name -> EXPR type string,
38+
enabling type-aware validation when available.
3739
3840
Raises:
3941
ValueError: If the expression cannot be evaluated with the given symbol names
4042
"""
41-
self._expresion_tree.validate_symbol_refs(symbols=symbols)
43+
self._expresion_tree.validate_symbol_refs(symbols=symbols, symbol_types=symbol_types)
44+
45+
@property
46+
def called_functions(self) -> set:
47+
"""Names of functions invoked by this expression (empty for the legacy
48+
name-only parser)."""
49+
return getattr(self._expresion_tree, "called_functions", set())
4250

4351
def evaluate(
4452
self, *, symtab: SymbolTable, path_format: Optional[Any] = None

‎src/openjd/model/_format_strings/_nodes.py‎

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ class Node(ABC):
1616
"""
1717

1818
@abstractmethod
19-
def validate_symbol_refs(self, *, symbols: set[str]) -> None: # pragma: no cover
19+
def validate_symbol_refs(
20+
self, *, symbols: set[str], symbol_types: Any = None
21+
) -> None: # pragma: no cover
2022
"""Verifies that the expression rooted at this node is valid
2123
given the definitions of symbols in a symbol table.
2224
@@ -74,7 +76,7 @@ class FullNameNode(Node):
7476

7577
name: str
7678

77-
def validate_symbol_refs(self, *, symbols: set[str]) -> None:
79+
def validate_symbol_refs(self, *, symbols: set[str], symbol_types: Any = None) -> None:
7880
if self.name not in symbols:
7981
msg = f"Variable {self.name} does not exist at this location."
8082
distance, closest_matches = closest(symbols, self.name)
@@ -120,11 +122,45 @@ def __init__(self, expr: str) -> None:
120122
if not self._parsed.accessed_symbols:
121123
static_validate_symbol_free(expr)
122124

123-
def validate_symbol_refs(self, *, symbols: set[str]) -> None:
125+
@property
126+
def called_functions(self) -> set:
127+
"""Names of functions invoked by this expression."""
128+
return set(self._parsed.called_functions)
129+
130+
def validate_symbol_refs(self, *, symbols: set[str], symbol_types: Any = None) -> None:
131+
accessed = set(self._parsed.accessed_symbols)
132+
133+
# Typed validation: if every accessed symbol resolves to a defined
134+
# symbol prefix whose EXPR type is known (e.g. job parameters), validate
135+
# the whole expression against those types. This catches type mismatches
136+
# and resolves method/property access (e.g. Param.File.name on a PATH).
137+
# Only attempted when ALL accessed prefixes are typed-known, so an
138+
# unknown-typed symbol (e.g. a `let` name) safely falls back to the
139+
# name-only check below rather than risking a wrong-type rejection.
140+
if symbol_types and accessed:
141+
from ._expr_support import (
142+
longest_defined_prefix,
143+
validate_typed_expression,
144+
)
145+
146+
prefix_types: dict = {}
147+
fully_typed = True
148+
for name in accessed:
149+
prefix = longest_defined_prefix(name, symbols)
150+
if prefix is None or prefix not in symbol_types:
151+
fully_typed = False
152+
break
153+
prefix_types[prefix] = symbol_types[prefix]
154+
if fully_typed:
155+
# Raises model ExpressionError on a type/method error.
156+
validate_typed_expression(self._parsed, typed_symbols=prefix_types)
157+
self._check_comprehension_shadowing(symbols)
158+
return
159+
124160
# accessed_symbols are the free variable references in full dotted
125161
# spelling (e.g. "Param.X"); local let-bound names are excluded by the
126162
# engine. Compare against the set of symbols visible at this location.
127-
missing = set(self._parsed.accessed_symbols) - symbols
163+
missing = accessed - symbols
128164
if missing:
129165
from ._edit_distance import closest
130166

@@ -137,6 +173,18 @@ def validate_symbol_refs(self, *, symbols: set[str]) -> None:
137173
elif len(closest_matches) > 1:
138174
msg += f" Did you mean one of: {', '.join(sorted(closest_matches))}"
139175
raise ValueError(msg)
176+
self._check_comprehension_shadowing(symbols)
177+
178+
def _check_comprehension_shadowing(self, symbols: set) -> None:
179+
# Comprehension/local-binding variables may not shadow a variable that
180+
# is in scope (RFC 0007 §3.6). `local_bindings` are the names bound by
181+
# comprehensions/let-expressions inside this expression.
182+
shadowed = set(self._parsed.local_bindings) & symbols
183+
if shadowed:
184+
raise ValueError(
185+
f"Variable {sorted(shadowed)[0]!r} bound by a comprehension shadows "
186+
"a variable that already exists at this location."
187+
)
140188

141189
def evaluate(self, *, symtab: SymbolTable, path_format: Any = None) -> Any:
142190
from ._expr_support import (

0 commit comments

Comments
 (0)