CompilerLanguage internals
23Grammar Summary
The complete formal grammar is in docs/grammar.md, written in EBNF following ISO/IEC 14977. What follows is a condensed overview of the major productions.
23.1 Program Structure
program = { directive } [ namespace_decl ] { top_level_item }
top_level_item = import_decl | module_decl | var_declaration
| function_declaration | extern_function_decl | extern_block
| c_header_import | intrinsic_decl
| struct_declaration | class_declaration
| enum_declaration | trait_declaration
| type_alias_declaration | implementation_block
23.2 Statements
statement = var_declaration | function_declaration | struct_declaration
| class_declaration | enum_declaration | trait_declaration
| type_alias_declaration | implementation_block
| if_statement | while_statement | for_statement
| loop_statement | do_while_statement
| match_statement | switch_statement | static_match
| break_statement | continue_statement | return_statement
| unsafe_block | block | expression_statement
static_match = "static" "match" "(" type ")" "{" static_match_arm* "}"
static_match_arm = ( type { "|" type } | "_" ) "=>" ( block | expression )
static_match is also an expression form (see section 12.7).
23.3 Expression Hierarchy
Expressions are stratified by precedence; each level delegates to the next-higher-precedence level.
expression = assignment_expr
assignment_expr = coalesce_expr [ assignment_op assignment_expr ]
coalesce_expr = pipe_expr [ "??" coalesce_expr ]
pipe_expr = conditional_expr { ( "|>" | "<|" ) conditional_expr }
conditional_expr = logical_or_expr [ "?" expression ":" conditional_expr ]
logical_or_expr = logical_and_expr { "||" logical_and_expr }
logical_and_expr = bitwise_or_expr { "&&" bitwise_or_expr }
bitwise_or_expr = bitwise_xor_expr { "|" bitwise_xor_expr }
bitwise_xor_expr = bitwise_and_expr { "^" bitwise_and_expr }
bitwise_and_expr = equality_expr { "&" equality_expr }
equality_expr = relational_expr { ( "==" | "!=" ) relational_expr }
relational_expr = shift_expr { ( "<" | ">" | "<=" | ">=" | "<=>" ) shift_expr }
shift_expr = additive_expr { ( "<<" | ">>" ) additive_expr }
additive_expr = multiplicative_expr { ( "+" | "-" ) multiplicative_expr }
multiplicative_expr = cast_expr { ( "*" | "/" | "%" ) cast_expr }
cast_expr = unary_expr { "as" type }
unary_expr = unary_op unary_expr | postfix_expr
postfix_expr = primary_expr { postfix_op }
For the full grammar, including type and pattern productions, see grammar.md.