/
grammar.bnf
108 lines (92 loc) · 4.11 KB
/
grammar.bnf
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
CONST ::= "const"
REF ::= "ref"
ALPHA ::= "A-Za-z"
NUM ::= "0-9"
UNDER ::= "_"
ESCAPE ::= "\n"|"\r"|"\t"|"\b"|"\\"|"\'"|"\""
EOL ::= ";"|"\n"
IDENTIFIER ::= (ALPHA|UNDER)(NUM|ALPHA|UNDER)*
BUILTIN ::= CONST? REF? ("int"|"float"|"string"|"bool")
TYPE ::= BUILTIN
| CONST? REF? IDENTIFIER
| CONST? REF? "[" TYPE "]"
| CONST? REF? "{" TYPE ("," TYPE)* ","? "}"
| "typeof" logic_or
STRING ::= "\"" (NUM|ALPHA|UNDER|ESCAPE)* "\""
| STRING+
NUMBER ::= NUM+ "."? NUM+ ("e" NUM+)? ;
LITERAL ::= "false"|"true"|"null"|STRING|NUMBER
expression ::= comma
comma ::= assignment ("," assignment)*
assignment ::= (call ".")? IDENTIFIER ("="|"+="|"-="|"*="|"/=") assignment
| ternary
ternary ::= logic_or ("?" logic_or ":" ternary)*
logic_or ::= logic_and (("or"|"||") logic_and)*
logic_and ::= bit_or (("and"|"&&") bit_or)*
bit_or ::= bit_xor ("|" bit_xor)*
bit_xor ::= bit_and ("^" bit_and)*
bit_and ::= equality ("&" equality)*
equality ::= comparison (("!="|"==") comparison)*
comparison ::= range ((">"|">="|"<"|"<=") range)*
range ::= bitshift ((".."|"..=") bitshift)*
bitshift ::= addition (("<<"|">>") addition)*
addition ::= multiplication (("-"|"+"|"%") multiplication)*
multiplication ::= unary (("/"|"*") unary)*
unary ::= ("!"|"-"|"~"|"++"|"--") unary
| call
arguments ::= assignment ("," assignment)*
call ::= list (("(" arguments? ")")|("." IDENTIFIER))*
list ::= "[" (assignment ("," assignment)* ","?)? "]"
| "[" assignment ";" assignment "]"
| tuple
tuple ::= "{" assignment ("," assignment)* ","? "}"
| get
get ::= expression "." IDENTIFIER
| primary
index ::= expression "[" expression "]" ("=" assignment)?
super ::= "super" "." IDENTIFIER
primary ::= LITERAL|IDENTIFIER|index
| super|"this"
| "(" expression ")"
| expression "::" IDENTIFIER
| "move" IDENTIFIER
declaration ::= statement
| import
| var
| var_tuple
| fn
| type_decl
| class
import ::= "import" STRING EOL
var ::= ("var"|"const"|"ref") IDENTIFIER (":" TYPE)? "=" expression EOL
ident_tuple ::= "{" IDENTIFIER|ident_tuple ("," (IDENTIFIER|ident_tuple))* ","? "}"
var_tuple ::= ("var"|"const"|"ref") ident_tuple (":" TYPE)? "=" expression EOL
params ::= (IDENTIFIER|ident_tuple ":" TYPE)*
fn ::= "fn" "~"? IDENTIFIER "(" params ")" "->" TYPE block
type_decl ::= "type" IDENTIFIER "=" TYPE EOL
class ::= "class" IDENTIFIER "{" ((("private"|"public"|"protected")(var|fn))|comment)* "}"
statement ::= expr_stmt
| for
| if
| while
| switch
| break
| continue
| return
| block
| comment
expr_stmt ::= expression EOL
for ::= "for" "(" (var|expr_stmt|";") expression? ";" expression? ")" block
if ::= "if" expression block ("else" if|block)?
while ::= "while" expression block
switch ::= "switch" expression "{" (expression "->" statement)* ("default" "->" statement)? "}"
break ::= "break" EOL
continue ::= "continue" EOL
return ::= "return" expression? EOL
block ::= "{" (var|statement)* "}"
CHAR ::= " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
comment ::= single_comment
| multi_comment
single_comment ::= "//" CHAR* "\n"
multi_comment ::= "/*" (CHAR*|multi_comment) "*/"
program ::= declaration* EOF