Public repository: timo / feel

Reader

An interpreter of FEEL, written in Rust, with integrations for PostgreSQL, WASM, Lua, and Python.

fix(feel-core): a name never starts with a keyword (§10.3.1.4)

draft feat/special-char-names

fix(feel-core): a name never starts with a keyword (§10.3.1.4)

§10.3.1.4: "A name start (grammar rule 26) SHALL NOT be a literal
terminal symbol", while "a name part MAY be". Runs now start only at a
`Name` token: `if-then`, `true-x`, `return-i` are never names; keywords
stay legal as later run words (`x-in`, `a-if`). Supersedes the earlier
value-keyword allowance; docs and tests follow.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Changeset93c4c8ad8806c641be63020e99d0f31596697859
Author (claimed)Timo Stolz <timo.stolz@gmail.com>,
Parentsf13bbd34e454
Children
Pushlog
by timo via ssh

modified DECISIONS.md +7 −6

@@ -2248,7 +2248,7 @@
22482248
 ## ADR-47: Special-character names — parse-time scope probe (grammar rules 25–30, §10.3.1.6)
22492249
 
22502250
 - **Context:** A FEEL name may contain the *additional name symbols* `- + * / .` (rule 30) and
2251
-  reserved words as name parts, so `a-b`, `Date-Time`, `x.y`, `if-then` are token sequences that
2251
+  reserved words as name *parts*, so `a-b`, `Date-Time`, `x.y`, `x-in` are token sequences that
22522252
   are indistinguishable from arithmetic/path access at the token level. §10.3.1.6 resolves the
22532253
   ambiguity **against the scope**: "name tokens are matched from left to right against the
22542254
   names in-scope, and the longest match is preferred"; spaces are *not* part of the name
@@ -2285,8 +2285,9 @@
22852285
      like `total price` is already one word unit — `total price-total` is reachable) and before
22862286
      `mark_reverse_open` (a fused `Name` completes a value for the `]`-role classifier). It
22872287
      scans maximal runs `word ((-|+|*|/|.) word)*`, where a word is a joined `Name` payload or
2288
-     a keyword's spelling (`tok_word`; keywords are legal name parts — this is what makes
2289
-     `if-then` reachable). Candidate prefixes are tried **longest first** against the probe;
2288
+     — from the second word on — a keyword's spelling (`tok_word`; §10.3.1.4: "a name part MAY
2289
+     be a literal terminal symbol", so `x-in` is reachable, but "a name start SHALL NOT be",
2290
+     so `if-then` is not a name). Candidate prefixes are tried **longest first** against the probe;
22902291
      the first in-scope prefix fuses into one `Name` spanning it, the scan resumes *after* the
22912292
      fused name (`Date-Time - Date-Time2` fuses both names and keeps the minus an operator —
22922293
      the subtraction the TCK expects), and after a miss it resumes *inside* the run
@@ -2295,8 +2296,8 @@
22952296
      A run only *starts* in a variable position: never at a word directly after an unfused
22962297
      `.` (a path member resolves against the navigated structure, not the root scope — a
22972298
      root key `y-z` must not turn `x.y-z` into `x.(y-z)`; it stays `(x.y) - z`), and never
2298
-     at a pure structure keyword (`return`, `in`, `then`, `else`, `satisfies`, `and`, `or`,
2299
-     `between` — `return -i` yields no `return-i` candidate); such keywords stay legal as
2299
+     at a keyword at all (§10.3.1.4 forbids a literal terminal as name start — `return -i`
2300
+     yields no `return-i` candidate, `if-then` stays `if - then`); keywords stay legal as
23002301
      *later* run words (`x-in`). Without a probe the pass is a no-op.
23012302
   3. **API policy** — `compile_with_probe(expr, probe)` and probe-aware one-shot `evaluate`
23022303
      (the context is the scope) in feel-core and every wrapper; `compile()` and the
@@ -2342,7 +2343,7 @@
23422343
   - *`?` and non-XID name starts* (rule 28, `what if?`) stay deferred with the 0083-feel-unicode
23432344
     widening.
23442345
 
2345
-- **Consequence:** `a-b`, `Date-Time`, `x.y` and `if-then` resolve per §10.3.1.6 in the
2346
+- **Consequence:** `a-b`, `Date-Time`, `x.y` and `x-in` resolve per §10.3.1.6 in the
23462347
   one-shot paths of feel-core/feel-py/feel-wasm/feel-lua and per row in feel-pg; `compile`
23472348
   remains the deterministic parse-once contract. The wrappers' one-shot `evaluate` now
23482349
   converts the context (and registry) *before* parsing, so when both the context and the

modified README.md +1 −1

@@ -53,7 +53,7 @@
5353
   `\d`) is preserved verbatim so it reaches the regex provider untouched.
5454
 - **Special-character names (§10.3.1.6, ADR-47):** a name may contain the
5555
   additional name symbols `- + * / .` (`a-b`, `Date-Time`, `x.y` — including
56
-  reserved words as parts, `if-then`). The ambiguity against arithmetic and path
56
+  reserved words as non-initial parts, `x-in`; a name never starts with one). The ambiguity against arithmetic and path
5757
   access is resolved against the **scope**: longest in-scope match preferred,
5858
   left to right; spaces are not part of the name (`a - b` is the same candidate
5959
   as `a-b`); `(a)-(b)` forces the operator reading. One-shot `evaluate` resolves

modified ROADMAP.md +5 −4

@@ -270,8 +270,9 @@
270270
   `fuse_special_names` runs **after** `join_names` (a multi-word part like
271271
   `total price` must already be one word unit — `total price-total` is reachable)
272272
   and before `mark_reverse_open`: it scans maximal runs
273
-  `word ((-|+|*|/|.) word)*` — a word is a joined `Name` payload or a keyword's
274
-  spelling (`tok_word`; keywords are legal name parts, `if-then` is reachable) —
273
+  `word ((-|+|*|/|.) word)*` — a word is a joined `Name` payload or, after the
274
+  first, a keyword's spelling (`tok_word`; keywords are legal name *parts*,
275
+  `x-in`, but never a name start, §10.3.1.4) —
275276
   tries candidate prefixes **longest-first** against the probe, and fuses the
276277
   first hit. The scan resumes *after* a fused name (`Date-Time - Date-Time2`
277278
   keeps the minus an operator — the TCK 0007 expectation) and *inside* the run
@@ -279,8 +280,8 @@
279280
   operators (`..` `...` `**` `->`) are not name symbols. A run only *starts* in
280281
   a variable position: never at a path member (a word after an unfused `.` —
281282
   `x.y-z` stays `(x.y) - z` even when the root binds `y-z`) and never at a
282
-  structure keyword (`return`, `in`, `then`, `else`, `satisfies`, `and`, `or`,
283
-  `between` — `return -i` yields no candidate). API:
283
+  keyword (§10.3.1.4: a name start is never a literal terminal — `return -i`
284
+  yields no candidate, `if-then` is not a name). API:
284285
   `compile_with_probe` + probe-aware one-shot `evaluate` (the context is the
285286
   scope); `compile()` stays probe-free (byte-identical; all 3145 pre-existing
286287
   cases re-verified). Metadata: `CompiledExpr::has_name_runs()` (this parse

modified crates/feel-core/src/lexer.rs +17 −28

@@ -598,37 +598,26 @@
598598
 /// One word unit of a name run: its spelling and `(byte start, byte end)` span.
599599
 type RunUnit<'a> = (&'a str, usize, usize);
600600
 
601
-/// The word a token contributes to a name run **when the run starts at it**: a
602
-/// `Name` (the ordinary case — a value/operand position), or a keyword that can
603
-/// itself occupy a value position (`if`-expressions, the literals `true`/`false`
604
-/// /`null`, the comprehension starters, `not`, `function` literals — this is
605
-/// what makes names like `if-then` reachable). Pure structure/operator keywords
606
-/// (`return`, `in`, `then`, `else`, `satisfies`, `and`, `or`, `between`) never
607
-/// start a run: they hold no value position, so a candidate like `return-i`
608
-/// (from `return -i`) could never be a legitimate name — only a bound one that
609
-/// would corrupt the parse, and (in feel-pg) a per-row re-parse or an IMMUTABLE
610
-/// error for nothing. They remain legal as *later* run words (`x-in`; ADR-47).
601
+/// The word a token contributes to a name run **when the run starts at it**:
602
+/// only a `Name`. §10.3.1.4 is explicit — "A name start (grammar rule 26) SHALL
603
+/// NOT be a literal terminal symbol" — so no keyword (`if`, `true`, `return`,
604
+/// `in`, …) can begin a name, while "a name part MAY be a literal terminal
605
+/// symbol": keywords stay legal as *later* run words (`x-in`, `a-if`). Besides
606
+/// conformance this keeps spurious candidates out (`return -i` yields no
607
+/// `return-i`, which if bound would corrupt the loop parse and, in feel-pg, force
608
+/// a per-row re-parse or an IMMUTABLE error; ADR-47).
611609
 fn run_start_word(tok: &Tok) -> Option<&str> {
612
-    Some(match tok {
613
-        Tok::Name(s) => s.as_str(),
614
-        Tok::If => "if",
615
-        Tok::True => "true",
616
-        Tok::False => "false",
617
-        Tok::Null => "null",
618
-        Tok::For => "for",
619
-        Tok::Some => "some",
620
-        Tok::Every => "every",
621
-        Tok::Not => "not",
622
-        Tok::Function => "function",
623
-        _ => return None,
624
-    })
610
+    match tok {
611
+        Tok::Name(s) => Some(s.as_str()),
612
+        _ => None,
613
+    }
625614
 }
626615
 
627616
 /// Scan the maximal special-character name run starting at token `i`:
628
-/// `word ((-|+|*|/|.) word)*`, where a word is a joined `Name` payload or a
629
-/// keyword's spelling (`tok_word` — keywords are legal name parts, and this is
630
-/// what makes `if-then`-style names reachable; §10.3.1.6). The run must START
631
-/// in a value position ([`run_start_word`]) that is not a path member — a word
617
+/// `word ((-|+|*|/|.) word)*`, where a later word is a joined `Name` payload or
618
+/// a keyword's spelling (`tok_word` — keywords are legal name *parts*,
619
+/// §10.3.1.4; `x-in`). The run must START at a `Name` ([`run_start_word`] — a
620
+/// name start is never a keyword) that is not a path member — a word
632621
 /// directly after an unfused `.` resolves against the *structure* the path
633622
 /// navigates, never against the outer scope, so a root key like `y-z` must not
634623
 /// turn `x.y-z` into a member access (`x.(y-z)`); it stays `(x.y) - z`.
@@ -689,7 +678,7 @@
689678
 /// subtraction the TCK expects, reproduced by longest-match-at-each-position.
690679
 /// When no prefix is in scope the run is left alone (arithmetic stands) and the
691680
 /// scan resumes at the next token, so `a-b-c` with only `b-c` bound parses as
692
-/// `a - b-c`. A keyword word-unit makes reserved-word parts reachable (`if-then`)
681
+/// `a - b-c`. A keyword word-unit makes reserved-word parts reachable (`x-in`)
693682
 /// but, without a symbol, adjacency never forms a run (`a and b` is untouched).
694683
 ///
695684
 /// Returns the tokens plus `true` when any run was seen — the parse consulted

modified crates/feel-core/src/lib.rs +17 −13

@@ -485,9 +485,9 @@
485485
         assert_eq!(eval_in("a*b", &[("a*b", num(2.0))]), num(2.0));
486486
         assert_eq!(eval_in("a/b", &[("a/b", num(3.0))]), num(3.0));
487487
         assert_eq!(eval_in("a.b", &[("a.b", num(4.0))]), num(4.0));
488
-        // Keywords are legal name parts in symbol runs (`if-then`; unfused, the
489
-        // `if` keyword would make it a parse error).
490
-        assert_eq!(eval_in("if-then", &[("if-then", b(true))]), b(true));
488
+        // Keywords are legal name PARTS in symbol runs (`x-in`; unfused, `in`
489
+        // would make it a membership test) — but never a name START (§10.3.1.4).
490
+        assert_eq!(eval_in("x-in", &[("x-in", b(true))]), b(true));
491491
         // A multi-word name part joins before fusion (`total price-total`).
492492
         assert_eq!(
493493
             eval_in("total price-total", &[("total price-total", num(5.0))]),
@@ -585,7 +585,8 @@
585585
         assert!(name_run_candidates("1 + 1").is_empty());
586586
         assert!(name_run_candidates("a and b").is_empty());
587587
         assert_eq!(name_run_candidates("x.y"), vec!["x.y"]);
588
-        assert_eq!(name_run_candidates("if-then"), vec!["if-then"]);
588
+        assert_eq!(name_run_candidates("x-in"), vec!["x-in"]);
589
+        assert!(name_run_candidates("if-then").is_empty()); // keyword start
589590
         assert_eq!(
590591
             name_run_candidates("Date-Time - Date-Time2"),
591592
             vec![
@@ -675,12 +676,11 @@
675676
     }
676677
 
677678
     #[test]
678
-    fn structure_keywords_never_start_name_runs() {
679
-        // A run must start in a VALUE position: a Name, or a keyword that can
680
-        // occupy one (`if`, `true`, `null`, …). Pure structure keywords (`return`,
681
-        // `in`, `then`, `else`, `satisfies`, `and`, `or`, `between`) never can —
682
-        // `return -i` must not produce the candidate `return-i` (which, if bound,
683
-        // would corrupt the loop parse; and in feel-pg would force per-row
679
+    fn keywords_never_start_name_runs() {
680
+        // A run must start at a Name: §10.3.1.4 — "A name start SHALL NOT be a
681
+        // literal terminal symbol", so no keyword starts a run. `return -i`
682
+        // must not produce the candidate `return-i` (which, if bound, would
683
+        // corrupt the loop parse; and in feel-pg would force per-row
684684
         // re-parses / IMMUTABLE errors).
685685
         assert!(name_run_candidates("for i in [1,2] return -i").is_empty());
686686
         assert!(name_run_candidates("if a then -b else -c").is_empty());
@@ -698,10 +698,14 @@
698698
                 .unwrap()
699699
                 .has_name_runs()
700700
         );
701
-        // Value-capable keywords still fuse as run starts (`if-then`), and
702
-        // structure keywords are still legal as NON-starting run words (`x-in`).
703
-        assert_eq!(eval_in("if-then", &[("if-then", b(true))]), b(true));
701
+        // Value keywords do not start runs either: a bound `if-then` is not a
702
+        // name (it stays the — invalid — `if - then`), nor is `true-x`.
703
+        let if_then = ctx_of(&[("if-then", b(true))]);
704
+        assert!(evaluate("if-then", &if_then).is_err());
705
+        assert_eq!(eval_in("true-x", &[("true-x", num(1.0))]), Value::Null);
706
+        // Keywords stay legal as NON-starting run words (`x-in`, `a-if`).
704707
         assert_eq!(eval_in("x-in", &[("x-in", num(4.0))]), num(4.0));
708
+        assert_eq!(eval_in("a-if", &[("a-if", num(5.0))]), num(5.0));
705709
     }
706710
 
707711
     #[test]

modified docs/special-char-names-research.md +2 −1

@@ -118,7 +118,8 @@
118118
   `word (op word)*` with op ∈ {`-`, `+`, `*`, `/`} (and `.` for dot-names),
119119
   where a word is a `Name` or a keyword's spelling (`tok_word` already exists,
120120
   `lexer.rs` — keywords are legal name parts, which is what makes `if-then`
121
-  reachable). At each run start, try candidates **longest-first** against the
121
+  reachable). *[Superseded: §10.3.1.4 forbids a keyword as name start —
122
+  shipped runs start only at a `Name`; `x-in` is reachable, `if-then` is not.]* At each run start, try candidates **longest-first** against the
122123
   probe; fuse the longest hit into one `Name` token (space-normalised payload,
123124
   as today) and continue after it. This is mechanically precedented:
124125
   `match_keyword_phrase` / `mark_instance_of` fuse tokens the same way. Est.