Skip to content

Commit 91a46f0

Browse files
committed
unified: stop "!" bleeding through
You know the drill. We just make an anonymous node named instead. In this case, however, we have to be a bit more clever about how to rewrite it. We turn the sequence of a type followed by an optional ! into a _choice_ between mere type or type followed by bang (the latter being our new named node).
1 parent 37e1e3c commit 91a46f0

2 files changed

Lines changed: 16 additions & 11 deletions

File tree

unified/extractor/tree-sitter-swift/grammar.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,9 @@ module.exports = grammar({
401401
type_annotation: ($) =>
402402
seq(":", field("type", $._possibly_implicitly_unwrapped_type)),
403403
_possibly_implicitly_unwrapped_type: ($) =>
404-
seq($._type, optional(token.immediate("!"))),
404+
choice($._type, $.implicitly_unwrapped_type),
405+
implicitly_unwrapped_type: ($) =>
406+
seq($._type, token.immediate("!")),
405407
_type: ($) =>
406408
prec.right(
407409
PRECS.ty,

unified/extractor/tree-sitter-swift/node-types.yml

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ named:
226226
body: function_body
227227
default_value*: expression
228228
name+: [referenceable_operator, simple_identifier, unannotated_type]
229-
return_type*: ["!", type_modifiers, unannotated_type]
229+
return_type*: [implicitly_unwrapped_type, type_modifiers, unannotated_type]
230230
function_modifier:
231231
function_type:
232232
$children?: [throws, throws_clause]
@@ -248,6 +248,9 @@ named:
248248
if_statement:
249249
$children*: [else, if_statement, statements]
250250
condition+: if_condition
251+
implicitly_unwrapped_type:
252+
$children?: type_modifiers
253+
name: unannotated_type
251254
import_declaration:
252255
$children+: [identifier, modifiers]
253256
infix_expression:
@@ -257,8 +260,8 @@ named:
257260
inheritance_constraint:
258261
$children*: attribute
259262
constrained_type+: [".", identifier, simple_identifier, unannotated_type]
260-
inherits_from+: ["!", type_modifiers, unannotated_type]
261-
name: unannotated_type
263+
inherits_from+: [implicitly_unwrapped_type, type_modifiers, unannotated_type]
264+
name?: unannotated_type
262265
inheritance_modifier:
263266
inheritance_specifier:
264267
inherits_from: [function_type, suppressed_constraint, user_type]
@@ -280,7 +283,7 @@ named:
280283
lambda_function_type:
281284
$children*: [lambda_function_type_parameters, throws, throws_clause]
282285
name?: unannotated_type
283-
return_type*: ["!", type_modifiers, unannotated_type]
286+
return_type*: [implicitly_unwrapped_type, type_modifiers, unannotated_type]
284287
lambda_function_type_parameters:
285288
$children+: lambda_parameter
286289
lambda_literal:
@@ -291,7 +294,7 @@ named:
291294
$children?: [parameter_modifiers, self_expression]
292295
external_name?: simple_identifier
293296
name*: [simple_identifier, unannotated_type]
294-
type*: ["!", type_modifiers, unannotated_type]
297+
type*: [implicitly_unwrapped_type, type_modifiers, unannotated_type]
295298
line_str_text:
296299
line_string_literal:
297300
interpolation*: interpolated_expression
@@ -348,7 +351,7 @@ named:
348351
$children?: parameter_modifiers
349352
external_name?: simple_identifier
350353
name+: [simple_identifier, unannotated_type]
351-
type+: ["!", type_modifiers, unannotated_type]
354+
type+: [implicitly_unwrapped_type, type_modifiers, unannotated_type]
352355
parameter_modifier:
353356
parameter_modifiers:
354357
$children+: parameter_modifier
@@ -391,7 +394,7 @@ named:
391394
$children*: [attribute, modifiers, parameter, statements, throws, throws_clause, type_constraints, type_parameters]
392395
default_value*: expression
393396
name*: [referenceable_operator, simple_identifier, unannotated_type]
394-
return_type*: ["!", type_modifiers, unannotated_type]
397+
return_type*: [implicitly_unwrapped_type, type_modifiers, unannotated_type]
395398
protocol_property_declaration:
396399
$children+: [modifiers, protocol_property_requirements, type_annotation, type_constraints]
397400
name: pattern
@@ -437,7 +440,7 @@ named:
437440
$children+: [attribute, computed_property, modifiers, parameter, type_constraints, type_parameters]
438441
default_value*: expression
439442
name?: unannotated_type
440-
return_type*: ["!", type_modifiers, unannotated_type]
443+
return_type*: [implicitly_unwrapped_type, type_modifiers, unannotated_type]
441444
super_expression:
442445
suppressed_constraint:
443446
suppressed: type_identifier
@@ -472,8 +475,8 @@ named:
472475
name*: [simple_identifier, unannotated_type]
473476
type*: [type_modifiers, unannotated_type]
474477
type_annotation:
475-
name: unannotated_type
476-
type+: ["!", type_modifiers, unannotated_type]
478+
name?: unannotated_type
479+
type+: [implicitly_unwrapped_type, type_modifiers, unannotated_type]
477480
type_arguments:
478481
$children*: type_modifiers
479482
name+: unannotated_type

0 commit comments

Comments
 (0)