feat!: default name resolution is `quoted` (ADR-48 revision)
draft feat/name-resolution-v2
feat!: default name resolution is `quoted` (ADR-48 revision) `a-b` is now subtraction and `` `a-b` `` the name unless `symbols` is chosen explicitly — the meaning comes from the text, never from the data, as in Camunda. Under `symbols` the root-vs-local asymmetry (root names resolve, loop variables/parameters/filter elements do not) is not explainable to users, and a row gaining a key flips `unit - price` into a lookup. - feel-core: `NameResolution::default()` is `Quoted`; spec tests and the fixture harness opt into `Symbols` (only the 8 hand-written 0007 cases need it — all other fixtures pass under `Quoted`). - feel-pg: `name_resolution DEFAULT 'quoted'`. - py/lua/wasm: `names` defaults to quoted; docs and tests updated; one test per surface pins the default. - names bench keeps measuring `Symbols`. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
| Changeset | da382279c66c4c72ad01310614fb7558b5515d07 |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@gmail.com>, |
| Parents | 7690f6d5c4ed |
| Children | |
| Pushlog | by timo via ssh |
modified DECISIONS.md +19 −2
| @@ -2416,14 +2416,15 @@ | ||
| 2416 | 2416 | |
| 2417 | 2417 | 2. **Name-resolution level as a parse option.** `#[non_exhaustive] enum NameResolution`: |
| 2418 | 2418 | - `Quoted` — backticks only; no run fusion, no candidates. Camunda semantics; cheapest. |
| 2419 | - - `Symbols` (**default**) — backticks plus ADR-47 runs over `- + * / .`. | |
| 2419 | + **Default since the revision below** (originally `Symbols`). | |
| 2420 | + - `Symbols` — backticks plus ADR-47 runs over `- + * / .` (the spec mode). | |
| 2420 | 2421 | - `Full` — additionally keyword-joined names (`Profit and loss`); **deferred** (see |
| 2421 | 2422 | below), the variant is reserved so it can land without an API break. |
| 2422 | 2423 | |
| 2423 | 2424 | API: `ParseOptions { names: NameResolution }` on `compile`/`evaluate` variants in |
| 2424 | 2425 | feel-core; a keyword argument in the wrappers (`feel.compile(expr, names="quoted")`); |
| 2425 | 2426 | in feel-pg a **function argument with a default** (`name_resolution text DEFAULT |
| 2426 | - 'symbols'`) — **never a GUC**: a session setting would make `feel_eval_immutable`'s | |
| 2427 | + 'quoted'` since the revision; planned as `'symbols'`) — **never a GUC**: a session setting would make `feel_eval_immutable`'s | |
| 2427 | 2428 | result depend on something other than its arguments and silently break expression |
| 2428 | 2429 | indexes. The level is stored in `CompiledExpr`, shown in its `Debug`/`repr`, and part |
| 2429 | 2430 | of every cache key. "Who wants spec compatibility pays with performance": `Full` costs |
| @@ -2567,3 +2568,19 @@ | ||
| 2567 | 2568 | rows got 21 % faster (no per-row re-parse), the formula 27 % faster (no 190 lookups per |
| 2568 | 2569 | row). `'quoted'` buys nothing extra once the pre-check applies; it matters for contexts |
| 2569 | 2570 | that do carry special keys. |
| 2571 | + | |
| 2572 | +- **Revision (2026-10-14): the default is `Quoted`.** After A–C, the remaining `Symbols` | |
| 2573 | + behaviour is consistent but not *explainable* without knowing the implementation: names | |
| 2574 | + bound at the root and paths from there are resolved, names the expression binds itself | |
| 2575 | + (loop variables, parameters, context entries, filter elements) and computed bases are not — | |
| 2576 | + because the decision is made at parse time, when only the root scope exists. A user reads | |
| 2577 | + FEEL as "a name counts where it is in scope", and under `Symbols` a row gaining a key | |
| 2578 | + (`unit-price` next to `unit`, `price`) silently turns `unit - price` into a lookup. The | |
| 2579 | + default therefore becomes the one-sentence rule *"`-` is always minus; names with special | |
| 2580 | + characters or keywords go in backticks"* — data-independent, zero-cost, valid in every name | |
| 2581 | + slot, identical to Camunda. `Symbols` stays the explicit spec mode (third-party DMN models, | |
| 2582 | + the TCK). Measured: of the fixture suite, only the 8 hand-written `0007-date-time-names` | |
| 2583 | + cases need `Symbols`; all other ≈ 3145 cases pass under `Quoted`. The fixture harness runs | |
| 2584 | + in `Symbols` (it *is* the spec); feel-core's spec tests opt in explicitly; each surface has a | |
| 2585 | + test pinning the `Quoted` default. A spec-faithful alternative that decides at evaluation | |
| 2586 | + time (per arithmetic chain, with the live scope) is being prototyped separately. | |
modified README.md +10 −7
| @@ -58,14 +58,16 @@ | ||
| 58 | 58 | **scope**: longest in-scope match preferred, left to right; spaces are not |
| 59 | 59 | part of the name (`a - b` is the same candidate as `a-b`); `(a)-(b)` forces |
| 60 | 60 | the operator reading. On a static path the navigated value is the scope |
| 61 | - (`applicant.first-name`). Both `evaluate` and `compile` resolve per context — | |
| 62 | - a compiled program caches one parse per distinct scope reading. | |
| 61 | + (`applicant.first-name`). This is the **opt-in** `"symbols"` mode; both | |
| 62 | + `evaluate` and `compile` then resolve per context — a compiled program | |
| 63 | + caches one parse per distinct scope reading. | |
| 63 | 64 | - **Backtick-quoted names** (`` `a-b` ``, `` `Profit and loss` ``, |
| 64 | 65 | `` applicant.`first-name` `` — a non-standard extension with feel-scala's |
| 65 | 66 | syntax, so Camunda expressions run unchanged): one name, decided by the text, |
| 66 | - in every name slot. A `names` option selects `"symbols"` (default) or | |
| 67 | - `"quoted"` (backticks only, `a-b` always subtraction — Camunda semantics); | |
| 68 | - feel-pg takes it as the `name_resolution` argument (ADR-48). Inside | |
| 67 | + in every name slot. A `names` option selects `"quoted"` (**the default** — | |
| 68 | + backticks only, `a-b` always subtraction, Camunda semantics) or `"symbols"` | |
| 69 | + (the DMN spec's scope-dependent reading); feel-pg takes it as the | |
| 70 | + `name_resolution` argument (ADR-48). Inside | |
| 69 | 71 | backticks every character counts, spaces included: `` `a b` `` is the key |
| 70 | 72 | with two spaces, `` ` a` `` the one with a leading space — unlike unquoted |
| 71 | 73 | multi-word names, whose spaces collapse to one. |
| @@ -77,8 +79,9 @@ | ||
| 77 | 79 | with `unit`, `price` and `unit-price` all in the context, both `unit-price` |
| 78 | 80 | and `unit - price` read the key `unit-price`, so adding that key to a row |
| 79 | 81 | silently turns a subtraction into a lookup; `(unit)-(price)` forces the |
| 80 | - subtraction. For data you do not control (external jsonb), `"quoted"` makes | |
| 81 | - `-` always mean minus. | |
| 82 | + subtraction. That is why `"quoted"` is the default: `-` always means minus, | |
| 83 | + whatever the data; choose `"symbols"` for third-party DMN models written | |
| 84 | + with unquoted special names. | |
| 82 | 85 | |
| 83 | 86 | | context `unit`, `price`, `unit-price` | `"symbols"` | `"quoted"` | |
| 84 | 87 | |---|---|---| |
modified ROADMAP.md +4 −1
| @@ -319,7 +319,10 @@ | ||
| 319 | 319 | ### v2: levels, backticks, adaptive compile, path members (ADR-48 — A–C ✅ shipped, D open) |
| 320 | 320 | |
| 321 | 321 | **Status:** A–C implemented on branch `feat/name-resolution-v2` (stacked on |
| 322 | -`feat/special-char-names`); measurements, deviations from the plan (trace-keyed | |
| 322 | +`feat/special-char-names`); default level revised to `quoted` (backticks only — | |
| 323 | +`symbols` is the opt-in spec mode; see ADR-48 "Revision"). An evaluation-time | |
| 324 | +alternative for a spec-faithful `symbols` is being prototyped on a stacked | |
| 325 | +branch. Measurements, deviations from the plan (trace-keyed | |
| 323 | 326 | cache, O(1) special-key pre-check, member runs stop at `.`) and two |
| 324 | 327 | pre-existing list-selection bugs fixed on the way (feel-pg jsonb arrays, Lua |
| 325 | 328 | sequences) are recorded in [DECISIONS.md](DECISIONS.md) ADR-48. Remaining v1 |
modified crates/feel-core/benches/names.rs +16 −5
| @@ -3,17 +3,28 @@ | ||
| 3 | 3 | //! a 190-candidate name-only formula, a bound hyphen name, a nested hyphen |
| 4 | 4 | //! member, and one compiled program alternating between two scopes. |
| 5 | 5 | //! |
| 6 | -//! Uses only API present since ADR-47 (`compile`, `evaluate`, | |
| 7 | -//! `CompiledExpr::evaluate`), so the same file benches both branches. | |
| 8 | -//! Note: before ADR-48 `compile` was probe-free, so its `eval` numbers for the | |
| 9 | -//! bound/member workloads measure the *arithmetic* (wrong) reading. | |
| 6 | +//! Runs at `NameResolution::Symbols` (the spec mode; the crate default became | |
| 7 | +//! `Quoted` after the ADR-48 measurements, which used this file with the | |
| 8 | +//! then-default `compile`/`evaluate`). | |
| 10 | 9 | //! |
| 11 | 10 | //! Run with: `cargo bench -p feel-core --bench names`. |
| 12 | 11 | |
| 13 | 12 | use std::collections::BTreeMap; |
| 14 | 13 | |
| 15 | 14 | use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion}; |
| 16 | -use feel_core::{compile, evaluate, Context, Value}; | |
| 15 | +use feel_core::{CompiledExpr, Context, FeelError, NameResolution, ParseOptions, Value}; | |
| 16 | + | |
| 17 | +fn symbols() -> ParseOptions { | |
| 18 | + ParseOptions::names(NameResolution::Symbols) | |
| 19 | +} | |
| 20 | + | |
| 21 | +fn compile(src: &str) -> Result<CompiledExpr, FeelError> { | |
| 22 | + feel_core::compile_with(src, &symbols()) | |
| 23 | +} | |
| 24 | + | |
| 25 | +fn evaluate(src: &str, ctx: &Context) -> Result<Value, FeelError> { | |
| 26 | + feel_core::evaluate_with(src, ctx, &symbols()) | |
| 27 | +} | |
| 17 | 28 | |
| 18 | 29 | fn ctx_of(pairs: &[(&str, Value)]) -> Context { |
| 19 | 30 | let mut c = Context::empty(); |
modified crates/feel-core/src/lib.rs +48 −14
| @@ -210,15 +210,23 @@ | ||
| 210 | 210 | /// How far the parser goes in reading FEEL's ambiguous names (ADR-48) — a parse |
| 211 | 211 | /// option ([`ParseOptions`]). Backtick-quoted names (`` `a-b` ``, the feel-scala |
| 212 | 212 | /// extension) work at every level. |
| 213 | +/// | |
| 214 | +/// The default is [`Quoted`](NameResolution::Quoted): the meaning of an | |
| 215 | +/// expression then comes from its text alone, never from the data. Opt into | |
| 216 | +/// [`Symbols`](NameResolution::Symbols) for the DMN spec's scope-dependent | |
| 217 | +/// reading (unquoted names from third-party DMN models, the TCK). | |
| 213 | 218 | #[non_exhaustive] |
| 214 | 219 | #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)] |
| 215 | 220 | pub enum NameResolution { |
| 216 | 221 | /// Only backtick-quoted names are special; `a-b` is always subtraction — |
| 217 | - /// Camunda/feel-scala semantics. No scope checks at all: the cheapest level. | |
| 222 | + /// Camunda/feel-scala semantics. No scope checks at all: the cheapest level, | |
| 223 | + /// and the default. | |
| 224 | + #[default] | |
| 218 | 225 | Quoted, |
| 219 | - /// Backticks plus §10.3.1.6 runs over `- + * / .`, resolved against the | |
| 220 | - /// scope (ADR-47) and, on static paths, against the navigated value (ADR-48). | |
| 221 | - #[default] | |
| 226 | + /// The spec mode (§10.3.1.6): backticks plus runs over `- + * / .`, | |
| 227 | + /// resolved against the root scope (ADR-47) and, on static paths, against | |
| 228 | + /// the navigated value (ADR-48). Names bound inside the expression (loop | |
| 229 | + /// variables, parameters, filter elements) are not consulted. | |
| 222 | 230 | Symbols, |
| 223 | 231 | } |
| 224 | 232 | |
| @@ -238,7 +246,7 @@ | ||
| 238 | 246 | #[non_exhaustive] |
| 239 | 247 | #[derive(Debug, Default, Clone, PartialEq, Eq)] |
| 240 | 248 | pub struct ParseOptions { |
| 241 | - /// The name-resolution level (default [`NameResolution::Symbols`]). | |
| 249 | + /// The name-resolution level (default [`NameResolution::Quoted`]). | |
| 242 | 250 | pub names: NameResolution, |
| 243 | 251 | } |
| 244 | 252 | |
| @@ -524,13 +532,15 @@ | ||
| 524 | 532 | } |
| 525 | 533 | |
| 526 | 534 | /// Parse a FEEL expression into a reusable [`CompiledExpr`] without evaluating |
| 527 | -/// it, at the default [`NameResolution::Symbols`] level. | |
| 535 | +/// it, at the default [`NameResolution::Quoted`] level: `a-b` is subtraction, | |
| 536 | +/// `` `a-b` `` the name. | |
| 528 | 537 | /// |
| 529 | 538 | /// Returns [`FeelError::Parse`] if the expression is syntactically invalid. |
| 530 | 539 | /// |
| 531 | -/// **Scope-adaptive** (ADR-48): special-character names (`a-b`, `Date-Time`, | |
| 540 | +/// With [`compile_with`] at [`NameResolution::Symbols`] the result is | |
| 541 | +/// **scope-adaptive** (ADR-48): special-character names (`a-b`, `Date-Time`, | |
| 532 | 542 | /// `applicant.first-name`) resolve per evaluation against the context, exactly |
| 533 | -/// like the one-shot [`evaluate`] — see [`CompiledExpr`]. | |
| 543 | +/// like the one-shot [`evaluate_with`] — see [`CompiledExpr`]. | |
| 534 | 544 | /// |
| 535 | 545 | /// `ExprParser::new()` is now cheap (the external `logos` lexer builds no runtime |
| 536 | 546 | /// matcher — see `lexer.rs`), so there is no parser to cache; the cost is the |
| @@ -586,10 +596,11 @@ | ||
| 586 | 596 | |
| 587 | 597 | /// Parse and evaluate a FEEL expression against a context. |
| 588 | 598 | /// |
| 589 | -/// One parse, pinned to `context` as the §10.3.1.6 scope (ADR-47): a name like | |
| 590 | -/// `a-b` resolves to the context binding when one exists, and to subtraction | |
| 591 | -/// otherwise. Prefer [`compile`] when evaluating the same expression more than | |
| 592 | -/// once. | |
| 599 | +/// At the default [`NameResolution::Quoted`] level `a-b` is subtraction and | |
| 600 | +/// `` `a-b` `` the name. With [`evaluate_with`] at [`NameResolution::Symbols`] | |
| 601 | +/// the context is the §10.3.1.6 scope (ADR-47): `a-b` resolves to the context | |
| 602 | +/// binding when one exists. Prefer [`compile`] when evaluating the same | |
| 603 | +/// expression more than once. | |
| 593 | 604 | /// |
| 594 | 605 | /// ``` |
| 595 | 606 | /// use feel_core::{evaluate, Context, Value}; |
| @@ -641,6 +652,15 @@ | ||
| 641 | 652 | |
| 642 | 653 | #[cfg(test)] |
| 643 | 654 | mod tests { |
| 655 | + // The spec tests below exercise the scope-dependent reading: shadow the | |
| 656 | + // crate's `evaluate`/`compile` (default `Quoted`, ADR-48) with | |
| 657 | + // `Symbols`-mode versions. `default_level_is_quoted` uses `super::`. | |
| 658 | + fn evaluate(src: &str, ctx: &Context) -> Result<Value, FeelError> { | |
| 659 | + super::evaluate_with(src, ctx, &ParseOptions::names(NameResolution::Symbols)) | |
| 660 | + } | |
| 661 | + fn compile(src: &str) -> Result<CompiledExpr, FeelError> { | |
| 662 | + super::compile_with(src, &ParseOptions::names(NameResolution::Symbols)) | |
| 663 | + } | |
| 644 | 664 | use super::*; |
| 645 | 665 | |
| 646 | 666 | /// Evaluate against an empty context, panicking on error, and collapse error |
| @@ -805,7 +825,7 @@ | ||
| 805 | 825 | assert_eq!(prog.scoped_parses(), 1); |
| 806 | 826 | |
| 807 | 827 | // `compile_with_probe` PINS its probe's scope: no adaptation later. |
| 808 | - let pinned = compile_with_probe("a-b", &bound, &ParseOptions::default()).unwrap(); | |
| 828 | + let pinned = compile_with_probe("a-b", &bound, &ParseOptions::names(NameResolution::Symbols)).unwrap(); | |
| 809 | 829 | assert_eq!(pinned.evaluate(&bound).unwrap().into_lenient(), num(10.0)); |
| 810 | 830 | assert_eq!(pinned.evaluate(&parts).unwrap().into_lenient(), Value::Null); |
| 811 | 831 | |
| @@ -1000,11 +1020,25 @@ | ||
| 1000 | 1020 | evaluate_with("applicant.first-name", &app, "ed).unwrap().into_lenient(), |
| 1001 | 1021 | Value::Null |
| 1002 | 1022 | ); |
| 1003 | - // Symbols (the default) resolves against the scope. | |
| 1023 | + // Symbols resolves against the scope. | |
| 1004 | 1024 | assert_eq!(evaluate("a-b", &ctx).unwrap(), num(100.0)); |
| 1005 | 1025 | assert_eq!(NameResolution::from_name("Quoted"), Some(NameResolution::Quoted)); |
| 1006 | 1026 | assert_eq!(NameResolution::from_name("symbols"), Some(NameResolution::Symbols)); |
| 1007 | 1027 | assert_eq!(NameResolution::from_name("full"), None); |
| 1028 | + } | |
| 1029 | + | |
| 1030 | + #[test] | |
| 1031 | + fn default_level_is_quoted() { | |
| 1032 | + // ADR-48 (revised): the crate default reads the text, not the data. | |
| 1033 | + let ctx = ctx_of(&[("a", num(7.0)), ("b", num(3.0)), ("a-b", num(100.0))]); | |
| 1034 | + assert_eq!(super::evaluate("a-b", &ctx).unwrap(), num(4.0)); | |
| 1035 | + assert_eq!(super::evaluate("`a-b`", &ctx).unwrap(), num(100.0)); | |
| 1036 | + let prog = super::compile("a-b").unwrap(); | |
| 1037 | + assert_eq!(prog.name_resolution(), NameResolution::Quoted); | |
| 1038 | + assert_eq!(prog.evaluate(&ctx).unwrap(), num(4.0)); | |
| 1039 | + let app = ctx_of(&[("applicant", ctx_value(&[("first-name", s("Ada"))]))]); | |
| 1040 | + assert_eq!(super::evaluate("applicant.`first-name`", &app).unwrap(), s("Ada")); | |
| 1041 | + assert_eq!(ParseOptions::default().names, NameResolution::Quoted); | |
| 1008 | 1042 | } |
| 1009 | 1043 | |
| 1010 | 1044 | #[test] |
modified crates/feel-core/tests/fixtures_test.rs +4 −2
| @@ -13,7 +13,7 @@ | ||
| 13 | 13 | use std::fs; |
| 14 | 14 | use std::path::{Path, PathBuf}; |
| 15 | 15 | |
| 16 | -use feel_core::{coerce, evaluate, parse_type, Context, Value}; | |
| 16 | +use feel_core::{coerce, evaluate_with, parse_type, Context, NameResolution, ParseOptions, Value}; | |
| 17 | 17 | |
| 18 | 18 | fn fixtures_dir() -> PathBuf { |
| 19 | 19 | Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures") |
| @@ -75,7 +75,9 @@ | ||
| 75 | 75 | let ctx = context_from_case(case, id); |
| 76 | 76 | // Collapse error provenance: TCK expectations are spec-shaped (a type error |
| 77 | 77 | // is just `null`), so we compare against the lenient view (ADR-14). |
| 78 | - let evaluated = evaluate(expr, &ctx) | |
| 78 | + // The TCK is the DMN spec: unquoted special-character names resolve | |
| 79 | + // against the scope (§10.3.1.6), so run in `Symbols` mode (ADR-48). | |
| 80 | + let evaluated = evaluate_with(expr, &ctx, &ParseOptions::names(NameResolution::Symbols)) | |
| 79 | 81 | .unwrap_or_else(|e| panic!("[{id}] evaluating `{expr}` failed: {e}")) |
| 80 | 82 | .into_lenient(); |
| 81 | 83 | |
modified crates/feel-lua/src/lib.rs +9 −7
| @@ -53,11 +53,12 @@ | ||
| 53 | 53 | /// Rust integration tests call directly against an embedded VM. It registers |
| 54 | 54 | /// `evaluate`, `evaluate_strict` and `compile` on a fresh table and returns it. |
| 55 | 55 | /// |
| 56 | -/// Special-character names (§10.3.1.6): a name like `a-b` resolves to the table | |
| 57 | -/// key `a-b` when present, and to subtraction otherwise — in the one-shot | |
| 58 | -/// `evaluate`/`evaluate_strict` and, per call, in the scope-adaptive `compile` | |
| 59 | -/// userdata (ADR-48). An optional last argument `names` selects the level: | |
| 60 | -/// `"symbols"` (default) or `"quoted"` (backticks only; `` `a-b` `` always works). | |
| 56 | +/// Special-character names (ADR-48): an optional last argument `names` selects | |
| 57 | +/// the level. The default `"quoted"` reads `a-b` as subtraction and `` `a-b` `` | |
| 58 | +/// as the name — the text decides, as in Camunda. `"symbols"` opts into the DMN | |
| 59 | +/// spec's reading (§10.3.1.6): `a-b` resolves to the table key `a-b` when | |
| 60 | +/// present — in the one-shot `evaluate`/`evaluate_strict` and, per call, in | |
| 61 | +/// the scope-adaptive `compile` userdata. | |
| 61 | 62 | pub fn build_module(lua: &Lua) -> mlua::Result<Table> { |
| 62 | 63 | let exports = lua.create_table()?; |
| 63 | 64 | exports.set( |
| @@ -108,8 +109,9 @@ | ||
| 108 | 109 | |
| 109 | 110 | /// A parsed FEEL expression, reusable across many evaluations (the `feel.compile` |
| 110 | 111 | /// result). Exposed to Lua as userdata with `:evaluate` / `:evaluate_strict`. |
| 111 | -/// **Scope-adaptive** (ADR-48): each call resolves special-character names | |
| 112 | -/// against its own context, with a small cache of per-scope parses. | |
| 112 | +/// Compiled with `"symbols"` it is **scope-adaptive** (ADR-48): each call | |
| 113 | +/// resolves special-character names against its own context, with a small | |
| 114 | +/// cache of per-scope parses. | |
| 113 | 115 | struct CompiledExpr { |
| 114 | 116 | inner: CoreCompiled, |
| 115 | 117 | } |
modified crates/feel-lua/tests/feel.rs +18 −12
| @@ -117,35 +117,35 @@ | ||
| 117 | 117 | // `a-b` is the in-scope NAME when the table binds it, subtraction otherwise; |
| 118 | 118 | // `a - b` is the same candidate (spaces are not part of the name). |
| 119 | 119 | let n: f64 = lua |
| 120 | - .load(r#"return feel.evaluate("a-b", { ["a-b"] = 5 })"#) | |
| 120 | + .load(r#"return feel.evaluate("a-b", { ["a-b"] = 5 }, nil, "symbols")"#) | |
| 121 | 121 | .eval() |
| 122 | 122 | .unwrap(); |
| 123 | 123 | assert_eq!(n, 5.0); |
| 124 | 124 | let n: f64 = lua |
| 125 | - .load(r#"return feel.evaluate("a - b", { ["a-b"] = 5 })"#) | |
| 125 | + .load(r#"return feel.evaluate("a - b", { ["a-b"] = 5 }, nil, "symbols")"#) | |
| 126 | 126 | .eval() |
| 127 | 127 | .unwrap(); |
| 128 | 128 | assert_eq!(n, 5.0); |
| 129 | 129 | let n: f64 = lua |
| 130 | - .load(r#"return feel.evaluate("a-b", { a = 7, b = 3 })"#) | |
| 130 | + .load(r#"return feel.evaluate("a-b", { a = 7, b = 3 }, nil, "symbols")"#) | |
| 131 | 131 | .eval() |
| 132 | 132 | .unwrap(); |
| 133 | 133 | assert_eq!(n, 4.0); |
| 134 | 134 | // `(a)-(b)` is the escape hatch: subtraction even when `a-b` is the name. |
| 135 | 135 | let n: f64 = lua |
| 136 | - .load(r#"return feel.evaluate("(a)-(b)", { a = 7, b = 3, ["a-b"] = 100 })"#) | |
| 136 | + .load(r#"return feel.evaluate("(a)-(b)", { a = 7, b = 3, ["a-b"] = 100 }, nil, "symbols")"#) | |
| 137 | 137 | .eval() |
| 138 | 138 | .unwrap(); |
| 139 | 139 | assert_eq!(n, 4.0); |
| 140 | 140 | // A hyphen name as a path base: `x-y.hour` fuses `x-y`, `.hour` stays a member. |
| 141 | 141 | let n: i64 = lua |
| 142 | - .load(r#"return feel.evaluate("x-y.hour", { ["x-y"] = { hour = 3 } })"#) | |
| 142 | + .load(r#"return feel.evaluate("x-y.hour", { ["x-y"] = { hour = 3 } }, nil, "symbols")"#) | |
| 143 | 143 | .eval() |
| 144 | 144 | .unwrap(); |
| 145 | 145 | assert_eq!(n, 3); |
| 146 | 146 | // A member word never fuses against the root scope: `x.y-z` is (x.y) - z. |
| 147 | 147 | let n: i64 = lua |
| 148 | - .load(r#"return feel.evaluate("x.y-z", { x = { y = 10 }, z = 3, ["y-z"] = 1 })"#) | |
| 148 | + .load(r#"return feel.evaluate("x.y-z", { x = { y = 10 }, z = 3, ["y-z"] = 1 }, nil, "symbols")"#) | |
| 149 | 149 | .eval() |
| 150 | 150 | .unwrap(); |
| 151 | 151 | assert_eq!(n, 7); |
| @@ -157,12 +157,12 @@ | ||
| 157 | 157 | // The ambient `functions` table is part of the parse-time scope (ADR-47): a |
| 158 | 158 | // hyphen-named host function is callable. |
| 159 | 159 | let n: i64 = lua |
| 160 | - .load(r#"return feel.evaluate("my-fn(2)", {}, { ["my-fn"] = function(x) return x * 10 end })"#) | |
| 160 | + .load(r#"return feel.evaluate("my-fn(2)", {}, { ["my-fn"] = function(x) return x * 10 end }, "symbols")"#) | |
| 161 | 161 | .eval() |
| 162 | 162 | .unwrap(); |
| 163 | 163 | assert_eq!(n, 20); |
| 164 | 164 | let v: Value = lua |
| 165 | - .load(r#"return feel.evaluate("my-fn(2)", {})"#) | |
| 165 | + .load(r#"return feel.evaluate("my-fn(2)", {}, nil, "symbols")"#) | |
| 166 | 166 | .eval() |
| 167 | 167 | .unwrap(); |
| 168 | 168 | assert!(matches!(v, Value::Nil)); |
| @@ -173,12 +173,12 @@ | ||
| 173 | 173 | let lua = vm(); |
| 174 | 174 | // ADR-48: one compiled program follows each call's context. |
| 175 | 175 | let n: f64 = lua |
| 176 | - .load(r#"local p = feel.compile("a-b"); return p:evaluate({ ["a-b"] = 5 })"#) | |
| 176 | + .load(r#"local p = feel.compile("a-b", "symbols"); return p:evaluate({ ["a-b"] = 5 })"#) | |
| 177 | 177 | .eval() |
| 178 | 178 | .unwrap(); |
| 179 | 179 | assert_eq!(n, 5.0); |
| 180 | 180 | let n: f64 = lua |
| 181 | - .load(r#"local p = feel.compile("a-b"); return p:evaluate({ a = 7, b = 3 })"#) | |
| 181 | + .load(r#"local p = feel.compile("a-b", "symbols"); return p:evaluate({ a = 7, b = 3 })"#) | |
| 182 | 182 | .eval() |
| 183 | 183 | .unwrap(); |
| 184 | 184 | assert_eq!(n, 4.0); |
| @@ -187,6 +187,12 @@ | ||
| 187 | 187 | #[test] |
| 188 | 188 | fn names_levels_backticks_and_members() { |
| 189 | 189 | let lua = vm(); |
| 190 | + // Default (ADR-48, revised): quoted — `a-b` is subtraction. | |
| 191 | + let n: f64 = lua | |
| 192 | + .load(r#"return feel.evaluate("a-b", { a = 7, b = 3, ["a-b"] = 100 })"#) | |
| 193 | + .eval() | |
| 194 | + .unwrap(); | |
| 195 | + assert_eq!(n, 4.0); | |
| 190 | 196 | let n: f64 = lua |
| 191 | 197 | .load(r#"return feel.evaluate("a-b", { a = 7, b = 3, ["a-b"] = 100 }, nil, "quoted")"#) |
| 192 | 198 | .eval() |
| @@ -203,7 +209,7 @@ | ||
| 203 | 209 | .unwrap(); |
| 204 | 210 | assert_eq!(n, 1.0); |
| 205 | 211 | let s: String = lua |
| 206 | - .load(r#"return feel.evaluate("applicant.first-name", { applicant = { ["first-name"] = "Ada" } })"#) | |
| 212 | + .load(r#"return feel.evaluate("applicant.first-name", { applicant = { ["first-name"] = "Ada" } }, nil, "symbols")"#) | |
| 207 | 213 | .eval() |
| 208 | 214 | .unwrap(); |
| 209 | 215 | assert_eq!(s, "Ada"); |
| @@ -214,7 +220,7 @@ | ||
| 214 | 220 | .unwrap(); |
| 215 | 221 | assert_eq!(n, 2); |
| 216 | 222 | let n: i64 = lua |
| 217 | - .load(r#"return feel.evaluate("orders.unit-price", { orders = { { ["unit-price"] = 3 } } })[1]"#) | |
| 223 | + .load(r#"return feel.evaluate("orders.unit-price", { orders = { { ["unit-price"] = 3 } } }, nil, "symbols")[1]"#) | |
| 218 | 224 | .eval() |
| 219 | 225 | .unwrap(); |
| 220 | 226 | assert_eq!(n, 3); |
modified crates/feel-pg/src/lib.rs +36 −17
| @@ -16,8 +16,9 @@ | ||
| 16 | 16 | //! Scope-dependent special-character names (`a-b`, `applicant.first-name`, |
| 17 | 17 | //! §10.3.1.6) resolve per row against the row's jsonb (ADR-47/48): the cached |
| 18 | 18 | //! [`CompiledExpr`] keeps one parse per distinct answer set, so neither function |
| 19 | -//! re-parses per row. A `name_resolution` argument selects the level (`'symbols'`, | |
| 20 | -//! the default, or `'quoted'` — backticks only, no per-row name checks). | |
| 19 | +//! re-parses per row. A `name_resolution` argument selects the level: `'quoted'` | |
| 20 | +//! (the default — backticks only, `a-b` is always subtraction, no per-row name | |
| 21 | +//! checks) or `'symbols'` (the spec's scope-dependent reading). | |
| 21 | 22 | //! |
| 22 | 23 | //! The JSON/jsonb boundary mapping lives here (not in `feel-core`), per the |
| 23 | 24 | //! project's boundary-mapping decision. It uses PostgreSQL's **binary** jsonb C |
| @@ -75,7 +76,8 @@ | ||
| 75 | 76 | /// a small per-call-site cache of parses keyed by those answers. A name and an |
| 76 | 77 | /// arithmetic reading thus coexist across rows of one query at no per-row parse |
| 77 | 78 | /// cost. `name_resolution => 'quoted'` (backticks only, Camunda semantics) skips |
| 78 | -/// the checks altogether; `'symbols'` is the default. | |
| 79 | +/// the checks altogether and is the default; `'symbols'` opts into the spec's | |
| 80 | +/// scope-dependent reading described here. | |
| 79 | 81 | /// |
| 80 | 82 | /// Declared **STABLE** (not VOLATILE): the only non-argument input is the |
| 81 | 83 | /// transaction start timestamp behind `now()`/`today()`, constant within a |
| @@ -99,7 +101,7 @@ | ||
| 99 | 101 | expr: &str, |
| 100 | 102 | context: default!(PgJsonb, "'{}'::jsonb"), |
| 101 | 103 | strict: default!(bool, "false"), |
| 102 | - name_resolution: default!(&str, "'symbols'"), | |
| 104 | + name_resolution: default!(&str, "'quoted'"), | |
| 103 | 105 | fcinfo: pg_sys::FunctionCallInfo, |
| 104 | 106 | ) -> PgJsonb { |
| 105 | 107 | let names = name_level(name_resolution, "feel_eval"); |
| @@ -172,13 +174,13 @@ | ||
| 172 | 174 | /// change. Fixed offsets (`+02:00`) and `Z` are tzdb-independent and fully |
| 173 | 175 | /// supported — all an index needs. |
| 174 | 176 | /// |
| 175 | -/// Special-character names resolve per row exactly as in [`feel_eval`] (ADR-48): | |
| 176 | -/// a row binding `a-b` indexes the name, a row binding only `a` and `b` the | |
| 177 | -/// difference — as §10.3.1.6 demands for each row. That is still IMMUTABLE: the | |
| 178 | -/// parse is a pure function of the expression, the `name_resolution` argument and | |
| 179 | -/// the row, so the result depends on the arguments alone. (Never a GUC: a session | |
| 180 | -/// setting would break that.) `name_resolution => 'quoted'` gives one parse for | |
| 181 | -/// every row. | |
| 177 | +/// Special-character names follow `name_resolution` exactly as in [`feel_eval`] | |
| 178 | +/// (ADR-48). The default `'quoted'` gives one parse for every row. Under | |
| 179 | +/// `'symbols'` a row binding `a-b` indexes the name and a row binding only `a` | |
| 180 | +/// and `b` the difference — as §10.3.1.6 demands for each row — which is still | |
| 181 | +/// IMMUTABLE: the parse is a pure function of the expression, the | |
| 182 | +/// `name_resolution` argument and the row, so the result depends on the | |
| 183 | +/// arguments alone. (Never a GUC: a session setting would break that.) | |
| 182 | 184 | /// |
| 183 | 185 | /// Math/regex builtins (`exp`/`log`/`sqrt`, `matches`/`replace`/`split`) are taken |
| 184 | 186 | /// as immutable; their results are stable except across a libc/`regex` upgrade — |
| @@ -196,7 +198,7 @@ | ||
| 196 | 198 | expr: &str, |
| 197 | 199 | context: default!(PgJsonb, "'{}'::jsonb"), |
| 198 | 200 | strict: default!(bool, "false"), |
| 199 | - name_resolution: default!(&str, "'symbols'"), | |
| 201 | + name_resolution: default!(&str, "'quoted'"), | |
| 200 | 202 | fcinfo: pg_sys::FunctionCallInfo, |
| 201 | 203 | ) -> PgJsonb { |
| 202 | 204 | let names = name_level(name_resolution, "feel_eval_immutable"); |
| @@ -655,7 +657,7 @@ | ||
| 655 | 657 | #[pg_test] |
| 656 | 658 | fn special_char_name_resolves_from_jsonb_key() { |
| 657 | 659 | let matched = Spi::get_one::<bool>( |
| 658 | - r#"SELECT feel_eval('a-b', '{"a-b": 5}'::jsonb) = '5'::jsonb"#, | |
| 660 | + r#"SELECT feel_eval('a-b', '{"a-b": 5}'::jsonb, name_resolution => 'symbols') = '5'::jsonb"#, | |
| 659 | 661 | ) |
| 660 | 662 | .expect("SPI query failed"); |
| 661 | 663 | assert_eq!(matched, Some(true)); |
| @@ -675,7 +677,7 @@ | ||
| 675 | 677 | // `a-b` read the NAME, rows binding only `a`/`b` the difference — the |
| 676 | 678 | // adaptive CompiledExpr picks the parse per row (ADR-48). |
| 677 | 679 | let matched = Spi::get_one::<bool>( |
| 678 | - r#"SELECT bool_and(feel_eval('a-b', c) = e) | |
| 680 | + r#"SELECT bool_and(feel_eval('a-b', c, name_resolution => 'symbols') = e) | |
| 679 | 681 | FROM (VALUES ('{"a-b": 5}'::jsonb, '5'::jsonb), |
| 680 | 682 | ('{"a": 7, "b": 3}', '4'), |
| 681 | 683 | ('{"a-b": 5, "a": 7, "b": 3}', '5'), |
| @@ -690,7 +692,7 @@ | ||
| 690 | 692 | // ADR-48 drops ADR-47's ERROR: the parse is a pure function of |
| 691 | 693 | // (expr, name_resolution, row), so per-row names are still IMMUTABLE. |
| 692 | 694 | let matched = Spi::get_one::<bool>( |
| 693 | - r#"SELECT bool_and(feel_eval_immutable('a-b', c) = e) | |
| 695 | + r#"SELECT bool_and(feel_eval_immutable('a-b', c, name_resolution => 'symbols') = e) | |
| 694 | 696 | FROM (VALUES ('{"a-b": 5}'::jsonb, '5'::jsonb), |
| 695 | 697 | ('{"a": 7, "b": 3}', '4')) t(c, e)"#, |
| 696 | 698 | ) |
| @@ -708,9 +710,11 @@ | ||
| 708 | 710 | // The path-member probe navigates the lazy jsonb (ADR-48). |
| 709 | 711 | let matched = Spi::get_one::<bool>( |
| 710 | 712 | r#"SELECT feel_eval('applicant.first-name', |
| 711 | - '{"applicant": {"first-name": "Ada"}}'::jsonb) = '"Ada"'::jsonb | |
| 713 | + '{"applicant": {"first-name": "Ada"}}'::jsonb, | |
| 714 | + name_resolution => 'symbols') = '"Ada"'::jsonb | |
| 712 | 715 | AND feel_eval('orders.unit-price', |
| 713 | - '{"orders": [{"unit-price": 2}, {"unit-price": 3}]}'::jsonb) | |
| 716 | + '{"orders": [{"unit-price": 2}, {"unit-price": 3}]}'::jsonb, | |
| 717 | + name_resolution => 'symbols') | |
| 714 | 718 | = '[2, 3]'::jsonb"#, |
| 715 | 719 | ) |
| 716 | 720 | .expect("SPI query failed"); |
| @@ -726,6 +730,21 @@ | ||
| 726 | 730 | = '[1, 2, null]'::jsonb |
| 727 | 731 | AND feel_eval('o.items.x', '{"o": {"items": [{"x": 1}]}}'::jsonb) |
| 728 | 732 | = '[1]'::jsonb"#, |
| 733 | + ) | |
| 734 | + .expect("SPI query failed"); | |
| 735 | + assert_eq!(matched, Some(true)); | |
| 736 | + } | |
| 737 | + | |
| 738 | + #[pg_test] | |
| 739 | + fn default_level_is_quoted() { | |
| 740 | + // ADR-48 (revised): without `name_resolution`, `a-b` is subtraction and | |
| 741 | + // `` `a-b` `` the key — the text decides, not the row. | |
| 742 | + let matched = Spi::get_one::<bool>( | |
| 743 | + r#"SELECT feel_eval('a-b', '{"a-b": 5, "a": 7, "b": 3}'::jsonb) = '4'::jsonb | |
| 744 | + AND feel_eval('`a-b`', '{"a-b": 5}'::jsonb) = '5'::jsonb | |
| 745 | + AND feel_eval('applicant.`first-name`', | |
| 746 | + '{"applicant": {"first-name": "Ada"}}'::jsonb) = '"Ada"'::jsonb | |
| 747 | + AND feel_eval_immutable('a-b', '{"a-b": 5, "a": 7, "b": 3}'::jsonb) = '4'::jsonb"#, | |
| 729 | 748 | ) |
| 730 | 749 | .expect("SPI query failed"); |
| 731 | 750 | assert_eq!(matched, Some(true)); |
modified crates/feel-py/src/lib.rs +12 −12
| @@ -52,14 +52,12 @@ | ||
| 52 | 52 | /// A null arising from a type/domain error is returned as `None`; see |
| 53 | 53 | /// `evaluate_strict` to raise instead. |
| 54 | 54 | /// |
| 55 | -/// The context **is** the scope for §10.3.1.6 name resolution (ADR-47): a name | |
| 56 | -/// like `a-b` resolves to the context binding `a-b` when one exists, and to | |
| 57 | -/// subtraction otherwise. The ambient `functions` registry (ADR-23) counts as | |
| 58 | -/// scope too — mirroring `eval_call`, which resolves a call head against | |
| 59 | -/// context, built-ins and (third) the registry — so a hyphen-named host | |
| 60 | -/// function is callable as `my-fn(1)`. `names` selects the level (ADR-48): | |
| 61 | -/// `"symbols"` (default) or `"quoted"` — backticks only, `a-b` always | |
| 62 | -/// subtraction (Camunda semantics); `` `a-b` `` works at every level. | |
| 55 | +/// Special-character names (ADR-48): by default (`names="quoted"`) `a-b` is | |
| 56 | +/// subtraction and `` `a-b` `` the name — the text decides, not the data, as in | |
| 57 | +/// Camunda. `names="symbols"` opts into the DMN spec's reading (§10.3.1.6, | |
| 58 | +/// ADR-47): the context is the scope, so `a-b` resolves to the binding `a-b` | |
| 59 | +/// when one exists; the ambient `functions` registry (ADR-23) counts as scope | |
| 60 | +/// too, so a hyphen-named host function is callable as `my-fn(1)`. | |
| 63 | 61 | #[pyfunction] |
| 64 | 62 | #[pyo3(signature = (expr, context=None, functions=None, names=None))] |
| 65 | 63 | fn evaluate<'py>( |
| @@ -105,9 +103,10 @@ | ||
| 105 | 103 | /// Parse a FEEL expression into a reusable `CompiledExpr` (raises `FeelError` on a |
| 106 | 104 | /// parse error). Does not evaluate. |
| 107 | 105 | /// |
| 108 | -/// **Scope-adaptive** (ADR-48): special-character names (`a-b`, | |
| 106 | +/// `names` as for [`evaluate`]. With `names="symbols"` the result is | |
| 107 | +/// **scope-adaptive** (ADR-48): special-character names (`a-b`, | |
| 109 | 108 | /// `applicant.first-name`) resolve per evaluation against that call's context |
| 110 | -/// and `functions`, exactly like the one-shot [`evaluate`]. `names` as there. | |
| 109 | +/// and `functions`. | |
| 111 | 110 | #[pyfunction] |
| 112 | 111 | #[pyo3(signature = (expr, names=None))] |
| 113 | 112 | fn compile(expr: &str, names: Option<&str>) -> PyResult<CompiledExpr> { |
| @@ -120,8 +119,9 @@ | ||
| 120 | 119 | /// A parsed FEEL expression, reusable across many evaluations: parsing happens |
| 121 | 120 | /// once, each call only maps the context across the boundary. |
| 122 | 121 | /// |
| 123 | -/// **Scope-adaptive** (ADR-48): each evaluation resolves special-character | |
| 124 | -/// names against its own context, with a small cache of per-scope parses. | |
| 122 | +/// Compiled with `names="symbols"` it is **scope-adaptive** (ADR-48): each | |
| 123 | +/// evaluation resolves special-character names against its own context, with a | |
| 124 | +/// small cache of per-scope parses. | |
| 125 | 125 | #[pyclass(name = "CompiledExpr", module = "feel")] |
| 126 | 126 | struct CompiledExpr { |
| 127 | 127 | inner: CoreCompiled, |
modified crates/feel-py/tests/test_feel.py +15 −14
| @@ -271,40 +271,41 @@ | ||
| 271 | 271 | def test_special_char_name_resolves_against_scope(): |
| 272 | 272 | # `a-b` is the in-scope NAME when the dict binds it, subtraction otherwise; |
| 273 | 273 | # `a - b` is the same candidate (spaces are not part of the name). |
| 274 | - assert feel.evaluate("a-b", {"a-b": 5}) == 5 | |
| 275 | - assert feel.evaluate("a - b", {"a-b": 5}) == 5 | |
| 276 | - assert feel.evaluate("a-b", {"a": 7, "b": 3}) == 4 | |
| 274 | + assert feel.evaluate("a-b", {"a-b": 5}, names="symbols") == 5 | |
| 275 | + assert feel.evaluate("a - b", {"a-b": 5}, names="symbols") == 5 | |
| 276 | + assert feel.evaluate("a-b", {"a": 7, "b": 3}, names="symbols") == 4 | |
| 277 | 277 | # `(a)-(b)` is the escape hatch: subtraction even when `a-b` is the name. |
| 278 | - assert feel.evaluate("(a)-(b)", {"a": 7, "b": 3, "a-b": 100}) == 4 | |
| 278 | + assert feel.evaluate("(a)-(b)", {"a": 7, "b": 3, "a-b": 100}, names="symbols") == 4 | |
| 279 | 279 | # A hyphen name as a path base: `x-y.hour` fuses `x-y`, `.hour` stays a member. |
| 280 | - assert feel.evaluate("x-y.hour", {"x-y": {"hour": 3}}) == 3 | |
| 280 | + assert feel.evaluate("x-y.hour", {"x-y": {"hour": 3}}, names="symbols") == 3 | |
| 281 | 281 | # A member word never fuses against the root scope: `x.y-z` is (x.y) - z. |
| 282 | - assert feel.evaluate("x.y-z", {"x": {"y": 10}, "z": 3, "y-z": 1}) == 7 | |
| 282 | + assert feel.evaluate("x.y-z", {"x": {"y": 10}, "z": 3, "y-z": 1}, names="symbols") == 7 | |
| 283 | 283 | |
| 284 | 284 | |
| 285 | 285 | def test_special_char_name_resolves_host_function_from_registry(): |
| 286 | 286 | # The ambient `functions` registry is part of the parse-time scope: a call |
| 287 | 287 | # head resolves against it (third, after context and built-ins), so a |
| 288 | 288 | # hyphen-named host function is callable (ADR-47). |
| 289 | - assert feel.evaluate("my-fn(2)", {}, functions={"my-fn": lambda x: x * 10}) == 20 | |
| 289 | + assert feel.evaluate("my-fn(2)", {}, functions={"my-fn": lambda x: x * 10}, names="symbols") == 20 | |
| 290 | 290 | # Without the registry entry the run stays arithmetic (`my - fn(2)` -> null). |
| 291 | - assert feel.evaluate("my-fn(2)", {}) is None | |
| 291 | + assert feel.evaluate("my-fn(2)", {}, names="symbols") is None | |
| 292 | 292 | |
| 293 | 293 | |
| 294 | 294 | def test_compile_class_is_scope_adaptive(): |
| 295 | 295 | # ADR-48: one compiled program, both readings — it follows each context. |
| 296 | - prog = feel.compile("a-b") | |
| 296 | + prog = feel.compile("a-b", names="symbols") | |
| 297 | 297 | assert prog.evaluate({"a-b": 5}) == 5 |
| 298 | 298 | assert prog.evaluate({"a": 7, "b": 3}) == 4 |
| 299 | 299 | # The registry is part of the compiled path's scope too. |
| 300 | - call = feel.compile("my-fn(2)") | |
| 300 | + call = feel.compile("my-fn(2)", names="symbols") | |
| 301 | 301 | assert call.evaluate({}, functions={"my-fn": lambda x: x * 10}) == 20 |
| 302 | 302 | assert call.evaluate({}) is None |
| 303 | 303 | |
| 304 | 304 | |
| 305 | 305 | def test_names_levels_and_backticks(): |
| 306 | 306 | ctx = {"a": 7, "b": 3, "a-b": 100} |
| 307 | - assert feel.evaluate("a-b", ctx) == 100 | |
| 307 | + assert feel.evaluate("a-b", ctx, names="symbols") == 100 | |
| 308 | + assert feel.evaluate("a-b", ctx) == 4 # default: quoted (ADR-48) | |
| 308 | 309 | assert feel.evaluate("a-b", ctx, names="quoted") == 4 |
| 309 | 310 | assert feel.compile("a-b", names="quoted").evaluate(ctx) == 4 |
| 310 | 311 | assert feel.evaluate("`a-b`", ctx, names="quoted") == 100 |
| @@ -315,9 +316,9 @@ | ||
| 315 | 316 | |
| 316 | 317 | def test_nested_hyphen_members(): |
| 317 | 318 | # The member probe navigates lazy host objects (ADR-48). |
| 318 | - assert feel.evaluate("applicant.first-name", {"applicant": {"first-name": "Ada"}}) == "Ada" | |
| 319 | - assert feel.evaluate("orders.unit-price", {"orders": [{"unit-price": 2}, {"unit-price": 3}]}) == [2, 3] | |
| 320 | - assert feel.evaluate("o.items.x", {"o": {"items": [{"x": 1}, {"x": 2}]}}) == [1, 2] | |
| 319 | + assert feel.evaluate("applicant.first-name", {"applicant": {"first-name": "Ada"}}, names="symbols") == "Ada" | |
| 320 | + assert feel.evaluate("orders.unit-price", {"orders": [{"unit-price": 2}, {"unit-price": 3}]}, names="symbols") == [2, 3] | |
| 321 | + assert feel.evaluate("o.items.x", {"o": {"items": [{"x": 1}, {"x": 2}]}}, names="symbols") == [1, 2] | |
| 321 | 322 | |
| 322 | 323 | |
| 323 | 324 | # --- host callables: Python functions invoked from FEEL (ADR-23) ---------- # |
modified crates/feel-wasm/smoke.mjs +9 −6
| @@ -188,7 +188,8 @@ | ||
| 188 | 188 | } |
| 189 | 189 | prog.free(); |
| 190 | 190 | |
| 191 | -// Special-character names (§10.3.1.6, ADR-47): the context is the scope. `a-b` | |
| 191 | +// Special-character names (§10.3.1.6, ADR-47), in the spec mode "symbols" | |
| 192 | +// (the default is "quoted", ADR-48): the context is the scope. `a-b` | |
| 192 | 193 | // is the in-scope NAME when the object binds it, subtraction otherwise; `a - b` |
| 193 | 194 | // is the same candidate (spaces are not part of the name); `(a)-(b)` is the |
| 194 | 195 | // escape hatch; a hyphen name works as a path base. The compiled class is |
| @@ -203,7 +204,7 @@ | ||
| 203 | 204 | ["x.y-z", { x: { y: 10 }, z: 3, "y-z": 1 }, 7], |
| 204 | 205 | ]; |
| 205 | 206 | for (const [expr, ctx, expected] of nameCases) { |
| 206 | - const got = evaluate(expr, ctx); | |
| 207 | + const got = evaluate(expr, ctx, undefined, "symbols"); | |
| 207 | 208 | console.log(`${expr} (special-char name) => ${JSON.stringify(got)}`); |
| 208 | 209 | if (!eq(got, expected)) { |
| 209 | 210 | console.error(`FAIL: expected ${JSON.stringify(expected)}, got ${JSON.stringify(got)}`); |
| @@ -213,7 +214,7 @@ | ||
| 213 | 214 | { |
| 214 | 215 | // The ambient `functions` registry is part of the parse-time scope: a |
| 215 | 216 | // hyphen-named host function is callable. |
| 216 | - const got = evaluate("my-fn(2)", {}, { "my-fn": (x) => x * 10 }); | |
| 217 | + const got = evaluate("my-fn(2)", {}, { "my-fn": (x) => x * 10 }, "symbols"); | |
| 217 | 218 | if (!eq(got, 20)) { |
| 218 | 219 | console.error(`FAIL: my-fn(2) via registry expected 20, got ${JSON.stringify(got)}`); |
| 219 | 220 | ok = false; |
| @@ -224,8 +225,10 @@ | ||
| 224 | 225 | [["`Profit and loss`", { "Profit and loss": 1 }], 1], |
| 225 | 226 | [["a-b", { a: 7, b: 3, "a-b": 100 }, undefined, "quoted"], 4], |
| 226 | 227 | [["`a-b`", { "a-b": 5 }, undefined, "quoted"], 5], |
| 227 | - [["applicant.first-name", { applicant: { "first-name": "Ada" } }], "Ada"], | |
| 228 | - [["orders.unit-price", { orders: [{ "unit-price": 2 }, { "unit-price": 3 }] }], [2, 3]], | |
| 228 | + [["applicant.first-name", { applicant: { "first-name": "Ada" } }, undefined, "symbols"], "Ada"], | |
| 229 | + [["orders.unit-price", { orders: [{ "unit-price": 2 }, { "unit-price": 3 }] }, undefined, "symbols"], [2, 3]], | |
| 230 | + // Default (ADR-48, revised): quoted — `a-b` is subtraction. | |
| 231 | + [["a-b", { a: 7, b: 3, "a-b": 100 }], 4], | |
| 229 | 232 | [["o.items.x", { o: { items: [{ x: 1 }, { x: 2 }] } }], [1, 2]], |
| 230 | 233 | ]; |
| 231 | 234 | for (const [args, expected] of v2Cases) { |
| @@ -237,7 +240,7 @@ | ||
| 237 | 240 | } |
| 238 | 241 | } |
| 239 | 242 | { |
| 240 | - const prog2 = compile("a-b"); | |
| 243 | + const prog2 = compile("a-b", "symbols"); | |
| 241 | 244 | const compiledNameCases = [ |
| 242 | 245 | [{ "a-b": 5 }, 5], // scope-adaptive (ADR-48): the key, per call |
| 243 | 246 | [{ a: 7, b: 3 }, 4], |
modified crates/feel-wasm/src/lib.rs +9 −9
| @@ -37,11 +37,11 @@ | ||
| 37 | 37 | /// returned as plain `null` (the spec-shaped result); see `evaluate_strict` to |
| 38 | 38 | /// surface such errors as a thrown exception instead. |
| 39 | 39 | /// |
| 40 | -/// The context **is** the scope for §10.3.1.6 name resolution (ADR-47): a name | |
| 41 | -/// like `a-b` resolves to the context binding `a-b` when one exists, and to | |
| 42 | -/// subtraction otherwise. `names` selects the level (ADR-48): `"symbols"` | |
| 43 | -/// (default when omitted) or `"quoted"` — backticks only, `a-b` always | |
| 44 | -/// subtraction (Camunda semantics); `` `a-b` `` works at every level. | |
| 40 | +/// Special-character names (ADR-48): `names` selects the level. The default | |
| 41 | +/// (omitted or `"quoted"`) reads `a-b` as subtraction and `` `a-b` `` as the | |
| 42 | +/// name — the text decides, as in Camunda. `"symbols"` opts into the DMN spec's | |
| 43 | +/// reading (§10.3.1.6, ADR-47): the context is the scope, so `a-b` resolves to | |
| 44 | +/// the binding `a-b` when one exists. | |
| 45 | 45 | #[wasm_bindgen] |
| 46 | 46 | pub fn evaluate( |
| 47 | 47 | expr: &str, |
| @@ -84,16 +84,16 @@ | ||
| 84 | 84 | /// `FinalizationRegistry`, so the WASM-side memory is reclaimed on GC; calling |
| 85 | 85 | /// `free()` explicitly is an optional escape hatch, not required. |
| 86 | 86 | /// |
| 87 | -/// **Scope-adaptive** (ADR-48): each evaluation resolves special-character | |
| 88 | -/// names against its own context, with a small cache of per-scope parses. | |
| 87 | +/// Compiled with `"symbols"` it is **scope-adaptive** (ADR-48): each | |
| 88 | +/// evaluation resolves special-character names against its own context, with a | |
| 89 | +/// small cache of per-scope parses. | |
| 89 | 90 | #[wasm_bindgen] |
| 90 | 91 | pub struct CompiledExpr { |
| 91 | 92 | inner: CoreCompiled, |
| 92 | 93 | } |
| 93 | 94 | |
| 94 | 95 | /// Parse a FEEL expression into a reusable [`CompiledExpr`] (throws on a parse |
| 95 | -/// error). Does not evaluate. Scope-adaptive — see [`CompiledExpr`]; `names` | |
| 96 | -/// as for [`evaluate`]. | |
| 96 | +/// error). Does not evaluate. `names` as for [`evaluate`]; see [`CompiledExpr`]. | |
| 97 | 97 | #[wasm_bindgen] |
| 98 | 98 | pub fn compile(expr: &str, names: Option<String>) -> Result<CompiledExpr, JsError> { |
| 99 | 99 | let options = parse_options(names.as_deref())?; |