2023-10-19 19:40:03 +00:00
|
|
|
(* Conlang Expression Grammar *)
|
2024-01-21 11:32:18 +00:00
|
|
|
Start = File ;
|
|
|
|
|
|
|
|
Mutability = "mut"? ;
|
|
|
|
Visibility = "pub"? ;
|
|
|
|
|
|
|
|
|
|
|
|
File = Item* EOI ;
|
|
|
|
|
|
|
|
|
|
|
|
Item = Visibility (
|
|
|
|
Const | Static | Module
|
|
|
|
| Function | Struct | Enum
|
|
|
|
| Impl
|
|
|
|
) ;
|
|
|
|
|
|
|
|
|
|
|
|
(* item *)
|
|
|
|
Const = "const" Identifier ':' Type = Expr ';' ;
|
|
|
|
|
|
|
|
Static = "static" Mutability Identifier ':' Type = Expr ';' ;
|
|
|
|
|
|
|
|
Module = "mod" Identifier '{' (Item)* '}' ;
|
|
|
|
|
|
|
|
Function = "fn" Identifier '(' (Param ',')* Param? ')' ( '->' Type) Block ;
|
|
|
|
Param = Identifier ':' Type ;
|
|
|
|
|
|
|
|
Struct = "struct" Identifier (TupleStruct | StructStruct)?;
|
|
|
|
StructStruct= '{' (Identifier ':' Type),* '}' ;
|
|
|
|
TupleStruct = TyTuple ;
|
|
|
|
|
|
|
|
Enum = "enum" Identifier (TupleEnum | StructEnum)? ;
|
|
|
|
TupleEnum = TyTuple;
|
|
|
|
StructEnum = '{' (Identifier ':' Type)* '}' ;
|
|
|
|
|
2023-10-20 20:33:02 +00:00
|
|
|
|
2023-10-26 19:33:56 +00:00
|
|
|
(* statement *)
|
2024-01-21 11:32:18 +00:00
|
|
|
Stmt = ';' | (Let | Item | Expr ';'?) ;
|
|
|
|
Let = "let" Mutability Identifier (':' Ty)? ('=' Expr)? ;
|
|
|
|
(* TODO: Closure syntax *)
|
|
|
|
Closure = "cl" '(' Param* ')' Block ;
|
|
|
|
|
|
|
|
|
|
|
|
(* type *)
|
|
|
|
Ty = Never | Empty | TyTuple | Path | TyRef | TyFn ;
|
2024-01-04 08:18:09 +00:00
|
|
|
Never = '!' ;
|
|
|
|
Empty = '(' ')' ;
|
2024-01-21 11:32:18 +00:00
|
|
|
TyTuple = '(' (Ty ',')* Ty? ')' ;
|
|
|
|
TyRef = ('&' | '&&')* Path ;
|
|
|
|
TyFn = "fn" TyTuple (-> Ty)? ;
|
|
|
|
|
|
|
|
|
|
|
|
(* path *)
|
|
|
|
Path = '::'? PathPart ('::' PathPart)* ;
|
|
|
|
PathPart = "super" | "self" | Identifier ;
|
|
|
|
Identifier = IDENTIFIER ;
|
|
|
|
|
|
|
|
|
|
|
|
(* literal *)
|
|
|
|
Bool = "true" | "false" ;
|
2024-01-04 08:18:09 +00:00
|
|
|
|
|
|
|
|
2024-01-21 11:32:18 +00:00
|
|
|
(* expr *)
|
|
|
|
ExprKind = Assign | Compare | Range | Logic | Bitwise | Shift
|
|
|
|
| Factor | Term | Unary | Member | Call | Index
|
|
|
|
| Path | Literal | Array | ArrayRep | AddrOf
|
|
|
|
| Block | Group
|
|
|
|
| While | If | For | Break | Return | Continue ;
|
2023-10-26 19:33:56 +00:00
|
|
|
|
2023-10-26 19:36:55 +00:00
|
|
|
Expr = Assign ;
|
2023-10-19 19:40:03 +00:00
|
|
|
|
2024-01-21 11:32:18 +00:00
|
|
|
Assign = Path (AssignOp Assign ) | Compare ;
|
|
|
|
|
|
|
|
Binary = Compare | Range | Logic | Bitwise | Shift | Factor | Term ;
|
|
|
|
Compare = Range (CompareOp Range )* ;
|
|
|
|
Range = Logic (RangeOp Logic )* ;
|
|
|
|
Logic = Bitwise (LogicOp Bitwise)* ;
|
|
|
|
Bitwise = Shift (BitwiseOp Shift )* ;
|
|
|
|
Shift = Factor (ShiftOp Factor )* ;
|
|
|
|
Factor = Term (FactorOp Term )* ;
|
|
|
|
Term = Unary (FactorOp Unary )* ;
|
|
|
|
|
|
|
|
Unary = (UnaryOp)* Member ;
|
|
|
|
|
|
|
|
Member = Call ('.' Call)* ;
|
|
|
|
|
|
|
|
Call = Index ('(' Tuple? ')')* ;
|
|
|
|
|
|
|
|
Index = Primary ('[' Indices ']')* ;
|
|
|
|
Indices = (Expr ',')* Expr? ;
|
|
|
|
|
|
|
|
Primary = Literal | Path | Array | ArrayRep | AddrOf
|
|
|
|
| Block | Group
|
|
|
|
| If | While | For | Break | Return | Continue;
|
|
|
|
|
|
|
|
Literal = STRING | CHARACTER | FLOAT | INTEGER | Bool ;
|
|
|
|
|
|
|
|
Array = '[' (Expr ',')* Expr? ']' ;
|
|
|
|
ArrayRep = '[' Expr ';' Expr ']' ;
|
|
|
|
|
|
|
|
AddrOf = ('&' | '&&')* Expr ;
|
|
|
|
|
|
|
|
Block = '{' Stmt* '}';
|
|
|
|
|
|
|
|
Group = '(' (Empty | Expr | Tuple) ')' ;
|
|
|
|
Tuple = (Expr ',')* Expr? ;
|
|
|
|
Empty = ;
|
|
|
|
|
|
|
|
While = "while" Expr Block Else? ;
|
|
|
|
If = "if" Expr Block Else? ;
|
|
|
|
For = "for" Identifier "in" Expr Block Else? ;
|
|
|
|
Else = "else" Expr ;
|
2023-10-19 19:40:03 +00:00
|
|
|
Break = "break" Expr ;
|
|
|
|
Return = "return" Expr ;
|
|
|
|
Continue = "continue" ;
|
2024-01-21 11:32:18 +00:00
|
|
|
|
|
|
|
AssignKind = '=' | '+=' | '-=' | '*=' | '/=' |
|
|
|
|
'&=' | '|=' | '^=' |'<<=' |'>>=' ;
|
|
|
|
|
|
|
|
BinaryKind = CompareOp | RangeOp | LogicOp | BitwiseOp
|
|
|
|
| ShiftOp | TermOp | FactorOp ;
|
|
|
|
CompareOp = '<' | '<=' | '==' | '!=' | '>=' | '>' ;
|
|
|
|
RangeOp = '..' | '..=' ;
|
|
|
|
LogicOp = '&&' | '||' | '^^' ;
|
|
|
|
BitwiseOp = '&' | '|' | '^' ;
|
|
|
|
ShiftOp = '<<' | '>>';
|
|
|
|
TermOp = '+' | '-' ;
|
|
|
|
FactorOp = '*' | '/' | '%' ;
|
|
|
|
|
|
|
|
UnaryKind = '*' | '-' | '!' | '@' | '#' | '~' ;
|