Repository navigation
Expand file tree
/
Copy path_expression.py
More file actions
147 lines (119 loc) · 5.97 KB
/
Copy path_expression.py
File metadata and controls
147 lines (119 loc) · 5.97 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
import numbers
from typing import Any, Optional, Union
from .._errors import ExpressionError
from .._symbol_table import SymbolTable
from ._nodes import Node
from ._parser import parse_format_string_expr
from .._types import ModelParsingContextInterface
class InterpolationExpression:
expr: str
_expression_tree: Node
def __init__(self, expr: str, *, context: ModelParsingContextInterface) -> None:
"""Constructor.
Raises:
ExpressionError: The provided expression cannot be parsed.
TokenError: The provided expression contains nonvalid or unexpected tokens.
Args:
expr (str): The expression
"""
self.expr = expr
# Raises: ExpressionError, TokenError
self._expresion_tree = parse_format_string_expr(expr, context=context)
def validate_symbol_refs(self, *, symbols: set[str], symbol_types: Any = None) -> None:
"""Check whether this expression can be evaluated correctly given a set of symbol names.
Args:
symbols (set[str]): The names of symbols visible to this expression.
symbol_types: Optional mapping of symbol name -> EXPR type string,
enabling type-aware validation when available.
Raises:
ValueError: If the expression cannot be evaluated with the given symbol names
"""
self._expresion_tree.validate_symbol_refs(symbols=symbols, symbol_types=symbol_types)
@property
def called_functions(self) -> set:
"""Names of functions invoked by this expression (empty for the legacy
name-only parser)."""
return getattr(self._expresion_tree, "called_functions", set())
@property
def accessed_symbols(self) -> set:
"""Free symbol references in this expression in full dotted spelling
(empty for the legacy name-only parser, which exposes no such set)."""
return getattr(self._expresion_tree, "accessed_symbols", set())
def evaluate(
self, *, symtab: SymbolTable, path_format: Optional[Any] = None
) -> Union[numbers.Real, str, Any]:
"""Evaluate the expression given a SymbolTable.
Args:
symtab (SymbolTable): A symbol table containing values to use in the evaluation.
path_format (Any): Optional EXPR PathFormat for PATH-typed values.
Raises:
ExpressionError: If the expression could not be evaluated.
Returns:
The resulting value. For legacy (non-EXPR) expressions this is a
``numbers.Real`` or ``str``; EXPR expressions may also yield
``bool``, ``list``, or ``None``.
"""
try:
result = self._expresion_tree.evaluate(symtab=symtab, path_format=path_format)
except ValueError as exc:
raise ExpressionError(f"Expression failed validation: {str(exc)}")
# EXPR-backed nodes may legitimately return non-scalar values; the
# legacy name path is still restricted to Real/str to preserve its
# exact behavior.
if getattr(self._expresion_tree, "allows_nonscalar", False):
return result
if isinstance(result, (numbers.Real, str)):
return result
raise ExpressionError(f"Nonvalid result type: {result} of type {type(result)}")
@property
def is_expr(self) -> bool:
"""Whether this expression uses the EXPR language (Expression Language),
rather than the base 2023-09 value-reference grammar."""
return getattr(self._expresion_tree, "allows_nonscalar", False)
def evaluate_value(
self,
*,
symtab: SymbolTable,
path_format: Optional[Any] = None,
target_type: Optional[Any] = None,
) -> Any:
"""Evaluate the expression, preferring the engine's typed value form.
``target_type`` (an ``openjd.expr.ExprType``), when given, applies to
EXPR-backed expressions: the result is coerced toward it.
For EXPR-backed expressions this returns the engine ``ExprValue``,
which keeps the result's EXPR type (path/int/float/list/...) and its
string-rendering fidelity when re-seeded into a symbol table — the
session runtime's ``let`` bindings (RFC 0007) rely on this. Legacy
(non-EXPR) expressions return their native value, identical to
:meth:`evaluate`.
Raises:
ExpressionError: If the expression could not be evaluated.
"""
evaluate_value = getattr(self._expresion_tree, "evaluate_value", None)
if evaluate_value is None:
return self.evaluate(symtab=symtab, path_format=path_format)
try:
if target_type is not None:
return evaluate_value(
symtab=symtab, path_format=path_format, target_type=target_type
)
return evaluate_value(symtab=symtab, path_format=path_format)
except ValueError as exc:
raise ExpressionError(f"Expression failed validation: {str(exc)}")
def evaluate_to_str(self, *, symtab: SymbolTable, path_format: Optional[Any] = None) -> str:
"""Evaluate the expression and coerce the result to the string form that
is substituted into the surrounding format string.
For EXPR expressions this uses the engine's spec-defined string coercion
(RFC 0005) rather than Python's ``str()``, so booleans, null, and lists
render per the specification rather than as Python reprs.
Args:
symtab (SymbolTable): A symbol table containing values to use in the evaluation.
path_format (Any): Optional EXPR PathFormat for PATH-typed values.
Raises:
ExpressionError: If the expression could not be evaluated.
"""
try:
return self._expresion_tree.evaluate_to_str(symtab=symtab, path_format=path_format)
except ValueError as exc:
raise ExpressionError(f"Expression failed validation: {str(exc)}")