-
Notifications
You must be signed in to change notification settings - Fork 441
/
Copy pathRawSyntax.swift
979 lines (900 loc) · 31.4 KB
/
RawSyntax.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
@_spi(RawSyntax) public typealias RawSyntaxBuffer = ArenaAllocatedBufferPointer<RawSyntax?>
typealias RawTriviaPieceBuffer = ArenaAllocatedBufferPointer<RawTriviaPiece>
fileprivate extension SyntaxKind {
/// Whether this node kind should be considered as `hasError` for purposes of `RecursiveRawSyntaxFlags`.
var hasError: Bool {
return self == .unexpectedNodes || self.isMissing
}
}
struct RecursiveRawSyntaxFlags: OptionSet, Sendable {
let rawValue: UInt8
/// Whether the tree contained by this layout has any
/// - missing nodes or
/// - unexpected nodes or
/// - tokens with a ``TokenDiagnostic`` of severity `error`
static let hasError = RecursiveRawSyntaxFlags(rawValue: 1 << 0)
/// Whether the tree contained by this layout has any tokens with a
/// ``TokenDiagnostic`` of severity `warning`.
static let hasWarning = RecursiveRawSyntaxFlags(rawValue: 1 << 1)
static let hasSequenceExpr = RecursiveRawSyntaxFlags(rawValue: 1 << 2)
static let hasMaximumNestingLevelOverflow = RecursiveRawSyntaxFlags(rawValue: 1 << 3)
}
/// Node data for RawSyntax tree. Tagged union plus common data.
internal struct RawSyntaxData: Sendable {
internal enum Payload: Sendable {
/// - Important: A raw syntax node for a parsed token must always be allocated in a `ParsingRawSyntaxArena` so we can
/// parse the trivia in the token.
case parsedToken(ParsedToken)
case materializedToken(MaterializedToken)
case layout(Layout)
}
/// Token with lazy trivia parsing.
///
/// The RawSyntax's `arena` must have a valid trivia parsing function to
/// lazily materialize the leading/trailing trivia pieces.
struct ParsedToken: Sendable {
var tokenKind: RawTokenKind
/// Whole text of this token including leading/trailing trivia.
var wholeText: SyntaxText
/// Range of the actual token’s text.
///
/// Text in `wholeText` before `textRange.lowerBound` is leading trivia and
/// after `textRange.upperBound` is trailing trivia.
var textRange: Range<SyntaxText.Index>
var presence: SourcePresence
/// Store the members of ``TokenDiagnostic`` individually so the compiler can pack
/// `ParsedToken` more efficiently (saving 2 bytes)
/// `tokenDiagnosticByteOffset` is ignored if `tokenDiagnosticKind` is `nil`
private var tokenDiagnosticKind: TokenDiagnostic.Kind?
private var tokenDiagnosticByteOffset: UInt16
var tokenDiagnostic: TokenDiagnostic? {
get {
if let kind = tokenDiagnosticKind {
return TokenDiagnostic(kind, byteOffset: tokenDiagnosticByteOffset)
} else {
return nil
}
}
set {
if let newValue {
self.tokenDiagnosticKind = newValue.kind
self.tokenDiagnosticByteOffset = newValue.byteOffset
} else {
self.tokenDiagnosticKind = nil
self.tokenDiagnosticByteOffset = 0
}
}
}
init(
tokenKind: RawTokenKind,
wholeText: SyntaxText,
textRange: Range<SyntaxText.Index>,
presence: SourcePresence,
tokenDiagnostic: TokenDiagnostic?
) {
self.tokenKind = tokenKind
self.wholeText = wholeText
self.textRange = textRange
self.presence = presence
self.tokenDiagnosticKind = tokenDiagnostic?.kind
self.tokenDiagnosticByteOffset = tokenDiagnostic?.byteOffset ?? 0
}
}
/// Token typically created with `TokenSyntax.<someToken>`.
struct MaterializedToken: Sendable {
var tokenKind: RawTokenKind
var tokenText: SyntaxText
var triviaPieces: RawTriviaPieceBuffer
var numLeadingTrivia: UInt32
var byteLength: UInt32
var presence: SourcePresence
/// Store the members of ``TokenDiagnostic`` individually so the compiler can pack
/// `ParsedToken` more efficiently (saving 2 bytes)
/// `tokenDiagnosticByteOffset` is ignored if `tokenDiagnosticKind` is `nil`
private var tokenDiagnosticKind: TokenDiagnostic.Kind?
private var tokenDiagnosticByteOffset: UInt16
init(
tokenKind: RawTokenKind,
tokenText: SyntaxText,
triviaPieces: RawTriviaPieceBuffer,
numLeadingTrivia: UInt32,
byteLength: UInt32,
presence: SourcePresence,
tokenDiagnostic: TokenDiagnostic?
) {
self.tokenKind = tokenKind
self.tokenText = tokenText
self.triviaPieces = triviaPieces
self.numLeadingTrivia = numLeadingTrivia
self.byteLength = byteLength
self.presence = presence
self.tokenDiagnosticKind = tokenDiagnostic?.kind
self.tokenDiagnosticByteOffset = tokenDiagnostic?.byteOffset ?? 0
}
var tokenDiagnostic: TokenDiagnostic? {
get {
if let kind = tokenDiagnosticKind {
return TokenDiagnostic(kind, byteOffset: tokenDiagnosticByteOffset)
} else {
return nil
}
}
set {
if let newValue {
self.tokenDiagnosticKind = newValue.kind
self.tokenDiagnosticByteOffset = newValue.byteOffset
} else {
self.tokenDiagnosticKind = nil
self.tokenDiagnosticByteOffset = 0
}
}
}
}
/// Layout node including collections.
struct Layout: Sendable {
var kind: SyntaxKind
var layout: RawSyntaxBuffer
var byteLength: Int
/// Number of nodes in this subtree, excluding this node.
var descendantCount: Int
var recursiveFlags: RecursiveRawSyntaxFlags
}
var payload: Payload
var arenaReference: RawSyntaxArenaRef
}
extension RawSyntaxData.ParsedToken {
var tokenText: SyntaxText {
SyntaxText(rebasing: wholeText[textRange])
}
var leadingTriviaText: SyntaxText {
SyntaxText(rebasing: wholeText[..<textRange.lowerBound])
}
var trailingTriviaText: SyntaxText {
SyntaxText(rebasing: wholeText[textRange.upperBound...])
}
}
extension RawSyntaxData.MaterializedToken {
var leadingTrivia: RawTriviaPieceBuffer {
RawTriviaPieceBuffer(rebasing: triviaPieces[..<Int(numLeadingTrivia)])
}
var trailingTrivia: RawTriviaPieceBuffer {
RawTriviaPieceBuffer(rebasing: triviaPieces[Int(numLeadingTrivia)...])
}
}
/// Represents the raw tree structure underlying the syntax tree. These nodes
/// have no notion of identity and only provide structure to the tree. They
/// are immutable and can be freely shared between syntax nodes.
@_spi(RawSyntax)
public struct RawSyntax: Sendable {
/// Pointer to the actual data which resides in a RawSyntaxArena.
var pointer: ArenaAllocatedPointer<RawSyntaxData>
init(pointer: ArenaAllocatedPointer<RawSyntaxData>) {
self.pointer = pointer
}
init(arena: __shared RawSyntaxArena, payload: RawSyntaxData.Payload) {
let arenaRef = RawSyntaxArenaRef(arena)
let data = RawSyntaxData(
payload: payload,
arenaReference: arenaRef
)
self.init(pointer: ArenaAllocatedPointer(arena.intern(data)))
}
var rawData: RawSyntaxData {
@_transparent unsafeAddress { pointer.pointer }
}
public var arena: RetainedRawSyntaxArena {
arenaReference.retained
}
internal var arenaReference: RawSyntaxArenaRef {
rawData.arenaReference
}
internal var payload: RawSyntaxData.Payload {
get { rawData.payload }
}
}
// MARK: - Accessors
extension RawSyntax {
/// The syntax kind of this raw syntax.
@_spi(RawSyntax)
public var kind: SyntaxKind {
switch rawData.payload {
case .parsedToken(_): return .token
case .materializedToken(_): return .token
case .layout(let dat): return dat.kind
}
}
/// Whether or not this node is a token one.
@_spi(RawSyntax)
public var isToken: Bool {
kind == .token
}
var recursiveFlags: RecursiveRawSyntaxFlags {
switch view {
case .token(let tokenView):
var recursiveFlags: RecursiveRawSyntaxFlags = []
if tokenView.presence == .missing {
recursiveFlags.insert(.hasError)
}
switch tokenView.tokenDiagnostic?.severity {
case .error:
recursiveFlags.insert(.hasError)
case .warning:
recursiveFlags.insert(.hasWarning)
case nil:
break
}
return recursiveFlags
case .layout(let layoutView):
return layoutView.recursiveFlags
}
}
/// Total number of nodes in this sub-tree, including `self` node.
var totalNodes: Int {
switch rawData.payload {
case .parsedToken(_),
.materializedToken(_):
return 1
case .layout(let dat):
return dat.descendantCount + 1
}
}
/// The "width" of the node.
///
/// Sum of text byte lengths of all present descendant token nodes.
@_spi(RawSyntax)
public var byteLength: Int {
switch rawData.payload {
case .parsedToken(let dat):
if dat.presence == .present {
return dat.wholeText.count
} else {
return 0
}
case .materializedToken(let dat):
if dat.presence == .present {
return Int(dat.byteLength)
} else {
return 0
}
case .layout(let dat):
return dat.byteLength
}
}
var totalLength: SourceLength {
SourceLength(utf8Length: byteLength)
}
/// Replaces the leading trivia of the first token in this syntax tree by `leadingTrivia`.
/// If the syntax tree did not contain a token and thus no trivia could be attached to it, `nil` is returned.
/// - Parameters:
/// - leadingTrivia: The trivia to attach.
/// - arena: RawSyntaxArena to the result node data resides.
@_spi(RawSyntax)
public func withLeadingTrivia(_ leadingTrivia: Trivia, arena: RawSyntaxArena) -> RawSyntax? {
switch view {
case .token(let tokenView):
return .makeMaterializedToken(
kind: tokenView.formKind(),
leadingTrivia: leadingTrivia,
trailingTrivia: tokenView.formTrailingTrivia(),
presence: tokenView.presence,
tokenDiagnostic: tokenView.tokenDiagnostic,
arena: arena
)
case .layout(let layoutView):
for (index, child) in layoutView.children.enumerated() {
if let replaced = child?.withLeadingTrivia(leadingTrivia, arena: arena) {
return layoutView.replacingChild(at: index, with: replaced, arena: arena)
}
}
return nil
}
}
/// Replaces the trailing trivia of the last token in this syntax tree by `trailingTrivia`.
/// If the syntax tree did not contain a token and thus no trivia could be attached to it, `nil` is returned.
/// - Parameters:
/// - trailingTrivia: The trivia to attach.
/// - arena: RawSyntaxArena to the result node data resides.
@_spi(RawSyntax)
public func withTrailingTrivia(_ trailingTrivia: Trivia, arena: RawSyntaxArena) -> RawSyntax? {
switch view {
case .token(let tokenView):
return .makeMaterializedToken(
kind: tokenView.formKind(),
leadingTrivia: tokenView.formLeadingTrivia(),
trailingTrivia: trailingTrivia,
presence: tokenView.presence,
tokenDiagnostic: tokenView.tokenDiagnostic,
arena: arena
)
case .layout(let layoutView):
for (index, child) in layoutView.children.enumerated().reversed() {
if let replaced = child?.withTrailingTrivia(trailingTrivia, arena: arena) {
return layoutView.replacingChild(at: index, with: replaced, arena: arena)
}
}
return nil
}
}
}
extension RawTriviaPiece {
/// Call `body` with the syntax text of this trivia piece.
///
/// If `isEphemeral` is `true`, the ``SyntaxText`` argument is only guaranteed
/// to be valid within the call.
func withSyntaxText(body: (SyntaxText, _ isEphemeral: Bool) throws -> Void) rethrows {
if let syntaxText = storedText {
try body(syntaxText, /*isEphemeral*/ false)
return
}
var description = ""
write(to: &description)
try description.withUTF8 { buffer in
try body(
SyntaxText(baseAddress: buffer.baseAddress, count: buffer.count),
/*isEphemeral*/ true
)
}
}
}
extension RawSyntax {
/// Enumerate all of the syntax text present in this node, and all
/// of its children, to give a source-accurate view of the bytes.
///
/// Unlike `description`, this provides a source-accurate representation
/// even in the presence of malformed UTF-8 in the input source.
///
/// If `isEphemeral` is `true`, the ``SyntaxText`` arguments passed to the
/// visitor are only guaranteed to be valid within that call. Otherwise, they
/// are valid as long as the raw syntax is alive.
public func withEachSyntaxText(body: (SyntaxText, _ isEphemeral: Bool) throws -> Void) rethrows {
switch rawData.payload {
case .parsedToken(let dat):
if dat.presence == .present {
try body(dat.wholeText, /*isEphemeral*/ false)
}
case .materializedToken(let dat):
if dat.presence == .present {
for p in dat.leadingTrivia {
try p.withSyntaxText(body: body)
}
try body(dat.tokenText, /*isEphemeral*/ false)
for p in dat.trailingTrivia {
try p.withSyntaxText(body: body)
}
}
case .layout(let dat):
for case let child? in dat.layout {
try child.withEachSyntaxText(body: body)
}
}
}
/// Retrieve the syntax text as an array of bytes that models the input
/// source even in the presence of invalid UTF-8.
public var syntaxTextBytes: [UInt8] {
var result: [UInt8] = []
var buf: SyntaxText = ""
withEachSyntaxText { syntaxText, isEphemeral in
if isEphemeral {
result.append(contentsOf: buf)
result.append(contentsOf: syntaxText)
buf = ""
} else if let base = buf.baseAddress, base + buf.count == syntaxText.baseAddress {
buf = SyntaxText(baseAddress: base, count: buf.count + syntaxText.count)
} else {
result.append(contentsOf: buf)
buf = syntaxText
}
}
result.append(contentsOf: buf)
return result
}
}
extension RawSyntax: TextOutputStreamable, CustomStringConvertible {
/// Prints the RawSyntax node, and all of its children, to the provided
/// stream. This implementation must be source-accurate.
/// - Parameter stream: The stream on which to output this node.
public func write<Target: TextOutputStream>(to target: inout Target) {
switch rawData.payload {
case .parsedToken(let dat):
if dat.presence == .present {
String(syntaxText: dat.wholeText).write(to: &target)
}
case .materializedToken(let dat):
if dat.presence == .present {
for p in dat.leadingTrivia { p.write(to: &target) }
String(syntaxText: dat.tokenText).write(to: &target)
for p in dat.trailingTrivia { p.write(to: &target) }
}
case .layout(let dat):
for case let child? in dat.layout {
child.write(to: &target)
}
}
}
/// A source-accurate description of this node.
public var description: String {
var s = ""
self.write(to: &s)
return s
}
}
extension RawSyntax {
/// Return the first token of a layout node that should be traversed by `viewMode`.
func firstToken(viewMode: SyntaxTreeViewMode) -> RawSyntaxTokenView? {
guard viewMode.shouldTraverse(node: self) else { return nil }
switch view {
case .token(let tokenView):
return tokenView
case .layout(let layoutView):
for child in layoutView.children {
if let token = child?.firstToken(viewMode: viewMode) {
return token
}
}
return nil
}
}
/// Return the last token of a layout node that should be traversed by `viewMode`.
func lastToken(viewMode: SyntaxTreeViewMode) -> RawSyntaxTokenView? {
guard viewMode.shouldTraverse(node: self) else { return nil }
switch view {
case .token(let tokenView):
return tokenView
case .layout(let layoutView):
for child in layoutView.children.reversed() {
if let token = child?.lastToken(viewMode: viewMode) {
return token
}
}
return nil
}
}
func formLeadingTrivia() -> Trivia {
firstToken(viewMode: .sourceAccurate)?.formLeadingTrivia() ?? []
}
func formTrailingTrivia() -> Trivia {
lastToken(viewMode: .sourceAccurate)?.formTrailingTrivia() ?? []
}
}
extension RawSyntax {
@_spi(RawSyntax)
public var leadingTriviaByteLength: Int {
firstToken(viewMode: .sourceAccurate)?.leadingTriviaByteLength ?? 0
}
@_spi(RawSyntax)
public var trailingTriviaByteLength: Int {
lastToken(viewMode: .sourceAccurate)?.trailingTriviaByteLength ?? 0
}
@_spi(RawSyntax)
public var leadingTriviaPieces: [RawTriviaPiece]? {
firstToken(viewMode: .sourceAccurate)?.leadingRawTriviaPieces
}
@_spi(RawSyntax)
public var trailingTriviaPieces: [RawTriviaPiece]? {
lastToken(viewMode: .sourceAccurate)?.trailingRawTriviaPieces
}
/// The length of this node’s content, without the first leading and the last
/// trailing trivia. Intermediate trivia inside a layout node is included in
/// this.
var trimmedByteLength: Int {
let result = byteLength - leadingTriviaByteLength - trailingTriviaByteLength
precondition(result >= 0)
return result
}
var leadingTriviaLength: SourceLength {
SourceLength(utf8Length: leadingTriviaByteLength)
}
var trailingTriviaLength: SourceLength {
SourceLength(utf8Length: trailingTriviaByteLength)
}
/// The length of this node excluding its leading and trailing trivia.
var trimmedLength: SourceLength {
SourceLength(utf8Length: trimmedByteLength)
}
}
// MARK: - Factories.
extension RawSyntax {
/// "Designated" factory method to create a parsed token node.
///
/// - Parameters:
/// - kind: Token kind.
/// - wholeText: Whole text of this token including trailing/leading trivia.
/// - textRange: Range of the token text in `wholeText`.
/// - presence: Whether the token appeared in the source code or if it was synthesized.
/// - arena: RawSyntaxArena to the result node data resides.
internal static func parsedToken(
kind: RawTokenKind,
wholeText: SyntaxText,
textRange: Range<SyntaxText.Index>,
presence: SourcePresence,
tokenDiagnostic: TokenDiagnostic?,
arena: __shared ParsingRawSyntaxArena
) -> RawSyntax {
assert(
arena.contains(text: wholeText),
"token text must be managed by the arena"
)
let payload = RawSyntaxData.ParsedToken(
tokenKind: kind,
wholeText: wholeText,
textRange: textRange,
presence: presence,
tokenDiagnostic: tokenDiagnostic
)
precondition(
kind != .keyword || Keyword(payload.tokenText) != nil,
"If kind is keyword, the text must be a known token kind"
)
return RawSyntax(arena: arena, payload: .parsedToken(payload))
}
/// "Designated" factory method to create a materialized token node.
///
/// This should not be called directly.
/// Use `makeMaterializedToken(arena:kind:leadingTrivia:trailingTrivia:)` or
/// `makeMissingToken(arena:kind:)` instead.
///
/// - Parameters:
/// - kind: Token kind.
/// - text: Token text.
/// - triviaPieces: Raw trivia pieces including leading and trailing trivia.
/// - numLeadingTrivia: Number of leading trivia pieces in `triviaPieces`.
/// - byteLength: Byte length of this token including trivia.
/// - presence: Whether the token appeared in the source code or if it was synthesized.
/// - arena: RawSyntaxArena to the result node data resides.
internal static func materializedToken(
kind: RawTokenKind,
text: SyntaxText,
triviaPieces: RawTriviaPieceBuffer,
numLeadingTrivia: UInt32,
byteLength: UInt32,
presence: SourcePresence,
tokenDiagnostic: TokenDiagnostic?,
arena: __shared RawSyntaxArena
) -> RawSyntax {
let payload = RawSyntaxData.MaterializedToken(
tokenKind: kind,
tokenText: text,
triviaPieces: triviaPieces,
numLeadingTrivia: numLeadingTrivia,
byteLength: byteLength,
presence: presence,
tokenDiagnostic: tokenDiagnostic
)
precondition(kind != .keyword || Keyword(text) != nil, "If kind is keyword, the text must be a known token kind")
return RawSyntax(arena: arena, payload: .materializedToken(payload))
}
/// Factory method to create a materialized token node.
///
/// - Parameters:
/// - kind: Token kind.
/// - text: Token text.
/// - leadingTriviaPieceCount: Number of leading trivia pieces.
/// - trailingTriviaPieceCount: Number of trailing trivia pieces.
/// - presence: Whether the token appeared in the source code or if it was synthesized.
/// - arena: RawSyntaxArena to the result node data resides.
/// - initializingLeadingTriviaWith: A closure that initializes leading trivia pieces.
/// - initializingTrailingTriviaWith: A closure that initializes trailing trivia pieces.
public static func makeMaterializedToken(
kind: RawTokenKind,
text: SyntaxText,
leadingTriviaPieceCount: Int,
trailingTriviaPieceCount: Int,
presence: SourcePresence,
tokenDiagnostic: TokenDiagnostic?,
arena: __shared RawSyntaxArena,
initializingLeadingTriviaWith: (UnsafeMutableBufferPointer<RawTriviaPiece>) -> Void,
initializingTrailingTriviaWith: (UnsafeMutableBufferPointer<RawTriviaPiece>) -> Void
) -> RawSyntax {
precondition(kind.defaultText == nil || text.isEmpty || kind.defaultText == text)
let totalTriviaCount = leadingTriviaPieceCount + trailingTriviaPieceCount
let triviaBuffer = arena.allocateRawTriviaPieceBuffer(count: totalTriviaCount)
initializingLeadingTriviaWith(
UnsafeMutableBufferPointer(rebasing: triviaBuffer[..<leadingTriviaPieceCount])
)
initializingTrailingTriviaWith(
UnsafeMutableBufferPointer(rebasing: triviaBuffer[leadingTriviaPieceCount...])
)
let byteLength = text.count + triviaBuffer.reduce(0, { $0 + $1.byteLength })
return .materializedToken(
kind: kind,
text: text,
triviaPieces: RawTriviaPieceBuffer(UnsafeBufferPointer(triviaBuffer)),
numLeadingTrivia: numericCast(leadingTriviaPieceCount),
byteLength: numericCast(byteLength),
presence: presence,
tokenDiagnostic: tokenDiagnostic,
arena: arena
)
}
/// Factory method to create a materialized token node.
///
/// - Parameters:
/// - arena: RawSyntaxArena to the result node data resides.
/// - kind: Token kind.
/// - text: Token text.
/// - leadingTrivia: Leading trivia.
/// - trailingTrivia: Trailing trivia.
static func makeMaterializedToken(
kind: TokenKind,
leadingTrivia: Trivia,
trailingTrivia: Trivia,
presence: SourcePresence,
tokenDiagnostic: TokenDiagnostic?,
arena: __shared RawSyntaxArena
) -> RawSyntax {
let decomposed = kind.decomposeToRaw()
let rawKind = decomposed.rawKind
let text = (decomposed.string.map({ arena.intern($0) }) ?? decomposed.rawKind.defaultText ?? "")
return .makeMaterializedToken(
kind: rawKind,
text: text,
leadingTriviaPieceCount: leadingTrivia.count,
trailingTriviaPieceCount: trailingTrivia.count,
presence: presence,
tokenDiagnostic: tokenDiagnostic,
arena: arena,
initializingLeadingTriviaWith: { buffer in
guard var ptr = buffer.baseAddress else { return }
for piece in leadingTrivia {
ptr.initialize(to: .make(piece, arena: arena))
ptr += 1
}
},
initializingTrailingTriviaWith: { buffer in
guard var ptr = buffer.baseAddress else { return }
for piece in trailingTrivia {
ptr.initialize(to: .make(piece, arena: arena))
ptr += 1
}
}
)
}
static func makeMissingToken(
kind: TokenKind,
arena: __shared RawSyntaxArena
) -> RawSyntax {
let (rawKind, _) = kind.decomposeToRaw()
return .materializedToken(
kind: rawKind,
text: rawKind.defaultText ?? "",
triviaPieces: RawTriviaPieceBuffer(),
numLeadingTrivia: 0,
byteLength: 0,
presence: .missing,
tokenDiagnostic: nil,
arena: arena
)
}
}
extension RawSyntax {
/// "Designated" factory method to create a layout node.
///
/// This should not be called directly.
/// Use `makeLayout(arena:kind:uninitializedCount:initializingWith:)` or
/// `makeEmptyLayout(arena:kind:)` instead.
///
/// - Parameters:
/// - arena: RawSyntaxArena to the result node data resides.
/// - kind: Syntax kind. This should not be `.token`.
/// - layout: Layout buffer of the children.
/// - byteLength: Computed total byte length of this node.
/// - descendantCount: Total number of the descendant nodes in `layout`.
fileprivate static func layout(
kind: SyntaxKind,
layout: RawSyntaxBuffer,
byteLength: Int,
descendantCount: Int,
recursiveFlags: RecursiveRawSyntaxFlags,
arena: __shared RawSyntaxArena
) -> RawSyntax {
validateLayout(layout: layout, as: kind)
let payload = RawSyntaxData.Layout(
kind: kind,
layout: layout,
byteLength: byteLength,
descendantCount: descendantCount,
recursiveFlags: recursiveFlags
)
return RawSyntax(arena: arena, payload: .layout(payload))
}
/// Factory method to create a layout node.
///
/// - Parameters:
/// - arena: RawSyntaxArena to the result node data resides.
/// - kind: Syntax kind.
/// - count: Number of children.
/// - initializer: A closure that initializes elements.
public static func makeLayout(
kind: SyntaxKind,
uninitializedCount count: Int,
isMaximumNestingLevelOverflow: Bool = false,
arena: __shared RawSyntaxArena,
initializingWith initializer: (UnsafeMutableBufferPointer<RawSyntax?>) -> Void
) -> RawSyntax {
// Allocate and initialize the list.
let layoutBuffer = arena.allocateRawSyntaxBuffer(count: count)
initializer(layoutBuffer)
// Calculate the "byte width".
var byteLength = 0
var descendantCount = 0
var recursiveFlags = RecursiveRawSyntaxFlags()
if kind.hasError {
recursiveFlags.insert(.hasError)
}
for case let node? in layoutBuffer {
byteLength += node.byteLength
descendantCount += node.totalNodes
recursiveFlags.insert(node.recursiveFlags)
arena.addChild(node.arenaReference)
}
if kind == .sequenceExpr {
recursiveFlags.insert(.hasSequenceExpr)
}
if isMaximumNestingLevelOverflow {
recursiveFlags.insert(.hasMaximumNestingLevelOverflow)
}
return .layout(
kind: kind,
layout: RawSyntaxBuffer(UnsafeBufferPointer(layoutBuffer)),
byteLength: byteLength,
descendantCount: descendantCount,
recursiveFlags: recursiveFlags,
arena: arena
)
}
static func makeEmptyLayout(
kind: SyntaxKind,
arena: __shared RawSyntaxArena
) -> RawSyntax {
var recursiveFlags = RecursiveRawSyntaxFlags()
if kind.hasError {
recursiveFlags.insert(.hasError)
}
return .layout(
kind: kind,
layout: RawSyntaxBuffer(),
byteLength: 0,
descendantCount: 0,
recursiveFlags: recursiveFlags,
arena: arena
)
}
static func makeLayout(
kind: SyntaxKind,
from collection: some Collection<RawSyntax?>,
arena: __shared RawSyntaxArena,
leadingTrivia: Trivia? = nil,
trailingTrivia: Trivia? = nil
) -> RawSyntax {
if leadingTrivia != nil || trailingTrivia != nil {
var layout = Array(collection)
if let leadingTrivia = leadingTrivia,
// Find the index of the first non-empty node so we can attach the trivia to it.
let idx = layout.firstIndex(where: { $0 != nil && ($0!.isToken || $0!.totalNodes > 1) })
{
layout[idx] = layout[idx]!.withLeadingTrivia(
leadingTrivia + (layout[idx]?.formLeadingTrivia() ?? []),
arena: arena
)
}
if let trailingTrivia = trailingTrivia,
// Find the index of the first non-empty node so we can attach the trivia to it.
let idx = layout.lastIndex(where: { $0 != nil && ($0!.isToken || $0!.totalNodes > 1) })
{
layout[idx] = layout[idx]!.withTrailingTrivia(
(layout[idx]?.formTrailingTrivia() ?? []) + trailingTrivia,
arena: arena
)
}
return .makeLayout(kind: kind, from: layout, arena: arena)
}
return .makeLayout(kind: kind, uninitializedCount: collection.count, arena: arena) {
_ = $0.initialize(from: collection)
}
}
}
// MARK: - Debugging.
extension RawSyntax: CustomDebugStringConvertible {
private func debugWrite(to target: inout some TextOutputStream, indent: Int, withChildren: Bool = false) {
let childIndent = indent + 2
switch rawData.payload {
case .parsedToken(let dat):
target.write(".parsedToken(")
target.write(String(describing: dat.tokenKind))
target.write(" wholeText=\(dat.wholeText.debugDescription)")
target.write(" textRange=\(dat.textRange.description)")
case .materializedToken(let dat):
target.write(".materializedToken(")
target.write(String(describing: dat.tokenKind))
target.write(" text=\(dat.tokenText.debugDescription)")
target.write(" numLeadingTrivia=\(dat.numLeadingTrivia)")
target.write(" byteLength=\(dat.byteLength)")
break
case .layout(let dat):
target.write(".layout(")
target.write(String(describing: kind))
target.write(" byteLength=\(dat.byteLength)")
target.write(" descendantCount=\(dat.descendantCount)")
if withChildren {
for (num, child) in dat.layout.enumerated() {
target.write("\n")
target.write(String(repeating: " ", count: childIndent))
target.write("\(num): ")
if let child = child {
child.debugWrite(to: &target, indent: childIndent)
} else {
target.write("<nil>")
}
}
}
break
}
target.write(")")
}
@_spi(RawSyntax)
public var debugDescription: String {
var string = ""
debugWrite(to: &string, indent: 0, withChildren: false)
return string
}
}
extension RawSyntax: CustomReflectable {
@_spi(RawSyntax)
public var customMirror: Mirror {
let mirrorChildren: [Any]
switch view {
case .token:
mirrorChildren = []
case .layout(let layoutView):
mirrorChildren = layoutView.children.map {
child in child ?? (nil as Any?) as Any
}
}
return Mirror(self, unlabeledChildren: mirrorChildren)
}
}
enum RawSyntaxView {
case token(RawSyntaxTokenView)
case layout(RawSyntaxLayoutView)
}
extension RawSyntax {
var view: RawSyntaxView {
switch raw.payload {
case .parsedToken, .materializedToken:
return .token(tokenView!)
case .layout:
return .layout(layoutView!)
}
}
}
extension RawSyntax: Identifiable {
public struct ID: Hashable, @unchecked Sendable {
/// The pointer to the start of the `RawSyntax` node.
fileprivate var pointer: UnsafeRawPointer
fileprivate init(_ raw: RawSyntax) {
self.pointer = raw.pointer.unsafeRawPointer
}
}
public var id: ID {
return ID(self)
}
}
/// See `SyntaxMemoryLayout`.
let RawSyntaxDataMemoryLayouts: [String: SyntaxMemoryLayout.Value] = [
"RawSyntaxData": .init(RawSyntaxData.self),
"RawSyntaxData.Layout": .init(RawSyntaxData.Layout.self),
"RawSyntaxData.ParsedToken": .init(RawSyntaxData.ParsedToken.self),
"RawSyntaxData.MaterializedToken": .init(RawSyntaxData.MaterializedToken.self),
"RawSyntax?": .init(RawSyntax?.self),
]