Program → Padding StatementList Padding
StatementList → (Statement ';'?)*
Statement → Assignment
| Block
| Break
| Continue
| ExpressionStmt
| For
| Function
| If
| Loop
| Return
| While
Assignment → AssignmentTarget '=' Expression
AssignmentTarget → Identifier ('[' Expression ']')*
Block → '{' StatementList '}'
Break → 'break'
Continue → 'continue'
ExpressionStmt → Expression
For → 'for' Identifier 'in' Expression Block
Function → 'fn' Identifier '(' Parameters? ','? ')' Block
If → 'if' '(' Expression ')' Block ('else' Block)?
Loop → 'loop' Block
Return → 'return' Expression?
While → 'while' '(' Expression ')' Block
Parameters → Identifier (',' Identifier)*
Expression → LogicalOr
LogicalOr → LogicalAnd ('||' LogicalAnd)*
LogicalAnd → Equality ('&&' Equality)*
Equality → Relational (('==' | '!=') Relational)*
Relational → Sum (('>=' | '<=' | '>' | '<') Sum)*
Sum → Product (('+' | '-') Product)*
Product → Power (('*' | '/' | '%') Power)*
Power → Unary ('^' Power)?
Unary → ('-' | '!')* Postfix
Postfix → Atom (Call | ListAccess)*
Atom → Number
| Boolean
| Null
| String
| List
| FunctionExpression
| Identifier
| '(' Expression ')'
Number → Digit+ ('.' Digit+)?
Boolean → 'true' | 'false'
Null → 'null'
String → '"' [^"]* '"' | "'" [^']* "'"
List → '[' Arguments? ','? ']'
FunctionExpression
→ 'fn' '(' Parameters? ','? ')' Block
Call → '(' Arguments? ','? ')'
ListAccess → '[' Expression ']'
Arguments → Expression (',' Expression)*
Identifier → Letter (Letter | Digit)*
Padding → (Whitespace | LineComment)*
LineComment → '//' [^\n]*
Whitespace → Unicode whitespace
Digit → '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
Letter → 'a' | 'b' | ... | 'z' | 'A' | 'B' | ... | 'Z' | '_'