-
Notifications
You must be signed in to change notification settings - Fork 1
/
grammar.bnf
262 lines (180 loc) · 8.83 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Our BNF syntax used in the specification below works as follows:
//
// foo ::= bar baz
// Foo derives bar followed by baz.
//
// foo ::= bar | baz
// Foo derives either bar or baz.
//
// bar ::= baz*
// Bar derives a sequence of zero or more baz.
//
// bar ::= baz+
// Bar derives a sequence of one or more baz.
//
// bar ::= foo? baz?
// Bar derives optional foo followed by optional baz.
//
// bar ::= baz%","+
// Bar derives a sequence of one or more baz where consecutive baz"s are separated by a comma
// without a trailing comma.
//
// baz ::= ()
// Baz has a nulling rule (derives an epsilon string).
//
// foo ::= &(bar baz)
// Foo makes a zero-width assertion that bar and baz are present ahead.
//
// foo ::= !(bar baz)
// Foo makes a zero-width assertion that bar and baz are NOT present ahead.
//
// #start foo
// Declare start symbol.
//
// /\w+/ -> ident
// Declare regexp token calle.
#start program
program ::= module
module ::= top_level_statement*
top_level_statement ::= function_definition_forbid_self
| struct_definition
| implementation
| submodule
| module_declaration
| use_statement
| global_declaration
global_declaration ::= "global" ident global_type_annotation "=" literal ";"
submodule ::= "mod" ident "{" module "}"
contract ::= "contract" ident "{" module "}"
function_definition_allow_self ::= attribute? function_modifiers "fn" ident generics "(" function_parameters_allow_self ")" function_return_type block
function_definition_forbid_self ::= attribute? function_modifiers "fn" ident generics "(" function_parameters_forbid_self ")" function_return_type block
function_modifiers ::= "unconstrained"? "open"?
generics ::= ("<" ident%","+ ">")?
struct_definition ::= "struct" ident generics "{" struct_fields "}"
lambda_return_type ::= ("->" type)?
function_return_type ::= ("->" optional_visibility type)?
#atomic
attribute ::= "#[" ("foreign(" ASCII_ALPHA+ ")" | "builtin(" ASCII_ALPHA+ ")" | "oracle(" ASCII_ALPHA+ ")" | "test") "]"
struct_fields ::= (ident ":" type)%","...*
lambda_parameters ::= pattern%","*
function_parameters_forbid_self ::= (pattern ":" optional_visibility type)%","...*
function_parameters_allow_self ::= (self_parameter | pattern ":" optional_visibility type)%","...*
self_parameter ::= "self"
implementation ::= "impl" generics type "{" function_definition_allow_self* "}"
block ::= "{" statement_not_last* expression? "}"
global_type_annotation ::= (":" type)?
optional_type_annotation ::= (":" type)?
module_declaration ::= mod_keyword ident ";"
#atomic
mod_keyword ::= "mod" WHITESPACE
use_statement ::= "use" WHITESPACE path (WHITESPACE "as" ident)? ";"
path ::= "crate" "::" idents | "dep" "::" idents | idents
idents ::= ident%"::"+
// disallow keywords
#atomic
ident ::= !"as" !"assert" !"asserteq" !"bool" !"char" !"comptime" !"constrain" !"contract" !"crate" !"dep" !"distinct" !"else" !"field" !"fn" !"for" !"formatstring" !"global" !"if" !"impl" !"in" !"internal" !"let" !"mod" !"mut" !"open" !"pub" !"return" !"string" !"struct" !"trait" !"type" !"unconstrained" !"use" !"where" !"while" (ASCII_ALPHA | "_") (ASCII_ALPHANUMERIC | "_")*
statement_not_last ::= declaration ";" | assignment ";" | expression ";" | expression_not_semi
// "constrain" is deprecated
// constrain ::= "constrain" expression
declaration ::= let_keyword pattern optional_type_annotation "=" expression
#atomic
let_keyword ::= "let" WHITESPACE
pattern ::= "mut" pattern
| "(" pattern%","* ")"
| path "{" (ident | ident ":" pattern)%","* "}"
| ident
assignment ::= lvalue assign_operator expression
#atomic
assign_operator ::= ("+" | "-" | "*" | "/" | "%" | "&" | "^" | "<<" | ">>" | "|" | "") "="
lvalue ::= ident ("." (ident | lvalue_integer) | "[" expression "]")*
#atomic
lvalue_integer ::= (NONZERO ASCII_DIGIT{,18} | "0")
type ::= type_inner
type_inner ::= int_type | function_type | vec_type | field_type | bool_type
| string_type | named_type | array_type
| tuple_type
optional_visibility ::= "pub"?
// DEPRECATED
// maybe_comp_time ::= "comptime"?
field_type ::= "Field"
bool_type ::= "bool"
string_type ::= "str" ("<" (variable | integer) ">")?
int_type ::= int_type_token
#atomic
int_type_token ::= "u" ASCII_DIGIT ASCII_DIGIT?
named_type ::= path generic_type_args
vec_type ::= "Vec" generic_type_args
generic_type_args ::= ("<" (type &("," | ">") | type_expression)%","+ ","? ">")?
array_type ::= "[" type (";" (variable | integer))? "]"
tuple_type ::= "(" (type%","+ ","?)? ")"
function_type ::= "fn" "(" (type%","+ ","?)? ")" "->" type
////////////////////////////////////////////////////////////////////////////////
/// type_expression ::= expression_with_precedence(lowest_type_precedence, type_expression, true)
/// expression ::= expression_with_precedence(Lowest, expression, false)
/// expression_with_precedence(Highest, ExprParser, true) ::= type_expression_term
/// expression_with_precedence(Highest, ExprParser, false) ::= term
/// expression_with_precedence(precedence, ExprParser, true) ::= expression_with_precedence(precedence.next_type_precedence(), ExprParser, true)
/// (
/// operator_with_precedence(precedence)
/// expression_with_precedence(precedence.next_type_precedence(), ExprParser, true)
/// )*
/// expression_with_precedence(precedence, ExprParser, false) ::= expression_with_precedence(precedence.next(), ExprParser, false)
/// (
/// operator_with_precedence(precedence)
/// expression_with_precedence(precedence.next(), ExprParser, false)
/// )*
/// EQUIVALENT TO:
expression ::= term (operator term)*
expression_no_constructors ::= term_no_constructors (operator term_no_constructors)*
expression_not_semi ::= if_expr | block
operator ::= "==" | "!=" | "|" | "^" | "&" | "<<" | ">>" | "<=" | ">=" | "<" | ">" | "+" | "-" | "/" | "*" | "%"
type_expression ::= type_expression_term (type_operator type_expression_term)*
type_operator ::= "+" | "-" | "/" | "*" | "%"
type_expression_term ::= negation_in_type_expression | type_expression_atom
////////////////////////////////////////////////////////////////////////////////
term ::= not | negation | atom_or_right_unary
term_no_constructors ::= not_no_constructors | negation_no_constructors | atom_or_right_unary_no_constructors
atom_or_right_unary ::= atom (call_rhs | array_rhs | cast_rhs | member_rhs)*
atom_or_right_unary_no_constructors ::= atom_no_constructors (call_rhs | array_rhs | cast_rhs | member_rhs)*
call_rhs ::= "(" expression_list ")"
array_rhs ::= "[" expression "]"
cast_rhs ::= WHITESPACE "as" WHITESPACE type
member_rhs ::= "." field_name ("(" expression_list ")")?
if_expr ::= "if" WHITESPACE expression_no_constructors block ("else" (block | if_expr))?
lambda ::= "|" lambda_parameters "|" lambda_return_type expression
for_expr ::= for_keyword ident in_keyword for_range block
#atomic
for_keyword ::= "for" WHITESPACE
#atomic
in_keyword ::= "in" WHITESPACE
for_range ::= expression_no_constructors ".." expression_no_constructors | expression_no_constructors
array_expr ::= standard_array | array_sugar
standard_array ::= "[" expression%","...* "]"
array_sugar ::= "[" expression ";" expression "]"
expression_list ::= (expression%","+ ","?)?
not ::= "!" term
not_no_constructors ::= "!" term_no_constructors
negation ::= "-" term
negation_no_constructors ::= "-" term_no_constructors
negation_in_type_expression ::= "-" type_expression_term
atom ::= tuple | if_expr | array_expr | constructor | lambda | block | variable | literal
atom_no_constructors ::= tuple | if_expr | array_expr | lambda | block | variable | literal
type_expression_atom ::= variable | type_literal | "(" type_expression ")"
tuple ::= "(" expression_list ")"
field_name ::= ident | integer
// lexer allows only even number of digits for hex
#atomic
integer ::= "0x" "00"* (hex hex){1,8} | NONZERO ASCII_DIGIT{1,18} | "0"
#atomic
NONZERO ::= "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
#atomic
hex ::= ASCII_DIGIT | "a" | "b" | "c" | "d" | "e" | "f" | "A" | "B" | "C" | "D" | "E" | "F"
constructor ::= path "{" constructor_field%","...* "}"
constructor_field ::= ident (":" expression)?
variable ::= path
literal ::= integer | bool | str
type_literal ::= integer
literal_or_collection ::= literal | constructor | array_expr
bool ::= "true" | "false"
str ::= "\"" (!"\"" ANY)* "\""
WHITESPACE ::= " " | "\t" | "\n"