Grammar#

EBNF source

expression     = let | pipeline ;
let            = "let", assignment, { ",", assignment }, ":", expression ;
assignment     = target, "=", logicalOr ;
pipeline       = logicalOr, { "|", stage } ;
stage          = "map", [ binding ], logicalOr
               | "filter", [ binding ], logicalOr
               | "any", [ binding ], logicalOr
               | "all", [ binding ], logicalOr
               | "reduce", assignment, (",", binding | ":"), logicalOr ;
binding        = (target | identifier, ",", target), ":" ;
target         = identifier | arrayPattern | objectPattern ;
arrayPattern   = "[", [ [ patternElement ], { ",", [ patternElement ] } ], "]" ;
objectPattern  = "{", [ patternField, { ",", patternField }, [ "," ] ], "}" ;
patternField   = identifier, [ "=", logicalOr ]
               | (identifier | string), ":", patternElement ;
patternElement = target, [ "=", logicalOr ] ;
logicalOr      = logicalAnd, { "||", logicalAnd } ;
logicalAnd     = coalesce, { "&&", coalesce } ;
coalesce       = equality, [ "??", coalesce ] ;
equality       = comparison, { ("==" | "!="), comparison } ;
comparison     = additive, [ ("<" | "<=" | ">" | ">=" | "in"), additive ] ;
additive       = multiplicative, { ("+" | "-"), multiplicative } ;
multiplicative = unary, { ("*" | "/" | "%"), unary } ;
unary          = ("!" | "+" | "-"), unary | postfix ;

postfix        = primary, { access | call } ;
access         = ".", identifier
               | "?.", identifier
               | "[", expression, "]"
               | "?.", "[", expression, "]" ;
call           = "(", [ arguments ], ")" ;
arguments      = element, { ",", element }, [ "," ] ;

primary        = literal | identifier | "this" | "(", expression, ")"
               | array | object | case ;
array          = "[", [ element, { ",", element }, [ "," ] ], "]" ;
element        = [ "..." ], expression ;
object         = "{", [ member, { ",", member }, [ "," ] ], "}" ;
member         = field | "...", expression ;
field          = (identifier | string | "[", expression, "]"), ":",
                 expression | identifier ;
case           = "case", [ logicalOr ], "{", arm, { ",", arm },
                 ",", expression, [ "," ], "}" ;
arm            = expression, ":", expression ;
literal        = number | string | "true" | "false" | "null" ;

(*
## Terminals

- `number`: unsigned JSON number; signs are unary operators.
- `string`: double-quoted JSON string.
- `identifier`: Unicode letter or underscore, followed by letters, digits,
  or underscores.

## Keywords

- `true`, `false`, `null`, `case`, `let`, and `this` cannot be variable or
  binding names, including shorthand fields.
- Keywords remain valid property names and explicit object keys.
- `in` is contextual and is also a valid identifier.

## Tokenization

- Whitespace is ignored between tokens.
- Operators use longest match.
- `//` starts a comment that runs to the end of the line.

## Grouping

- A stage body is a `logicalOr`; a nested pipeline or `let` needs parentheses.
- A stage binding is recognized by the colon after its complete target or
  `key, target` pair.
- A `case` subject beginning with `{` needs parentheses.
*)