Skip to content

Commit 70b2819

Browse files
committed
[CodeGeneration] Use 'String?' for 'Token.characters'
'String' is natually a collection of characters. Eliminate 'Trivia.CharactersLen' because 'Trivia.characters.utf8.count' is straightforward enough, and it was technically wrong because it returned character count, but not utf8 byte count.
1 parent a41749d commit 70b2819

File tree

2 files changed

+24
-31
lines changed

2 files changed

+24
-31
lines changed

CodeGeneration/Sources/SyntaxSupport/Trivia.swift

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ public class Trivia {
3939
/// The doc comment describing the trivia.
4040
public let comment: SwiftSyntax.Trivia
4141

42-
/// The list of characters that make up the trivia.
43-
///
44-
/// Useful for multi-character trivias like `\r\n`.
45-
public let characters: [Character]
42+
/// The characters that make up the trivia.
43+
public let characters: String?
4644

4745
/// The traits.
4846
public let traits: TriviaTraits
@@ -65,13 +63,10 @@ public class Trivia {
6563
}
6664
}
6765

68-
/// The length of the `characters` array.
69-
public var charactersLen: Int { characters.count }
70-
7166
/// Indicates if the trivia is a collection of characters.
7267
///
7368
/// If `true`, the trivia is made up of multiple characters.
74-
public var isCollection: Bool { charactersLen > 0 }
69+
public var isCollection: Bool { characters != nil }
7570

7671
/// Initializes a new `Trivia` instance.
7772
///
@@ -83,7 +78,7 @@ public class Trivia {
8378
init(
8479
name: TokenSyntax,
8580
comment: SwiftSyntax.Trivia,
86-
characters: [Character] = [],
81+
characters: String? = nil,
8782
traits: TriviaTraits = []
8883
) {
8984
self.name = name
@@ -97,7 +92,7 @@ public let TRIVIAS: [Trivia] = [
9792
Trivia(
9893
name: "Backslash",
9994
comment: #"A backslash that is at the end of a line in a multi-line string literal to escape the newline."#,
100-
characters: ["\\"]
95+
characters: "\\"
10196
),
10297

10398
Trivia(
@@ -109,14 +104,14 @@ public let TRIVIAS: [Trivia] = [
109104
Trivia(
110105
name: "CarriageReturn",
111106
comment: #"A newline '\r' character."#,
112-
characters: ["\r"],
107+
characters: "\r",
113108
traits: [.whitespace, .newline]
114109
),
115110

116111
Trivia(
117112
name: "CarriageReturnLineFeed",
118113
comment: #"A newline consists of contiguous '\r' and '\n' characters."#,
119-
characters: ["\r", "\n"],
114+
characters: "\r\n",
120115
traits: [.whitespace, .newline]
121116
),
122117

@@ -136,7 +131,7 @@ public let TRIVIAS: [Trivia] = [
136131
Trivia(
137132
name: "Formfeed",
138133
comment: #"A form-feed 'f' character."#,
139-
characters: ["\u{000C}"],
134+
characters: "\u{000C}",
140135
traits: [.whitespace]
141136
),
142137

@@ -149,27 +144,27 @@ public let TRIVIAS: [Trivia] = [
149144
Trivia(
150145
name: "Newline",
151146
comment: #"A newline '\n' character."#,
152-
characters: ["\n"],
147+
characters: "\n",
153148
traits: [.whitespace, .newline]
154149
),
155150

156151
Trivia(
157152
name: "Pound",
158153
comment: #"A '#' that is at the end of a line in a multi-line string literal to escape the newline."#,
159-
characters: ["#"]
154+
characters: "#"
160155
),
161156

162157
Trivia(
163158
name: "Space",
164159
comment: #"A space ' ' character."#,
165-
characters: [" "],
160+
characters: " ",
166161
traits: [.whitespace, .spaceOrTab]
167162
),
168163

169164
Trivia(
170165
name: "Tab",
171166
comment: #"A tab '\t' character."#,
172-
characters: ["\t"],
167+
characters: "\t",
173168
traits: [.whitespace, .spaceOrTab]
174169
),
175170

@@ -182,7 +177,7 @@ public let TRIVIAS: [Trivia] = [
182177
Trivia(
183178
name: "VerticalTab",
184179
comment: #"A vertical tab '\v' character."#,
185-
characters: ["\u{000B}"],
180+
characters: "\u{000B}",
186181
traits: [.whitespace]
187182
),
188183
]

CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/TriviaPiecesFile.swift

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,9 @@ let triviaPiecesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
6868

6969
try SwitchExprSyntax("switch self") {
7070
for trivia in TRIVIAS {
71-
if trivia.isCollection {
72-
let joined = trivia.characters.map { "\($0)" }.joined()
71+
if let characters = trivia.characters {
7372
SwitchCaseSyntax("case let .\(trivia.enumCaseName)(count):") {
74-
ExprSyntax("printRepeated(\(literal: joined), count: count)")
73+
ExprSyntax("printRepeated(\(literal: characters), count: count)")
7574
}
7675
} else {
7776
SwitchCaseSyntax("case let .\(trivia.enumCaseName)(text):") {
@@ -112,11 +111,10 @@ let triviaPiecesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
112111
"""
113112
) {
114113
for trivia in TRIVIAS {
115-
if trivia.isCollection {
116-
let joined = trivia.characters.map { "\($0)" }.joined()
114+
if let characters = trivia.characters {
117115
DeclSyntax(
118116
"""
119-
/// Returns a piece of trivia for some number of \(literal: joined) characters.
117+
/// Returns a piece of trivia for some number of \(literal: characters) characters.
120118
public static func \(trivia.enumCaseName)(_ count: Int) -> Trivia {
121119
return [.\(trivia.enumCaseName)(count)]
122120
}
@@ -125,7 +123,7 @@ let triviaPiecesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
125123

126124
DeclSyntax(
127125
"""
128-
/// Gets a piece of trivia for \(literal: joined) characters.
126+
/// Gets a piece of trivia for \(literal: characters) characters.
129127
public static var \(trivia.lowerName): Trivia {
130128
return .\(trivia.enumCaseName)(1)
131129
}
@@ -151,10 +149,10 @@ let triviaPiecesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
151149
try VariableDeclSyntax("public var sourceLength: SourceLength") {
152150
try SwitchExprSyntax("switch self") {
153151
for trivia in TRIVIAS {
154-
if trivia.isCollection {
152+
if let characters = trivia.characters {
155153
SwitchCaseSyntax("case let .\(trivia.enumCaseName)(count):") {
156-
if trivia.charactersLen != 1 {
157-
StmtSyntax("return SourceLength(utf8Length: count * \(raw: trivia.charactersLen))")
154+
if characters.utf8.count != 1 {
155+
StmtSyntax("return SourceLength(utf8Length: count * \(raw: characters.utf8.count))")
158156
} else {
159157
StmtSyntax("return SourceLength(utf8Length: count)")
160158
}
@@ -231,10 +229,10 @@ let triviaPiecesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
231229
try VariableDeclSyntax("public var byteLength: Int") {
232230
try SwitchExprSyntax("switch self") {
233231
for trivia in TRIVIAS {
234-
if trivia.isCollection {
232+
if let characters = trivia.characters {
235233
SwitchCaseSyntax("case let .\(trivia.enumCaseName)(count):") {
236-
if trivia.charactersLen != 1 {
237-
StmtSyntax("return count * \(raw: trivia.charactersLen)")
234+
if characters.utf8.count != 1 {
235+
StmtSyntax("return count * \(raw: characters.utf8.count)")
238236
} else {
239237
StmtSyntax("return count")
240238
}

0 commit comments

Comments
 (0)