program → declaration* EOF ;
declaration → classDecl
| funDecl
| varDecl
| statement ;
classDecl → "class" IDENTIFIER ( "<" IDENTIFIER )?
"{" method* "}" ;
funDecl → "fun" function ;
varDecl → "var" IDENTIFIER ( "=" expression )? EOL ;
statement → exprStmt
| forStmt
| ifStmt
| printStmt
| returnStmt
| whileStmt
| throwStmt
| block ;
exprStmt → expression EOL ;
forStmt → "for" "(" ( varDecl | exprStmt | ";" )
expression? ";"
expression? ")" EOL? statement ;
ifStmt → "if" "(" expression ")" statement ( "else" statement )? ;
printStmt → "print" expression EOL ;
returnStmt → "return" expression? EOL ;
whileStmt → "while" "(" expression ")" statement EOL? ;
throwStmt → "throw" expression EOL ;
block → "{" declaration* "}" ;
expression → assignment ;
assignment → ( call "." )? IDENTIFIER "=" assignment
| logic_or;
logic_or → logic_and ( "or" logic_and )* ;
logic_and → equality ( "and" equality )* ;
equality → comparison ( ( "!=" | "==" ) comparison )* ;
comparison → addition ( ( ">" | ">=" | "<" | "<=" ) addition )* ;
addition → multiplication ( ( "-" | "+" ) multiplication )* ;
multiplication → unary ( ( "/" | "*" ) unary )* ;
unary → ( "!" | "-" ) unary | call ;
call → primary ( "(" arguments? ")" | "." IDENTIFIER | "[" arguments? "]")* ;
primary → "true" | "false" | "nil" | "self"
| NUMBER | STRING | IDENTIFIER | "(" expression ")"
| "super" "." IDENTIFIER ;
method → scope function
function → IDENTIFIER "(" parameters? ")" block ;
parameters → IDENTIFIER ( "," IDENTIFIER )* ;
arguments → expression ( "," expression )* ;
scope → private | protected | public
NUMBER → DIGIT+ ( "." DIGIT+ )? ;
STRING → '"' <any char except '"'>* '"' ;
IDENTIFIER → ALPHA ( ALPHA | DIGIT )* ;
ALPHA → 'a' ... 'z' | 'A' ... 'Z' | '_' ;
DIGIT → '0' ... '9' ;
EOL → '\n'