85
85
#include < vector>
86
86
87
87
namespace swift {
88
+
89
+ namespace json {
90
+ template <class T > struct ObjectTraits ;
91
+ }
92
+
88
93
namespace syntax {
89
94
90
95
class AbsolutePosition ;
@@ -118,6 +123,9 @@ enum class TriviaKind {
118
123
// / A documentation block comment, starting with '/**' and ending with '*/.
119
124
DocBlockComment,
120
125
126
+ // / Any skipped garbage text.
127
+ GarbageText,
128
+
121
129
// / A backtick '`' character, used to escape identifiers.
122
130
Backtick,
123
131
};
@@ -133,63 +141,88 @@ enum class TriviaKind {
133
141
// /
134
142
// / In general, you should deal with the actual Trivia collection instead
135
143
// / of individual pieces whenever possible.
136
- struct TriviaPiece {
144
+ class TriviaPiece {
137
145
TriviaKind Kind;
138
146
unsigned Count;
139
147
OwnedString Text;
140
148
141
- TriviaPiece (const TriviaKind Kind, const unsigned Count,
142
- const OwnedString Text)
143
- : Kind(Kind), Count(Count), Text(Text) {}
149
+ TriviaPiece (const TriviaKind Kind, const OwnedString Text)
150
+ : Kind(Kind), Count(1 ), Text(Text) {}
151
+ TriviaPiece (const TriviaKind Kind, const unsigned Count)
152
+ : Kind(Kind), Count(Count), Text() {}
144
153
154
+ friend struct json ::ObjectTraits<TriviaPiece>;
155
+
156
+ public:
145
157
// / Return a piece of trivia for some number of space characters in a row.
146
158
static TriviaPiece spaces (unsigned Count) {
147
- return TriviaPiece {TriviaKind::Space, Count, OwnedString{} };
159
+ return {TriviaKind::Space, Count};
148
160
}
149
161
150
162
// / Return a piece of trivia for some number of tab characters in a row.
151
163
static TriviaPiece tabs (unsigned Count) {
152
- return TriviaPiece {TriviaKind::Tab, Count, OwnedString{}};
164
+ return {TriviaKind::Tab, Count};
165
+ }
166
+
167
+ // / Return a piece of trivia for some number of vertical tab characters in a
168
+ // / row.
169
+ static TriviaPiece verticalTabs (unsigned Count) {
170
+ return {TriviaKind::VerticalTab, Count};
171
+ }
172
+
173
+ // / Return a piece of trivia for some number of form-feed characters in a row.
174
+ static TriviaPiece formfeeds (unsigned Count) {
175
+ return {TriviaKind::Formfeed, Count};
153
176
}
154
177
155
178
// / Return a piece of trivia for some number of newline characters
156
179
// / in a row.
157
180
static TriviaPiece newlines (unsigned Count) {
158
- return TriviaPiece {TriviaKind::Newline, Count, OwnedString{} };
181
+ return {TriviaKind::Newline, Count};
159
182
}
160
183
161
184
// / Return a piece of trivia for a single line of ('//') developer comment.
162
185
static TriviaPiece lineComment (const OwnedString Text) {
163
- return TriviaPiece {TriviaKind::LineComment, 1 , Text};
186
+ return {TriviaKind::LineComment, Text};
164
187
}
165
188
166
189
// / Return a piece of trivia for a block comment ('/* ... */')
167
190
static TriviaPiece blockComment (const OwnedString Text) {
168
- return TriviaPiece {TriviaKind::BlockComment, 1 , Text};
191
+ return {TriviaKind::BlockComment, Text};
169
192
}
170
193
171
194
// / Return a piece of trivia for a single line of ('///') doc comment.
172
195
static TriviaPiece docLineComment (const OwnedString Text) {
173
- return TriviaPiece {TriviaKind::DocLineComment, 1 , Text};
196
+ return {TriviaKind::DocLineComment, Text};
174
197
}
175
198
176
199
// / Return a piece of trivia for a documentation block comment ('/** ... */')
177
200
static TriviaPiece docBlockComment (const OwnedString Text) {
178
- return TriviaPiece {TriviaKind::DocBlockComment, 1 , Text};
201
+ return {TriviaKind::DocBlockComment, Text};
202
+ }
203
+
204
+ // / Return a piece of trivia for any skipped garbage text.
205
+ static TriviaPiece garbageText (const OwnedString Text) {
206
+ return {TriviaKind::GarbageText, Text};
179
207
}
180
208
181
209
// / Return a piece of trivia for a single backtick '`' for escaping
182
210
// / an identifier.
183
211
static TriviaPiece backtick () {
184
- return TriviaPiece {TriviaKind::Backtick, 1 , OwnedString{} };
212
+ return {TriviaKind::Backtick, 1 };
185
213
}
186
214
215
+ // / Return kind of the trivia.
216
+ TriviaKind getKind () const { return Kind; }
217
+
218
+ // / Return textual length of the trivia.
187
219
size_t getTextLength () const {
188
220
switch (Kind) {
189
221
case TriviaKind::LineComment:
190
222
case TriviaKind::BlockComment:
191
223
case TriviaKind::DocBlockComment:
192
224
case TriviaKind::DocLineComment:
225
+ case TriviaKind::GarbageText:
193
226
return Text.size ();
194
227
case TriviaKind::Newline:
195
228
case TriviaKind::Space:
@@ -340,15 +373,15 @@ struct Trivia {
340
373
if (Count == 0 ) {
341
374
return {};
342
375
}
343
- return {{ TriviaPiece {TriviaKind::Space, Count, OwnedString{}} }};
376
+ return {{TriviaPiece::spaces ( Count) }};
344
377
}
345
378
346
379
// / Return a collection of trivia of some number of tab characters in a row.
347
380
static Trivia tabs (unsigned Count) {
348
381
if (Count == 0 ) {
349
382
return {};
350
383
}
351
- return {{ TriviaPiece {TriviaKind::Tab, Count, OwnedString{}} }};
384
+ return {{TriviaPiece::tabs ( Count) }};
352
385
}
353
386
354
387
// / Return a collection of trivia of some number of newline characters
@@ -357,43 +390,49 @@ struct Trivia {
357
390
if (Count == 0 ) {
358
391
return {};
359
392
}
360
- return {{ TriviaPiece {TriviaKind::Newline, Count, OwnedString{}} }};
393
+ return {{TriviaPiece::newlines ( Count) }};
361
394
}
362
395
363
396
// / Return a collection of trivia with a single line of ('//')
364
397
// developer comment.
365
398
static Trivia lineComment (const OwnedString Text) {
366
399
assert (Text.str ().startswith (" //" ));
367
- return {{ TriviaPiece {TriviaKind::LineComment, 1 , Text} }};
400
+ return {{TriviaPiece::lineComment ( Text) }};
368
401
}
369
402
370
403
// / Return a collection of trivia with a block comment ('/* ... */')
371
404
static Trivia blockComment (const OwnedString Text) {
372
405
assert (Text.str ().startswith (" /*" ));
373
406
assert (Text.str ().endswith (" */" ));
374
- return {{ TriviaPiece {TriviaKind::BlockComment, 1 , Text} }};
407
+ return {{TriviaPiece::blockComment ( Text) }};
375
408
}
376
409
377
410
// / Return a collection of trivia with a single line of ('///') doc comment.
378
411
static Trivia docLineComment (const OwnedString Text) {
379
412
assert (Text.str ().startswith (" ///" ));
380
- return {{ TriviaPiece {TriviaKind::DocLineComment, 1 , Text} }};
413
+ return {{TriviaPiece::docLineComment ( Text) }};
381
414
}
382
415
383
416
// / Return a collection of trivia with a documentation block
384
417
// comment ('/** ... */')
385
418
static Trivia docBlockComment (const OwnedString Text) {
386
419
assert (Text.str ().startswith (" /**" ));
387
420
assert (Text.str ().endswith (" */" ));
388
- return {{ TriviaPiece {TriviaKind::DocBlockComment, 1 , Text} }};
421
+ return {{TriviaPiece::docBlockComment (Text)}};
422
+ }
423
+
424
+ // / Return a collection of trivia with any skipped garbage text.
425
+ static Trivia garbageText (const OwnedString Text) {
426
+ assert (Text.size () > 0 );
427
+ return {{TriviaPiece::garbageText (Text)}};
389
428
}
390
429
391
430
// / Return a piece of trivia for a single backtick '`' for escaping
392
431
// / an identifier.
393
432
static Trivia backtick () {
394
- return {{ TriviaPiece {TriviaKind::Backtick, 1 , OwnedString{}} }};
433
+ return {{TriviaPiece::backtick () }};
395
434
}
396
435
};
397
- }
398
- }
436
+ } // namespace syntax
437
+ } // namespace swift
399
438
#endif // SWIFT_SYNTAX_TRIVIA_H
0 commit comments