Skip to main content
v0.8.0

Grammar

Program
HeaderDecl
Used by Program
ImportDecl
→ "import" ImportPath ";"
Used by HeaderDecl
ExportDecl
→ "export" ImportPath ";"
Used by HeaderDecl
ImportPath
PathRoot
→ "root" | "std" | "self" | "super" | IDENTIFIER
Used by ImportPath
ImportTree
→ ImportItem| IDENTIFIER "::" ImportTree| "{" ImportItem ( "," ImportItem )* ","? "}"| "*"
Used by ImportPath
ImportItem
→ IDENTIFIER ( "as" IDENTIFIER )?
Used by ImportTree
Declaration
Used by ProgramBlock
LetDeclaration
→ "let" IDENTIFIER ( ":" Type )? "=" Expression ";"
Used by Declaration
MutDeclaration
→ "mut" IDENTIFIER ( ":" Type )? "=" Expression ";"
FunDeclaration
→ "pub"? "fun" IDENTIFIER GenericParams? "(" Params? ")" ( "->" Type )? WhereClause? Block
StructDeclaration
→ "pub"? "struct" IDENTIFIER GenericParams? WhereClause? "{" StructFields "}"
Used by Declaration
EnumDeclaration
→ "pub"? "enum" IDENTIFIER GenericParams? WhereClause? "{" EnumVariants "}"
Used by Declaration
ImplBlock
→ "impl" ( Type "for" )? Type "{" FunDeclaration* "}"
Used by Declaration
TraitDeclaration
→ "pub"? "aspect" IDENTIFIER "{" TraitMethod* "}"
Used by Declaration
TraitMethod
→ "fun" IDENTIFIER "(" Params? ")" ( "->" Type )? ( Block | ";" )
Params
→ Param ( "," Param )* ","?
Param
→ ( "mut" )? "self" | IDENTIFIER ( ":" Type )?
Used by Params
StructFields
→ StructField ( "," StructField )* ","?
StructField
→ IDENTIFIER ":" Type
Used by StructFields
EnumVariants
→ EnumVariant ( "," EnumVariant )* ","?
EnumVariant
→ IDENTIFIER ( "{" StructFields "}" )?
Used by EnumVariants
GenericParams
→ "<" GenericParam ( "," GenericParam )* ">"
GenericParam
→ IDENTIFIER ( ":" BoundList )? // since v0.7.0; RFC-0034
BoundList
→ Type ( "+" Type )* // since v0.7.0; RFC-0034
WhereClause
→ "where" WhereConstraint ( "," WhereConstraint )* // since v0.7.0; RFC-0002
WhereConstraint
→ IDENTIFIER ":" BoundList // BoundList since v0.7.0; RFC-0034
Used by WhereClause
Statement
Used by Declaration
ExpressionStatement
→ Expression ";"
Block
→ "{" Declaration* "}"
IfStatement
→ "if" "(" Expression ")" Block ( "else" ( IfStatement | Block ) )?
Used by Statement
WhileStatement
→ "while" "(" Expression ")" Block
Used by Statement
ForStatement
→ "for" "(" ForInit Expression? ";" Expression? ")" Block| "for" "(" "let" IDENTIFIER "in" Expression ")" Block
Used by Statement
ForInit
Used by ForStatement
LoopStatement
→ "loop" Block
Used by Statement
ReturnStatement
→ "return" Expression? ";"
Used by Statement
BreakStatement
→ "break" Expression? ";"
Used by Statement
ContinueStatement
→ "continue" ";"
Used by Statement
Expression
AssignmentExpression
Used by Expression
LValue
→ IDENTIFIER | CallExpression "." IDENTIFIER | CallExpression "[" Expression "]"
AssignOp
→ "=" | "+=" | "-=" | "*=" | "/=" | "%="
LogicalOrExpression
LogicalAndExpression
ComparisonExpression
→ TermExpression ( ( ">" | ">=" | "<" | "<=" | "!=" | "==" ) TermExpression )?
TermExpression
→ FactorExpression ( ( "+" | "-" ) FactorExpression )*
FactorExpression
→ CastExpression ( ( "*" | "/" | "%" ) CastExpression )*
CastExpression
→ AscribeExpression ( "as" Type )*
AscribeExpression
→ UnaryExpression ( ":" Type )?
UnaryExpression
→ ( "!" | "-" | "*" | "&" | "&mut" ) UnaryExpression | PostfixExpression
PostfixExpression
→ PrimaryExpression ( "(" Arguments? ")" | "." IDENTIFIER | "[" Expression "]" | "?" )*
Arguments
→ Expression ( "," Expression )* ","?
PrimaryExpression
→ INT | FLOAT | STRING | "true" | "false" | "None" | "()"| "(" Expression ( "," Expression )+ ")" // tuple| "(" Expression ")"| "[" ( Expression ( "," Expression )* ","? )? "]" // array literal| "[" Expression ";" INT "]" // repeat construction [expr; N]| Path| StructLiteral| MatchExpression| IfExpression| LoopExpression| ClosureExpression
Path
→ ( "root" | "std" | "self" | "super" | IDENTIFIER ) ( "::" IDENTIFIER )*
StructLiteral
→ Path "{" FieldInit ( "," FieldInit )* ","? "}"
FieldInit
→ IDENTIFIER ( ":" Expression )? // omitting ": Expression" uses the local variable of the same name
MatchExpression
→ "match" Expression "{" MatchArm ( "," MatchArm )* ","? "}"
MatchArm
→ Pattern ( "if" Expression )? "=>" Expression
IfExpression
→ "if" "(" Expression ")" Block "else" Block
LoopExpression
→ "loop" Block
ClosureExpression
→ "(" Params? ")" ( "->" Type )? Block
Pattern
→ "_"| "None"| IDENTIFIER| "(" Pattern ( "," Pattern )* ")" // tuple pattern| IDENTIFIER "::" IDENTIFIER ( "{" PatternFields "}" )?| INT | FLOAT | STRING | "true" | "false"
Used by MatchArm
PatternFields
→ IDENTIFIER ( "," IDENTIFIER )*
Used by Pattern
Type
→ IDENTIFIER ( "<" TypeArgs ">" )?| "*" "mut"? Type // regular pointer types| "()"| "(" Type ( "," Type )+ ")" // tuple type| Type "[]" // array shorthand| "[" Type ";" INT "]" // fixed-size array type (since v0.19.0; RFC-0053)| "(" TypeList? ")" "->" Type // function / closure type| "impl" Type // anonymous bounded param; parameter position only (since v0.7.0; RFC-0035)
TypeArgs
→ Type ( "," Type )*
Used by Type
TypeList
→ Type ( "," Type )*
Used by Type