| 1 | // (S-)FEEL grammar — the "core language" slice. |
| 2 | // |
| 3 | // Scope: literals (number, string, boolean, null, list), arithmetic |
| 4 | // (`+ - * /`, `**`, unary `-`), comparison (`< <= > >= = !=`, `between`, `in`), |
| 5 | // three-valued logic (`and`/`or`/`not`), control flow (`if/then/else`, |
| 6 | // `for … return`, `some/every … satisfies`), variable references, and |
| 7 | // built-in function calls (`abs(-1)`, `string length("x")`, …; see DECISIONS.md |
| 8 | // ADR-15/ADR-17). Names may contain spaces (`full name`, `string length`): the |
| 9 | // lexer joins multi-word names into one `Name` token (ADR-17), so the grammar is |
| 10 | // unchanged — `Name` simply carries a space-normalised payload. Still a |
| 11 | // deliberate subset of full FEEL: no special characters in names (`+ - * / . '`), |
| 12 | // no temporal types, no ranges as first-class values (see ADR-7/ADR-8). It is |
| 13 | // left-recursive on purpose; lalrpop (LR(1)) handles that natively, which is the |
| 14 | // reason we chose it. |
| 15 | |
| 16 | use crate::ast::{ArgEntry, ArithOp, CallArgs, CmpOp, Expr, FeelType, Interval, IterSource, Quantifier, TypeArg, UnaryTest, callargs_from, generic_type, simple_type, unary_test_from_expr}; |
| 17 | use crate::lexer::{LexError, Tok}; |
| 18 | |
| 19 | grammar; |
| 20 | |
| 21 | // External tokens: the parser consumes the `logos`-based stream from |
| 22 | // `crate::lexer::lex` (see `lexer.rs`) instead of lalrpop's built-in regex lexer. |
| 23 | // The `Num`/`Str`/`Name` terminals carry their payload; everything else is a unit |
| 24 | // token. This replaces the old in-grammar `r"…"` terminal rules. |
| 25 | extern { |
| 26 | type Location = usize; |
| 27 | type Error = LexError; |
| 28 | |
| 29 | enum Tok { |
| 30 | "for" => Tok::For, |
| 31 | "in" => Tok::In, |
| 32 | "return" => Tok::Return, |
| 33 | "if" => Tok::If, |
| 34 | "then" => Tok::Then, |
| 35 | "else" => Tok::Else, |
| 36 | "some" => Tok::Some, |
| 37 | "every" => Tok::Every, |
| 38 | "satisfies" => Tok::Satisfies, |
| 39 | "or" => Tok::Or, |
| 40 | "and" => Tok::And, |
| 41 | "between" => Tok::Between, |
| 42 | "true" => Tok::True, |
| 43 | "false" => Tok::False, |
| 44 | "null" => Tok::Null, |
| 45 | "not" => Tok::Not, |
| 46 | "function" => Tok::Function, |
| 47 | "instance of" => Tok::InstanceOf, |
| 48 | "->" => Tok::Arrow, |
| 49 | "<" => Tok::Lt, |
| 50 | "<=" => Tok::Le, |
| 51 | ">" => Tok::Gt, |
| 52 | ">=" => Tok::Ge, |
| 53 | "=" => Tok::Eq, |
| 54 | "!=" => Tok::Ne, |
| 55 | "+" => Tok::Plus, |
| 56 | "-" => Tok::Minus, |
| 57 | "*" => Tok::Star, |
| 58 | "/" => Tok::Slash, |
| 59 | "**" => Tok::StarStar, |
| 60 | "." => Tok::Dot, |
| 61 | ".." => Tok::DotDot, |
| 62 | "..." => Tok::DotDotDot, |
| 63 | "(" => Tok::LParen, |
| 64 | ")" => Tok::RParen, |
| 65 | "[" => Tok::LBracket, |
| 66 | "]" => Tok::RBracket, |
| 67 | // A `]` retagged by the lexer as a reverse-open interval opener (`]a..b]` ≡ |
| 68 | // `(a..b]`). A distinct terminal so `]` is not both closer and opener (not |
| 69 | // LR(1)-resolvable); only ever starts a range literal / interval test. |
| 70 | RBracketOpen => Tok::RBracketOpen, |
| 71 | "{" => Tok::LBrace, |
| 72 | "}" => Tok::RBrace, |
| 73 | "," => Tok::Comma, |
| 74 | ":" => Tok::Colon, |
| 75 | Num => Tok::Num(<f64>), |
| 76 | Str => Tok::Str(<String>), |
| 77 | TemporalLit => Tok::TemporalLit(<String>), |
| 78 | Name => Tok::Name(<String>), |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | pub Expr: Expr = TextualExpr; |
| 83 | |
| 84 | /// A structural FEEL type expression (`number`, `list<string>`, |
| 85 | /// `context<a: number>`, …), used by `parse_type` at the crate root. |
| 86 | pub Type: FeelType = InstanceType; |
| 87 | |
| 88 | // Loosest-binding forms. `for`/`if`/`some`/`every` extend to the end of their |
| 89 | // sub-expressions (which are themselves full TextualExprs), enabling nesting |
| 90 | // like `for i in [1,2,3] return if i > 0 then i else -i`. |
| 91 | TextualExpr: Expr = { |
| 92 | // `for`/`some`/`every` take one *or more* comma-separated iteration contexts |
| 93 | // (`for i in a, j in b return …`; FEEL rules 44/46) via the `Comma<IterCtx>` |
| 94 | // helper. A top-level `,` is only consumed here — `IterSrc` never eats one — |
| 95 | // so after each context the parser sees `,` or `return`/`satisfies`, which |
| 96 | // keeps it LR(1) (the `build.rs` gate fails on any conflict). |
| 97 | "for" <bs:Comma<IterCtx>> "return" <b:TextualExpr> => |
| 98 | Expr::For { bindings: bs, body: Box::new(b) }, |
| 99 | "if" <c:TextualExpr> "then" <t:TextualExpr> "else" <e:TextualExpr> => |
| 100 | Expr::If(Box::new(c), Box::new(t), Box::new(e)), |
| 101 | "some" <bs:Comma<IterCtx>> "satisfies" <p:TextualExpr> => |
| 102 | Expr::Quantified { kind: Quantifier::Some, bindings: bs, predicate: Box::new(p) }, |
| 103 | "every" <bs:Comma<IterCtx>> "satisfies" <p:TextualExpr> => |
| 104 | Expr::Quantified { kind: Quantifier::Every, bindings: bs, predicate: Box::new(p) }, |
| 105 | // A FEEL function literal `function(a, b) body` (ADR-23/ADR-35 follow-up). Like |
| 106 | // `if`/`for`, the body is a full TextualExpr. `function` is a keyword token so |
| 107 | // `function(` is unambiguously a literal. Each formal parameter is a bare name or |
| 108 | // a typed name `name: Type` (FEEL grammar rule 56); the optional type annotation |
| 109 | // is now preserved alongside the name for invocation-context coercion (§10.3.2.9.4). |
| 110 | "function" "(" ")" <b:TextualExpr> => |
| 111 | Expr::FnDef { params: vec![], body: Box::new(b) }, |
| 112 | "function" "(" <ps:Comma<FormalParam>> ")" <b:TextualExpr> => |
| 113 | Expr::FnDef { params: ps, body: Box::new(b) }, |
| 114 | Disjunction, |
| 115 | }; |
| 116 | |
| 117 | Disjunction: Expr = { |
| 118 | <l:Disjunction> "or" <r:Conjunction> => Expr::Or(Box::new(l), Box::new(r)), |
| 119 | Conjunction, |
| 120 | }; |
| 121 | |
| 122 | Conjunction: Expr = { |
| 123 | <l:Conjunction> "and" <r:Comparison> => Expr::And(Box::new(l), Box::new(r)), |
| 124 | Comparison, |
| 125 | }; |
| 126 | |
| 127 | // Comparison bounds are `Sum` (not full expressions) so that the `and` in |
| 128 | // `x between a and b` is unambiguously the between-`and`, not logical `and`. |
| 129 | Comparison: Expr = { |
| 130 | <l:Sum> <op:CmpOp> <r:Sum> => Expr::Compare(op, Box::new(l), Box::new(r)), |
| 131 | <v:Sum> "between" <lo:Sum> "and" <hi:Sum> => Expr::Between(Box::new(v), Box::new(lo), Box::new(hi)), |
| 132 | <v:Sum> "in" <t:InRhs> => Expr::In(Box::new(v), t), |
| 133 | // Type predicate `value instance of type` (DMN §10.3.2.9, rule 51). The lexer |
| 134 | // fuses `instance of` into one operator token, so after a `Sum` the lookahead is |
| 135 | // unambiguous (no clash with the comparison/`between`/`in` alternatives). |
| 136 | <v:Sum> "instance of" <t:InstanceType> => Expr::InstanceOf(Box::new(v), t), |
| 137 | Sum, |
| 138 | }; |
| 139 | |
| 140 | // The restricted `type` grammar (spec rule 52), reachable only after `instance of`. |
| 141 | // `<`/`>` are the comparison tokens reused here as generic brackets; the field-vs- |
| 142 | // type decision in `TypeArg` is the same one-token (`:`) lookahead the `Arg` rule |
| 143 | // already uses, so this stays LR(1) (the build.rs gate fails on any conflict). |
| 144 | InstanceType: FeelType = { |
| 145 | TypeName, |
| 146 | <n:Name> "<" <args:Comma<TypeArg>> ">" => generic_type(n, args), |
| 147 | "function" "<" ">" "->" <r:InstanceType> => FeelType::Function(vec![], Box::new(r)), |
| 148 | "function" "<" <ps:Comma<InstanceType>> ">" "->" <r:InstanceType> => FeelType::Function(ps, Box::new(r)), |
| 149 | }; |
| 150 | |
| 151 | // A primitive / `Any` / `Null` / unknown type name. The keyword-containing temporal |
| 152 | // names (`date and time`, `years and months duration`, `days and time duration`) are |
| 153 | // already a single `Name` token by the time the parser sees them — the lexer joins |
| 154 | // these reserved phrases wherever they occur (ADR-25) — so one `Name` covers every |
| 155 | // named type. `simple_type` maps it (an unrecognised name → `FeelType::Unknown`). |
| 156 | TypeName: FeelType = { |
| 157 | <n:Name> => simple_type(&n), |
| 158 | }; |
| 159 | |
| 160 | // One argument inside a generic `<…>`: a bare type (`list`/`range`/`function` params) |
| 161 | // or a named field (`context<a: string>`). After a `Name`, lookahead `:` ⇒ field, |
| 162 | // otherwise the `Name` begins a type — mirrors the call-`Arg` rule below. |
| 163 | TypeArg: TypeArg = { |
| 164 | <t:InstanceType> => TypeArg::Ty(t), |
| 165 | <k:Name> ":" <t:InstanceType> => TypeArg::Field(k, t), |
| 166 | }; |
| 167 | |
| 168 | CmpOp: CmpOp = { |
| 169 | "<" => CmpOp::Lt, |
| 170 | "<=" => CmpOp::Le, |
| 171 | ">" => CmpOp::Gt, |
| 172 | ">=" => CmpOp::Ge, |
| 173 | "=" => CmpOp::Eq, |
| 174 | "!=" => CmpOp::Ne, |
| 175 | }; |
| 176 | |
| 177 | Sum: Expr = { |
| 178 | <l:Sum> "+" <r:Product> => Expr::Arith(ArithOp::Add, Box::new(l), Box::new(r)), |
| 179 | <l:Sum> "-" <r:Product> => Expr::Arith(ArithOp::Sub, Box::new(l), Box::new(r)), |
| 180 | Product, |
| 181 | }; |
| 182 | |
| 183 | Product: Expr = { |
| 184 | <l:Product> "*" <r:Power> => Expr::Arith(ArithOp::Mul, Box::new(l), Box::new(r)), |
| 185 | <l:Product> "/" <r:Power> => Expr::Arith(ArithOp::Div, Box::new(l), Box::new(r)), |
| 186 | Power, |
| 187 | }; |
| 188 | |
| 189 | // Exponentiation is LEFT-associative in DMN FEEL: `3 ** 4 ** 5` == (3**4)**5 |
| 190 | // (verified against DMN-TCK 0075-feel-exponent). |
| 191 | Power: Expr = { |
| 192 | <l:Power> "**" <r:Unary> => Expr::Arith(ArithOp::Pow, Box::new(l), Box::new(r)), |
| 193 | Unary, |
| 194 | }; |
| 195 | |
| 196 | // Unary minus binds tighter than `**`: `-3 ** 2` == (-3)**2 == 9 (per TCK 0075). |
| 197 | Unary: Expr = { |
| 198 | "-" <e:Unary> => Expr::Neg(Box::new(e)), |
| 199 | Postfix, |
| 200 | }; |
| 201 | |
| 202 | // Postfix path access `a.b.c` binds tighter than unary minus, so `-a.b` is |
| 203 | // `-(a.b)`. `..` is its own (longer) token, so it never collides with `.`. |
| 204 | // |
| 205 | // A built-in call `name(args)` also lives here. After a `Name`, an LR(1) |
| 206 | // lookahead of `(` shifts into the call; any other lookahead reduces to |
| 207 | // `Primary → Name → Var`, so plain variable references are unaffected. Multi-word |
| 208 | // heads (`string length(…)`) are a single `Name` token by the time the parser |
| 209 | // sees them (the lexer joined them), so this production needs no change. |
| 210 | // `not(...)` keeps its own Primary rule because `not` is a keyword token (not a |
| 211 | // `Name`), so it never reaches this production. |
| 212 | // A postfix filter / index `value[expr]` (DMN §10.3.2.5) also lives here, at the |
| 213 | // same precedence as path access. A `[` in *primary* position is a list literal |
| 214 | // (`Primary → List`); a `[` directly after a completed `Postfix` is unambiguously |
| 215 | // a subscript; interval `[a..b]` only follows `in`. This mirrors the shift-after- |
| 216 | // value pattern of the `.` rule above and stays LR(1) (verified: lalrpop builds |
| 217 | // conflict-free). |
| 218 | Postfix: Expr = { |
| 219 | <e:Postfix> "." <n:Name> => Expr::Path(Box::new(e), n), |
| 220 | // Descendant navigation `e ... name` (DMN §10.3.2.7, grammar rule 68): collect |
| 221 | // every value bound to `name` in `e` and all nested contexts, depth-first. Same |
| 222 | // precedence/position as the `.` path operator (it too binds tighter than unary |
| 223 | // minus); `...` is its own (longest) token, so it never collides with `.`/`..`. |
| 224 | // LR(1)-clean (the build.rs no-conflict gate proves it): `...` is a unique |
| 225 | // lookahead after a completed `Postfix`, distinct from the `.` path and the `..` |
| 226 | // range/iteration tokens. |
| 227 | <e:Postfix> "..." <n:Name> => Expr::Descendant(Box::new(e), n), |
| 228 | <e:Postfix> "[" <idx:Expr> "]" => Expr::Filter(Box::new(e), Box::new(idx)), |
| 229 | <n:Name> "(" ")" => Expr::FnCall(n, CallArgs::default()), |
| 230 | <n:Name> "(" <a:Comma<Arg>> ")" => Expr::FnCall(n, callargs_from(a)), |
| 231 | // Value-invocation of a *literal* callee (DMN §10.3.2.13.2, ADR-38). `Name` is |
| 232 | // excluded so named calls stay on the FnCall path (builtin/host/scope dispatch). |
| 233 | // LR(1)-clean: each alternative reduces to `Invocable` on a lookahead other than |
| 234 | // `(`, or shifts into the call on `(`; `Name` is in `Primary` not `Invocable`, so |
| 235 | // this production never competes with `Name "(" … ")"`. |
| 236 | <e:Invocable> "(" ")" => Expr::Invoke(Box::new(e), CallArgs::default()), |
| 237 | <e:Invocable> "(" <a:Comma<Arg>> ")" => Expr::Invoke(Box::new(e), callargs_from(a)), |
| 238 | Primary, |
| 239 | }; |
| 240 | |
| 241 | // Literal expressions that may appear as a value-invocation callee (ADR-38). Every |
| 242 | // element here is a non-function, so `Invoke` always evaluates to null (DMN |
| 243 | // §10.3.2.13.2). `Name` is deliberately absent — named calls use `FnCall`. |
| 244 | Invocable: Expr = { |
| 245 | Num => Expr::Number(<>), |
| 246 | Str => Expr::Str(<>), |
| 247 | TemporalLit => Expr::TemporalLit(<>), |
| 248 | "true" => Expr::Bool(true), |
| 249 | "false" => Expr::Bool(false), |
| 250 | "null" => Expr::Null, |
| 251 | }; |
| 252 | |
| 253 | // One call argument: positional (`expr`) or named (`name: expr`). Both start with |
| 254 | // a `Name`; the decision is the one-token lookahead `:` (named) vs. anything else |
| 255 | // (the `Name` reduces to a `Var`, i.e. a positional expression) — clean LR(1). |
| 256 | // `callargs_from` then sorts a list into positional/named, allowing a mix (ADR-23). |
| 257 | Arg: ArgEntry = { |
| 258 | <n:Name> ":" <v:Expr> => ArgEntry::Named(n, v), |
| 259 | <e:Expr> => ArgEntry::Pos(e), |
| 260 | }; |
| 261 | |
| 262 | // One formal parameter in a function literal (FEEL grammar rule 56, ADR-35 follow-up). |
| 263 | // Either a bare name (`a`) or a typed name (`a: number`, `fn1: function<number>->number`). |
| 264 | // The type annotation is preserved alongside the name for invocation-context coercion |
| 265 | // (§10.3.2.9.4): `None` for an untyped parameter, `Some(t)` when a type is present. |
| 266 | // LR(1)-clean: after `Name`, a lookahead `:` shifts into the typed branch while |
| 267 | // `,`/`)` reduce to the bare branch (mirrors the `Arg` rule's one-token decision). |
| 268 | FormalParam: (String, Option<FeelType>) = { |
| 269 | <n:Name> => (n, None), |
| 270 | <n:Name> ":" <t:InstanceType> => (n, Some(t)), |
| 271 | }; |
| 272 | |
| 273 | Primary: Expr = { |
| 274 | Num => Expr::Number(<>), |
| 275 | Str => Expr::Str(<>), |
| 276 | TemporalLit => Expr::TemporalLit(<>), |
| 277 | "true" => Expr::Bool(true), |
| 278 | "false" => Expr::Bool(false), |
| 279 | "null" => Expr::Null, |
| 280 | List, |
| 281 | RangeLit, |
| 282 | Context, |
| 283 | "not" "(" <e:Expr> ")" => Expr::Not(Box::new(e)), |
| 284 | "(" <e:Expr> ")" => e, |
| 285 | // A parenthesised comparison-test *value* `(< 10)` / `(= 10)` / `(!= 10)` (DMN |
| 286 | // §10.3.4.6; ADR-32). LR(1)-clean: no `CmpOp` token is in FIRST(Expr), so seeing a |
| 287 | // `CmpOp` right after `(` is an unambiguous new path that never competes with the |
| 288 | // `"(" Expr ")"` grouping or the `"(" Sum ".." …` range form. It evaluates to an |
| 289 | // opaque `Value::UnaryTestVal`, NOT a promoted range (the measured 0068 semantics). |
| 290 | "(" <op:CmpOp> <e:Sum> ")" => Expr::UnaryTestVal(op, Box::new(e)), |
| 291 | Name => Expr::Var(<>), |
| 292 | }; |
| 293 | |
| 294 | List: Expr = { |
| 295 | "[" "]" => Expr::List(vec![]), |
| 296 | "[" <es:Comma<Expr>> "]" => Expr::List(es), |
| 297 | }; |
| 298 | |
| 299 | // A range / interval literal in *value* position (DMN §10.3.4.6), e.g. `[1..10)` or |
| 300 | // `(a..b]`. It shares its opening `"[" Sum` (resp. `"(" Sum`) prefix with the list |
| 301 | // literal (resp. the parenthesised-expression form): the parser commits to a range |
| 302 | // only on seeing `..` after the first endpoint — a `]`/`,`/`)` instead reduces that |
| 303 | // `Sum` up to a full `Expr` for `List` / grouping. The branch is on disjoint |
| 304 | // lookahead (`..` is unique here), so it stays LR(1)-clean *without* a separate |
| 305 | // `IntervalStart` nonterminal — factoring `IntervalStart → "["` is exactly what |
| 306 | // clashed with `List` on the bare `[` (build.rs's no-conflict gate proves this). |
| 307 | // The `in`-RHS *test* form keeps its own `Interval` rule below (a unary test, not a |
| 308 | // value), reachable only after `in`, so the two never share an LR state. |
| 309 | // A range literal with optional, *unbounded* endpoints, following FEEL grammar rule 66 |
| 310 | // to the letter (ADR-31). Rule 66 has exactly three alternatives — (a) both endpoints |
| 311 | // present, (b) lower omitted behind an *open* start, (c) upper omitted behind an *open* |
| 312 | // end — so AT MOST ONE endpoint may be omitted, and only behind an OPEN bracket. Hence: |
| 313 | // * the *closed* opener `"["` keeps its low endpoint MANDATORY (`Sum`), with any |
| 314 | // `HiEnd` (bounded, or the open `")"` for an unbounded high → rule 66 (c)); |
| 315 | // * an *open* opener `"("` / `]`(RBracketOpen) splits in two: with a *present* low it |
| 316 | // takes any `HiEnd` (rule 66 a/c); with an *omitted* low (`"(" ".."`) it requires a |
| 317 | // *bounded* high `Endpoint IntervalEnd` (rule 66 b) — which is what forbids the |
| 318 | // both-omitted `(..)` (no rule-66 alternative; a parse error, like `range("(..)")` |
| 319 | // is null). |
| 320 | // `[1..]` / `[..10]` (closed bracket, no value) and `(..)` (both omitted) thus do not |
| 321 | // parse. An omitted endpoint becomes `None` (≠ a present `null` bound). `..` ∉ FIRST(Sum) |
| 322 | // keeps the present-vs-omitted-low choice one-token-clean; `]` after `[` is still `List`. |
| 323 | RangeLit: Expr = { |
| 324 | "[" <lo:Sum> ".." <hi:HiEnd> => |
| 325 | Expr::RangeLit(Interval { start_open: false, start: Some(Box::new(lo)), end: hi.0, end_open: hi.1 }), |
| 326 | "(" <lo:Sum> ".." <hi:HiEnd> => |
| 327 | Expr::RangeLit(Interval { start_open: true, start: Some(Box::new(lo)), end: hi.0, end_open: hi.1 }), |
| 328 | "(" ".." <hi:Endpoint> <eo:IntervalEnd> => |
| 329 | Expr::RangeLit(Interval { start_open: true, start: None, end: Some(Box::new(hi)), end_open: eo }), |
| 330 | // Reverse-open `]a..b]` ≡ `(a..b]`: an exclusive lower bound spelt with a leading |
| 331 | // `]`. The lexer retags that opener to `RBracketOpen` (a distinct terminal), so this |
| 332 | // shares no LR state with the `]` closer — same start-openness as the `(` form, and |
| 333 | // likewise split into present-low / omitted-low (omitted ⇒ bounded high). |
| 334 | RBracketOpen <lo:Sum> ".." <hi:HiEnd> => |
| 335 | Expr::RangeLit(Interval { start_open: true, start: Some(Box::new(lo)), end: hi.0, end_open: hi.1 }), |
| 336 | RBracketOpen ".." <hi:Endpoint> <eo:IntervalEnd> => |
| 337 | Expr::RangeLit(Interval { start_open: true, start: None, end: Some(Box::new(hi)), end_open: eo }), |
| 338 | }; |
| 339 | |
| 340 | // The part of an interval *after* the `..` for a *present-low* range: the high endpoint |
| 341 | // plus closing-bracket openness. Either a bounded endpoint followed by `IntervalEnd` |
| 342 | // (`]`/`)`/`[`), or — for an *unbounded* high (`[1..)`; ADR-31, rule 66 (c)) — the bare |
| 343 | // *open* `")"`. There is deliberately NO unbounded-high `"]"` (a closed bracket with no |
| 344 | // value is invalid per rule 66) and no reverse-close `[`: `[` is in FIRST(Endpoint) (a |
| 345 | // nested high endpoint, `[1..[2..3]]`), so after `..` a `[` always shifts into |
| 346 | // `Endpoint`. That keeps the fork LR(1)-clean (build.rs gate). The bare `)` never clashes |
| 347 | // with `Endpoint` (`)` ∉ FIRST(Endpoint)). Only the present-low productions reach `HiEnd`; |
| 348 | // an omitted low takes the inline *bounded* high above, so `(..)` cannot be built. |
| 349 | HiEnd: (Option<Box<Expr>>, bool) = { |
| 350 | <hi:Endpoint> <eo:IntervalEnd> => (Some(Box::new(hi)), eo), |
| 351 | ")" => (None, true), |
| 352 | }; |
| 353 | |
| 354 | // Context literal `{a: 1, b: a + 1}`. Keys are a bare Name or a string literal |
| 355 | // (FEEL allows both). Entries keep their written order so the evaluator can bind |
| 356 | // them progressively. |
| 357 | Context: Expr = { |
| 358 | "{" "}" => Expr::ContextLit(vec![]), |
| 359 | "{" <es:Comma<Entry>> "}" => Expr::ContextLit(es), |
| 360 | }; |
| 361 | |
| 362 | Entry: (String, Box<Expr>) = { |
| 363 | <k:Key> ":" <v:Expr> => (k, Box::new(v)), |
| 364 | }; |
| 365 | |
| 366 | Key: String = { |
| 367 | Name, |
| 368 | Str, |
| 369 | }; |
| 370 | |
| 371 | // One-or-more comma-separated, no trailing comma. |
| 372 | Comma<T>: Vec<T> = { |
| 373 | <mut v:(<T> ",")*> <last:T> => { v.push(last); v }, |
| 374 | }; |
| 375 | |
| 376 | // One iteration context `name in source` (FEEL rules 44/46). Collected into a |
| 377 | // list by `Comma<IterCtx>` in the `for`/`some`/`every` rules above. |
| 378 | IterCtx: (String, IterSource) = { |
| 379 | <v:Name> "in" <s:IterSrc> => (v, s), |
| 380 | }; |
| 381 | |
| 382 | // Iteration source for `for`/`some`/`every`: a bare integer range `a..b` |
| 383 | // (generates an ascending/descending integer list) or a list expression. |
| 384 | IterSrc: IterSource = { |
| 385 | <lo:Sum> ".." <hi:Sum> => IterSource::Range(Box::new(lo), Box::new(hi)), |
| 386 | <e:Disjunction> => IterSource::Expr(Box::new(e)), |
| 387 | }; |
| 388 | |
| 389 | // The right-hand side of membership `in` — *positive unary tests* (DMN rule 49), a |
| 390 | // grammatical category distinct from `expression`. Its own nonterminals (this is the |
| 391 | // whole reason lalrpop's separate categories suffice — no hand-written parser needed): |
| 392 | // * a single comparison test `<op> endpoint` (`< 10`, `= e`, `!= e`); |
| 393 | // * a single interval `[a..b]` / `(a..b)` / `[a..b)` / `(a..b]` (any endpoints); |
| 394 | // * a comma-list of tests in brackets or parens — `x in [1,2,3]`, `x in (false,true)`, |
| 395 | // `x in (1,<5,>=10)`, `x in [[2..4],[1..3]]`, `x in (=10)` — an OR of the element |
| 396 | // tests (`eval_in` already OR-folds `Vec<UnaryTest>`, so this needs no eval change); |
| 397 | // * a bare endpoint expression that does NOT start with `[`/`(` — a scalar (`1 in 1` |
| 398 | // is `1 = 1`), a variable, a `range(...)` call, an `@"…"` temporal, or arithmetic |
| 399 | // of those. |
| 400 | // The bare branch goes through the bracket-free `NbSum` chain below, NOT `Sum`. That is |
| 401 | // the lever that keeps `[`/`(` out of the bare branch, so after `in [` (resp. `in (`) |
| 402 | // there is exactly ONE bracket production — the test-list / interval — instead of two |
| 403 | // competing ones (test-list vs. the value `List`/`RangeLit` reachable through a full |
| 404 | // `Sum`). With only one, the interval-vs-list split is the same first-element factoring |
| 405 | // as `RangeLit`: parse the first `Sum`, then branch on `..` (interval) vs. `,`/`]`/`)` |
| 406 | // (list). That is a FIRST-overlap on a shared prefix, not a token-role conflict, so it |
| 407 | // is LR(1)-clean (the build.rs no-conflict gate proves it). Nested intervals / lists in |
| 408 | // a test-list reuse the value-position `Sum` (a `[2..4]` element arrives as a `RangeLit` |
| 409 | // value that `unary_test_from_expr` reclassifies to an interval test), so they live one |
| 410 | // nesting level down and never compete in the `in [` state. A leading `]a..b]` reverse |
| 411 | // interval is *not* here (it would overload `]` as an opener — the one genuinely fatal, |
| 412 | // deferred case; see ROADMAP). |
| 413 | InRhs: Vec<UnaryTest> = { |
| 414 | <op:CmpOp> <e:Sum> => vec![UnaryTest::Compare(op, Box::new(e))], |
| 415 | <e:NbSum> => vec![unary_test_from_expr(e)], |
| 416 | // Interval *tests* with optional unbounded endpoints (ADR-31): `x in [1..)`, |
| 417 | // `x in (..10]`. Same rule-66 factoring as the value-position `RangeLit` above — an |
| 418 | // omitted endpoint needs an *open* bracket and at most one may be omitted, so the |
| 419 | // open openers split into present-low (`HiEnd`) / omitted-low (bounded high). `[1..]`, |
| 420 | // `[..10]`, `(..)` are parse errors here too. An omitted endpoint becomes `None` (an |
| 421 | // unconditionally-satisfied side), distinct from a present `null` bound (three-valued). |
| 422 | "[" <lo:Sum> ".." <hi:HiEnd> => |
| 423 | vec![UnaryTest::Interval(Interval { start_open: false, start: Some(Box::new(lo)), end: hi.0, end_open: hi.1 })], |
| 424 | "(" <lo:Sum> ".." <hi:HiEnd> => |
| 425 | vec![UnaryTest::Interval(Interval { start_open: true, start: Some(Box::new(lo)), end: hi.0, end_open: hi.1 })], |
| 426 | "(" ".." <hi:Endpoint> <eo:IntervalEnd> => |
| 427 | vec![UnaryTest::Interval(Interval { start_open: true, start: None, end: Some(Box::new(hi)), end_open: eo })], |
| 428 | // Reverse-open interval *test* `x in ]a..b]` (exclusive lower, ≡ `(a..b]`); the |
| 429 | // lexer's `RBracketOpen` opener keeps this off the `]`-closer LR state. |
| 430 | RBracketOpen <lo:Sum> ".." <hi:HiEnd> => |
| 431 | vec![UnaryTest::Interval(Interval { start_open: true, start: Some(Box::new(lo)), end: hi.0, end_open: hi.1 })], |
| 432 | RBracketOpen ".." <hi:Endpoint> <eo:IntervalEnd> => |
| 433 | vec![UnaryTest::Interval(Interval { start_open: true, start: None, end: Some(Box::new(hi)), end_open: eo })], |
| 434 | "[" <ts:Comma<UnaryTestElem>> "]" => ts, |
| 435 | "(" <ts:Comma<UnaryTestElem>> ")" => ts, |
| 436 | // Negation of a positive unary-test list, `x in not(t1, …)` (DMN rule 49). In the |
| 437 | // unary-test context `not(…)` is ALWAYS test negation (≡ `not(x satisfies t)`, |
| 438 | // Kleene), never the boolean `not(…)` built-in. A dedicated `InRhs` production |
| 439 | // (not reachable through the bare `NbSum` chain — `NbPrimary` deliberately drops |
| 440 | // the boolean `"not" "(" Expr ")"` so the bare branch can never start with `not`, |
| 441 | // which is what keeps this fork LR(1)-clean: after `in`, `not` has exactly ONE |
| 442 | // production here). The inner elements reuse `UnaryTestElem`, so `not(5)` is the |
| 443 | // negated `= 5` test, `not(<5)` a negated comparison, `not([1..10])` a negated |
| 444 | // interval — all via `unary_test_from_expr`, like a bare list. |
| 445 | "not" "(" <ts:Comma<UnaryTestElem>> ")" => vec![UnaryTest::Not(ts)], |
| 446 | }; |
| 447 | |
| 448 | // One element of a bracketed / parenthesised test-list: a comparison test (`< 5`, |
| 449 | // `= e`, `!= e`), or a bare endpoint via the full `Sum` — which covers a value, a |
| 450 | // nested list value, and a nested interval `[2..4]` (a `RangeLit` value that |
| 451 | // `unary_test_from_expr` reclassifies to a `UnaryTest::Interval`). Using the full |
| 452 | // `Sum` here is safe: an element only competes with the single-interval production on |
| 453 | // the shared `"[" Sum` prefix, resolved one token later by `..` vs. `,`/`]`/`)`. |
| 454 | UnaryTestElem: UnaryTest = { |
| 455 | <op:CmpOp> <e:Sum> => UnaryTest::Compare(op, Box::new(e)), |
| 456 | <e:Sum> => unary_test_from_expr(e), |
| 457 | }; |
| 458 | |
| 459 | // Bracket-free arithmetic chain — a copy of `Sum`/`Product`/`Power`/`Unary`/`Postfix` |
| 460 | // whose *leading* primary is never `[`- or `(`-led (`NbPrimary` drops `List`, `RangeLit` |
| 461 | // and the `"(" Expr ")"` grouping). It is the bare-endpoint branch of `InRhs`: by never |
| 462 | // starting with `[`/`(` it leaves the `in [` / `in (` states with a single bracket |
| 463 | // production each, which is what keeps Form 1 LR(1)-clean (see `InRhs`). Only the |
| 464 | // *leftmost* primary is restricted — right operands and inner sub-expressions stay the |
| 465 | // full `Sum`/`Expr` nonterminals — so `1 + 2`, `-5`, `range("[1..3]")`, `a.b`, `xs[1]` |
| 466 | // are all still reachable as bare endpoints; only a *leading* `[`/`(` is peeled off to |
| 467 | // the dedicated bracket productions above. (Cost: a bare endpoint can no longer *begin* |
| 468 | // with a grouping/list/range literal, e.g. `x in (1+2)*3` — not a shape the TCK uses; |
| 469 | // `x in (1+2)`, `x in [a..b]`, `x in [1,2,3]` are all still covered by the rules above.) |
| 470 | NbSum: Expr = { |
| 471 | <l:NbSum> "+" <r:Product> => Expr::Arith(ArithOp::Add, Box::new(l), Box::new(r)), |
| 472 | <l:NbSum> "-" <r:Product> => Expr::Arith(ArithOp::Sub, Box::new(l), Box::new(r)), |
| 473 | NbProduct, |
| 474 | }; |
| 475 | |
| 476 | NbProduct: Expr = { |
| 477 | <l:NbProduct> "*" <r:Power> => Expr::Arith(ArithOp::Mul, Box::new(l), Box::new(r)), |
| 478 | <l:NbProduct> "/" <r:Power> => Expr::Arith(ArithOp::Div, Box::new(l), Box::new(r)), |
| 479 | NbPower, |
| 480 | }; |
| 481 | |
| 482 | NbPower: Expr = { |
| 483 | <l:NbPower> "**" <r:Unary> => Expr::Arith(ArithOp::Pow, Box::new(l), Box::new(r)), |
| 484 | NbUnary, |
| 485 | }; |
| 486 | |
| 487 | NbUnary: Expr = { |
| 488 | "-" <e:Unary> => Expr::Neg(Box::new(e)), |
| 489 | NbPostfix, |
| 490 | }; |
| 491 | |
| 492 | NbPostfix: Expr = { |
| 493 | <e:NbPostfix> "." <n:Name> => Expr::Path(Box::new(e), n), |
| 494 | <e:NbPostfix> "[" <idx:Expr> "]" => Expr::Filter(Box::new(e), Box::new(idx)), |
| 495 | <n:Name> "(" ")" => Expr::FnCall(n, CallArgs::default()), |
| 496 | <n:Name> "(" <a:Comma<Arg>> ")" => Expr::FnCall(n, callargs_from(a)), |
| 497 | NbPrimary, |
| 498 | }; |
| 499 | |
| 500 | NbPrimary: Expr = { |
| 501 | Num => Expr::Number(<>), |
| 502 | Str => Expr::Str(<>), |
| 503 | TemporalLit => Expr::TemporalLit(<>), |
| 504 | "true" => Expr::Bool(true), |
| 505 | "false" => Expr::Bool(false), |
| 506 | "null" => Expr::Null, |
| 507 | Context, |
| 508 | // NOTE: unlike `Primary`, the bare-`InRhs` `NbPrimary` deliberately OMITS the |
| 509 | // boolean `"not" "(" Expr ")"` production. In the unary-test context after `in`, |
| 510 | // `not(…)` is always *test* negation, handled by the dedicated `InRhs → "not" …` |
| 511 | // production above; dropping it here is what keeps that fork LR(1)-clean (the bare |
| 512 | // branch can no longer reach a competing `not`). A boolean `not(…)` is of course |
| 513 | // still reachable everywhere else through `Primary`. |
| 514 | Name => Expr::Var(<>), |
| 515 | }; |
| 516 | |
| 517 | // The HIGH endpoint of an interval — a filter-free clone of the `Sum` chain. It is |
| 518 | // identical to `Sum`/`Product`/`Power`/`Unary`/`Postfix` except `EpPostfix` drops the |
| 519 | // `Postfix "[" Expr "]"` filter production, so a `[` *after* the high endpoint is |
| 520 | // unambiguously the reverse-CLOSE bracket of `[a..b[` (≡ `[a..b)`), never a subscript. |
| 521 | // Only the high endpoint needs this — the low endpoint is always followed by `..`, so |
| 522 | // no `[` can attach there. The base case reuses the shared `Primary` (which carries no |
| 523 | // filter; the filter lives only in `Postfix`), so a grouping / list / nested range |
| 524 | // endpoint still parses. Cost: an interval's high endpoint cannot itself end in a |
| 525 | // `[...]` subscript (e.g. `[a..xs[2]]`) — a shape no test/TCK case uses. Mirrors the |
| 526 | // `NbSum` precedent (a restricted arithmetic clone for one syntactic position). |
| 527 | Endpoint: Expr = { |
| 528 | <l:Endpoint> "+" <r:EpProduct> => Expr::Arith(ArithOp::Add, Box::new(l), Box::new(r)), |
| 529 | <l:Endpoint> "-" <r:EpProduct> => Expr::Arith(ArithOp::Sub, Box::new(l), Box::new(r)), |
| 530 | EpProduct, |
| 531 | }; |
| 532 | |
| 533 | EpProduct: Expr = { |
| 534 | <l:EpProduct> "*" <r:EpPower> => Expr::Arith(ArithOp::Mul, Box::new(l), Box::new(r)), |
| 535 | <l:EpProduct> "/" <r:EpPower> => Expr::Arith(ArithOp::Div, Box::new(l), Box::new(r)), |
| 536 | EpPower, |
| 537 | }; |
| 538 | |
| 539 | EpPower: Expr = { |
| 540 | <l:EpPower> "**" <r:EpUnary> => Expr::Arith(ArithOp::Pow, Box::new(l), Box::new(r)), |
| 541 | EpUnary, |
| 542 | }; |
| 543 | |
| 544 | EpUnary: Expr = { |
| 545 | "-" <e:EpUnary> => Expr::Neg(Box::new(e)), |
| 546 | EpPostfix, |
| 547 | }; |
| 548 | |
| 549 | // Like `Postfix` but WITHOUT the `[Expr]` filter production (see `Endpoint`). |
| 550 | EpPostfix: Expr = { |
| 551 | <e:EpPostfix> "." <n:Name> => Expr::Path(Box::new(e), n), |
| 552 | <n:Name> "(" ")" => Expr::FnCall(n, CallArgs::default()), |
| 553 | <n:Name> "(" <a:Comma<Arg>> ")" => Expr::FnCall(n, callargs_from(a)), |
| 554 | Primary, |
| 555 | }; |
| 556 | |
| 557 | IntervalEnd: bool = { |
| 558 | "]" => false, // closed |
| 559 | ")" => true, // open |
| 560 | // Reverse-CLOSE `[a..b[` ≡ `[a..b)`: an exclusive upper bound spelt with a |
| 561 | // trailing `[`. Admissible only because the high endpoint is the filter-free |
| 562 | // `Endpoint` nonterminal (no `Postfix "[" Expr "]"`), so this `[` cannot also be |
| 563 | // a subscript on the endpoint — keeping it LR(1)-clean (build.rs gate). |
| 564 | "[" => true, // open (reverse-close) |
| 565 | }; |
| 566 | |
| 567 | // `Num` (f64 literal), `Str` (double-quoted, no escapes) and `Name` (a Unicode |
| 568 | // identifier: XID-start then XID-continue, so `über`/`preis`/`naïve` parse, and |
| 569 | // longest-match keeps `fortune` a name not the keyword `for`; multi-word names |
| 570 | // like `full name` arrive pre-joined) are produced by the external `logos` lexer |
| 571 | // — see `lexer.rs` and the `extern` block above. |