-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtree-sitter-go.ebnf
426 lines (309 loc) · 10.9 KB
/
tree-sitter-go.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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//
// From tree-sitter-go/src/grammar.json
//
//
// EBNF to generate railroad diagram at
// (IPV6) https://www.bottlecaps.de/rr/ui
// (IPV4) https://rr.red-dove.com/ui
//
source_file ::=
( _statement ( '\n' | ';' | '\0' ) | _top_level_declaration ( '\n' | ';' | '\0' ) )* _top_level_declaration?
_top_level_declaration ::=
package_clause
| function_declaration
| method_declaration
| import_declaration
package_clause ::=
'package' _package_identifier
import_declaration ::=
'import' ( import_spec | import_spec_list )
import_spec ::=
( dot | blank_identifier | _package_identifier )? _string_literal
dot ::=
'.'
blank_identifier ::=
'_'
import_spec_list ::=
'(' ( import_spec ( ( '\n' | ';' | '\0' ) import_spec )* ( '\n' | ';' | '\0' )? )? ')'
_declaration ::=
const_declaration
| type_declaration
| var_declaration
const_declaration ::=
'const' ( const_spec | '(' ( const_spec ( '\n' | ';' | '\0' ) )* ')' )
const_spec ::=
( identifier ( ',' identifier )* ) ( _type? '=' expression_list )?
var_declaration ::=
'var' ( var_spec | '(' ( var_spec ( '\n' | ';' | '\0' ) )* ')' )
var_spec ::=
( identifier ( ',' identifier )* ) ( _type ( '=' expression_list )? | '=' expression_list )
function_declaration ::=
'func' identifier type_parameter_list? parameter_list ( parameter_list | _simple_type )? block?
method_declaration ::=
'func' parameter_list _field_identifier parameter_list ( parameter_list | _simple_type )? block?
type_parameter_list ::=
'[' parameter_declaration ( ',' parameter_declaration )* ','? ']'
parameter_list ::=
'(' ( ( ( parameter_declaration | variadic_parameter_declaration ) ( ',' ( parameter_declaration | variadic_parameter_declaration ) )* )? ','? )? ')'
parameter_declaration ::=
( identifier ( ',' identifier )* )? _type
variadic_parameter_declaration ::=
identifier? '...' _type
type_alias ::=
_type_identifier '=' _type
type_declaration ::=
'type' ( type_spec | type_alias | '(' ( ( type_spec | type_alias ) ( '\n' | ';' | '\0' ) )* ')' )
type_spec ::=
_type_identifier type_parameter_list? _type
field_name_list ::=
_field_identifier ( ',' _field_identifier )*
expression_list ::=
_expression ( ',' _expression )*
_type ::=
_simple_type
| parenthesized_type
parenthesized_type ::=
'(' _type ')'
_simple_type ::=
_type_identifier
| generic_type
| qualified_type
| pointer_type
| struct_type
| interface_type
| array_type
| slice_type
| map_type
| channel_type
| function_type
| union_type
| negated_type
generic_type ::=
( _type_identifier | qualified_type | union_type | negated_type ) type_arguments
type_arguments ::=
'[' _type ( ',' _type )* ','? ']'
pointer_type ::=
'*' _type
array_type ::=
'[' _expression ']' _type
implicit_length_array_type ::=
'[' '...' ']' _type
slice_type ::=
'[' ']' _type
struct_type ::=
'struct' field_declaration_list
union_type ::=
_type '|' _type
negated_type ::=
'~' _type
field_declaration_list ::=
'{' ( field_declaration ( ( '\n' | ';' | '\0' ) field_declaration )* ( '\n' | ';' | '\0' )? )? '}'
field_declaration ::=
( _field_identifier ( ',' _field_identifier )* _type | '*'? ( _type_identifier | qualified_type | generic_type ) ) _string_literal?
interface_type ::=
'interface' '{' ( _interface_body ( ( '\n' | ';' | '\0' ) _interface_body )* ( '\n' | ';' | '\0' )? )? '}'
_interface_body ::=
method_spec
| struct_elem
| _simple_type
struct_elem ::=
struct_term ( '|' struct_term )*
struct_term ::=
( '~' | '*' )? struct_type
method_spec ::=
_field_identifier parameter_list ( parameter_list | _simple_type )?
map_type ::=
'map' '[' _type ']' _type
channel_type ::=
'chan' _type | 'chan' '<-' _type | '<-' 'chan' _type
function_type ::=
'func' parameter_list ( parameter_list | _simple_type )?
block ::=
'{' _statement_list? '}'
_statement_list ::=
_statement ( ( '\n' | ';' | '\0' ) _statement )* ( ( '\n' | ';' | '\0' ) empty_labeled_statement? )?
| empty_labeled_statement
_statement ::=
_declaration
| _simple_statement
| return_statement
| go_statement
| defer_statement
| if_statement
| for_statement
| expression_switch_statement
| type_switch_statement
| select_statement
| labeled_statement
| fallthrough_statement
| break_statement
| continue_statement
| goto_statement
| block
| empty_statement
empty_statement ::=
';'
_simple_statement ::=
expression_statement
| send_statement
| inc_statement
| dec_statement
| assignment_statement
| short_var_declaration
expression_statement ::=
_expression
send_statement ::=
_expression '<-' _expression
receive_statement ::=
( expression_list ( '=' | ':=' ) )? _expression
inc_statement ::=
_expression '++'
dec_statement ::=
_expression '--'
assignment_statement ::=
expression_list ( '*=' | '/=' | '%=' | '<<=' | '>>=' | '&=' | '&^=' | '+=' | '-=' | '|=' | '^=' | '=' ) expression_list
short_var_declaration ::=
expression_list ':=' expression_list
labeled_statement ::=
identifier ':' _statement
empty_labeled_statement ::=
identifier ':'
fallthrough_statement ::=
'fallthrough'
break_statement ::=
'break' identifier?
continue_statement ::=
'continue' identifier?
goto_statement ::=
'goto' identifier
return_statement ::=
'return' expression_list?
go_statement ::=
'go' _expression
defer_statement ::=
'defer' _expression
if_statement ::=
'if' ( _simple_statement ';' )? _expression block ( 'else' ( block | if_statement ) )?
for_statement ::=
'for' ( _expression | for_clause | range_clause )? block
for_clause ::=
_simple_statement? ';' _expression? ';' _simple_statement?
range_clause ::=
( expression_list ( '=' | ':=' ) )? 'range' _expression
expression_switch_statement ::=
'switch' ( _simple_statement ';' )? _expression? '{' ( expression_case | default_case )* '}'
expression_case ::=
'case' expression_list ':' _statement_list?
default_case ::=
'default' ':' _statement_list?
type_switch_statement ::=
'switch' _type_switch_header '{' ( type_case | default_case )* '}'
_type_switch_header ::=
( _simple_statement ';' )? ( expression_list ':=' )? _expression '.' '(' 'type' ')'
type_case ::=
'case' ( _type ( ',' _type )* ) ':' _statement_list?
select_statement ::=
'select' '{' ( communication_case | default_case )* '}'
communication_case ::=
'case' ( send_statement | receive_statement ) ':' _statement_list?
_expression ::=
unary_expression
| binary_expression
| selector_expression
| index_expression
| slice_expression
| call_expression
| type_assertion_expression
| type_conversion_expression
| identifier
| ( 'new' | 'make' )
| composite_literal
| func_literal
| _string_literal
| int_literal
| float_literal
| imaginary_literal
| rune_literal
| nil
| true
| false
| iota
| parenthesized_expression
parenthesized_expression ::=
'(' _expression ')'
call_expression ::=
( 'new' | 'make' ) special_argument_list | _expression type_arguments? argument_list
variadic_argument ::=
_expression '...'
special_argument_list ::=
'(' _type ( ',' _expression )* ','? ')'
argument_list ::=
'(' ( ( _expression | variadic_argument ) ( ',' ( _expression | variadic_argument ) )* ','? )? ')'
selector_expression ::=
_expression '.' _field_identifier
index_expression ::=
_expression '[' _expression ']'
slice_expression ::=
_expression '[' ( _expression? ':' _expression? | _expression? ':' _expression ':' _expression ) ']'
type_assertion_expression ::=
_expression '.' '(' _type ')'
type_conversion_expression ::=
_type '(' _expression ','? ')'
composite_literal ::=
( map_type | slice_type | array_type | implicit_length_array_type | struct_type | _type_identifier | generic_type | qualified_type ) literal_value
literal_value ::=
'{' ( ( ( literal_element | keyed_element ) ( ',' ( literal_element | keyed_element ) )* )? ','? )? '}'
literal_element ::=
_expression
| literal_value
keyed_element ::=
literal_element ':' literal_element
func_literal ::=
'func' parameter_list ( parameter_list | _simple_type )? block
unary_expression ::=
( '+' | '-' | '!' | '^' | '*' | '&' | '<-' ) _expression
binary_expression ::=
_expression ( '*' | '/' | '%' | '<<' | '>>' | '&' | '&^' ) _expression
| _expression ( '+' | '-' | '|' | '^' ) _expression
| _expression ( '==' | '!=' | '<' | '<=' | '>' | '>=' ) _expression
| _expression '&&' _expression
| _expression '||' _expression
qualified_type ::=
_package_identifier '.' _type_identifier
identifier ::=
[_\p{XID_Start}][_\p{XID_Continue}]*
_type_identifier ::=
identifier
_field_identifier ::=
identifier
_package_identifier ::=
identifier
_string_literal ::=
raw_string_literal
| interpreted_string_literal
raw_string_literal ::=
( '`' [^`]* '`' )
interpreted_string_literal ::=
'"' ( _interpreted_string_literal_basic_content | escape_sequence )* '"'
_interpreted_string_literal_basic_content ::=
[^"#x0A\]+
escape_sequence ::=
( '\\' ( [^xuU] | [0-9]'{2,3}' | 'x'[0-9a-fA-F]'{2,}' | 'u'[0-9a-fA-F]'{4}' | 'U'[0-9a-fA-F]'{8}' ) )
int_literal ::=
( '0' ( 'b' | 'B' ) '_'? [01] ( '_'? [01] )* | '0' | [1-9] ( '_'? [0-9] ( '_'? [0-9] )* )? | '0' ( 'o' | 'O' )? '_'? [0-7] ( '_'? [0-7] )* | '0' ( 'x' | 'X' ) '_'? [0-9a-fA-F] ( '_'? [0-9a-fA-F] )* )
float_literal ::=
( [0-9] ( '_'? [0-9] )* '.' ( [0-9] ( '_'? [0-9] )* )? ( ( 'e' | 'E' ) ( '+' | '-' )? [0-9] ( '_'? [0-9] )* )? | [0-9] ( '_'? [0-9] )* ( 'e' | 'E' ) ( '+' | '-' )? [0-9] ( '_'? [0-9] )* | '.' [0-9] ( '_'? [0-9] )* ( ( 'e' | 'E' ) ( '+' | '-' )? [0-9] ( '_'? [0-9] )* )? | '0' ( 'x' | 'X' ) ( '_'? [0-9a-fA-F] ( '_'? [0-9a-fA-F] )* '.' ( [0-9a-fA-F] ( '_'? [0-9a-fA-F] )* )? | '_'? [0-9a-fA-F] ( '_'? [0-9a-fA-F] )* | '.' [0-9a-fA-F] ( '_'? [0-9a-fA-F] )* ) ( 'p' | 'P' ) ( '+' | '-' )? [0-9] ( '_'? [0-9] )* )
imaginary_literal ::=
( ( [0-9] ( '_'? [0-9] )* | '0' ( 'b' | 'B' ) '_'? [01] ( '_'? [01] )* | '0' | [1-9] ( '_'? [0-9] ( '_'? [0-9] )* )? | '0' ( 'o' | 'O' )? '_'? [0-7] ( '_'? [0-7] )* | '0' ( 'x' | 'X' ) '_'? [0-9a-fA-F] ( '_'? [0-9a-fA-F] )* | [0-9] ( '_'? [0-9] )* '.' ( [0-9] ( '_'? [0-9] )* )? ( ( 'e' | 'E' ) ( '+' | '-' )? [0-9] ( '_'? [0-9] )* )? | [0-9] ( '_'? [0-9] )* ( 'e' | 'E' ) ( '+' | '-' )? [0-9] ( '_'? [0-9] )* | '.' [0-9] ( '_'? [0-9] )* ( ( 'e' | 'E' ) ( '+' | '-' )? [0-9] ( '_'? [0-9] )* )? | '0' ( 'x' | 'X' ) ( '_'? [0-9a-fA-F] ( '_'? [0-9a-fA-F] )* '.' ( [0-9a-fA-F] ( '_'? [0-9a-fA-F] )* )? | '_'? [0-9a-fA-F] ( '_'? [0-9a-fA-F] )* | '.' [0-9a-fA-F] ( '_'? [0-9a-fA-F] )* ) ( 'p' | 'P' ) ( '+' | '-' )? [0-9] ( '_'? [0-9] )* ) 'i' )
rune_literal ::=
( "'" ( [^'\] | '\\' ( 'x' [0-9a-fA-F] [0-9a-fA-F] | [0-7] [0-7] [0-7] | 'u' [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] | 'U' [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] | ( 'a' | 'b' | 'f' | 'n' | 'r' | 't' | 'v' | '\\' | "'" | '"' ) ) ) "'" )
nil ::=
'nil'
true ::=
'true'
false ::=
'false'
iota ::=
'iota'
comment ::=
( '//' '.'* | '/*' [^*]*'\'*+([^/*][^*]*'\'*+)* '/' )