fix(feel-core): name runs start only in variable positions (ADR-47)
draft feat/special-char-names
fix(feel-core): name runs start only in variable positions (ADR-47) A word directly after an unfused `.` is a path member and resolves against the navigated structure, never the root scope: a root key `y-z` no longer turns `x.y-z` into `x.(y-z)` — it stays `(x.y) - z`. Pure structure keywords (`return`, `in`, `then`, `else`, `satisfies`, `and`, `or`, `between`) no longer start a run either, so `return -i` yields no `return-i` candidate (which, if bound, corrupted the parse and forced feel-pg per-row re-parses / IMMUTABLE errors). Pins the root-only probe gap for nested keys (`applicant.first-name`). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| Changeset | 2647ab9465443e059e6c0fc628589c731363619b |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@gmail.com>, |
| Parents | fa1c0cd5c621 |
| Children | |
| Pushlog | by timo via ssh |
modified crates/feel-core/src/lexer.rs +42 −6
| @@ -598,16 +598,52 @@ | ||
| 598 | 598 | /// One word unit of a name run: its spelling and `(byte start, byte end)` span. |
| 599 | 599 | type RunUnit<'a> = (&'a str, usize, usize); |
| 600 | 600 | |
| 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). | |
| 611 | +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 | + }) | |
| 625 | +} | |
| 626 | + | |
| 601 | 627 | /// Scan the maximal special-character name run starting at token `i`: |
| 602 | 628 | /// `word ((-|+|*|/|.) word)*`, where a word is a joined `Name` payload or a |
| 603 | 629 | /// keyword's spelling (`tok_word` — keywords are legal name parts, and this is |
| 604 | -/// what makes `if-then`-style names reachable; §10.3.1.6). Parens, commas, | |
| 605 | -/// brackets, literals, adjacency (`a and b`) and any other token end the run. | |
| 606 | -/// Returns the per-unit `(word, start, end)` and the separator chars. | |
| 607 | -/// A lone word is not a run — `None`. | |
| 608 | -fn scan_name_run<'a>(toks: &'a [Spanned], i: usize) -> Option<(Vec<RunUnit<'a>>, Vec<char>)> { | |
| 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 | |
| 632 | +/// directly after an unfused `.` resolves against the *structure* the path | |
| 633 | +/// navigates, never against the outer scope, so a root key like `y-z` must not | |
| 634 | +/// turn `x.y-z` into a member access (`x.(y-z)`); it stays `(x.y) - z`. | |
| 635 | +/// Parens, commas, brackets, literals, adjacency (`a and b`) and any other | |
| 636 | +/// token end the run. Returns the per-unit `(word, start, end)` and the | |
| 637 | +/// separator chars. A lone word is not a run — `None`. | |
| 638 | +fn scan_name_run(toks: &[Spanned], i: usize) -> Option<(Vec<RunUnit<'_>>, Vec<char>)> { | |
| 639 | + // Member position: the previous token is a `.` that no fusion consumed (a | |
| 640 | + // fused span always ends with a word, so an unfused dot at i-1 is exactly | |
| 641 | + // the member position; ADR-47). | |
| 642 | + if i > 0 && matches!(&toks[i - 1], Ok((_, Tok::Dot, _))) { | |
| 643 | + return None; | |
| 644 | + } | |
| 609 | 645 | let (word, start, end) = match &toks[i] { |
| 610 | - Ok((s, tok, e)) => (tok_word(tok)?, *s, *e), | |
| 646 | + Ok((s, tok, e)) => (run_start_word(tok)?, *s, *e), | |
| 611 | 647 | Err(_) => return None, |
| 612 | 648 | }; |
| 613 | 649 | let mut units = vec![(word, start, end)]; |
modified crates/feel-core/src/lib.rs +78 −0
| @@ -602,6 +602,84 @@ | ||
| 602 | 602 | // Declaration slots are deferred (ADR-47): `for a-b in …` declares no |
| 603 | 603 | // hyphen loop variable — it stays a parse error. |
| 604 | 604 | assert!(compile("for a-b in [1] return a-b").is_err()); |
| 605 | + } | |
| 606 | + | |
| 607 | + #[test] | |
| 608 | + fn path_member_word_never_fuses_against_outer_scope() { | |
| 609 | + // A word directly after an unfused `.` is a MEMBER position, not a | |
| 610 | + // variable reference: it must never start a name run. A root key may not | |
| 611 | + // change how a member access parses (ADR-47; the base of the path still | |
| 612 | + // forms legitimate run candidates, so a root key `x.y` keeps beating the | |
| 613 | + // path by longest-match — see `special_char_name_vs_path_and_members`). | |
| 614 | + let ctx = ctx_of(&[ | |
| 615 | + ("x", ctx_value(&[("y", num(10.0))])), | |
| 616 | + ("z", num(3.0)), | |
| 617 | + ("y-z", num(1.0)), | |
| 618 | + ]); | |
| 619 | + // `x.y-z` is (x.y) - z = 7 — even though the root binds `y-z`. | |
| 620 | + assert_eq!(evaluate("x.y-z", &ctx).unwrap().into_lenient(), num(7.0)); | |
| 621 | + // And identically without the root key (the two parses agree again). | |
| 622 | + let no_key = ctx_of(&[("x", ctx_value(&[("y", num(10.0))])), ("z", num(3.0))]); | |
| 623 | + assert_eq!(evaluate("x.y-z", &no_key).unwrap().into_lenient(), num(7.0)); | |
| 624 | + // The member-position rule also holds after a fused path base: the | |
| 625 | + // `hour` in `Date-Time2.hour - z` never starts a run. | |
| 626 | + let dt = ctx_of(&[ | |
| 627 | + ("Date-Time2", ctx_value(&[("hour", num(3.0))])), | |
| 628 | + ("z", num(4.0)), | |
| 629 | + ("hour-z", num(99.0)), | |
| 630 | + ]); | |
| 631 | + assert_eq!( | |
| 632 | + evaluate("Date-Time2.hour - z", &dt).unwrap().into_lenient(), | |
| 633 | + num(-1.0) | |
| 634 | + ); | |
| 635 | + // Deeper chains: only the base is a variable position (`p.q - s`). | |
| 636 | + let deep = ctx_of(&[ | |
| 637 | + ("p", ctx_value(&[("q", ctx_value(&[("r", num(2.0))]))])), | |
| 638 | + ("s", num(5.0)), | |
| 639 | + ("r-s", num(1.0)), | |
| 640 | + ]); | |
| 641 | + assert_eq!(evaluate("p.q.r-s", &deep).unwrap().into_lenient(), num(-3.0)); | |
| 642 | + // Pinned v1 gap (ADR-47 deviation "names in inner scopes"): only the root | |
| 643 | + // level is probed, so a nested hyphen KEY is not reachable as a member — | |
| 644 | + // `applicant.first-name` stays `applicant.first - name`. | |
| 645 | + let nested = ctx_of(&[( | |
| 646 | + "applicant", | |
| 647 | + ctx_value(&[("first-name", Value::String("Ada".into()))]), | |
| 648 | + )]); | |
| 649 | + assert_eq!( | |
| 650 | + evaluate("applicant.first-name", &nested).unwrap().into_lenient(), | |
| 651 | + Value::Null | |
| 652 | + ); | |
| 653 | + } | |
| 654 | + | |
| 655 | + #[test] | |
| 656 | + fn structure_keywords_never_start_name_runs() { | |
| 657 | + // A run must start in a VALUE position: a Name, or a keyword that can | |
| 658 | + // occupy one (`if`, `true`, `null`, …). Pure structure keywords (`return`, | |
| 659 | + // `in`, `then`, `else`, `satisfies`, `and`, `or`, `between`) never can — | |
| 660 | + // `return -i` must not produce the candidate `return-i` (which, if bound, | |
| 661 | + // would corrupt the loop parse; and in feel-pg would force per-row | |
| 662 | + // re-parses / IMMUTABLE errors). | |
| 663 | + assert!(name_run_candidates("for i in [1,2] return -i").is_empty()); | |
| 664 | + assert!(name_run_candidates("if a then -b else -c").is_empty()); | |
| 665 | + assert!(name_run_candidates("some i in xs satisfies -i > 0").is_empty()); | |
| 666 | + // A bound `return-i` changes nothing (the candidate no longer exists). | |
| 667 | + let ctx = ctx_of(&[("return-i", num(100.0))]); | |
| 668 | + assert_eq!( | |
| 669 | + evaluate("for i in [1,2] return -i", &ctx) | |
| 670 | + .unwrap() | |
| 671 | + .into_lenient(), | |
| 672 | + list(vec![num(-1.0), num(-2.0)]) | |
| 673 | + ); | |
| 674 | + assert!( | |
| 675 | + !compile_with_probe("for i in [1,2] return -i", &ctx) | |
| 676 | + .unwrap() | |
| 677 | + .has_name_runs() | |
| 678 | + ); | |
| 679 | + // Value-capable keywords still fuse as run starts (`if-then`), and | |
| 680 | + // structure keywords are still legal as NON-starting run words (`x-in`). | |
| 681 | + assert_eq!(eval_in("if-then", &[("if-then", b(true))]), b(true)); | |
| 682 | + assert_eq!(eval_in("x-in", &[("x-in", num(4.0))]), num(4.0)); | |
| 605 | 683 | } |
| 606 | 684 | |
| 607 | 685 | #[test] |