Skip to content

Commit 1a38c3a

Browse files
authored
Merge pull request #2077 from Matejkob/improve-TokenSpec-modeling
Improve modeling of `TokenSpec` in CodeGeneration
2 parents a7e833e + a022a1a commit 1a38c3a

File tree

4 files changed

+146
-113
lines changed

4 files changed

+146
-113
lines changed

CodeGeneration/Sources/SyntaxSupport/TokenSpec.swift

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,40 @@ public struct TokenSpec {
2121
case other
2222
}
2323

24+
/// The name of the token, suitable for use in variable or enum case names.
2425
public let varOrCaseName: TokenSyntax
2526

26-
/// If `true`, this is for an experimental language feature, and any public
27-
/// API generated should be SPI.
27+
/// Indicates if the token is part of an experimental language feature.
28+
///
29+
/// If `true`, this keyword is for an experimental language feature, and any public
30+
/// API generated should be marked as SPI
2831
public let isExperimental: Bool
2932

33+
/// The name of the token that can be shown in diagnostics.
3034
public let nameForDiagnostics: String
35+
36+
/// The actual text of the token, if available.
3137
public let text: String?
38+
39+
/// The kind of the token.
3240
public let kind: Kind
3341

42+
/// The attributes that should be printed on any API for the generated keyword.
43+
///
44+
/// This is typically used to mark APIs as SPI when the keyword is part of an experimental language feature.
45+
public var apiAttributes: AttributeListSyntax {
46+
guard isExperimental else { return "" }
47+
return AttributeListSyntax("@_spi(ExperimentalLanguageFeatures)").with(\.trailingTrivia, .newline)
48+
}
49+
50+
/// Initializes a new `TokenSpec` instance.
51+
///
52+
/// - Parameters:
53+
/// - name: A name of the token.
54+
/// - isExperimental: Indicates if the token is part of an experimental language feature.
55+
/// - nameForDiagnostics: A name of the token that can be shown in diagnostics.
56+
/// - text: An actual text of the token, if available.
57+
/// - kind: A kind of the token.
3458
fileprivate init(
3559
name: String,
3660
isExperimental: Bool = false,
@@ -45,13 +69,11 @@ public struct TokenSpec {
4569
self.kind = kind
4670
}
4771

48-
/// Retrieve the attributes that should be printed on any API for the
49-
/// generated token.
50-
public var apiAttributes: AttributeListSyntax {
51-
guard isExperimental else { return "" }
52-
return AttributeListSyntax("@_spi(ExperimentalLanguageFeatures)").with(\.trailingTrivia, .newline)
53-
}
54-
72+
/// Creates a new `TokenSpec` instance representing a punctuation token.
73+
///
74+
/// - Parameters:
75+
/// - name: A name of the token.
76+
/// - text: An actual text of the punctuation token.
5577
static func punctuator(name: String, text: String) -> TokenSpec {
5678
return TokenSpec(
5779
name: name,
@@ -61,6 +83,11 @@ public struct TokenSpec {
6183
)
6284
}
6385

86+
/// Creates a new `TokenSpec` instance representing a pound keyword token.
87+
///
88+
/// - Parameters:
89+
/// - name: A name of the token.
90+
/// - text: An actual text of the pound keyword token.
6491
static func poundKeyword(name: String, text: String) -> TokenSpec {
6592
return TokenSpec(
6693
name: name,
@@ -70,8 +97,14 @@ public struct TokenSpec {
7097
)
7198
}
7299

100+
/// Creates a new `TokenSpec` instance representing an other token.
101+
///
102+
/// - Parameters:
103+
/// - name: A name of the token.
104+
/// - nameForDiagnostics: A name of the token that can be shown in diagnostics.
105+
/// - text: An actual text of the token, if available.
73106
static func other(name: String, nameForDiagnostics: String, text: String? = nil) -> TokenSpec {
74-
TokenSpec(
107+
return TokenSpec(
75108
name: name,
76109
nameForDiagnostics: nameForDiagnostics,
77110
text: text,

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/TokenKindFile.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ let tokenKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
6464
}
6565
} else if let text = tokenSpec.text {
6666
SwitchCaseSyntax("case .\(tokenSpec.varOrCaseName):") {
67-
StmtSyntax("return #\"\(raw: text)\"#")
67+
StmtSyntax("return \(literal: text)")
6868
}
6969
} else {
7070
SwitchCaseSyntax("case .\(tokenSpec.varOrCaseName)(let text):") {
@@ -90,7 +90,7 @@ let tokenKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
9090
}
9191
} else if let text = tokenSpec.text {
9292
SwitchCaseSyntax("case .\(tokenSpec.varOrCaseName):") {
93-
StmtSyntax("return #\"\(raw: text)\"#")
93+
StmtSyntax("return \(literal: text)")
9494
}
9595
}
9696
}
@@ -173,7 +173,7 @@ let tokenKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
173173
for tokenSpec in Token.allCases.map(\.spec) {
174174
if let text = tokenSpec.text {
175175
SwitchCaseSyntax("case .\(tokenSpec.varOrCaseName):") {
176-
StmtSyntax("return #\"\(raw: text)\"#")
176+
StmtSyntax("return \(literal: text)")
177177
}
178178
}
179179
}

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/TokensFile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Utils
1818
let tokensFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
1919
try! ExtensionDeclSyntax("extension TokenSyntax") {
2020
for tokenSpec in Token.allCases.map(\.spec) {
21-
if let text = tokenSpec.text {
21+
if tokenSpec.text != nil {
2222
DeclSyntax(
2323
"""
2424
public static func \(tokenSpec.varOrCaseName)Token(

0 commit comments

Comments
 (0)