@@ -50,6 +50,8 @@ internal struct RawSyntaxData {
50
50
/// Text in `wholeText` before `textRange.lowerBound` is leading trivia and
51
51
/// after `textRange.upperBound` is trailing trivia.
52
52
var textRange : Range < SyntaxText . Index >
53
+
54
+ var presence : SourcePresence
53
55
}
54
56
55
57
/// Token typically created with `TokenSyntax.<someToken>`.
@@ -59,6 +61,7 @@ internal struct RawSyntaxData {
59
61
var triviaPieces : RawTriviaPieceBuffer
60
62
var numLeadingTrivia : UInt32
61
63
var byteLength : UInt32
64
+ var presence : SourcePresence
62
65
}
63
66
64
67
/// Layout node including collections.
@@ -183,12 +186,23 @@ extension RawSyntax {
183
186
184
187
/// The "width" of the node.
185
188
///
186
- /// Sum of text byte lengths of all descendant token nodes.
189
+ /// Sum of text byte lengths of all present descendant token nodes.
187
190
var byteLength : Int {
188
191
switch rawData. payload {
189
- case . parsedToken( let dat) : return dat. wholeText. count
190
- case . materializedToken( let dat) : return Int ( dat. byteLength)
191
- case . layout( let dat) : return dat. byteLength
192
+ case . parsedToken( let dat) :
193
+ if dat. presence == . present {
194
+ return dat. wholeText. count
195
+ } else {
196
+ return 0
197
+ }
198
+ case . materializedToken( let dat) :
199
+ if dat. presence == . present {
200
+ return Int ( dat. byteLength)
201
+ } else {
202
+ return 0
203
+ }
204
+ case . layout( let dat) :
205
+ return dat. byteLength
192
206
}
193
207
}
194
208
@@ -258,18 +272,19 @@ extension RawSyntax: TextOutputStreamable, CustomStringConvertible {
258
272
public func write< Target: TextOutputStream > ( to target: inout Target ) {
259
273
switch rawData. payload {
260
274
case . parsedToken( let dat) :
261
- String ( syntaxText: dat. wholeText) . write ( to: & target)
262
- break
275
+ if dat. presence == . present {
276
+ String ( syntaxText: dat. wholeText) . write ( to: & target)
277
+ }
263
278
case . materializedToken( let dat) :
264
- for p in dat. leadingTrivia { p. write ( to: & target) }
265
- String ( syntaxText: dat. tokenText) . write ( to: & target)
266
- for p in dat. trailingTrivia { p. write ( to: & target) }
267
- break
279
+ if dat. presence == . present {
280
+ for p in dat. leadingTrivia { p. write ( to: & target) }
281
+ String ( syntaxText: dat. tokenText) . write ( to: & target)
282
+ for p in dat. trailingTrivia { p. write ( to: & target) }
283
+ }
268
284
case . layout( let dat) :
269
285
for case let child? in dat. layout {
270
286
child. write ( to: & target)
271
287
}
272
- break
273
288
}
274
289
}
275
290
@@ -364,17 +379,23 @@ extension RawSyntax {
364
379
/// - kind: Token kind.
365
380
/// - wholeText: Whole text of this token including trailing/leading trivia.
366
381
/// - textRange: Range of the token text in `wholeText`.
382
+ /// - presence: Whether the token appeared in the source code or if it was synthesized.
367
383
/// - arena: SyntaxArea to the result node data resides.
368
384
internal static func parsedToken(
369
385
kind: RawTokenKind ,
370
386
wholeText: SyntaxText ,
371
387
textRange: Range < SyntaxText . Index > ,
388
+ presence: SourcePresence ,
372
389
arena: SyntaxArena
373
390
) -> RawSyntax {
374
391
assert ( arena. contains ( text: wholeText) ,
375
392
" token text must be managed by the arena " )
376
393
let payload = RawSyntaxData . ParsedToken (
377
- tokenKind: kind, wholeText: wholeText, textRange: textRange)
394
+ tokenKind: kind,
395
+ wholeText: wholeText,
396
+ textRange: textRange,
397
+ presence: presence
398
+ )
378
399
return RawSyntax ( arena: arena, payload: . parsedToken( payload) )
379
400
}
380
401
@@ -385,29 +406,34 @@ extension RawSyntax {
385
406
/// `makeMissingToken(arena:kind:)` instead.
386
407
///
387
408
/// - Parameters:
388
- /// - arena: SyntaxArea to the result node data resides.
389
409
/// - kind: Token kind.
390
410
/// - text: Token text.
391
411
/// - triviaPieces: Raw trivia pieces including leading and trailing trivia.
392
412
/// - numLeadingTrivia: Number of leading trivia pieces in `triviaPieces`.
393
413
/// - byteLength: Byte length of this token including trivia.
414
+ /// - presence: Whether the token appeared in the source code or if it was synthesized.
415
+ /// - arena: SyntaxArea to the result node data resides.
394
416
internal static func materializedToken(
395
417
kind: RawTokenKind ,
396
418
text: SyntaxText ,
397
419
triviaPieces: RawTriviaPieceBuffer ,
398
420
numLeadingTrivia: UInt32 ,
399
421
byteLength: UInt32 ,
422
+ presence: SourcePresence ,
400
423
arena: SyntaxArena
401
424
) -> RawSyntax {
402
425
assert ( arena. contains ( text: text) || kind. defaultText? . baseAddress == text. baseAddress,
403
426
" token text must be managed by the arena, or known default text for the token " )
404
427
assert ( triviaPieces. allSatisfy ( { $0. storedText. map ( { arena. contains ( text: $0) } ) ?? true } ) ,
405
428
" trivia text must be managed by the arena " )
406
429
let payload = RawSyntaxData . MaterializedToken (
407
- tokenKind: kind, tokenText: text,
430
+ tokenKind: kind,
431
+ tokenText: text,
408
432
triviaPieces: triviaPieces,
409
433
numLeadingTrivia: numLeadingTrivia,
410
- byteLength: byteLength)
434
+ byteLength: byteLength,
435
+ presence: presence
436
+ )
411
437
return RawSyntax ( arena: arena, payload: . materializedToken( payload) )
412
438
}
413
439
@@ -418,6 +444,7 @@ extension RawSyntax {
418
444
/// - text: Token text.
419
445
/// - leadingTriviaPieceCount: Number of leading trivia pieces.
420
446
/// - trailingTriviaPieceCount: Number of trailing trivia pieces.
447
+ /// - presence: Whether the token appeared in the source code or if it was synthesized.
421
448
/// - arena: SyntaxArea to the result node data resides.
422
449
/// - initializingLeadingTriviaWith: A closure that initializes leading trivia pieces.
423
450
/// - initializingTrailingTriviaWith: A closure that initializes trailing trivia pieces.
@@ -426,6 +453,7 @@ extension RawSyntax {
426
453
text: SyntaxText ,
427
454
leadingTriviaPieceCount: Int ,
428
455
trailingTriviaPieceCount: Int ,
456
+ presence: SourcePresence ,
429
457
arena: SyntaxArena ,
430
458
initializingLeadingTriviaWith: ( UnsafeMutableBufferPointer < RawTriviaPiece > ) -> Void ,
431
459
initializingTrailingTriviaWith : ( UnsafeMutableBufferPointer < RawTriviaPiece > ) -> Void
@@ -442,7 +470,9 @@ extension RawSyntax {
442
470
kind: kind, text: text, triviaPieces: RawTriviaPieceBuffer ( triviaBuffer) ,
443
471
numLeadingTrivia: numericCast ( leadingTriviaPieceCount) ,
444
472
byteLength: numericCast ( byteLength) ,
445
- arena: arena)
473
+ presence: presence,
474
+ arena: arena
475
+ )
446
476
}
447
477
448
478
/// Factory method to create a materialized token node.
@@ -462,20 +492,15 @@ extension RawSyntax {
462
492
) -> RawSyntax {
463
493
let decomposed = kind. decomposeToRaw ( )
464
494
let rawKind = decomposed. rawKind
465
- let text : SyntaxText
466
- switch presence {
467
- case . present:
468
- text = ( decomposed. string. map ( { arena. intern ( $0) } ) ??
469
- decomposed. rawKind. defaultText ??
470
- " " )
471
- case . missing:
472
- text = SyntaxText ( )
473
- }
495
+ let text = ( decomposed. string. map ( { arena. intern ( $0) } ) ??
496
+ decomposed. rawKind. defaultText ??
497
+ " " )
474
498
475
499
return . makeMaterializedToken(
476
500
kind: rawKind, text: text,
477
501
leadingTriviaPieceCount: leadingTrivia. count,
478
502
trailingTriviaPieceCount: trailingTrivia. count,
503
+ presence: presence,
479
504
arena: arena,
480
505
initializingLeadingTriviaWith: { buffer in
481
506
guard var ptr = buffer. baseAddress else { return }
@@ -499,8 +524,10 @@ extension RawSyntax {
499
524
) -> RawSyntax {
500
525
let ( rawKind, _) = kind. decomposeToRaw ( )
501
526
return . materializedToken(
502
- kind: rawKind, text: " " , triviaPieces: . init( start: nil , count: 0 ) ,
527
+ kind: rawKind, text: rawKind. defaultText ?? " " ,
528
+ triviaPieces: . init( start: nil , count: 0 ) ,
503
529
numLeadingTrivia: 0 , byteLength: 0 ,
530
+ presence: . missing,
504
531
arena: arena)
505
532
}
506
533
}
0 commit comments