-
Notifications
You must be signed in to change notification settings - Fork 2
/
tree-sitter-swift.ebnf
401 lines (292 loc) · 10.6 KB
/
tree-sitter-swift.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
//
// From tree-sitter-swift/src/grammar.json
//
//
// EBNF to generate railroad diagram at
// (IPV6) https://www.bottlecaps.de/rr/ui
// (IPV4) https://rr.red-dove.com/ui
//
program ::=
_statement*
_statement ::=
( _expression | _declaration | _loop_statement | if_statement | guard_statement | switch_statement | labeled_statement | break_statement | continue_statement | fallthrough_statement | return_statement | throw_statement | defer_statement | do_statement | build_configuration_statement | diagnostic_statement | line_control_statement ) ( ';' | '#x0A' )
_loop_statement ::=
for_statement
| while_statement
| repeat_while_statement
for_statement ::=
'for' 'case'? _pattern _type_annotation? 'in' _expression _code_block
while_statement ::=
'while' _condition_clause _code_block
_condition_clause ::=
_expression ( ',' _condition ( ',' _condition )* )?
| _condition ( ',' _condition )*
| availability_condition ',' _expression
_condition ::=
availability_condition
| case_condition
| optional_binding_condition
_availability_platforms ::=
'iOS'
| 'iOSApplicationExtension'
| 'macOS'
| 'macOSApplicationExtension'
| 'watchOS'
| 'tvOS'
availability_condition ::=
'#available' '(' ( '*' | _availability_platforms ( [0-9]+ ( '.' [0-9]+ ( '.' [0-9]+ )? )? ) ) ( ',' ( '*' | _availability_platforms ( [0-9]+ ( '.' [0-9]+ ( '.' [0-9]+ )? )? ) ) )* ')'
case_condition ::=
'case' _pattern '=' _expression
optional_binding_condition ::=
( 'let' | 'var' ) optional_binding ( ',' optional_binding ( ',' optional_binding )* )?
optional_binding ::=
_pattern '=' _expression
repeat_while_statement ::=
'repeat' _code_block 'while' _expression
if_statement ::=
'if' _condition_clause _code_block ( 'else' ( _code_block | if_statement ) )?
guard_statement ::=
'guard' _condition_clause 'else' _code_block
switch_statement ::=
'switch' _expression '{' case_statement* '}'
case_statement ::=
( 'case' _pattern ( ',' _pattern )* ':' | 'default' ':' ) _statement*
_code_block ::=
'{' _statement* '}'
labeled_statement ::=
identifier ':' ( _loop_statement | if_statement )
break_statement ::=
'break' identifier?
continue_statement ::=
'continue' identifier?
fallthrough_statement ::=
'fallthrough'
return_statement ::=
'return' _expression?
throw_statement ::=
'throw' _expression?
defer_statement ::=
'defer' _code_block
do_statement ::=
'do' _code_block catch_clause*
catch_clause ::=
'catch' _pattern? _code_block
build_configuration_statement ::=
'#if' _build_configuration _statement* ( '#elseif' _build_configuration _statement* )* ( '#else' _statement* )? '#endif'
_build_configuration ::=
'os' '(' ( 'iOS' | 'macOS' | 'watchOS' | 'tvOS' | 'Linux' ) ')'
| 'arch' '(' ( 'i386' | 'x86_64' | 'arm' | 'arm64' ) ')'
| 'canImport' '(' identifier ')'
| 'targetEnvironment' '(' ( 'simulator' | 'macCatalyst' ) ')'
| ( 'compiler' | 'swift' ) '(' ( '>=' | '<' ) semantic_version ')'
| identifier
| boolean_literal
| '(' _build_configuration ')'
| '!' _build_configuration
| _build_configuration '&&' _build_configuration
| _build_configuration '||' _build_configuration
line_control_statement ::=
'#line' ( [0-9]+ static_string_literal )?
diagnostic_statement ::=
( '#error' | '#warning' ) '(' static_string_literal ')'
_declaration ::=
import_declaration
| constant_declaration
| variable_declaration
| typealias_declaration
| function_declaration
| enum_declaration
| struct_declaration
| class_declaration
| protocol_declaration
| initializer_declaration
| deinitializer_declaration
| extension_declaration
| subscript_declaration
| operator_declaration
import_declaration ::=
'import' ( 'typealias' | 'struct' | 'class' | 'enum' | 'protocol' | 'var' | 'func' )? ( identifier | operator ) ( '.' ( identifier | operator ) )*
constant_declaration ::=
modifier* 'let' _pattern_initializer ( ',' _pattern_initializer )*
_pattern_initializer ::=
_pattern _type_annotation? ( '=' _expression )?
variable_declaration ::=
_variable_declaration_head ( _pattern_initializer ( ',' _pattern_initializer )* | _variable_name _type_annotation ( _code_block | _getter_setter_keyword_block ) )
_variable_declaration_head ::=
modifier* 'var'
_variable_name ::=
identifier
_getter_setter_keyword_block ::=
'{' ( _getter_keyword_clause _setter_keyword_clause? | _setter_keyword_clause _getter_keyword_clause ) '}'
_getter_keyword_clause ::=
'get'
_setter_keyword_clause ::=
'set'
typealias_declaration ::=
_typealias_head '=' type
_typealias_head ::=
modifier? 'typealias' identifier
function_declaration ::=
_function_head _function_signature _code_block?
_function_head ::=
modifier* 'func' ( identifier | operator )
_function_signature ::=
generic_clause? parameter_list ( 'throws' | 'rethrows' )? _function_return_statement?
parameter_list ::=
'(' ( _single_parameter ( ',' _single_parameter )* )? ')'
_function_return_statement ::=
'->' ( type ( '!' | '?' )? )
_parameter_clause ::=
'(' ( _single_parameter ( ',' _single_parameter )* ','? )? ')'
_single_parameter ::=
( identifier | '_' )? identifier? _type_annotation ( '!' | '?' )? ( '=' _expression )?
enum_declaration ::=
modifier* 'indirect'? 'enum' identifier '{' ( _declaration | case_declaration )* '}'
case_declaration ::=
'indirect'? 'case' identifier ( tuple_type | '=' ( static_string_literal | boolean_literal ) )?
modifier ::=
_access_control_modifier
| _declaration_modifier
_declaration_modifier ::=
'static'
| 'final'
| 'class'
_access_control_modifier ::=
'public'
| 'private'
| 'fileprivate'
| 'open'
| 'internal'
| 'private(set)'
| 'fileprivate(set)'
| 'internal(set)'
generic_clause ::=
'<' ( _single_generic_parameter ( ',' _single_generic_parameter )* )? '>'
_single_generic_parameter ::=
identifier ( ':' _type_declarator )?
struct_declaration ::=
modifier* 'struct' identifier '{' _declaration* '}'
class_declaration ::=
modifier* 'class' identifier '{' _declaration* '}'
protocol_declaration ::=
modifier* 'protocol' identifier '{' ( protocol_variable_declaration | protocol_method_declaration | protocol_initializer_declaration | protocol_subscript_declaration | protocol_typealias_declaration | associatedtype_declaration )* '}'
protocol_variable_declaration ::=
_variable_declaration_head identifier _type_annotation _getter_setter_keyword_block
protocol_method_declaration ::=
_function_head _function_signature
protocol_initializer_declaration ::=
_initializer_head _parameter_clause ( 'throws' | 'rethrows' )?
protocol_subscript_declaration ::=
_subscript_head _subscript_result _getter_setter_keyword_block
protocol_typealias_declaration ::=
_typealias_head ( '=' type )?
associatedtype_declaration ::=
'associatedtype' identifier _type_annotation?
initializer_declaration ::=
_initializer_head _parameter_clause ( 'throws' | 'rethrows' )? _code_block
_initializer_head ::=
'init' ( '!' | '?' )?
deinitializer_declaration ::=
'deinit' _code_block
extension_declaration ::=
modifier* 'extension' _type_identifier '{' _declaration* '}'
subscript_declaration ::=
_subscript_head _subscript_result ( _code_block | _getter_setter_keyword_block )
_subscript_head ::=
'subscript' _parameter_clause
_subscript_result ::=
'->' type
operator_declaration ::=
'prefix' 'operator' operator '{' '}'
| 'postfix' 'operator' operator '{' '}'
| 'infix' 'operator' operator '{' ( precedence_clause? associativity_clause? | associativity_clause precedence_clause? ) '}'
precedence_clause ::=
'precedence' [0-9]|[1-9][0-9]|'1'[0-9][0-9]|'2'([0-4][0-9]|'5'[0-5])
associativity_clause ::=
'associativity' ( 'left' | 'right' | 'none' )
_pattern ::=
wildcard_pattern
| value_binding_pattern
| enum_case_pattern
| optional_pattern
| is_pattern
| as_pattern
| _expression
wildcard_pattern ::=
'_'
value_binding_pattern ::=
( 'var' | 'let' ) _pattern
enum_case_pattern ::=
_type_identifier? '.' identifier _tuple_declaration?
optional_pattern ::=
_pattern '?'
is_pattern ::=
'is' type
as_pattern ::=
_pattern 'as' type
_expression ::=
identifier
| number
| boolean_literal
| nil
| static_string_literal
| _array_declaration
| _dictionary_declaration
| _tuple_declaration
boolean_literal ::=
'true'
| 'false'
static_string_literal ::=
'"'(('\'([\0tnr'"]|"u\{"[a-fA-F0-9]"{1,8}\}"))|[^"\\u000a\u000d])*"""
number ::=
( ( '0x' | '0X' ) [0-9a-fA-F]('_'?[0-9a-fA-F])* | [-\+]? ( '0' | '0'? [1-9] ( '_'? [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' | '0'? [1-9] ( '_'? [0-9]('_'?[0-9])* )? ) ( 'e' | 'E' ) [-\+]? [0-9]('_'?[0-9])* | [-\+]? [0-9]('_'?[0-9])* | ( '0b' | '0B' ) [0-1]('_'?[0-1])* | ( '0o' | '0O' ) [0-7]('_'?[0-7])* | ( ( '0x' | '0X' ) [0-9a-fA-F]('_'?[0-9a-fA-F])* | ( '0b' | '0B' ) [0-1]('_'?[0-1])* | ( '0o' | '0O' ) [0-7]('_'?[0-7])* | [0-9]('_'?[0-9])* ) 'n' )
nil ::=
'nil'
type ::=
tuple_type
| standard_type
| _type_identifier
_type_declarator ::=
standard_type
| tuple_type
| array_type
| dictionary_type
| identifier
standard_type ::=
( 'Bool' | 'Character' | 'StaticString' | 'String' | 'Void' | 'Double' | 'Float' | 'Int' | 'UInt' | 'UInt8' | 'UInt16' | 'UInt32' | 'UInt64' | 'Int8' | 'Int16' | 'Int32' | 'Int64' | 'Float32' | 'Float64' | 'Float80' )
tuple_type ::=
'(' ( ( _variable_name _type_annotation | _type_declarator ) ( ',' ( _variable_name _type_annotation | _type_declarator ) )* )? ')'
_tuple_declaration ::=
'(' ( ( identifier ':' )? _expression ( ',' ( identifier ':' )? _expression )* )? ')'
array_type ::=
_array_type_shorthand
| _array_type_full
_array_type_full ::=
'Array' '<' _type_declarator '>'
_array_type_shorthand ::=
'[' _type_declarator ']'
_array_declaration ::=
'[' ( _expression? ( ',' _expression? )* )? ']'
dictionary_type ::=
_dictionary_type_shorthand
| _dictionary_type_full
_dictionary_type_full ::=
'Dictionary' '<' _type_declarator ',' _type_declarator '>'
_dictionary_type_shorthand ::=
'[' _type_declarator ':' _type_declarator ']'
_dictionary_declaration ::=
'[' ( _expression? ':' _expression? ( ',' _expression? ':' _expression? )* )? ']'
_type_annotation ::=
':' ( type | array_type | dictionary_type )
_type_identifier ::=
_type_name ( '.' _type_identifier )?
_type_name ::=
identifier
identifier ::=
( [A-Za-z_] ( [A-Za-z_] | [0-9] )*? | '`' [A-Za-z_] ( [A-Za-z_] | [0-9] )*? '`' )
operator ::=
( '/' | '=' | '-' | '+' | '!' | '*' | '%' | '<' | '>' | '&' | '|' | '^' | '~' | '?' )+
semantic_version ::=
([0-9]+'.')?([0-9]+'.')?('\'*|[0-9]+)
comment ::=
( '\/{2,3}'[^/]'.'* | '\/\'*'{1,}'[^*]*'\'*+([^/*][^*]*'\'*+)*'\/' )