12
12
13
13
import SwiftSyntax
14
14
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
+
15
35
public class Trivia {
16
36
/// The name of the trivia.
17
37
public let name : TokenSyntax
18
38
19
39
/// The doc comment describing the trivia.
20
40
public let comment : SwiftSyntax . Trivia
21
41
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 ?
31
44
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
36
47
37
48
/// The name of the trivia in lowercase.
38
49
public var lowerName : TokenSyntax { . identifier( lowercaseFirstWord ( name: name. text) ) }
@@ -52,168 +63,109 @@ public class Trivia {
52
63
}
53
64
}
54
65
55
- /// The length of the `characters` array.
56
- public var charactersLen : Int { characters. count }
57
-
58
66
/// Indicates if the trivia is a collection of characters.
59
67
///
60
68
/// 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 }
72
70
73
71
/// Initializes a new `Trivia` instance.
74
72
///
75
73
/// - Parameters:
76
74
/// - name: A name of the trivia.
77
75
/// - comment: A doc comment describing the trivia.
78
76
/// - characters: A list of characters that make up the trivia.
79
- /// - swiftCharacters: A list of characters as they would appear in Swift code.
80
77
/// - isComment: Indicates if the trivia represents a comment.
81
78
init (
82
79
name: TokenSyntax ,
83
80
comment: SwiftSyntax . Trivia ,
84
- characters: [ Character ] = [ ] ,
85
- swiftCharacters: [ Character ] = [ ] ,
86
- isComment: Bool = false
81
+ characters: String ? = nil ,
82
+ traits: TriviaTraits = [ ]
87
83
) {
88
84
self . name = name
89
85
self . comment = comment
90
- self . isComment = isComment
91
86
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
100
88
}
101
89
}
102
90
103
91
public let TRIVIAS : [ Trivia ] = [
104
92
Trivia (
105
93
name: " Backslash " ,
106
94
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: " \\ "
113
96
) ,
114
97
115
98
Trivia (
116
99
name: " BlockComment " ,
117
100
comment: #"A developer block comment, starting with '/*' and ending with '*/'."# ,
118
- isComment : true
101
+ traits : [ . comment ]
119
102
) ,
120
103
121
104
Trivia (
122
105
name: " CarriageReturn " ,
123
106
comment: #"A newline '\r' character."# ,
124
- characters: [
125
- Character ( " \r " )
126
- ] ,
127
- swiftCharacters: [
128
- Character ( " \r " )
129
- ]
107
+ characters: " \r " ,
108
+ traits: [ . whitespace, . newline]
130
109
) ,
131
110
132
111
Trivia (
133
112
name: " CarriageReturnLineFeed " ,
134
113
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]
143
116
) ,
144
117
145
118
Trivia (
146
119
name: " DocBlockComment " ,
147
120
comment: #"A documentation block comment, starting with '/**' and ending with '*/'."# ,
148
- isComment : true
121
+ traits : [ . comment ]
149
122
) ,
150
123
151
124
Trivia (
152
125
name: " DocLineComment " ,
153
126
comment: #"A documentation line comment, starting with '///' and excluding the trailing newline."# ,
154
- isComment : true
127
+ traits : [ . comment ]
155
128
) ,
156
129
157
130
// Swift don't support form feed '\f' so we use the raw unicode
158
131
Trivia (
159
132
name: " Formfeed " ,
160
133
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]
167
136
) ,
168
137
169
138
Trivia (
170
139
name: " LineComment " ,
171
140
comment: #"A developer line comment, starting with '//' and excluding the trailing newline."# ,
172
- isComment : true
141
+ traits : [ . comment ]
173
142
) ,
174
143
175
144
Trivia (
176
145
name: " Newline " ,
177
146
comment: #"A newline '\n' character."# ,
178
- characters: [
179
- Character ( " \n " )
180
- ] ,
181
- swiftCharacters: [
182
- Character ( " \n " )
183
- ]
147
+ characters: " \n " ,
148
+ traits: [ . whitespace, . newline]
184
149
) ,
185
150
186
151
Trivia (
187
152
name: " Pound " ,
188
153
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: " # "
195
155
) ,
196
156
197
157
Trivia (
198
158
name: " Space " ,
199
159
comment: #"A space ' ' character."# ,
200
- characters: [
201
- Character ( " " )
202
- ] ,
203
- swiftCharacters: [
204
- Character ( " " )
205
- ]
160
+ characters: " " ,
161
+ traits: [ . whitespace, . spaceOrTab]
206
162
) ,
207
163
208
164
Trivia (
209
165
name: " Tab " ,
210
166
comment: #"A tab '\t' character."# ,
211
- characters: [
212
- Character ( " \t " )
213
- ] ,
214
- swiftCharacters: [
215
- Character ( " \t " )
216
- ]
167
+ characters: " \t " ,
168
+ traits: [ . whitespace, . spaceOrTab]
217
169
) ,
218
170
219
171
Trivia (
@@ -225,11 +177,7 @@ public let TRIVIAS: [Trivia] = [
225
177
Trivia (
226
178
name: " VerticalTab " ,
227
179
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]
234
182
) ,
235
183
]
0 commit comments