(* Marqdo Statement and Expression Grammar Status: current language contract, breaking rewrite (RFC-LANG-0002). This replaces the earlier v1 expression-only grammar in place. There is one grammar for every executable payload. Markdown AST decides which closed carrier (Strong / Emph / inline Code) is a payload and where it lives; this grammar decides what that payload means. No second parser and no per-file syntax selector exists. See docs/STATUS.md for implementation and release status. *) (* One payload is exactly one statement and must consume the whole payload. *) payload = statement , EOF ; statement = if_stmt | else_if_stmt | else_stmt | while_stmt | break_stmt | continue_stmt | end_stmt | return_stmt | params_stmt | assignment | expression ; if_stmt = "if" , space_required , expression ; else_if_stmt = "else" , space_required , "if" , space_required , expression ; else_stmt = "else" ; while_stmt = "while" , space_required , expression ; break_stmt = "break" ; continue_stmt = "continue" ; end_stmt = "end" ; return_stmt = "return" , space_required , expression ; params_stmt = "params" , "(" , [ parameter_list ] , ")" ; (* `if`, `else`, `while`, `end`, `return` and `params` are reserved words. `break` and `continue` are contextual: a complete payload of exactly that identifier takes precedence over the ordinary expression production. Calls, assignments and uses inside expressions retain their identifier meaning. Neither loop-control statement takes operands or a label. No trailing colon, `elif`, `for`, `try`, empty `return`, `end if`, or semicolon-separated multi-statement payload is supported. The frontend pairs statements inside each heading execution segment. Each `if` takes zero or more `else if` arms and at most one final `else`. Each `if` and `while` takes one closing `end`. A loop control statement requires an open `while` in the same segment. See mapping.md. *) (* Function parameter declaration. It is legal only as the first direct execution payload of an Exec H2 function, at most once, and never inside a list or block quote. The declaration is compile-time metadata, not a runtime call. *) parameter_list = parameter , { "," , parameter } , [ "," ] ; parameter = identifier , [ ":" , type_name ] , [ "=" , expression ] ; type_name = identifier ; (* Parameter names are unique; positional parameters precede defaulted ones. The type annotation is optional to preserve the existing untyped signature form (`params(n)`); types and default expressions reuse the existing signature rules. *) (* Assignment and place writes are unchanged from the previous revision. *) assignment = assign_target , "=" , expression ; assign_target = identifier | postfix_expr , "[" , expression , "]" ; (* The index target writes one byte into an addressable Memory region; other base values are rejected at run time. Member assignment (`a.b = 1`) and chained assignment are parse errors. See ADR-0022. *) expression = or_expr ; or_expr = and_expr , { "or" , and_expr } ; and_expr = compare_expr , { "and" , compare_expr } ; compare_expr = add_expr , [ compare_op , add_expr ] ; (* non-associative: "a < b < c" is a parse error; write "a < b and b < c" instead. *) compare_op = "==" | "!=" | "<" | "<=" | ">" | ">=" ; add_expr = mul_expr , { ( "+" | "-" ) , mul_expr } ; mul_expr = unary_expr , { ( "*" | "/" | "//" | "%" ) , unary_expr } ; unary_expr = ( "not" | "-" | "+" ) , unary_expr | power_expr ; power_expr = postfix_expr , [ "**" , unary_expr ] ; (* right associative: 2 ** 3 ** 2 == 2 ** (3 ** 2); -x ** 2 == -(x ** 2); 2 ** -1 is valid *) postfix_expr = primary , { call_suffix | member_suffix | index_suffix } ; call_suffix = "(" , [ argument_list ] , ")" ; argument_list = argument , { "," , argument } , [ "," ] ; argument = [ identifier , "=" ] , expression ; (* positional arguments must all precede named arguments; duplicate argument names are errors *) member_suffix = "." , identifier ; index_suffix = "[" , expression , "]" ; primary = int_literal | float_literal | text_literal | "true" | "false" | "None" | identifier | "(" , expression , ")" | list_literal | record_literal ; list_literal = "[" , [ expression , { "," , expression } , [ "," ] ] , "]" ; record_literal = "{" , [ record_entry , { "," , record_entry } , [ "," ] ] , "}" ; record_entry = ( identifier | text_literal ) , ":" , expression ; (* Lexical rules *) identifier = xid_start , { xid_continue } ; (* Unicode XID per UAX #31: Chinese, Latin, etc. are valid. Reserved words cannot be identifiers: true, false, None, and, or, not, if, else, while, end, return, params. *) int_literal = digit , { digit } ; (* arbitrary precision decimal integer; no sign here (unary minus handles negatives); no separators; no hex/octal. *) float_literal = digit , { digit } , "." , digit , { digit } , [ ( "e" | "E" ) , [ "+" | "-" ] , digit , { digit } ] ; (* requires digits on both sides of the dot; ".5" and "1." are invalid. Floats are IEEE-754 binary64. *) text_literal = '"' , { text_char | escape } , '"' ; escape = "\\" , ( '"' | "\\" | "n" | "t" | "r" | "u{" , hexdigit , { hexdigit } , "}" ) ; text_char = ? any Unicode scalar value except '"' and "\\" ? ; (* Whitespace: any Unicode whitespace separates tokens. Newlines inside a code-span payload are whitespace. There are no comments. `space_required` means at least one whitespace character after the keyword. *) (* Notes on the Markdown boundary *) (* - A "**" inside a payload is the power operator only when it survived Markdown parsing inside a code span or text that is not emphasis. Authors should put multiplication/power expressions in code spans when Markdown emphasis would otherwise consume the asterisks. *) (* - Payload spans returned by the parser are relative to the concatenated payload; the frontend maps them back through the text/code segments to file spans. *) (* - Maximum nesting depth is 128; exceeding it is an expr.parse error. *) (* - Every payload must be fully consumed. A trailing token is a syntax error, never an ignored suffix. *)