Conlang/grammar.ebnf

57 lines
1.8 KiB
EBNF
Raw Normal View History

(* Conlang Expression Grammar *)
Start = Program ;
Program = Stmt* EOI ;
(* literal *)
Literal = STRING | CHARACTER | FLOAT | INTEGER | Bool ;
Bool = "true" | "false" ;
Identifier = IDENTIFIER ;
(* # Statements *)
(* statement *)
Stmt = Fn | Let | Expr ';' ;
Let = "let" "mut"? Identifier (':' Type)? ('=' Expr)? ';' ;
Fn = "fn" Identifier Block ; (* TODO: params, return value*)
(* # Expressions *)
(* expression *)
Expr = Assign ;
Block = '{' Expr '}' ;
Group = '(' Expr? ')' ;
Primary = Item | Identifier | Literal
| Block | Group | Branch ;
(* expression::math *)
Assign = Compare (AssignOp Compare)* ;
Compare = Range (CompareOp Range )* ;
Range = Logic (RangeOp Logic )* ;
Logic = Bitwise (LogicOp Bitwise)* ;
Bitwise = Shift (BitwiseOp Shift )* ;
Shift = Term (ShiftOp Term )* ;
Term = Factor (TermOp Factor )* ;
Factor = Unary (FactorOp Unary )* ;
Unary = (UnaryOp)* Primary ;
(* expression::math::operator *)
AssignOp = '=' | "+=" | "-=" | "*=" | "/=" |
"&=" | "|=" | "^=" |"<<=" |">>=" ;
CompareOp = '<' | "<=" | "==" | "!=" | ">=" | '>' ;
RangeOp = ".." | "..=" ;
LogicOp = "&&" | "||" | "^^" ;
BitwiseOp = '&' | '|' | '^' ;
ShiftOp = "<<" | ">>";
TermOp = '+' | '-' ;
FactorOp = '*' | '/' | '%' ;
UnaryOp = '*' | '&' | '-' | '!' ;
(* expression::control *)
Branch = While | If | For | Break | Return | Continue ;
If = "if" Expr Block (Else)? ;
While = "while" Expr Block (Else)? ;
For = "for" Identifier "in" Expr Block (Else)? ;
Else = "else" Block ;
Break = "break" Expr ;
Return = "return" Expr ;
Continue = "continue" ;