-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathAttributeNodes.py
463 lines (440 loc) · 20.5 KB
/
AttributeNodes.py
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
from .Child import Child
from .Node import Node # noqa: I201
ATTRIBUTE_NODES = [
# token-list -> token? token-list?
Node('TokenList', kind='SyntaxCollection',
element='Token'),
# token-list -> token token-list?
Node('NonEmptyTokenList', kind='SyntaxCollection',
element='Token', omit_when_empty=True),
Node('CustomAttribute', kind='Syntax',
description='''
A custom `@` attribute.
''',
children=[
Child('AtSignToken', kind='AtSignToken',
description='The `@` sign.'),
Child('AttributeName', kind='Type', classification='Attribute',
description='The name of the attribute.'),
Child('LeftParen', kind='LeftParenToken',
is_optional=True),
Child('ArgumentList', kind='TupleExprElementList',
collection_element_name='Argument', is_optional=True),
Child('RightParen', kind='RightParenToken',
is_optional=True),
]),
# attribute -> '@' identifier '('?
# ( identifier
# | string-literal
# | integer-literal
# | availability-spec-list
# | specialize-attr-spec-list
# | implements-attr-arguments
# | named-attribute-string-argument
# | back-deploy-attr-spec-list
# )? ')'?
Node('Attribute', kind='Syntax',
description='''
An `@` attribute.
''',
children=[
Child('AtSignToken', kind='AtSignToken',
description='The `@` sign.'),
Child('AttributeName', kind='Token', classification='Attribute',
description='The name of the attribute.'),
Child('LeftParen', kind='LeftParenToken', is_optional=True,
description='''
If the attribute takes arguments, the opening parenthesis.
'''),
Child('Argument', kind='Syntax', is_optional=True,
node_choices=[
Child('Identifier', kind='IdentifierToken'),
Child('String', kind='StringLiteralToken'),
Child('Integer', kind='IntegerLiteralToken'),
Child('Availability', kind='AvailabilitySpecList'),
Child('SpecializeArguments',
kind='SpecializeAttributeSpecList'),
Child('ObjCName', kind='ObjCSelector'),
Child('ImplementsArguments',
kind='ImplementsAttributeArguments'),
Child('DifferentiableArguments',
kind='DifferentiableAttributeArguments'),
Child('DerivativeRegistrationArguments',
kind='DerivativeRegistrationAttributeArguments'),
Child('NamedAttributeString',
kind='NamedAttributeStringArgument'),
Child('BackDeployArguments',
kind='BackDeployAttributeSpecList'),
# TokenList for custom effects which are parsed by
# `FunctionEffects.parse()` in swift.
Child('TokenList', kind='TokenList',
collection_element_name='Token'),
], description='''
The arguments of the attribute. In case the attribute
takes multiple arguments, they are gather in the
appropriate takes first.
'''),
Child('RightParen', kind='RightParenToken', is_optional=True,
description='''
If the attribute takes arguments, the closing parenthesis.
'''),
# TokenList to gather remaining tokens of invalid attributes
# FIXME: Remove this recovery option entirely
Child('TokenList', kind='TokenList',
collection_element_name='Token', is_optional=True),
]),
# attribute-list -> attribute attribute-list?
Node('AttributeList', kind='SyntaxCollection',
omit_when_empty=True,
element='Syntax', element_name='Attribute',
element_choices=[
'Attribute',
'CustomAttribute',
]),
# The argument of '@_specialize(...)'
# specialize-attr-spec-list -> labeled-specialize-entry
# specialize-spec-attr-list?
# | generic-where-clause
# specialize-spec-attr-list?
Node('SpecializeAttributeSpecList', kind='SyntaxCollection',
description='''
A collection of arguments for the `@_specialize` attribute
''',
element='Syntax', element_name='SpecializeAttribute',
element_choices=[
'LabeledSpecializeEntry',
'AvailabilityEntry',
'TargetFunctionEntry',
'GenericWhereClause',
]),
Node('AvailabilityEntry', kind='Syntax',
description='''
The availability argument for the _specialize attribute
''',
children=[
Child('Label', kind='IdentifierToken',
description='The label of the argument'),
Child('Colon', kind='ColonToken',
description='The colon separating the label and the value'),
Child('AvailabilityList', kind='AvailabilitySpecList',
collection_element_name='Availability'),
Child('Semicolon', kind='SemicolonToken'),
]),
# Representation of e.g. 'exported: true,'
# labeled-specialize-entry -> identifier ':' token ','?
Node('LabeledSpecializeEntry', kind='Syntax',
description='''
A labeled argument for the `@_specialize` attribute like
`exported: true`
''',
traits=['WithTrailingComma'],
children=[
Child('Label', kind='IdentifierToken',
description='The label of the argument'),
Child('Colon', kind='ColonToken',
description='The colon separating the label and the value'),
Child('Value', kind='Token',
description='The value for this argument'),
Child('TrailingComma', kind='CommaToken',
is_optional=True, description='''
A trailing comma if this argument is followed by another one
'''),
]),
# Representation of e.g. 'exported: true,'
# labeled-specialize-entry -> identifier ':' token ','?
Node('TargetFunctionEntry', kind='Syntax',
description='''
A labeled argument for the `@_specialize` attribute with a function
decl value like
`target: myFunc(_:)`
''',
traits=['WithTrailingComma'],
children=[
Child('Label', kind='IdentifierToken',
description='The label of the argument'),
Child('Colon', kind='ColonToken',
description='The colon separating the label and the value'),
Child('Delcname', kind='DeclName',
description='The value for this argument'),
Child('TrailingComma', kind='CommaToken',
is_optional=True, description='''
A trailing comma if this argument is followed by another one
'''),
]),
# The argument of '@_dynamic_replacement(for:)' or '@_private(sourceFile:)'
# named-attribute-string-arg -> 'name': string-literal
Node('NamedAttributeStringArgument', kind='Syntax',
description='''
The argument for the `@_dynamic_replacement` or `@_private`
attribute of the form `for: "function()"` or `sourceFile:
"Src.swift"`
''',
children=[
Child('NameTok', kind='Token',
description='The label of the argument'),
Child('Colon', kind='ColonToken',
description='The colon separating the label and the value'),
Child('StringOrDeclname', kind='Syntax', node_choices=[
Child('String', kind='StringLiteralToken'),
Child('Declname', kind='DeclName'),
]),
]),
Node('DeclName', kind='Syntax', children=[
Child('DeclBaseName', kind='Syntax', description='''
The base name of the protocol\'s requirement.
''',
node_choices=[
Child('Identifier', kind='IdentifierToken'),
Child('Operator', kind='PrefixOperatorToken'),
]),
Child('DeclNameArguments', kind='DeclNameArguments',
is_optional=True, description='''
The argument labels of the protocol\'s requirement if it
is a function requirement.
'''),
]),
# The argument of '@_implements(...)'
# implements-attr-arguments -> simple-type-identifier ','
# (identifier | operator) decl-name-arguments
Node('ImplementsAttributeArguments', kind='Syntax',
description='''
The arguments for the `@_implements` attribute of the form
`Type, methodName(arg1Label:arg2Label:)`
''',
children=[
Child('Type', kind='SimpleTypeIdentifier', description='''
The type for which the method with this attribute
implements a requirement.
'''),
Child('Comma', kind='CommaToken',
description='''
The comma separating the type and method name
'''),
Child('DeclBaseName', kind='Syntax', description='''
The base name of the protocol\'s requirement.
''',
node_choices=[
Child('Identifier', kind='IdentifierToken'),
Child('Operator', kind='PrefixOperatorToken'),
]),
Child('DeclNameArguments', kind='DeclNameArguments',
is_optional=True, description='''
The argument labels of the protocol\'s requirement if it
is a function requirement.
'''),
]),
# objc-selector-piece -> identifier? ':'?
Node('ObjCSelectorPiece', kind='Syntax',
description='''
A piece of an Objective-C selector. Either consisiting of just an
identifier for a nullary selector, an identifier and a colon for a
labeled argument or just a colon for an unlabeled argument
''',
children=[
Child('Name', kind='IdentifierToken', is_optional=True),
Child('Colon', kind='ColonToken', is_optional=True),
]),
# objc-selector -> objc-selector-piece objc-selector?
Node('ObjCSelector', kind='SyntaxCollection', element='ObjCSelectorPiece'),
# The argument of '@differentiable(...)'.
# differentiable-attr-arguments ->
# differentiability-kind? '.'? differentiability-params-clause? ','?
# generic-where-clause?
Node('DifferentiableAttributeArguments', kind='Syntax',
description='''
The arguments for the `@differentiable` attribute: an optional
differentiability kind, an optional differentiability parameter clause,
and an optional 'where' clause.
''',
children=[
Child('DiffKind', kind='IdentifierToken',
text_choices=['forward', 'reverse', 'linear'],
is_optional=True),
Child('DiffKindComma', kind='CommaToken', description='''
The comma following the differentiability kind, if it exists.
''', is_optional=True),
Child('DiffParams', kind='DifferentiabilityParamsClause',
is_optional=True),
Child('DiffParamsComma', kind='CommaToken', description='''
The comma following the differentiability parameters clause,
if it exists.
''', is_optional=True),
Child('WhereClause', kind='GenericWhereClause', is_optional=True),
]),
# differentiability-params-clause ->
# 'wrt' ':' (differentiability-param | differentiability-params)
Node('DifferentiabilityParamsClause', kind='Syntax',
description='A clause containing differentiability parameters.',
children=[
Child('WrtLabel', kind='IdentifierToken',
text_choices=['wrt'], description='The "wrt" label.'),
Child('Colon', kind='ColonToken', description='''
The colon separating "wrt" and the parameter list.
'''),
Child('Parameters', kind='Syntax',
node_choices=[
Child('Parameter', kind='DifferentiabilityParam'),
Child('ParameterList', kind='DifferentiabilityParams'),
]),
]),
# differentiability-params -> '(' differentiability-param-list ')'
Node('DifferentiabilityParams', kind='Syntax',
description='The differentiability parameters.',
children=[
Child('LeftParen', kind='LeftParenToken'),
Child('DiffParams', kind='DifferentiabilityParamList',
collection_element_name='DifferentiabilityParam',
description='The parameters for differentiation.'),
Child('RightParen', kind='RightParenToken'),
]),
# differentiability-param-list ->
# differentiability-param differentiability-param-list?
Node('DifferentiabilityParamList', kind='SyntaxCollection',
element='DifferentiabilityParam'),
# differentiability-param -> ('self' | identifier | integer-literal) ','?
Node('DifferentiabilityParam', kind='Syntax',
description='''
A differentiability parameter: either the "self" identifier, a function
parameter name, or a function parameter index.
''',
traits=['WithTrailingComma'],
children=[
Child('Parameter', kind='Syntax',
node_choices=[
Child('Self', kind='SelfToken'),
Child('Name', kind='IdentifierToken'),
Child('Index', kind='IntegerLiteralToken'),
]),
Child('TrailingComma', kind='CommaToken', is_optional=True),
]),
# The argument of the derivative registration attribute
# '@derivative(of: ...)' and the transpose registration attribute
# '@transpose(of: ...)'.
#
# derivative-registration-attr-arguments ->
# 'of' ':' func-decl-name ','? differentiability-params-clause?
Node('DerivativeRegistrationAttributeArguments', kind='Syntax',
description='''
The arguments for the '@derivative(of:)' and '@transpose(of:)'
attributes: the 'of:' label, the original declaration name, and an
optional differentiability parameter list.
''',
children=[
Child('OfLabel', kind='IdentifierToken', text_choices=['of'],
description='The "of" label.'),
Child('Colon', kind='ColonToken', description='''
The colon separating the "of" label and the original
declaration name.
'''),
Child('OriginalDeclName', kind='QualifiedDeclName',
description='The referenced original declaration name.'),
Child('Period', kind='PeriodToken',
description='''
The period separating the original declaration name and the
accessor name.
''', is_optional=True),
Child('AccessorKind', kind='IdentifierToken',
description='The accessor name.',
text_choices=['get', 'set'],
is_optional=True),
Child('Comma', kind='CommaToken', is_optional=True),
Child('DiffParams', kind='DifferentiabilityParamsClause',
is_optional=True),
]),
# An optionally qualified declaration name.
# Currently used only for `@derivative` and `@transpose` attribute.
# TODO(TF-1066): Use module qualified name syntax/parsing instead of custom
# qualified name syntax/parsing.
#
# qualified-decl-name ->
# base-type? '.'? (identifier | operator) decl-name-arguments?
# base-type ->
# member-type-identifier | base-type-identifier
Node('QualifiedDeclName', kind='Syntax',
description='''
An optionally qualified function declaration name (e.g. `+(_:_:)`,
`A.B.C.foo(_:_:)`).
''',
children=[
Child('BaseType', kind='Type', description='''
The base type of the qualified name, optionally specified.
''', is_optional=True),
Child('Dot', kind='Token',
token_choices=[
'PeriodToken', 'PrefixPeriodToken'
], is_optional=True),
Child('Name', kind='Token', description='''
The base name of the referenced function.
''',
token_choices=[
'IdentifierToken',
'UnspacedBinaryOperatorToken',
'SpacedBinaryOperatorToken',
'PrefixOperatorToken',
'PostfixOperatorToken',
]),
Child('Arguments', kind='DeclNameArguments',
is_optional=True, description='''
The argument labels of the referenced function, optionally
specified.
'''),
]),
# func-decl-name -> (identifier | operator) decl-name-arguments?
# NOTE: This is duplicated with `DeclName` above. Change `DeclName`
# description and use it if possible.
Node('FunctionDeclName', kind='Syntax',
description='A function declaration name (e.g. `foo(_:_:)`).',
children=[
Child('Name', kind='Syntax', description='''
The base name of the referenced function.
''',
node_choices=[
Child('Identifier', kind='IdentifierToken'),
Child('PrefixOperator', kind='PrefixOperatorToken'),
Child('SpacedBinaryOperator',
kind='SpacedBinaryOperatorToken'),
]),
Child('Arguments', kind='DeclNameArguments',
is_optional=True, description='''
The argument labels of the referenced function, optionally
specified.
'''),
]),
# The arguments of '@_backDeploy(...)'
# back-deploy-attr-spec-list -> 'before' ':' back-deploy-version-list
Node('BackDeployAttributeSpecList', kind='Syntax',
description='''
A collection of arguments for the `@_backDeploy` attribute
''',
children=[
Child('BeforeLabel', kind='IdentifierToken',
text_choices=['before'], description='The "before" label.'),
Child('Colon', kind='ColonToken', description='''
The colon separating "before" and the parameter list.
'''),
Child('VersionList', kind='BackDeployVersionList',
collection_element_name='Availability', description='''
The list of OS versions in which the declaration became ABI
stable.
'''),
]),
# back-deploy-version-list ->
# back-deploy-version-entry back-deploy-version-list?
Node('BackDeployVersionList', kind='SyntaxCollection',
element='BackDeployVersionArgument'),
# back-deploy-version-entry -> availability-version-restriction ','?
Node('BackDeployVersionArgument', kind='Syntax',
description='''
A single platform/version pair in a `@_backDeploy` attribute,
e.g. `iOS 10.1`.
''',
children=[
Child('AvailabilityVersionRestriction',
kind='AvailabilityVersionRestriction'),
Child('TrailingComma', kind='CommaToken', is_optional=True,
description='''
A trailing comma if the argument is followed by another
argument
'''),
]),
]