Skip to content

Commit db4c809

Browse files
committed
[NFC] Add module selector syntax nodes/children
Changes the syntax tree to represent module selectors: • A `ModuleSelectorSyntax` node represents a module selector abstractly. • The following nodes now have an optional `moduleSelector` child: • `DeclReferenceExprSyntax` • `IdentifierTypeSyntax` • `MacroExpansionExprSyntax` • `MemberTypeSyntax` • BasicFormat knows the preferred format for module selectors. Other components, particularly the parser, were also updated to continue building, though without any changes in behavior. Parser implementation will come in a future commit.
1 parent 03dddfa commit db4c809

36 files changed

+1370
-326
lines changed

CodeGeneration/Sources/SyntaxSupport/CommonNodes.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,25 @@ public let COMMON_NODES: [Node] = [
330330
]
331331
),
332332

333+
Node(
334+
kind: .moduleSelector,
335+
base: .syntax,
336+
experimentalFeature: .moduleSelector,
337+
nameForDiagnostics: "module selector",
338+
children: [
339+
Child(
340+
name: "moduleName",
341+
kind: .token(choices: [.token(.identifier)]),
342+
nameForDiagnostics: "module name"
343+
),
344+
Child(
345+
name: "colonColon",
346+
kind: .token(choices: [.token(.colonColon)]),
347+
nameForDiagnostics: "'::' operator"
348+
),
349+
]
350+
),
351+
333352
Node(
334353
kind: .pattern,
335354
base: .syntax,

CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,6 +1554,12 @@ public let DECL_NODES: [Node] = [
15541554
kind: .token(choices: [.token(.pound)]),
15551555
documentation: "The `#` sign."
15561556
),
1557+
Child(
1558+
name: "moduleSelector",
1559+
kind: .node(kind: .moduleSelector),
1560+
experimentalFeature: .moduleSelector,
1561+
isOptional: true
1562+
),
15571563
Child(
15581564
name: "macroName",
15591565
kind: .token(choices: [.token(.identifier)])

CodeGeneration/Sources/SyntaxSupport/ExprNodes.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,12 @@ public let EXPR_NODES: [Node] = [
10021002
base: .expr,
10031003
nameForDiagnostics: nil,
10041004
children: [
1005+
Child(
1006+
name: "moduleSelector",
1007+
kind: .node(kind: .moduleSelector),
1008+
experimentalFeature: .moduleSelector,
1009+
isOptional: true
1010+
),
10051011
Child(
10061012
name: "baseName",
10071013
kind: .token(choices: [
@@ -1358,6 +1364,12 @@ public let EXPR_NODES: [Node] = [
13581364
kind: .token(choices: [.token(.pound)]),
13591365
documentation: "The `#` sign."
13601366
),
1367+
Child(
1368+
name: "moduleSelector",
1369+
kind: .node(kind: .moduleSelector),
1370+
experimentalFeature: .moduleSelector,
1371+
isOptional: true
1372+
),
13611373
Child(
13621374
name: "macroName",
13631375
kind: .token(choices: [.token(.identifier)])

CodeGeneration/Sources/SyntaxSupport/SyntaxNodeKind.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ public enum SyntaxNodeKind: String, CaseIterable, IdentifierConvertible, TypeCon
206206
case missingPattern
207207
case missingStmt
208208
case missingType
209+
case moduleSelector
209210
case multipleTrailingClosureElement
210211
case multipleTrailingClosureElementList
211212
case namedOpaqueReturnType

CodeGeneration/Sources/SyntaxSupport/Traits.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ public let TRAITS: [Trait] = [
9292
traitName: "FreestandingMacroExpansion",
9393
children: [
9494
Child(name: "pound", kind: .token(choices: [.token(.pound)])),
95+
Child(
96+
name: "moduleSelector",
97+
kind: .node(kind: .moduleSelector),
98+
experimentalFeature: .moduleSelector,
99+
isOptional: true
100+
),
95101
Child(name: "macroName", kind: .token(choices: [.token(.identifier)])),
96102
Child(name: "genericArgumentClause", kind: .node(kind: .genericArgumentClause), isOptional: true),
97103
Child(name: "leftParen", kind: .token(choices: [.token(.leftParen)]), isOptional: true),

CodeGeneration/Sources/SyntaxSupport/TypeNodes.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,13 @@ public let TYPE_NODES: [Node] = [
365365
name: "period",
366366
kind: .token(choices: [.token(.period)])
367367
),
368+
Child(
369+
name: "moduleSelector",
370+
kind: .node(kind: .moduleSelector),
371+
experimentalFeature: .moduleSelector,
372+
nameForDiagnostics: "module selector",
373+
isOptional: true
374+
),
368375
Child(
369376
name: "name",
370377
kind: .token(choices: [.token(.identifier), .keyword(.self)]),
@@ -511,6 +518,13 @@ public let TYPE_NODES: [Node] = [
511518
base: .type,
512519
nameForDiagnostics: "type",
513520
children: [
521+
Child(
522+
name: "moduleSelector",
523+
kind: .node(kind: .moduleSelector),
524+
experimentalFeature: .moduleSelector,
525+
nameForDiagnostics: "module selector",
526+
isOptional: true
527+
),
514528
Child(
515529
name: "name",
516530
kind: .token(choices: [

Sources/SwiftBasicFormat/BasicFormat.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,12 @@ open class BasicFormat: SyntaxRewriter {
308308
case (.atSign, _),
309309
(.backslash, _),
310310
(.backtick, _),
311+
(.colonColon, .identifier),
311312
(.dollarIdentifier, .period), // a.b
312313
(.endOfFile, _),
313314
(.exclamationMark, .period), // myOptionalBar!.foo()
314315
(.regexPoundDelimiter, .regexSlash), // opening extended regex delimiter should never be separate by a space
316+
(.identifier, .colonColon),
315317
(.identifier, .leftAngle), // MyType<Int>
316318
(.identifier, .leftSquare), // myArray[1]
317319
(.identifier, .period), // a.b

Sources/SwiftParser/Attributes.swift

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,12 @@ extension Parser {
304304
unexpectedBeforeAtSign,
305305
atSign: atSign,
306306
unexpectedBeforeAttributeName,
307-
attributeName: RawIdentifierTypeSyntax(name: attributeName, genericArgumentClause: nil, arena: self.arena),
307+
attributeName: RawIdentifierTypeSyntax(
308+
moduleSelector: nil,
309+
name: attributeName,
310+
genericArgumentClause: nil,
311+
arena: self.arena
312+
),
308313
leftParen: nil,
309314
arguments: nil,
310315
rightParen: nil,
@@ -329,7 +334,9 @@ extension Parser {
329334

330335
extension RawLabeledExprSyntax {
331336
fileprivate init(
332-
_ unexpectedBeforeIdentifier: RawUnexpectedNodesSyntax? = nil,
337+
_ unexpectedBeforeModuleSelector: RawUnexpectedNodesSyntax? = nil,
338+
moduleSelector: RawModuleSelectorSyntax?,
339+
_ unexpectedBetweenModuleSelectorAndIdentifier: RawUnexpectedNodesSyntax? = nil,
333340
identifier: RawTokenSyntax,
334341
_ unexpectedBetweenIdentifierAndTrailingComma: RawUnexpectedNodesSyntax? = nil,
335342
trailingComma: RawTokenSyntax? = nil,
@@ -339,7 +346,9 @@ extension RawLabeledExprSyntax {
339346
label: nil,
340347
colon: nil,
341348
expression: RawDeclReferenceExprSyntax(
342-
unexpectedBeforeIdentifier,
349+
unexpectedBeforeModuleSelector,
350+
moduleSelector: moduleSelector,
351+
unexpectedBetweenModuleSelectorAndIdentifier,
343352
baseName: identifier,
344353
argumentNames: nil,
345354
arena: arena
@@ -361,6 +370,7 @@ extension Parser {
361370
let roleTrailingComma = self.consume(if: .comma)
362371

363372
let roleElement = RawLabeledExprSyntax(
373+
moduleSelector: nil,
364374
unexpectedBeforeRole,
365375
identifier: role,
366376
trailingComma: roleTrailingComma,
@@ -390,7 +400,12 @@ extension Parser {
390400
unexpectedBeforeAtSign,
391401
atSign: atSign,
392402
unexpectedBeforeDifferentiable,
393-
attributeName: RawIdentifierTypeSyntax(name: differentiable, genericArgumentClause: nil, arena: self.arena),
403+
attributeName: RawIdentifierTypeSyntax(
404+
moduleSelector: nil,
405+
name: differentiable,
406+
genericArgumentClause: nil,
407+
arena: self.arena
408+
),
394409
unexpectedBeforeLeftParen,
395410
leftParen: leftParen,
396411
arguments: .differentiableArguments(argument),
@@ -531,8 +546,13 @@ extension Parser {
531546
return RawAttributeSyntax(
532547
unexpectedBeforeAtSign,
533548
atSign: atSign,
534-
unexpectedBeforeDerivative,
535-
attributeName: RawIdentifierTypeSyntax(name: derivative, genericArgumentClause: nil, arena: self.arena),
549+
attributeName: RawIdentifierTypeSyntax(
550+
moduleSelector: nil,
551+
unexpectedBeforeDerivative,
552+
name: derivative,
553+
genericArgumentClause: nil,
554+
arena: self.arena
555+
),
536556
unexpectedBeforeLeftParen,
537557
leftParen: leftParen,
538558
arguments: .derivativeRegistrationArguments(argument),
@@ -554,7 +574,12 @@ extension Parser {
554574
unexpectedBeforeAtSign,
555575
atSign: atSign,
556576
unexpectedBeforeTranspose,
557-
attributeName: RawIdentifierTypeSyntax(name: transpose, genericArgumentClause: nil, arena: self.arena),
577+
attributeName: RawIdentifierTypeSyntax(
578+
moduleSelector: nil,
579+
name: transpose,
580+
genericArgumentClause: nil,
581+
arena: self.arena
582+
),
558583
unexpectedBeforeLeftParen,
559584
leftParen: leftParen,
560585
arguments: .derivativeRegistrationArguments(argument),
@@ -789,6 +814,7 @@ extension Parser {
789814
let (unexpectedBeforeIsolationKind, isolationKind) =
790815
self.expectIdentifier(allowKeywordsAsIdentifier: true)
791816
let isolationKindElement = RawLabeledExprSyntax(
817+
moduleSelector: nil,
792818
unexpectedBeforeIsolationKind,
793819
identifier: isolationKind,
794820
arena: self.arena
@@ -919,6 +945,7 @@ extension Parser {
919945
let declName: RawDeclReferenceExprSyntax
920946
if label.isMissing && colon.isMissing && self.atStartOfLine {
921947
declName = RawDeclReferenceExprSyntax(
948+
moduleSelector: nil,
922949
baseName: RawTokenSyntax(missing: .identifier, arena: self.arena),
923950
argumentNames: nil,
924951
arena: self.arena

Sources/SwiftParser/Declarations.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ extension Parser {
604604
unexpectedBeforeInherited = RawUnexpectedNodesSyntax([classKeyword], arena: self.arena)
605605
inherited = RawTypeSyntax(
606606
RawIdentifierTypeSyntax(
607+
moduleSelector: nil,
607608
name: missingToken(.identifier, text: "AnyObject"),
608609
genericArgumentClause: nil,
609610
arena: self.arena
@@ -2240,6 +2241,7 @@ extension Parser {
22402241
modifiers: attrs.modifiers,
22412242
unexpectedBeforePound,
22422243
pound: pound,
2244+
moduleSelector: nil,
22432245
unexpectedBeforeMacro,
22442246
macroName: macro,
22452247
genericArgumentClause: generics,

Sources/SwiftParser/Expressions.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,7 @@ extension Parser {
652652
if skipMemberName {
653653
let missingIdentifier = missingToken(.identifier)
654654
let declName = RawDeclReferenceExprSyntax(
655+
moduleSelector: nil,
655656
baseName: missingIdentifier,
656657
argumentNames: nil,
657658
arena: self.arena
@@ -664,6 +665,7 @@ extension Parser {
664665
if let indexOrSelf = self.consume(if: .integerLiteral, .keyword(.self)) {
665666
// Handle "x.42" - a tuple index.
666667
declName = RawDeclReferenceExprSyntax(
668+
moduleSelector: nil,
667669
baseName: indexOrSelf,
668670
argumentNames: nil,
669671
arena: self.arena
@@ -1276,6 +1278,7 @@ extension Parser {
12761278
let poundAvailable = self.parsePoundAvailableConditionElement()
12771279
return RawExprSyntax(
12781280
RawDeclReferenceExprSyntax(
1281+
moduleSelector: nil,
12791282
RawUnexpectedNodesSyntax([poundAvailable], arena: self.arena),
12801283
baseName: missingToken(.identifier),
12811284
argumentNames: nil,
@@ -1429,6 +1432,7 @@ extension Parser {
14291432
return RawMacroExpansionExprSyntax(
14301433
unexpectedBeforePound,
14311434
pound: pound,
1435+
moduleSelector: nil,
14321436
unexpectedBeforeMacroName,
14331437
macroName: macroName,
14341438
genericArgumentClause: generics,
@@ -1743,6 +1747,7 @@ extension Parser {
17431747
mutating func parseAnonymousClosureArgument() -> RawDeclReferenceExprSyntax {
17441748
let (unexpectedBeforeBaseName, baseName) = self.expect(.dollarIdentifier)
17451749
return RawDeclReferenceExprSyntax(
1750+
moduleSelector: nil,
17461751
unexpectedBeforeBaseName,
17471752
baseName: baseName,
17481753
argumentNames: nil,
@@ -2396,7 +2401,12 @@ extension Parser {
23962401
unknownAttr = RawAttributeSyntax(
23972402
atSign: at,
23982403
unexpectedBeforeIdent,
2399-
attributeName: RawIdentifierTypeSyntax(name: ident, genericArgumentClause: nil, arena: self.arena),
2404+
attributeName: RawIdentifierTypeSyntax(
2405+
moduleSelector: nil,
2406+
name: ident,
2407+
genericArgumentClause: nil,
2408+
arena: self.arena
2409+
),
24002410
leftParen: nil,
24012411
arguments: nil,
24022412
rightParen: nil,

0 commit comments

Comments
 (0)