-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrammer.ebnf
110 lines (90 loc) · 4 KB
/
grammer.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
program = { ( function_declaration | function_definition | struct_definition | enum_definition | import_statement | let_statement ) } ;
function_prototype = identifier "(" argument_list ")" [ type ] ;
function_declration = "extern" function_prototype ;
function_definition = "fn" function_prototype block ;
struct_definition = "struct" identifier "{" struct_fields [ { function_definition } ] "}" ;
struct_fields = struct_fields "," struct_field | struct_field ;
struct_field = identifier type ;
enum_definition = "enum" identifier "{" enum_fields "}" ;
enum_fields = enum_fields "," enum_field | enum_field ;
enum_field = identifier | identifier "=" number ;
type_alias = "type" identifier "=" base_type ;
block = "{" { statements } "}" ;
statements = statements statement | statement ;
statement = return_statement
| let_statement
| expression_statement
| break_statement
| struct_definition
| enum_definition
| import_statement
| defer_statement ;
return_statement = "return" expression ";" ;
let_statement = "let" [ "mut" ] identifier [ "[" [ expression ] "]" ] [ type ] "=" expression ";" ;
expression_statement = expression ";" ;
break_statement = "break" ";"
import_statement = "import" string ";"
defer_statement = "defer" block | "defer" expr ";"
expression = if_expression | while_expression | cast_expression | assignment ;
if_expression = "if" "(" expression ")" block [ { "else" "if" expression block } ] [ "else" block ] ;
while_expression = "while" "(" expression ")" block ;
cast_expression = expression "as" type ;
assignment = identifier "=" assignment
| "*" identifier "=" assignment
| array_deref "=" assignment
| equality ;
equality = comparison [ ( "!=" | "==" ) comparison ] ;
comparison = term [ ( ">" | ">=" | "<" | "<=" ) term ] ;
term = factor [ ( "-" | "+" ) factor ] ;
factor = unary [ ( "/" | "*" ) unary ] ;
unary = ( "!" | "-" | "*" | ( "&" [ "mut" ] ) ) unary | primary ;
primary = number
| string
| identifer
| struct_value
| enum_value
| get_expr
| function_call
| literal
| array_deref
| array_literal
| "(" expression ")" ;
function_call = callable "(" argument_list ")"
callable = identifier
| get_expr
array_deref = identifier "[" expression "]" ;
array_literal = "[" [ { comma_seperated_exprs } ] "]" ;
comma_seperated_exprs = comma_seperated_exprs "," expression
| expression
struct_value = identifier "{" struct_value_fields "}" ;
struct_value_fields = struct_value_fields "," struct_value_field | struct_value_field | %empty ;
struct_value_field = identifier ":" expression ;
get_expr = ( identifier | get_expr ) "." identifier
enum_value = identifier "::" identifier
literal = "true" | "false" | "null" ;
number = digits [ "." digits ] ;
digits = digit [ { digit } ] ;
string = "\"" [ { ( letter | escape_character ) } ] "\"" ;
escape_character = "\n" | "\t" | "\\" "\"" ;
argument_list = argument | argument_list "," argument | argument_list "," "..." [ type ] ;
argument = identifier [ type ] ;
type = ":" base_type ;
base_type = "any" | "bool" | "int" | "string"
| "u8" | "u16" | "u32" | "u64"
| "s8" | "s16" | "s32" | "s64"
| "f32" | "f64" | "float" | "double"
| "void" | "char" | base_type_ptr
| base_type_array | base_type_mut | identifier ;
base_type_ptr = base_type "*" ;
base_type_array = base_type "[" "]" ;
base_type_mut = "mut" base_type ;
identifier = ( letter | "_" ) [ { ( letter | digit | "_" ) } ] ;
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" ;