(* Rowan 1.2. Braces in quotes are language tokens. Outside quotes, braces mean repetition, brackets mean optional, and parentheses mean grouping. *) program = { sep }, [ statements ], EOF ; statements = statement, { sep, { sep }, statement }, { sep } ; sep = newline | ";" ; block = "{", { sep }, [ statements ], "}" ; statement = declaration | question | "say", template_text | "collection", identifier, ":", postfix, "in", expr | "test", string, block | "menu", template_text, "{", { sep }, { string, "=>", expr, { sep } }, "}" | "export", declaration | "set", identifier, { selector }, ( "=" | "+=" | "-=" | "*=" | "/=" | "%=" ), expr | "if", expr, block, [ "else", ( if_stmt | block ) ] | "while", expr, block | "for", identifier, { ",", identifier }, "in", expr, block | "return", [ expr ] | "break" | "continue" | "try", block, "catch", identifier, block | "throw", expr | "assert", expr, [ ",", expr ] | "use", string, "as", identifier | expr ; if_stmt = "if", expr, block, [ "else", ( if_stmt | block ) ] ; declaration = "let", identifier, "=", expr | "fn", identifier, parameters, funbody | "record", identifier, "(", [ fields ], ")" ; fields = field, { ",", field }, [ "," ] ; field = fieldname, [ ":", type ] ; type = "any" | "text" | "number" | "int" | "bool" | "list" | "map" ; parameters = "(", [ parameter, { ",", parameter }, [ "," ] ], ")" ; parameter = identifier, [ "=", expr ] ; funbody = block | "=>", expr ; expr = coalesce, { "|>", stage } ; stage = ("where" | "select" | "some" | "every"), coalesce | "summarize", "by", coalesce, "{", [ metric, { ",", metric }, [ "," ] ], "}" | "sort", "by", coalesce, [ "asc" | "desc" ] | ("group" | "distinct"), "by", coalesce | "join", coalesce, "on", coalesce, "equals", coalesce | ("sum" | "min" | "max" | "average" | "take" | "skip"), coalesce | "count" | "first" ; metric = fieldname, ":", ("count" | ("sum" | "average" | "min" | "max"), coalesce) ; coalesce = disjunction, { "??", disjunction } ; disjunction = conjunction, { "or", conjunction } ; conjunction = equality, { "and", equality } ; equality = comparison, { ("==" | "!="), comparison } ; comparison = range, { ("<" | "<=" | ">" | ">=" | "in"), range } ; range = addition, { "..", addition } ; addition = product, { ("+" | "-"), product } ; product = unary, { ("*" | "/" | "%"), unary } ; unary = ("not" | "+" | "-"), unary | postfix ; postfix = primary, { selector | slice | "(", [ arguments ], ")" } ; selector = ".", fieldname | "[", expr, "]" ; slice = "[", [ expr ], ":", [ expr ], "]" ; (* stop index excluded *) arguments = argument, { ",", argument }, [ "," ] ; argument = [ fieldname, ":" ], expr ; primary = number | string | "$", string | "true" | "false" | "none" | identifier | "(", expr, ")" | "[", [ expr, { ",", expr }, [ "," ] ], "]" | "form", "{", { sep }, question, { sep, { sep }, question }, { sep }, "}" | "match", "(", expr, ")", "{", { sep }, { arm, sep, { sep } }, "else", "=>", expr, { sep }, "}" | "{", [ member, { ",", member }, [ "," ] ], "}" | "fn", parameters, funbody | "when", expr, "then", expr, "else", coalesce | search ; member = (fieldname | string), ":", expr ; template_text = [ "$" ], string ; question = "ask", identifier, [ ":", ("text" | "int" | "number" | "bool") ], template_text, [ "from", expr | "between", equality, "and", expr ], [ "default", expr ] ; arm = "case", pattern, [ "if", expr ], "=>", expr ; pattern = identifier | "_" | string | [ "-" ], number | "true" | "false" | "none" | "[", [ pattern, { ",", pattern }, [ "," ] ], "]" | "{", [ keyed_pattern, { ",", keyed_pattern }, [ "," ] ], "}" ; keyed_pattern = (fieldname | string), ":", pattern ; search = "solve", [ "first" ], "{", { sep }, { clause, sep, { sep } }, "yield", expr, { sep }, "}" | "solve", "best", "{", { sep }, { clause, sep, { sep } }, objective, sep, { sep }, { objective, sep, { sep } }, "yield", expr, { sep }, "}" ; objective = ("maximize" | "minimize"), expr ; clause = "choose", identifier, "from", expr | "require", expr ; (* Lexical rules identifier : [A-Za-z_][A-Za-z_0-9]* excluding the reserved words below fieldname : identifier or reserved word number : (0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)? string : double-quoted JSON string, Unicode text comment : '#' to the end of the physical line newline : LF, CRLF or CR Reserved words: let set fn record if else while for in break continue return try catch throw assert use as export true false none and or not where select sort by asc desc group join on equals take skip distinct count sum min max average first solve choose from require yield some every when then ask, say, between, default, form, collection, match, case, menu, test, summarize, best, maximize and minimize are contextual keywords. They remain usable as ordinary names outside their special positions in search syntax. Type names and standard-library names are ordinary identifiers. *) (* Layout and context Newlines within parentheses, list literals, record literals and brackets are ignored. Statement blocks and search bodies restart statement layout. Newlines are accepted after binary operators and around a pipeline marker. A block may start on the next line. An else or catch may follow a newline. return without an expression ends at a statement boundary. export applies only to top-level declarations. return requires a function. break and continue require a lexically enclosing loop in the same function. Duplicate parameters, record fields, map literal keys and search choices are errors. Steps in a search are ordered and end with exactly one yield. *)