Skip to content

Commit da224d8

Browse files
authored
Merge pull request #2943 from rintaro/triviapiece-trait
[Syntax] Fix TriviaPiece.isNewline
2 parents 5e3ca66 + 70b2819 commit da224d8

File tree

3 files changed

+144
-162
lines changed

3 files changed

+144
-162
lines changed

CodeGeneration/Sources/SyntaxSupport/Trivia.swift

Lines changed: 48 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,38 @@
1212

1313
import SwiftSyntax
1414

15+
public struct TriviaTraits: OptionSet {
16+
public var rawValue: UInt8
17+
18+
public init(rawValue: UInt8) {
19+
self.rawValue = rawValue
20+
}
21+
22+
// Indicates this is a whitespace.
23+
public static var whitespace: Self { .init(rawValue: 1 << 0) }
24+
25+
// Indicates a newline in Swift source code.
26+
public static var newline: Self { .init(rawValue: 1 << 1) }
27+
28+
// Horizontal space.
29+
public static var spaceOrTab: Self { .init(rawValue: 1 << 2) }
30+
31+
// Comment in Swift source code.
32+
public static var comment: Self { .init(rawValue: 1 << 3) }
33+
}
34+
1535
public class Trivia {
1636
/// The name of the trivia.
1737
public let name: TokenSyntax
1838

1939
/// The doc comment describing the trivia.
2040
public let comment: SwiftSyntax.Trivia
2141

22-
/// The list of characters that make up the trivia.
23-
///
24-
/// Useful for multi-character trivias like `\r\n`.
25-
public let characters: [Character]
26-
27-
/// The list of characters as they would appear in Swift code.
28-
///
29-
/// This might differ from `characters` due to Swift's character escape requirements.
30-
public let swiftCharacters: [Character]
42+
/// The characters that make up the trivia.
43+
public let characters: String?
3144

32-
/// Indicates if the trivia represents a comment.
33-
///
34-
/// If `true`, the trivia is some form of a comment in the Swift code.
35-
public let isComment: Bool
45+
/// The traits.
46+
public let traits: TriviaTraits
3647

3748
/// The name of the trivia in lowercase.
3849
public var lowerName: TokenSyntax { .identifier(lowercaseFirstWord(name: name.text)) }
@@ -52,168 +63,109 @@ public class Trivia {
5263
}
5364
}
5465

55-
/// The length of the `characters` array.
56-
public var charactersLen: Int { characters.count }
57-
5866
/// Indicates if the trivia is a collection of characters.
5967
///
6068
/// If `true`, the trivia is made up of multiple characters.
61-
public var isCollection: Bool { charactersLen > 0 }
62-
63-
/// Indicates if the trivia contains only whitespace characters.
64-
public var isBlank: Bool {
65-
characters.contains { $0.isWhitespace }
66-
}
67-
68-
/// Indicates if the trivia contains newline characters.
69-
public var isNewLine: Bool {
70-
characters.contains { $0.isNewline }
71-
}
69+
public var isCollection: Bool { characters != nil }
7270

7371
/// Initializes a new `Trivia` instance.
7472
///
7573
/// - Parameters:
7674
/// - name: A name of the trivia.
7775
/// - comment: A doc comment describing the trivia.
7876
/// - characters: A list of characters that make up the trivia.
79-
/// - swiftCharacters: A list of characters as they would appear in Swift code.
8077
/// - isComment: Indicates if the trivia represents a comment.
8178
init(
8279
name: TokenSyntax,
8380
comment: SwiftSyntax.Trivia,
84-
characters: [Character] = [],
85-
swiftCharacters: [Character] = [],
86-
isComment: Bool = false
81+
characters: String? = nil,
82+
traits: TriviaTraits = []
8783
) {
8884
self.name = name
8985
self.comment = comment
90-
self.isComment = isComment
9186
self.characters = characters
92-
93-
// Swift sometimes doesn't support escaped characters like \f or \v;
94-
// we should allow specifying alternatives explicitly.
95-
if !swiftCharacters.isEmpty {
96-
self.swiftCharacters = swiftCharacters
97-
} else {
98-
self.swiftCharacters = characters
99-
}
87+
self.traits = traits
10088
}
10189
}
10290

10391
public let TRIVIAS: [Trivia] = [
10492
Trivia(
10593
name: "Backslash",
10694
comment: #"A backslash that is at the end of a line in a multi-line string literal to escape the newline."#,
107-
characters: [
108-
Character("\\")
109-
],
110-
swiftCharacters: [
111-
Character("\\")
112-
]
95+
characters: "\\"
11396
),
11497

11598
Trivia(
11699
name: "BlockComment",
117100
comment: #"A developer block comment, starting with '/*' and ending with '*/'."#,
118-
isComment: true
101+
traits: [.comment]
119102
),
120103

121104
Trivia(
122105
name: "CarriageReturn",
123106
comment: #"A newline '\r' character."#,
124-
characters: [
125-
Character("\r")
126-
],
127-
swiftCharacters: [
128-
Character("\r")
129-
]
107+
characters: "\r",
108+
traits: [.whitespace, .newline]
130109
),
131110

132111
Trivia(
133112
name: "CarriageReturnLineFeed",
134113
comment: #"A newline consists of contiguous '\r' and '\n' characters."#,
135-
characters: [
136-
Character("\r"),
137-
Character("\n"),
138-
],
139-
swiftCharacters: [
140-
Character("\r"),
141-
Character("\n"),
142-
]
114+
characters: "\r\n",
115+
traits: [.whitespace, .newline]
143116
),
144117

145118
Trivia(
146119
name: "DocBlockComment",
147120
comment: #"A documentation block comment, starting with '/**' and ending with '*/'."#,
148-
isComment: true
121+
traits: [.comment]
149122
),
150123

151124
Trivia(
152125
name: "DocLineComment",
153126
comment: #"A documentation line comment, starting with '///' and excluding the trailing newline."#,
154-
isComment: true
127+
traits: [.comment]
155128
),
156129

157130
// Swift don't support form feed '\f' so we use the raw unicode
158131
Trivia(
159132
name: "Formfeed",
160133
comment: #"A form-feed 'f' character."#,
161-
characters: [
162-
Character("\u{c}")
163-
],
164-
swiftCharacters: [
165-
Character("\u{240C}")
166-
]
134+
characters: "\u{000C}",
135+
traits: [.whitespace]
167136
),
168137

169138
Trivia(
170139
name: "LineComment",
171140
comment: #"A developer line comment, starting with '//' and excluding the trailing newline."#,
172-
isComment: true
141+
traits: [.comment]
173142
),
174143

175144
Trivia(
176145
name: "Newline",
177146
comment: #"A newline '\n' character."#,
178-
characters: [
179-
Character("\n")
180-
],
181-
swiftCharacters: [
182-
Character("\n")
183-
]
147+
characters: "\n",
148+
traits: [.whitespace, .newline]
184149
),
185150

186151
Trivia(
187152
name: "Pound",
188153
comment: #"A '#' that is at the end of a line in a multi-line string literal to escape the newline."#,
189-
characters: [
190-
Character("#")
191-
],
192-
swiftCharacters: [
193-
Character("#")
194-
]
154+
characters: "#"
195155
),
196156

197157
Trivia(
198158
name: "Space",
199159
comment: #"A space ' ' character."#,
200-
characters: [
201-
Character(" ")
202-
],
203-
swiftCharacters: [
204-
Character(" ")
205-
]
160+
characters: " ",
161+
traits: [.whitespace, .spaceOrTab]
206162
),
207163

208164
Trivia(
209165
name: "Tab",
210166
comment: #"A tab '\t' character."#,
211-
characters: [
212-
Character("\t")
213-
],
214-
swiftCharacters: [
215-
Character("\t")
216-
]
167+
characters: "\t",
168+
traits: [.whitespace, .spaceOrTab]
217169
),
218170

219171
Trivia(
@@ -225,11 +177,7 @@ public let TRIVIAS: [Trivia] = [
225177
Trivia(
226178
name: "VerticalTab",
227179
comment: #"A vertical tab '\v' character."#,
228-
characters: [
229-
Character("\u{b}")
230-
],
231-
swiftCharacters: [
232-
Character("\u{2B7F}")
233-
]
180+
characters: "\u{000B}",
181+
traits: [.whitespace]
234182
),
235183
]

0 commit comments

Comments
 (0)