-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
grammar.ebnf
86 lines (78 loc) · 3.62 KB
/
grammar.ebnf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
Program = { Item } ;
Item = FunctionDefinition | LetStmt ;
FunctionDefinition = 'fn' , ident , '(' , [ ParameterList ] , ')'
, [ '->' , Type ] , Block ;
ParameterList = Parameter , { ',' , Parameter } , [ ',' ] ;
Parameter = [ 'mut' ] , ident , ':' , Type ;
Block = '{' , { Statement } , [ Expression ] , '}' ;
Type = { '*' } , ( ident
| '(' , ')' ) ;
Statement = LetStmt | ReturnStmt | LoopStmt | WhileStmt | ForStmt
| BreakStmt | ContinueStmt | ExprStmt ;
LetStmt = 'let' , [ 'mut' ] , ident , [ ':' , Type ] , '='
, Expression , ';' ;
ReturnStmt = 'return' , [ Expression ] , ';' ;
LoopStmt = 'loop' , Block , [ ';' ] ;
WhileStmt = 'while' , Expression , Block , [ ';' ] ;
ForStmt = 'for' , ident , '=' , Expression , ';' , Expression
, ';' , Expression , Block , [ ';' ] ;
BreakStmt = 'break' , ';' ;
ContinueStmt = 'continue' , ';' ;
ExprStmt = ExprWithoutBlock , ';'
| ExprWithBlock , [ ';' ] ;
Expression = ExprWithoutBlock | ExprWithBlock ;
ExprWithBlock = Block | IfExpr ;
IfExpr = 'if' , Expression , Block , [ 'else' , ( IfExpr
| Block ) ] ;
ExprWithoutBlock = int
| float
| bool
| char
| ident
| PrefixExpr
| InfixExpr
| AssignExpr
| CallExpr
| CastExpr
| '(' , Expression , ')' ;
PrefixExpr = PREFIX_OPERATOR , Expression ;
InfixExpr = Expression , INFIX_OPERATOR , Expression ;
(* The left hand side can only be an `ident` or a `PrefixExpr` with the `*` operator *)
AssignExpr = Expression , ASSIGN_OPERATOR , Expression ;
CallExpr = ident , '(' , [ ArgumentList ] , ')' ;
ArgumentList = Expression , { ',' , Expression } , [ ',' ] ;
CastExpr = Expression , 'as' , Type ;
ident = LETTER , { LETTER | DIGIT } ;
int = DIGIT , { DIGIT | '_' }
| '0x' , HEX , { HEX | '_' } ;
float = DIGIT , { DIGIT | '_' } , ( '.' , DIGIT , { DIGIT | '_' }
| 'f' ) ;
char = "'" , ( ASCII_CHAR - '\'
| '\' , ( ESCAPE_CHAR
| "'"
| 'x' , 2 * HEX ) ) , "'" ;
bool = 'true' | 'false' ;
comment = '//' , { CHAR } , ? LF ?
| '/*' , { CHAR } , '*/' ;
LETTER = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I'
| 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R'
| 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z' | 'a'
| 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'j'
| 'k' | 'l' | 'm' | 'n' | 'o' | 'p' | 'q' | 'r' | 's'
| 't' | 'u' | 'v' | 'w' | 'x' | 'y' | 'z' | '_' ;
DIGIT = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8'
| '9' ;
HEX = DIGIT | 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'a'
| 'b' | 'c' | 'd' | 'e' | 'f' ;
CHAR = ? any UTF-8 character ? ;
ASCII_CHAR = ? any ASCII character ? ;
ESCAPE_CHAR = '\' | 'b' | 'n' | 'r' | 't' ;
PREFIX_OPERATOR = '!' | '-' | '&' | '*' ;
INFIX_OPERATOR = ARITHMETIC_OPERATOR | RELATIONAL_OPERATOR
| BITWISE_OPERATOR | LOGICAL_OPERATOR ;
ARITHMETIC_OPERATOR = '+' | '-' | '*' | '/' | '%' | '**' ;
RELATIONAL_OPERATOR = '==' | '!=' | '<' | '>' | '<=' | '>=' ;
BITWISE_OPERATOR = '<<' | '>>' | '|' | '&' | '^' ;
LOGICAL_OPERATOR = '&&' | '||' ;
ASSIGN_OPERATOR = '=' | '+=' | '-=' | '*=' | '/=' | '%='
| '**=' | '<<=' | '>>=' | '|=' | '&=' | '^=' ;