feat(core): reverse-open interval literals `]a..b]` (lexer retag pass)
public
feat(core): reverse-open interval literals `]a..b]` (lexer retag pass) Admit the exclusive-lower-bound spelling `]a..b]` (≡ `(a..b]`) in both value position (a `RangeLit`) and `in`-test position (a `UnaryTest::Interval`). The blocker was a token-role clash: `]` is the universal closer, so letting it also *open* a primary is not LR(1)-resolvable — the empty list `[]` collides with `[` followed by a reverse-open `]`. Resolve it in the lexer, not the grammar: a final post-pass (`mark_reverse_open`, alongside `mark_instance_of`/`join_names`) retags an *opening* `]` to a distinct `RBracketOpen` terminal. A `]` opens iff it sits in value-expected position (previous token does not complete a value) and a `..` follows at the same bracket depth before the group closes — so every list / filter / interval close and the empty list `[]` stay an ordinary `RBracket`. The grammar then admits `RBracketOpen Sum ".." Sum IntervalEnd` as a range literal and interval test (start_open = true); no `]`-closer LR state is shared, so the build.rs no-conflict gate stays green. Parser stays fully lalrpop-generated. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
| Changeset | 073f5fdc50556c1f7cf7ea15b3df2d04800029db |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@gmail.com>, |
| Parents | 7bc61ed86b5d |
| Children | |
| Pushlog | by timo via ssh |
modified crates/feel-core/src/feel.lalrpop +14 −0
| @@ -63,6 +63,10 @@ | ||
| 63 | 63 | ")" => Tok::RParen, |
| 64 | 64 | "[" => Tok::LBracket, |
| 65 | 65 | "]" => Tok::RBracket, |
| 66 | + // A `]` retagged by the lexer as a reverse-open interval opener (`]a..b]` ≡ | |
| 67 | + // `(a..b]`). A distinct terminal so `]` is not both closer and opener (not | |
| 68 | + // LR(1)-resolvable); only ever starts a range literal / interval test. | |
| 69 | + RBracketOpen => Tok::RBracketOpen, | |
| 66 | 70 | "{" => Tok::LBrace, |
| 67 | 71 | "}" => Tok::RBrace, |
| 68 | 72 | "," => Tok::Comma, |
| @@ -256,6 +260,12 @@ | ||
| 256 | 260 | "[" <lo:Sum> ".." <hi:Sum> <eo:IntervalEnd> => |
| 257 | 261 | Expr::RangeLit(Interval { start_open: false, start: Box::new(lo), end: Box::new(hi), end_open: eo }), |
| 258 | 262 | "(" <lo:Sum> ".." <hi:Sum> <eo:IntervalEnd> => |
| 263 | + Expr::RangeLit(Interval { start_open: true, start: Box::new(lo), end: Box::new(hi), end_open: eo }), | |
| 264 | + // Reverse-open `]a..b]` ≡ `(a..b]`: an exclusive lower bound spelt with a leading | |
| 265 | + // `]`. The lexer retags that opener to `RBracketOpen` (a distinct terminal), so | |
| 266 | + // this shares no LR state with the `]` closer — same start-openness as the `(` | |
| 267 | + // form, the close bracket still chosen by `IntervalEnd`. | |
| 268 | + RBracketOpen <lo:Sum> ".." <hi:Sum> <eo:IntervalEnd> => | |
| 259 | 269 | Expr::RangeLit(Interval { start_open: true, start: Box::new(lo), end: Box::new(hi), end_open: eo }), |
| 260 | 270 | }; |
| 261 | 271 | |
| @@ -325,6 +335,10 @@ | ||
| 325 | 335 | vec![UnaryTest::Interval(Interval { start_open: false, start: Box::new(lo), end: Box::new(hi), end_open: eo })], |
| 326 | 336 | "(" <lo:Sum> ".." <hi:Sum> <eo:IntervalEnd> => |
| 327 | 337 | vec![UnaryTest::Interval(Interval { start_open: true, start: Box::new(lo), end: Box::new(hi), end_open: eo })], |
| 338 | + // Reverse-open interval *test* `x in ]a..b]` (exclusive lower, ≡ `(a..b]`); the | |
| 339 | + // lexer's `RBracketOpen` opener keeps this off the `]`-closer LR state. | |
| 340 | + RBracketOpen <lo:Sum> ".." <hi:Sum> <eo:IntervalEnd> => | |
| 341 | + vec![UnaryTest::Interval(Interval { start_open: true, start: Box::new(lo), end: Box::new(hi), end_open: eo })], | |
| 328 | 342 | "[" <ts:Comma<UnaryTestElem>> "]" => ts, |
| 329 | 343 | "(" <ts:Comma<UnaryTestElem>> ")" => ts, |
| 330 | 344 | }; |
modified crates/feel-core/src/lexer.rs +89 −1
| @@ -112,6 +112,13 @@ | ||
| 112 | 112 | LBracket, |
| 113 | 113 | #[token("]")] |
| 114 | 114 | RBracket, |
| 115 | + // A `]` that *opens* a reverse-bracket interval `]a..b]` (the exclusive-lower | |
| 116 | + // notation for `(a..b]`). Never emitted by logos — `]` always lexes as | |
| 117 | + // `RBracket`; a post-pass ([`mark_reverse_open`]) retags the opening role to this | |
| 118 | + // distinct token. Without the split, `]` would be both a closer and a primary | |
| 119 | + // opener, which is not LR(1)-resolvable (the empty list `[]` collides with `[` | |
| 120 | + // then a reverse-open `]`; see ROADMAP). | |
| 121 | + RBracketOpen, | |
| 115 | 122 | #[token("{")] |
| 116 | 123 | LBrace, |
| 117 | 124 | #[token("}")] |
| @@ -293,7 +300,87 @@ | ||
| 293 | 300 | // (`year of birth`, the `day of week` built-in) stays an ordinary name-word. |
| 294 | 301 | let raw = mark_instance_of(raw); |
| 295 | 302 | // Pass 2: join multi-word names into single `Name` tokens (ADR-17). |
| 296 | - join_names(raw).into_iter() | |
| 303 | + let raw = join_names(raw); | |
| 304 | + // Pass 3: retag a reverse-open interval opener `]a..b]` to `RBracketOpen` (runs | |
| 305 | + // last so it sees joined `Name` tokens when classifying value-completed position). | |
| 306 | + mark_reverse_open(raw).into_iter() | |
| 307 | +} | |
| 308 | + | |
| 309 | +/// Retag every `]` that *opens* a reverse-bracket interval (`]a..b]`, FEEL's | |
| 310 | +/// exclusive-lower-bound spelling of `(a..b]`) as a distinct [`Tok::RBracketOpen`], | |
| 311 | +/// so the grammar can admit it as a range-literal opener. `]` otherwise plays only | |
| 312 | +/// the closer role; letting one terminal be both closer and primary opener is not | |
| 313 | +/// LR(1)-resolvable (the empty list `[]` collides with `[` then a reverse-open `]`). | |
| 314 | +/// | |
| 315 | +/// A `]` opens iff it is in **value-expected** position — the previous token does not | |
| 316 | +/// complete a value (start of input, or after an operator / `(` / `[` / `,` / `..` / | |
| 317 | +/// keyword) — **and** a `..` follows at the same bracket depth before the group | |
| 318 | +/// closes (the shape of a reverse-open interval; see [`reverse_open_ahead`]). Every | |
| 319 | +/// other `]` — a list/filter/interval close, and the empty list `[]` (no `..` ahead) | |
| 320 | +/// — stays an ordinary `RBracket`. Lex errors reset the position to value-expected. | |
| 321 | +fn mark_reverse_open(toks: Vec<Spanned>) -> Vec<Spanned> { | |
| 322 | + let mut out = toks; | |
| 323 | + let mut prev_ends_value = false; | |
| 324 | + for i in 0..out.len() { | |
| 325 | + let Ok((start, tok, end)) = &out[i] else { | |
| 326 | + prev_ends_value = false; | |
| 327 | + continue; | |
| 328 | + }; | |
| 329 | + if matches!(tok, Tok::RBracket) && !prev_ends_value && reverse_open_ahead(&out, i + 1) { | |
| 330 | + let (start, end) = (*start, *end); | |
| 331 | + out[i] = Ok((start, Tok::RBracketOpen, end)); | |
| 332 | + // An opener does not complete a value: the start endpoint follows. | |
| 333 | + prev_ends_value = false; | |
| 334 | + } else { | |
| 335 | + prev_ends_value = ends_value(tok); | |
| 336 | + } | |
| 337 | + } | |
| 338 | + out | |
| 339 | +} | |
| 340 | + | |
| 341 | +/// Whether a token in this position completes a value, so a following `]` is a | |
| 342 | +/// *closer* (not a reverse-open opener). True for the value-producing terminals and | |
| 343 | +/// the three closing brackets; false for operators, openers, separators and keywords. | |
| 344 | +fn ends_value(tok: &Tok) -> bool { | |
| 345 | + matches!( | |
| 346 | + tok, | |
| 347 | + Tok::Num(_) | |
| 348 | + | Tok::Str(_) | |
| 349 | + | Tok::TemporalLit(_) | |
| 350 | + | Tok::Name(_) | |
| 351 | + | Tok::True | |
| 352 | + | Tok::False | |
| 353 | + | Tok::Null | |
| 354 | + | Tok::RParen | |
| 355 | + | Tok::RBracket | |
| 356 | + | Tok::RBrace | |
| 357 | + ) | |
| 358 | +} | |
| 359 | + | |
| 360 | +/// Scan forward from `from` (just past a candidate opener `]`) for the `..` that | |
| 361 | +/// makes it a reverse-open interval. Returns true on a `..` at bracket depth 0; false | |
| 362 | +/// on a depth-0 `,` (a list separator), a closer that pops above the start depth, a | |
| 363 | +/// lex error, or end of input — none of which fit `] endpoint .. endpoint close`. | |
| 364 | +fn reverse_open_ahead(toks: &[Spanned], from: usize) -> bool { | |
| 365 | + let mut depth: i32 = 0; | |
| 366 | + for t in &toks[from..] { | |
| 367 | + let Ok((_, tok, _)) = t else { | |
| 368 | + return false; | |
| 369 | + }; | |
| 370 | + match tok { | |
| 371 | + Tok::LParen | Tok::LBracket | Tok::LBrace | Tok::RBracketOpen => depth += 1, | |
| 372 | + Tok::DotDot if depth == 0 => return true, | |
| 373 | + Tok::Comma if depth == 0 => return false, | |
| 374 | + Tok::RParen | Tok::RBracket | Tok::RBrace => { | |
| 375 | + if depth == 0 { | |
| 376 | + return false; | |
| 377 | + } | |
| 378 | + depth -= 1; | |
| 379 | + } | |
| 380 | + _ => {} | |
| 381 | + } | |
| 382 | + } | |
| 383 | + false | |
| 297 | 384 | } |
| 298 | 385 | |
| 299 | 386 | /// Replace every adjacent `Name("instance")` `Name("of")` pair with a single |
| @@ -472,6 +559,7 @@ | ||
| 472 | 559 | Tok::RParen => ")", |
| 473 | 560 | Tok::LBracket => "[", |
| 474 | 561 | Tok::RBracket => "]", |
| 562 | + Tok::RBracketOpen => "]", | |
| 475 | 563 | Tok::LBrace => "{", |
| 476 | 564 | Tok::RBrace => "}", |
| 477 | 565 | Tok::Comma => ",", |
modified crates/feel-core/src/lib.rs +16 −0
| @@ -415,6 +415,22 @@ | ||
| 415 | 415 | assert_eq!(ev("15 in [1..10]"), b(false)); |
| 416 | 416 | // A plain list literal is still a list, not a range. |
| 417 | 417 | assert_eq!(ev("[1, 2, 3]"), list(vec![num(1.0), num(2.0), num(3.0)])); |
| 418 | + } | |
| 419 | + | |
| 420 | + #[test] | |
| 421 | + fn reverse_open_bracket_intervals() { | |
| 422 | + // `]a..b]` is FEEL's exclusive-lower spelling of `(a..b]` — identical value. | |
| 423 | + assert_eq!(ev("]1..10] = (1..10]"), b(true)); | |
| 424 | + assert_eq!(ev("]1..10] = [1..10]"), b(false)); // start openness differs | |
| 425 | + assert_eq!(ev("]1..10) = (1..10)"), b(true)); // reverse-open + open upper | |
| 426 | + // Value position: a reverse-open literal nests as a list element. | |
| 427 | + assert_eq!(ev("[]1..2], (1..2]] = [(1..2], (1..2]]"), b(true)); | |
| 428 | + // Membership-test position: `x in ]a..b]` excludes the open lower bound. | |
| 429 | + assert_eq!(ev("5 in ]1..10]"), b(true)); | |
| 430 | + assert_eq!(ev("1 in ]1..10]"), b(false)); // open lower excludes | |
| 431 | + assert_eq!(ev("10 in ]1..10]"), b(true)); // closed upper includes | |
| 432 | + // The empty list `[]` is untouched by the opener retag (no `..` follows). | |
| 433 | + assert_eq!(ev("[]"), list(vec![])); | |
| 418 | 434 | } |
| 419 | 435 | |
| 420 | 436 | #[test] |