Language reference · section 5

5. Formal grammar

The grammar below matches crates/pin-compiler/src/grammar.lalrpop. Quoted words are keywords or punctuation. NAME, NUMBER, STRING, BLOCK, and RAW are token classes from section 4. NEWLINE, INDENT, and DEDENT come from the indentation rules in section 4.

program          = { statement } ;

statement        = simple_stmt , NEWLINE
                 | "return" , NEWLINE
                 | "break" , NEWLINE
                 | "return" , "if" , expr , NEWLINE
                 | "break" , "if" , expr , NEWLINE
                 | simple_stmt , "if" , expr , NEWLINE
                 | compound_stmt
                 | NAME , STRING , ":" , block                (* NAME must be "test" *)
                 | { decorator } , fn_decl
                 | { decorator } , "export" , fn_decl
                 | "export" , declaration
                 | "export" , "default" , expr , NEWLINE
                 | "from" , STRING , "import" , name_list , NEWLINE ;

simple_stmt      = contract , expr
                 | [ "mut" ] , NAME , [ ":" , type ] , "=" , expr
                 | "const" , NAME , [ ":" , type ] , "=" , expr
                 | [ "mut" ] , "[" , array_items , "]" , "=" , expr      (* pattern *)
                 | [ "mut" ] , "{" , object_fields , "}" , "=" , expr    (* pattern *)
                 | "yield" , expr
                 | "throw" , expr
                 | "return" , expr
                 | postfix , "." , prop_name , "=" , expr
                 | postfix , "[" , expr , "]" , "=" , expr
                 | expr ;

contract         = "requires" | "ensures" | "assert" ;

declaration      = [ "mut" ] , NAME , [ ":" , type ] , "=" , expr , NEWLINE
                 | "const" , NAME , [ ":" , type ] , "=" , expr , NEWLINE
                 | fn_decl | class_decl | regex_decl ;

compound_stmt    = "if" , expr , ":" , block , [ else_part ]
                 | "while" , expr , ":" , block
                 | "try" , ":" , block , [ "catch" , NAME , ":" , block ] , [ "finally" , ":" , block ]
                                                       (* at least one of catch or finally *)
                 | "for" , NAME , "in" , expr , ":" , block
                 | "for" , "await" , NAME , "in" , expr , ":" , block
                 | "for" , NAME , "of" , expr , ":" , block
                 | "for" , NAME , "," , NAME , "of" , expr , ":" , block
                 | [ "mut" ] , NAME , "=" , "match" , expr , ":" , match_block
                 | "const" , NAME , "=" , "match" , expr , ":" , match_block
                 | [ "mut" ] , NAME , "=" , "match" , ":" , cond_block
                 | "const" , NAME , "=" , "match" , ":" , cond_block
                 | [ "mut" ] , NAME , "=" , [ "async" ] , "(" , paren_items , ")" , "->" , ":" , block
                 | fn_decl | class_decl | regex_decl ;

else_part        = "else" , ":" , block
                 | "else" , "if" , expr , ":" , block , [ else_part ] ;

block            = NEWLINE , INDENT , statement , { statement } , DEDENT ;

match_block      = NEWLINE , INDENT , match_arm , { match_arm } , DEDENT ;
match_arm        = pattern , [ "if" , expr ] , "=>" , expr , NEWLINE ;
pattern          = pattern_atom , { "|" , pattern_atom } ;
pattern_atom     = NUMBER | "-" , NUMBER | STRING | "true" | "false" | "null" | NAME ;

cond_block       = NEWLINE , INDENT , cond_arm , { cond_arm } , DEDENT ;
cond_arm         = expr , "=>" , expr , NEWLINE
                 | "else" , "=>" , expr , NEWLINE ;

fn_decl          = [ "async" ] , "fn" , NAME , "(" , params , ")" , ":" , [ type , ":" ] , block ;
params           = [ param , { "," , param } ] ;
param            = [ "mut" ] , NAME , [ ":" , type ] , [ "=" , expr ] ;   (* not both mut and a default *)

class_decl       = "class" , NAME , [ "extends" , NAME ] , ":" , NEWLINE , INDENT , member , { member } , DEDENT ;
member           = NAME , "=" , expr , NEWLINE
                 | NAME , ":" , type , "=" , expr , NEWLINE
                 | [ "async" ] , NAME , "(" , params , ")" , ":" , [ type , ":" ] , block ;

regex_decl       = NAME , "=" , NAME , { NAME } , ":" , NEWLINE , INDENT , regex_row , { regex_row } , { regex_check } , DEDENT ;
regex_row        = RAW , NAME , NEWLINE
                 | "part" , RAW , NAME , NEWLINE ;
regex_check      = ( "matches" | "rejects" ) , STRING , { STRING } , NEWLINE ;

decorator        = "@" , NAME , [ "(" , decorator_option , { "," , decorator_option } , ")" ] , NEWLINE ;
decorator_option = NAME , ":" , NUMBER ;

name_list        = NAME , { "," , NAME } ;

expr             = arrow | pipe
                 | "if" , pipe , "then" , expr , "else" , expr ;

arrow            = [ "async" ] , "(" , paren_items , ")" , "->" , expr ;
paren_items      = [ paren_item , { "," , paren_item } ] ;
paren_item       = expr | NAME , ":" , type ;

pipe             = pipe , "|>" , nullish | nullish ;
nullish          = nullish , "??" , or | or ;
or               = or , "or" , and | and ;
and              = and , "and" , equality | equality ;
equality         = equality , ( "is" | "isnt" | "in" ) , compare | compare ;
compare          = additive , { ( "<" | "<=" | ">" | ">=" ) , additive } ;
additive         = additive , ( "+" | "-" ) , multiplicative | multiplicative ;
multiplicative   = multiplicative , ( "*" | "/" | "%" ) , power | power ;
power            = unary , [ "**" , power ] ;
unary            = "-" , unary | "not" , unary | "await" , unary | postfix ;

postfix          = postfix , "(" , call_args , ")"
                 | postfix , "[" , expr , "]"
                 | postfix , "." , prop_name
                 | postfix , "?." , prop_name
                 | atom ;
call_args        = [ call_arg , { "," , call_arg } ] ;
call_arg         = "..." , expr | expr | NAME , ":" , expr ;

atom             = NUMBER | STRING | BLOCK | template
                 | "true" | "false" | "null" | NAME
                 | "[" , array_items , "]"
                 | "[" , expr , ( ".." | "..." ) , expr , [ "by" , expr ] , "]"
                 | "[" , expr , comp_clause , { comp_clause } , "]"
                 | "{" , object_fields , "}"
                 | "new" , member_chain , "(" , array_items , ")"
                 | "(" , paren_items , ")" ;
array_items      = [ array_item , { "," , array_item } ] ;
array_item       = expr | "..." , expr ;
object_fields    = [ object_field , { "," , object_field } ] ;
object_field     = NAME , ":" , expr | STRING , ":" , expr | NAME | "..." , expr ;
comp_clause      = "for" , NAME , "in" , expr
                 | "for" , NAME , "of" , expr
                 | "for" , NAME , "," , NAME , "of" , expr
                 | "when" , expr ;
member_chain     = NAME , { "." , prop_name } ;
prop_name        = NAME | "requires" | "ensures" | "assert" | "then"
                 | "catch" | "finally" | "try" | "yield" | "break" ;
type             = type_atom , { "|" , type_atom } ;
type_atom        = type_base , { "[" , "]" } ;
type_base        = NAME | "(" , type , ")" ;

Notes on the grammar: