Expand description
Grammar parser.
This module parses the following grammar (minus comments, which are ignored):
program -> item* EOF;
item -> function | global | extern_fn; // TODO types.
extern_fn -> 'extern' 'fn' identifier '(' function_argument ( ',' function_argument )* ','? ')' return_decl? ;
global -> 'var' identifier ':' type '=' static_initializer ';' ;
static_initializer -> expression ; // The language does not currently support function calls.
function -> 'fn' identifier '(' function_argument ( ',' function_argument )* ','? ')' return_decl? body ;
function_argument -> identifier ':' '&'? type ;
return_decl -> '->' type ;
type -> identifier ;
body -> '{' statement* '}' ;
statement -> 'var' identifier (':' type)? '=' expression ';'
| 'return' expression? ';'
| 'break' expression? ';'
| 'continue' expression? ';'
| 'if' expression body ( 'else' body )?
| 'loop' body
| 'while' expression body
| expression ';'
expression -> binary1 ;
binary0 -> binary1 ( '=' binary1 )? ;
binary1 -> binary2 ( '||' binary2 )* ;
binary2 -> binary3 ( '&&' binary3 )* ;
binary3 -> binary4 ( ( '<' | '<=' | '>' | '>=' | '==' | '!=' ) binary4 )* ;
binary4 -> binary5 ( '|' binary5 )* ;
binary5 -> binary6 ( '^' binary6 )* ;
binary6 -> binary7 ( '&' binary7 )* ;
binary7 -> binary8 ( ( '<<' | '>>' ) binary8 )* ;
binary8 -> binary9 ( ( '+' | '-' ) binary9 )* ;
binary9 -> unary ( ( '*' | '/' ) unary )* ;
unary -> ('!' | '-' | '&' | '*' )* primary | call ;
primary -> ( literal | identifier ( '(' call_arguments ')' )? ) | '(' expression ')' ;
call_arguments -> expression ( ',' expression )* ','? ;
literal -> NUMBER | STRING | 'true' | 'false' ;NUMBER: Non-negative integers (binary, decimal, hexadecimal) and floats.
STRING: Double-quoted strings with escape sequences.
Structs§
- Default
Type Set - Use 64-bit integers and 64-bit floats (default).
- Parser
Error - Type
Set32 - Use 32-bit integers and floats.
- Type
Set128 - Use 128-bit integers and 64-bit floats.
Traits§
- Float
Parser - Parse literals into Somni floats.
- IntParser
- Parse literals into Somni integers.
- TypeSet
- Defines the numeric types used in the parser.