Module parser

Module parser 

Source
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 -> right_hand_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)? '=' right_hand_expression ';' (statement)?
           | 'return' right_hand_expression? ';' (statement)?
           | 'break' ';' (statement)?
           | 'continue' ';' (statement)?
           | 'if' right_hand_expression body ( 'else' body )? (statement)?
           | 'loop' body (statement)?
           | 'while' right_hand_expression body (statement)?
           | body (statement)?
           | expression ';' (statement)?
           | right_hand_expression // implicit return statmenet

expression -> (left_hand_expression '=')? right_hand_expression ;

left_hand_expression -> ( '*' )? identifier ; // Should be a valid expression, too.

right_hand_expression -> 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 ')' )? ) | '(' right_hand_expression ')' ;
call_arguments -> right_hand_expression ( ',' right_hand_expression )* ','? ;
literal -> NUMBER | STRING | 'true' | 'false' ;

NUMBER: Non-negative integers (binary, decimal, hexadecimal) and floats. STRING: Double-quoted strings with escape sequences.

Structs§

DefaultTypeSet
Use 64-bit integers and 64-bit floats (default).
TypeSet32
Use 32-bit integers and floats.
TypeSet128
Use 128-bit integers and 64-bit floats.

Traits§

FloatParser
Parse literals into Somni floats.
IntParser
Parse literals into Somni integers.
TypeSet
Defines the numeric types used in the parser.

Functions§

parse
parse_expression