Skip to content

Commit b0ccdd2

Browse files
committed
Introduce quitedText property on TokenSpec:
1 parent fb619f3 commit b0ccdd2

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

CodeGeneration/Sources/SyntaxSupport/TokenSpec.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ public struct TokenSpec {
3939
/// The kind of the token.
4040
public let kind: Kind
4141

42+
/// The text of the token enclosed in double quotes, if available.
43+
///
44+
/// This property is used in code generation where a string representation of the token is needed.
45+
public var quotedText: TokenSyntax? {
46+
guard let text = text else { return nil }
47+
return "\"\(raw: text)\""
48+
}
49+
4250
/// The attributes that should be printed on any API for the generated keyword.
4351
///
4452
/// This is typically used to mark APIs as SPI when the keyword is part of an experimental language feature.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ let tokenKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
6262
SwitchCaseSyntax("case .\(tokenSpec.varOrCaseName)(let assoc):") {
6363
StmtSyntax("return String(syntaxText: assoc.defaultText)")
6464
}
65-
} else if let text = tokenSpec.text {
65+
} else if let quotedText = tokenSpec.quotedText {
6666
SwitchCaseSyntax("case .\(tokenSpec.varOrCaseName):") {
67-
StmtSyntax("return #\"\(raw: text)\"#")
67+
StmtSyntax("return #\(quotedText)#")
6868
}
6969
} else {
7070
SwitchCaseSyntax("case .\(tokenSpec.varOrCaseName)(let text):") {
@@ -171,9 +171,9 @@ let tokenKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
171171
) {
172172
try! SwitchExprSyntax("switch self") {
173173
for tokenSpec in Token.allCases.map(\.spec) {
174-
if let text = tokenSpec.text {
174+
if let quotedText = tokenSpec.quotedText {
175175
SwitchCaseSyntax("case .\(tokenSpec.varOrCaseName):") {
176-
StmtSyntax("return #\"\(raw: text)\"#")
176+
StmtSyntax("return #\(quotedText)#")
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)