Public repository: timo / feel

Reader

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

feat(core): reverse-close interval literals `[a..b[` (filter-free endpoint)

public

feat(core): reverse-close interval literals `[a..b[` (filter-free endpoint)

Admit the exclusive-upper-bound spelling `[a..b[` (≡ `[a..b)`) in value and
`in`-test position by adding `[` as a third `IntervalEnd` bracket.

The blocker was a shift/reduce clash, resolvable purely in the grammar (no lexer
state, unlike `]a..b]`): the high endpoint is a `Sum` → `Postfix`, whose filter
production `Postfix "[" Expr "]"` wants to shift a trailing `[` as a subscript,
competing with reducing it as the interval close. Resolve it with a filter-free
high-endpoint nonterminal `Endpoint` (the `Sum` chain with the filter dropped from
`EpPostfix`, base case reusing the shared `Primary`), used for every interval's
high endpoint. With no `Postfix "[" …` item live there, a trailing `[` can only be
the close bracket — LR(1)-clean, build.rs gate green.

Cost: an interval's high endpoint can no longer itself end in a `[...]` subscript
(e.g. `[a..xs[2]]`), a shape no test/TCK case uses; the low endpoint and ordinary
filters are unaffected. Composes with `]a..b]`: `]a..b[` ≡ `(a..b)`.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Changesetc025ae646afa3ce3e02e0dfcb9acff94effb7200
Author (claimed)Timo Stolz <timo.stolz@gmail.com>,
Parents073f5fdc5055
Children
Pushlog
by timo via ssh

modified crates/feel-core/src/feel.lalrpop +51 −6

@@ -257,15 +257,15 @@
257257
 // The `in`-RHS *test* form keeps its own `Interval` rule below (a unary test, not a
258258
 // value), reachable only after `in`, so the two never share an LR state.
259259
 RangeLit: Expr = {
260
-    "[" <lo:Sum> ".." <hi:Sum> <eo:IntervalEnd> =>
260
+    "[" <lo:Sum> ".." <hi:Endpoint> <eo:IntervalEnd> =>
261261
         Expr::RangeLit(Interval { start_open: false, start: Box::new(lo), end: Box::new(hi), end_open: eo }),
262
-    "(" <lo:Sum> ".." <hi:Sum> <eo:IntervalEnd> =>
262
+    "(" <lo:Sum> ".." <hi:Endpoint> <eo:IntervalEnd> =>
263263
         Expr::RangeLit(Interval { start_open: true, start: Box::new(lo), end: Box::new(hi), end_open: eo }),
264264
     // Reverse-open `]a..b]` ≡ `(a..b]`: an exclusive lower bound spelt with a leading
265265
     // `]`. The lexer retags that opener to `RBracketOpen` (a distinct terminal), so
266266
     // this shares no LR state with the `]` closer — same start-openness as the `(`
267267
     // form, the close bracket still chosen by `IntervalEnd`.
268
-    RBracketOpen <lo:Sum> ".." <hi:Sum> <eo:IntervalEnd> =>
268
+    RBracketOpen <lo:Sum> ".." <hi:Endpoint> <eo:IntervalEnd> =>
269269
         Expr::RangeLit(Interval { start_open: true, start: Box::new(lo), end: Box::new(hi), end_open: eo }),
270270
 };
271271
 
@@ -331,13 +331,13 @@
331331
 InRhs: Vec<UnaryTest> = {
332332
     <op:CmpOp> <e:Sum> => vec![UnaryTest::Compare(op, Box::new(e))],
333333
     <e:NbSum> => vec![unary_test_from_expr(e)],
334
-    "[" <lo:Sum> ".." <hi:Sum> <eo:IntervalEnd> =>
334
+    "[" <lo:Sum> ".." <hi:Endpoint> <eo:IntervalEnd> =>
335335
         vec![UnaryTest::Interval(Interval { start_open: false, start: Box::new(lo), end: Box::new(hi), end_open: eo })],
336
-    "(" <lo:Sum> ".." <hi:Sum> <eo:IntervalEnd> =>
336
+    "(" <lo:Sum> ".." <hi:Endpoint> <eo:IntervalEnd> =>
337337
         vec![UnaryTest::Interval(Interval { start_open: true, start: Box::new(lo), end: Box::new(hi), end_open: eo })],
338338
     // Reverse-open interval *test* `x in ]a..b]` (exclusive lower, ≡ `(a..b]`); the
339339
     // lexer's `RBracketOpen` opener keeps this off the `]`-closer LR state.
340
-    RBracketOpen <lo:Sum> ".." <hi:Sum> <eo:IntervalEnd> =>
340
+    RBracketOpen <lo:Sum> ".." <hi:Endpoint> <eo:IntervalEnd> =>
341341
         vec![UnaryTest::Interval(Interval { start_open: true, start: Box::new(lo), end: Box::new(hi), end_open: eo })],
342342
     "[" <ts:Comma<UnaryTestElem>> "]" => ts,
343343
     "(" <ts:Comma<UnaryTestElem>> ")" => ts,
@@ -407,9 +407,54 @@
407407
     Name => Expr::Var(<>),
408408
 };
409409
 
410
+// The HIGH endpoint of an interval — a filter-free clone of the `Sum` chain. It is
411
+// identical to `Sum`/`Product`/`Power`/`Unary`/`Postfix` except `EpPostfix` drops the
412
+// `Postfix "[" Expr "]"` filter production, so a `[` *after* the high endpoint is
413
+// unambiguously the reverse-CLOSE bracket of `[a..b[` (≡ `[a..b)`), never a subscript.
414
+// Only the high endpoint needs this — the low endpoint is always followed by `..`, so
415
+// no `[` can attach there. The base case reuses the shared `Primary` (which carries no
416
+// filter; the filter lives only in `Postfix`), so a grouping / list / nested range
417
+// endpoint still parses. Cost: an interval's high endpoint cannot itself end in a
418
+// `[...]` subscript (e.g. `[a..xs[2]]`) — a shape no test/TCK case uses. Mirrors the
419
+// `NbSum` precedent (a restricted arithmetic clone for one syntactic position).
420
+Endpoint: Expr = {
421
+    <l:Endpoint> "+" <r:EpProduct> => Expr::Arith(ArithOp::Add, Box::new(l), Box::new(r)),
422
+    <l:Endpoint> "-" <r:EpProduct> => Expr::Arith(ArithOp::Sub, Box::new(l), Box::new(r)),
423
+    EpProduct,
424
+};
425
+
426
+EpProduct: Expr = {
427
+    <l:EpProduct> "*" <r:EpPower> => Expr::Arith(ArithOp::Mul, Box::new(l), Box::new(r)),
428
+    <l:EpProduct> "/" <r:EpPower> => Expr::Arith(ArithOp::Div, Box::new(l), Box::new(r)),
429
+    EpPower,
430
+};
431
+
432
+EpPower: Expr = {
433
+    <l:EpPower> "**" <r:EpUnary> => Expr::Arith(ArithOp::Pow, Box::new(l), Box::new(r)),
434
+    EpUnary,
435
+};
436
+
437
+EpUnary: Expr = {
438
+    "-" <e:EpUnary> => Expr::Neg(Box::new(e)),
439
+    EpPostfix,
440
+};
441
+
442
+// Like `Postfix` but WITHOUT the `[Expr]` filter production (see `Endpoint`).
443
+EpPostfix: Expr = {
444
+    <e:EpPostfix> "." <n:Name> => Expr::Path(Box::new(e), n),
445
+    <n:Name> "(" ")" => Expr::FnCall(n, CallArgs::default()),
446
+    <n:Name> "(" <a:Comma<Arg>> ")" => Expr::FnCall(n, callargs_from(a)),
447
+    Primary,
448
+};
449
+
410450
 IntervalEnd: bool = {
411451
     "]" => false, // closed
412452
     ")" => true,  // open
453
+    // Reverse-CLOSE `[a..b[` ≡ `[a..b)`: an exclusive upper bound spelt with a
454
+    // trailing `[`. Admissible only because the high endpoint is the filter-free
455
+    // `Endpoint` nonterminal (no `Postfix "[" Expr "]"`), so this `[` cannot also be
456
+    // a subscript on the endpoint — keeping it LR(1)-clean (build.rs gate).
457
+    "[" => true,  // open (reverse-close)
413458
 };
414459
 
415460
 // `Num` (f64 literal), `Str` (double-quoted, no escapes) and `Name` (a Unicode

modified crates/feel-core/src/lib.rs +18 −0

@@ -431,6 +431,24 @@
431431
         assert_eq!(ev("10 in ]1..10]"), b(true)); // closed upper includes
432432
         // The empty list `[]` is untouched by the opener retag (no `..` follows).
433433
         assert_eq!(ev("[]"), list(vec![]));
434
+    }
435
+
436
+    #[test]
437
+    fn reverse_close_bracket_intervals() {
438
+        // `[a..b[` is FEEL's exclusive-upper spelling of `[a..b)` — identical value.
439
+        assert_eq!(ev("[1..10[ = [1..10)"), b(true));
440
+        assert_eq!(ev("[1..10[ = [1..10]"), b(false)); // end openness differs
441
+        assert_eq!(ev("(1..10[ = (1..10)"), b(true)); // open lower + reverse-close
442
+        // Both reverse brackets at once: `]a..b[` ≡ `(a..b)`.
443
+        assert_eq!(ev("]1..10[ = (1..10)"), b(true));
444
+        // Membership-test position: `x in [a..b[` excludes the open upper bound.
445
+        assert_eq!(ev("9 in [1..10["), b(true));
446
+        assert_eq!(ev("10 in [1..10["), b(false)); // open upper excludes
447
+        assert_eq!(ev("1 in [1..10["), b(true)); // closed lower includes
448
+        // Arithmetic still works in the (filter-free) high endpoint.
449
+        assert_eq!(ev("9 in [1..5+5["), b(true));
450
+        // Regression: a `[` after a completed value is still a filter subscript.
451
+        assert_eq!(ev("[10, 20, 30][2]"), num(20.0));
434452
     }
435453
 
436454
     #[test]