Skip to content

Commit 658b088

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 9b197f1 commit 658b088

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
@@ -325,7 +325,12 @@ extension Parser {
325325
unexpectedBeforeAtSign,
326326
atSign: atSign,
327327
unexpectedBeforeAttributeName,
328-
attributeName: RawIdentifierTypeSyntax(name: attributeName, genericArgumentClause: nil, arena: self.arena),
328+
attributeName: RawIdentifierTypeSyntax(
329+
moduleSelector: nil,
330+
name: attributeName,
331+
genericArgumentClause: nil,
332+
arena: self.arena
333+
),
329334
leftParen: nil,
330335
arguments: nil,
331336
rightParen: nil,
@@ -349,7 +354,9 @@ extension Parser {
349354

350355
extension RawLabeledExprSyntax {
351356
fileprivate init(
352-
_ unexpectedBeforeIdentifier: RawUnexpectedNodesSyntax? = nil,
357+
_ unexpectedBeforeModuleSelector: RawUnexpectedNodesSyntax? = nil,
358+
moduleSelector: RawModuleSelectorSyntax?,
359+
_ unexpectedBetweenModuleSelectorAndIdentifier: RawUnexpectedNodesSyntax? = nil,
353360
identifier: RawTokenSyntax,
354361
_ unexpectedBetweenIdentifierAndTrailingComma: RawUnexpectedNodesSyntax? = nil,
355362
trailingComma: RawTokenSyntax? = nil,
@@ -359,7 +366,9 @@ extension RawLabeledExprSyntax {
359366
label: nil,
360367
colon: nil,
361368
expression: RawDeclReferenceExprSyntax(
362-
unexpectedBeforeIdentifier,
369+
unexpectedBeforeModuleSelector,
370+
moduleSelector: moduleSelector,
371+
unexpectedBetweenModuleSelectorAndIdentifier,
363372
baseName: identifier,
364373
argumentNames: nil,
365374
arena: arena
@@ -381,6 +390,7 @@ extension Parser {
381390
let roleTrailingComma = self.consume(if: .comma)
382391

383392
let roleElement = RawLabeledExprSyntax(
393+
moduleSelector: nil,
384394
unexpectedBeforeRole,
385395
identifier: role,
386396
trailingComma: roleTrailingComma,
@@ -410,7 +420,12 @@ extension Parser {
410420
unexpectedBeforeAtSign,
411421
atSign: atSign,
412422
unexpectedBeforeDifferentiable,
413-
attributeName: RawIdentifierTypeSyntax(name: differentiable, genericArgumentClause: nil, arena: self.arena),
423+
attributeName: RawIdentifierTypeSyntax(
424+
moduleSelector: nil,
425+
name: differentiable,
426+
genericArgumentClause: nil,
427+
arena: self.arena
428+
),
414429
unexpectedBeforeLeftParen,
415430
leftParen: leftParen,
416431
arguments: .differentiableArguments(argument),
@@ -551,8 +566,13 @@ extension Parser {
551566
return RawAttributeSyntax(
552567
unexpectedBeforeAtSign,
553568
atSign: atSign,
554-
unexpectedBeforeDerivative,
555-
attributeName: RawIdentifierTypeSyntax(name: derivative, genericArgumentClause: nil, arena: self.arena),
569+
attributeName: RawIdentifierTypeSyntax(
570+
moduleSelector: nil,
571+
unexpectedBeforeDerivative,
572+
name: derivative,
573+
genericArgumentClause: nil,
574+
arena: self.arena
575+
),
556576
unexpectedBeforeLeftParen,
557577
leftParen: leftParen,
558578
arguments: .derivativeRegistrationArguments(argument),
@@ -574,7 +594,12 @@ extension Parser {
574594
unexpectedBeforeAtSign,
575595
atSign: atSign,
576596
unexpectedBeforeTranspose,
577-
attributeName: RawIdentifierTypeSyntax(name: transpose, genericArgumentClause: nil, arena: self.arena),
597+
attributeName: RawIdentifierTypeSyntax(
598+
moduleSelector: nil,
599+
name: transpose,
600+
genericArgumentClause: nil,
601+
arena: self.arena
602+
),
578603
unexpectedBeforeLeftParen,
579604
leftParen: leftParen,
580605
arguments: .derivativeRegistrationArguments(argument),
@@ -809,6 +834,7 @@ extension Parser {
809834
let (unexpectedBeforeIsolationKind, isolationKind) =
810835
self.expectIdentifier(allowKeywordsAsIdentifier: true)
811836
let isolationKindElement = RawLabeledExprSyntax(
837+
moduleSelector: nil,
812838
unexpectedBeforeIsolationKind,
813839
identifier: isolationKind,
814840
arena: self.arena
@@ -939,6 +965,7 @@ extension Parser {
939965
let declName: RawDeclReferenceExprSyntax
940966
if label.isMissing && colon.isMissing && self.atStartOfLine {
941967
declName = RawDeclReferenceExprSyntax(
968+
moduleSelector: nil,
942969
baseName: RawTokenSyntax(missing: .identifier, arena: self.arena),
943970
argumentNames: nil,
944971
arena: self.arena

Sources/SwiftParser/Declarations.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ extension Parser {
610610
unexpectedBeforeInherited = RawUnexpectedNodesSyntax([classKeyword], arena: self.arena)
611611
inherited = RawTypeSyntax(
612612
RawIdentifierTypeSyntax(
613+
moduleSelector: nil,
613614
name: missingToken(.identifier, text: "AnyObject"),
614615
genericArgumentClause: nil,
615616
arena: self.arena
@@ -2246,6 +2247,7 @@ extension Parser {
22462247
modifiers: attrs.modifiers,
22472248
unexpectedBeforePound,
22482249
pound: pound,
2250+
moduleSelector: nil,
22492251
unexpectedBeforeMacro,
22502252
macroName: macro,
22512253
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)