-
Notifications
You must be signed in to change notification settings - Fork 441
/
Copy pathSyntaxProtocol.swift
745 lines (645 loc) · 26.1 KB
/
SyntaxProtocol.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
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
/// Provide common functionality for specialized syntax nodes. Extend this
/// protocol to provide common functionality for all syntax nodes.
///
/// - Important: Do not conform to this protocol yourself.
public protocol SyntaxProtocol: CustomStringConvertible,
CustomDebugStringConvertible, TextOutputStreamable, CustomReflectable, Sendable
{
/// Retrieve the generic syntax node that is represented by this node.
/// Do not retrieve this property directly. Use `Syntax(self)` instead.
var _syntaxNode: Syntax { get }
/// Converts the given specialized node to this type. Returns `nil` if the
/// conversion is not possible.
init?(_ node: __shared some SyntaxProtocol)
/// The statically allowed structure of the syntax node.
static var structure: SyntaxNodeStructure { get }
}
// MARK: Access underlying data
extension SyntaxProtocol {
@_spi(RawSyntax)
public var raw: RawSyntax {
return _syntaxNode.raw
}
}
// MARK: Casting
// Casting functions to specialized syntax nodes.
extension SyntaxProtocol {
/// Initializes a new instance of the conforming type from a given specialized syntax node.
///
/// Returns `nil` if the conversion isn't possible, or if the provided node is `nil`.
public init?<S: SyntaxProtocol>(_ node: S?) {
guard let node = node else {
return nil
}
self.init(node)
}
/// Checks if the current syntax node can be cast to a given specialized syntax type.
///
/// - Returns: `true` if the node can be cast, `false` otherwise.
public func `is`<S: SyntaxProtocol>(_ syntaxType: S.Type) -> Bool {
return self.as(syntaxType) != nil
}
/// Checks if the current syntax node can be upcast to ``Syntax`` node.
///
/// - Returns: `true` since the node can always be upcast to ``Syntax`` node.
///
/// - Note: This method overloads the general `is` method and is marked deprecated to produce a warning
/// informing the user that the upcast will always succeed.
@available(*, deprecated, message: "This cast will always succeed")
public func `is`(_ syntaxType: Syntax.Type) -> Bool {
return true
}
/// Checks if the current syntax node can be cast to its own type.
///
/// - Returns: `true` since the node is already of its own type.
///
/// - Note: This method overloads the general `is` method and is marked as deprecated to produce a warning,
/// informing the user that the cast will always succeed.
@available(*, deprecated, message: "This cast will always succeed")
public func `is`(_ syntaxType: Self.Type) -> Bool {
return true
}
/// Attempts to cast the current syntax node to a given specialized syntax type.
///
/// - Returns: An instance of the specialized type, or `nil` if the cast fails.
public func `as`<S: SyntaxProtocol>(_ syntaxType: S.Type) -> S? {
return S.init(self)
}
/// Attempts to upcast the current syntax node to ``Syntax`` node.
///
/// - Returns: The ``Syntax`` node created from the current syntax node, as the node can always be upcast to ``Syntax`` node.
///
/// - Note: This method overloads the general `as` method and is marked deprecated to produce a warning
/// informing the user the upcast should be performed using the base node's initializer.
@available(*, deprecated, message: "Use `Syntax.init` for upcasting.")
public func `as`(_ syntaxType: Syntax.Type) -> Syntax? {
return Syntax(self)
}
/// Casts the current syntax node to its own type.
///
/// - Returns: The current syntax node.
///
/// - Note: This method overloads the general `as` method and is marked as deprecated to produce a warning,
/// informing the user that the cast will always succeed.
@available(*, deprecated, message: "This cast will always succeed")
public func `as`(_ syntaxType: Self.Type) -> Self? {
return self
}
/// Force-casts the current syntax node to a given specialized syntax type.
///
/// - Returns: An instance of the specialized type.
/// - Warning: This function will crash if the cast is not possible. Use `as` to safely attempt a cast.
public func cast<S: SyntaxProtocol>(_ syntaxType: S.Type) -> S {
return self.as(S.self)!
}
/// Force-cast the current syntax node to ``Syntax`` node..
///
/// - Returns: The ``Syntax`` node created from the current syntax node, as the node can always be upcast to ``Syntax`` node.
///
/// - Note: This method overloads the general `as` method and is marked deprecated to produce a warning
/// informing the user the upcast should be performed using the base node's initializer.
@available(*, deprecated, message: "Use `Syntax.init` for upcasting.")
public func cast(_ syntaxType: Syntax.Type) -> Syntax {
return Syntax(self)
}
/// Force-casts the current syntax node to its own type.
///
/// - Returns: The current syntax node.
///
/// - Note: This method overloads the general `cast` method and is marked as deprecated to produce a warning,
/// informing the user that the cast will always succeed.
@available(*, deprecated, message: "This cast will always succeed")
public func cast(_ syntaxType: Self.Type) -> Self {
return self
}
}
// MARK: Modifying
extension SyntaxProtocol {
/// Returns a new syntax node that has the child at `keyPath` replaced by
/// `value`.
public func with<T>(_ keyPath: WritableKeyPath<Self, T>, _ value: T) -> Self {
var copy = self
copy[keyPath: keyPath] = value
return copy
}
/// Return this subtree with this node as the root, ie. detach this node
/// from its parent.
public var detached: Self {
if !self.hasParent {
return self
}
// Make sure `self` (and thus the arena of `self.raw`) can’t get deallocated
// before the detached node can be created.
return withExtendedLifetime(self) {
return Syntax(raw: self.raw, rawNodeArena: self.raw.arenaReference.retained).cast(Self.self)
}
}
}
// MARK: Type information
extension SyntaxProtocol {
/// The kind of the syntax node, e.g. if it is a ``SyntaxKind/functionDecl``.
///
/// If you want to check for a node's kind and cast the node to that type, see
/// `self.as(FunctionDeclSyntax.self)` or `self.as(SyntaxEnum.self)`.
public var kind: SyntaxKind {
return raw.kind
}
/// The dynamic metatype of the concrete node.
///
/// You almost always want to prefer this over `type(of: self)` because if
/// `self` is a ``DeclSyntax`` representing a ``FunctionDeclSyntax``,
/// `type(of: self)` will return ``DeclSyntax``, while `syntaxNodeType` looks
/// at the dynamic kind of this node and returns ``FunctionDeclSyntax``.
public var syntaxNodeType: SyntaxProtocol.Type {
return self.raw.kind.syntaxNodeType
}
}
// MARK: Children / parent / ancestor
extension SyntaxProtocol {
/// A sequence over the children of this node.
public func children(viewMode: SyntaxTreeViewMode) -> SyntaxChildren {
return SyntaxChildren(_syntaxNode, viewMode: viewMode)
}
/// The index of this node in a ``SyntaxChildren`` collection.
@available(*, deprecated, message: "Use index(of:) on the collection that contains this node")
public var index: SyntaxChildrenIndex {
return indexInParent
}
/// The index of this node in a ``SyntaxChildren`` collection.
internal var indexInParent: SyntaxChildrenIndex {
return SyntaxChildrenIndex(value: Syntax(self).layoutIndexInParent)
}
/// The parent of this syntax node, or `nil` if this node is the root.
public var parent: Syntax? {
return Syntax(self).parent
}
/// The root of the tree in which this node resides.
public var root: Syntax {
return Syntax(self).root
}
/// Whether or not this node has a parent.
public var hasParent: Bool {
return Syntax(self).hasParent
}
public var keyPathInParent: AnyKeyPath? {
guard let parent = self.parent else {
return nil
}
guard case .layout(let childrenKeyPaths) = parent.kind.syntaxNodeType.structure else {
return nil
}
return childrenKeyPaths[Syntax(self).layoutIndexInParent]
}
@available(*, deprecated, message: "Use previousToken(viewMode:) instead")
public var previousToken: TokenSyntax? {
return self.previousToken(viewMode: .sourceAccurate)
}
/// Applies `map` to this node and each of its ancestors until a non-`nil`
/// value is produced, then returns that value.
///
/// If no node has a non-`nil` mapping, returns `nil`.
public func ancestorOrSelf<T>(mapping map: (Syntax) -> T?) -> T? {
Syntax(self).ancestorOrSelf(mapping: map)
}
}
// MARK: Accessing tokens
extension SyntaxProtocol {
/// Recursively walks through the tree to find the token semantically before
/// this node.
public func previousToken(viewMode: SyntaxTreeViewMode) -> TokenSyntax? {
return self._syntaxNode.previousToken(viewMode: viewMode)
}
@available(*, deprecated, message: "Use nextToken(viewMode:) instead")
public var nextToken: TokenSyntax? {
return self.nextToken(viewMode: .sourceAccurate)
}
/// Recursively walks through the tree to find the next token semantically
/// after this node.
public func nextToken(viewMode: SyntaxTreeViewMode) -> TokenSyntax? {
return self._syntaxNode.nextToken(viewMode: viewMode)
}
@available(*, deprecated, message: "Use firstToken(viewMode: .sourceAccurate) instead")
public var firstToken: TokenSyntax? {
return self.firstToken(viewMode: .sourceAccurate)
}
/// Returns the first token node that is part of this syntax node.
public func firstToken(viewMode: SyntaxTreeViewMode) -> TokenSyntax? {
return self._syntaxNode.firstToken(viewMode: viewMode)
}
@available(*, deprecated, message: "Use lastToken(viewMode: .sourceAccurate) instead")
public var lastToken: TokenSyntax? {
return self.lastToken(viewMode: .sourceAccurate)
}
/// Returns the last token node that is part of this syntax node.
public func lastToken(viewMode: SyntaxTreeViewMode) -> TokenSyntax? {
return self._syntaxNode.lastToken(viewMode: viewMode)
}
/// Sequence of tokens that are part of this Syntax node.
public func tokens(viewMode: SyntaxTreeViewMode) -> TokenSequence {
return TokenSequence(_syntaxNode, viewMode: viewMode)
}
/// Find the syntax token at the given absolute position within this
/// syntax node or any of its children.
public func token(at position: AbsolutePosition) -> TokenSyntax? {
// If the position isn't within this node at all, return early.
guard position >= self.position && position < self.endPosition else {
return nil
}
// If we are a token syntax, that's it!
if let token = Syntax(self).as(TokenSyntax.self) {
return token
}
// Otherwise, it must be one of our children.
for child in children(viewMode: .sourceAccurate) {
if let token = child.token(at: position) {
return token
}
}
fatalError("Children of syntax node do not cover all positions in it")
}
/// If the node with the given `syntaxIdentifier` is a (recursive) child of this node, return the node with that
/// identifier.
///
/// If the identifier references a node from a different tree (ie. one that has a different root ID in the
/// ``SyntaxIdentifier``) or if no node with the given identifier is a child of this syntax node, returns `nil`.
public func node(at syntaxIdentifier: SyntaxIdentifier) -> Syntax? {
let syntax = Syntax(self)
guard syntax.raw.id == syntaxIdentifier.rootId else {
return nil
}
func _node(at indexInTree: UInt32, in node: Syntax) -> Syntax? {
let i = node.absoluteInfo.indexInTree
if i == indexInTree {
return node
}
guard i < indexInTree, indexInTree < i &+ UInt32(truncatingIfNeeded: node.raw.totalNodes) else {
return nil
}
for child in node.children(viewMode: .all) {
if let node = _node(at: indexInTree, in: child) {
return node
}
}
preconditionFailure("syntaxIdentifier is covered by this node but not any of its children?")
}
return _node(at: syntaxIdentifier.indexInTree.indexInTree, in: syntax)
}
}
// MARK: Recursive flags
extension SyntaxProtocol {
/// Whether the tree contained by this layout has any
/// - missing nodes or
/// - unexpected nodes or
/// - tokens with a ``TokenDiagnostic`` of severity ``TokenDiagnostic/Severity-swift.enum/error``.
public var hasError: Bool {
return raw.hasError
}
/// Whether the tree contained by this layout has any tokens with a
/// ``TokenDiagnostic`` of severity `warning`.
public var hasWarning: Bool {
return raw.recursiveFlags.contains(.hasWarning)
}
/// Whether this tree contains a missing token or unexpected node.
public var hasSequenceExpr: Bool {
return raw.recursiveFlags.contains(.hasSequenceExpr)
}
public var hasMaximumNestingLevelOverflow: Bool {
return raw.recursiveFlags.contains(.hasMaximumNestingLevelOverflow)
}
}
// MARK: Position / length / range
extension SyntaxProtocol {
/// The absolute position of the starting point of this node. If the first token
/// is with leading trivia, the position points to the start of the leading
/// trivia.
public var position: AbsolutePosition {
return Syntax(self).position
}
/// The absolute position of the starting point of this node, skipping any
/// leading trivia attached to the first token syntax.
public var positionAfterSkippingLeadingTrivia: AbsolutePosition {
return Syntax(self).positionAfterSkippingLeadingTrivia
}
/// The end position of this node's content, before any trailing trivia.
public var endPositionBeforeTrailingTrivia: AbsolutePosition {
return Syntax(self).endPositionBeforeTrailingTrivia
}
/// The end position of this node, including its trivia.
public var endPosition: AbsolutePosition {
return Syntax(self).endPosition
}
/// The length of this node including all of its trivia.
public var totalLength: SourceLength {
return raw.totalLength
}
/// The length this node takes up spelled out in the source, excluding its
/// leading or trailing trivia.
///
/// - Note: If this node consists of multiple tokens, only the first token’s
/// leading and the last token’s trailing trivia will be trimmed.
public var trimmedLength: SourceLength {
return raw.trimmedLength
}
/// The byte source range of this node including leading and trailing trivia.
@available(*, deprecated, renamed: "range")
public var totalByteRange: ByteSourceRange {
return ByteSourceRange(offset: position.utf8Offset, length: totalLength.utf8Length)
}
/// The range of this node including leading and trailing trivia.
public var range: Range<AbsolutePosition> {
return position..<endPosition
}
/// The byte source range of this node excluding leading and trailing trivia.
///
/// - Note: If this node consists of multiple tokens, only the first token’s
/// leading and the last token’s trailing trivia will be trimmed.
@available(*, deprecated, renamed: "trimmedRange")
public var trimmedByteRange: ByteSourceRange {
return ByteSourceRange(
offset: positionAfterSkippingLeadingTrivia.utf8Offset,
length: trimmedLength.utf8Length
)
}
/// The range of this node excluding leading and trailing trivia.
///
/// - Note: If this node consists of multiple tokens, only the first token’s
/// leading and the last token’s trailing trivia will be trimmed.
public var trimmedRange: Range<AbsolutePosition> {
return positionAfterSkippingLeadingTrivia..<endPositionBeforeTrailingTrivia
}
@available(*, deprecated, renamed: "trimmedLength")
public var contentLength: SourceLength {
return trimmedLength
}
@available(*, deprecated, renamed: "totalByteRange")
public var byteRange: ByteSourceRange {
return ByteSourceRange(offset: position.utf8Offset, length: totalLength.utf8Length)
}
/// The textual byte length of this node including leading and trailing trivia.
@available(*, deprecated, message: "Use totalLength.utf8Length")
public var byteSize: Int {
return totalLength.utf8Length
}
/// The textual byte length of this node excluding leading and trailing trivia.
@available(*, deprecated, message: "Use trimmedLength.utf8Length")
public var byteSizeAfterTrimmingTrivia: Int {
return trimmedLength.utf8Length
}
}
// MARK: Trivia
extension SyntaxProtocol {
/// The leading trivia of this syntax node.
///
/// Trivia is always attached to tokens, not to layout nodes. This will return the leading trivia of the first token
/// within the subtree. If no such token exists, this returns empty trivia.
///
/// - Note: ``Trivia`` is not able to represent invalid UTF-8 sequences. To get
/// the leading trivia text including all invalid UTF-8 sequences, use
/// ```
/// node.syntaxTextBytes.prefix(self.leadingTriviaLength.utf8Length)
/// ```
public var leadingTrivia: Trivia {
get {
return raw.formLeadingTrivia()
}
set {
self = Syntax(self).withLeadingTrivia(newValue, rawAllocationArena: RawSyntaxArena()).cast(Self.self)
}
}
/// The trailing trivia of this syntax node.
///
/// Trivia is always attached to tokens, not to layout nodes. This will return the trailing trivia of the last token
/// within the subtree. If no such token exists, this returns empty trivia.
///
/// Note: ``Trivia`` is not able to represent invalid UTF-8 sequences. To get
/// the leading trivia text including all invalid UTF-8 sequences, use
/// ```
/// node.syntaxTextBytes[(node.byteSize - node.trailingTriviaLength.utf8Length)...]
/// ```
public var trailingTrivia: Trivia {
get {
return raw.formTrailingTrivia()
}
set {
self = Syntax(self).withTrailingTrivia(newValue, rawAllocationArena: RawSyntaxArena()).cast(Self.self)
}
}
/// The length this node's leading trivia takes up spelled out in source.
public var leadingTriviaLength: SourceLength {
return raw.leadingTriviaLength
}
/// The length this node's trailing trivia takes up spelled out in source.
public var trailingTriviaLength: SourceLength {
return raw.trailingTriviaLength
}
}
// MARK: Content
/// Provides the source-accurate text for a node
extension SyntaxProtocol {
/// A source-accurate description of this node.
///
/// Note that the resulting String cannot represent invalid UTF-8 that
/// occurs within the syntax nodes. Use `syntaxTextBytes` for a
/// byte-accurate representation of this node in the presence of
/// invalid UTF-8.
public var description: String {
return Syntax(self).raw.description
}
/// 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] {
return Syntax(self).raw.syntaxTextBytes
}
/// Prints the raw value of this node to the provided stream.
/// - Parameter stream: The stream to which to print the raw tree.
public func write<Target>(to stream: inout Target)
where Target: TextOutputStream {
Syntax(self).raw.write(to: &stream)
}
/// A copy of this node without the leading trivia of the first token in the
/// node and the trailing trivia of the last token in the node.
///
/// The trimmed node is detached from its parent.
public var trimmed: Self {
// TODO: Should only need one new node here
return self.detached.with(\.leadingTrivia, []).with(\.trailingTrivia, [])
}
/// A copy of this node with pieces that match `matching` trimmed from the
/// leading trivia of the first token and trailing trivia of the last token.
public func trimmed(matching filter: (TriviaPiece) -> Bool) -> Self {
// TODO: Should only need one new node here
return self.with(
\.leadingTrivia,
Trivia(pieces: leadingTrivia.pieces.drop(while: filter))
).with(
\.trailingTrivia,
Trivia(pieces: trailingTrivia.pieces.reversed().drop(while: filter).reversed())
)
}
/// The description of this node with leading whitespace of the first token
/// and trailing whitespace of the last token removed.
public var trimmedDescription: String {
// TODO: We shouldn't need to create to copies just to get the trimmed
// description.
return self.trimmed.description
}
/// The description of this node with pieces that match `matching` removed
/// from the leading trivia of the first token and trailing trivia of the
/// last token.
public func trimmedDescription(matching filter: (TriviaPiece) -> Bool) -> String {
// TODO: We shouldn't need to create to copies just to get the trimmed
// description.
return self.trimmed(matching: filter).description
}
}
// MARK: Miscellaneous
extension SyntaxProtocol {
/// When isImplicit is true, the syntax node doesn't include any
/// underlying tokens, e.g. an empty CodeBlockItemList.
@available(*, deprecated, message: "Check children(viewMode: .all).isEmpty instead")
public var isImplicit: Bool {
return raw.isEmpty
}
/// Returns a value representing the unique identity of the node.
public var id: SyntaxIdentifier {
return Syntax(self).id
}
}
// MARK: Debug description
/// Provides debug descriptions for a node
extension SyntaxProtocol {
/// A simple description of this node (ie. its type).
public var debugDescription: String {
debugDescription()
}
public var customMirror: Mirror {
// Suppress printing of children when doing `po node` in the debugger.
// `debugDescription` already prints them in a nicer way.
return Mirror(self, children: [:])
}
/// Returns a summarized dump of this node.
/// - Parameters:
/// - includeTrivia: Add trivia to each dumped node, which the default
/// dump skips.
/// - converter: The location converter for the root of the tree. Adds
/// `[startLine:startCol...endLine:endCol]` to each node.
/// - mark: Adds `***` around the given node, intended to highlight it in
/// the dump.
/// - indentString: The starting indentation, empty by default. Each
/// additional indentation will add 2 spaces.
public func debugDescription(
includeTrivia: Bool = false,
converter: SourceLocationConverter? = nil,
mark: SyntaxProtocol? = nil,
indentString: String = ""
) -> String {
var str = ""
debugWrite(
to: &str,
includeTrivia: includeTrivia,
converter: converter,
mark: mark,
indentString: indentString
)
return str
}
private func debugWrite<Target: TextOutputStream>(
to target: inout Target,
includeTrivia: Bool,
converter: SourceLocationConverter? = nil,
mark: SyntaxProtocol? = nil,
indentString: String
) {
if let mark, self.id == mark.id {
target.write("*** ")
}
if let token = Syntax(self).as(TokenSyntax.self) {
target.write(String(describing: token.tokenKind))
if includeTrivia {
if !leadingTrivia.isEmpty {
target.write(" leadingTrivia=\(leadingTrivia.debugDescription)")
}
if !trailingTrivia.isEmpty {
target.write(" trailingTrivia=\(trailingTrivia.debugDescription)")
}
}
} else {
target.write(String(describing: syntaxNodeType))
}
let allChildren = children(viewMode: .all)
if let converter {
let range = sourceRange(converter: converter)
target.write(" [\(range.start.line):\(range.start.column)...\(range.end.line):\(range.end.column)]")
}
if let tokenView = raw.tokenView, tokenView.presence == .missing {
target.write(" MISSING")
}
if let mark, self.id == mark.id {
target.write(" ***")
}
for (num, child) in allChildren.enumerated() {
let isLastChild = num == allChildren.count - 1
target.write("\n")
target.write(indentString)
target.write(isLastChild ? "╰─" : "├─")
if let keyPath = child.keyPathInParent, let name = childName(keyPath) {
target.write("\(name): ")
} else if self.kind.isSyntaxCollection {
target.write("[\(num)]: ")
}
let childIndentString = indentString + (isLastChild ? " " : "│ ")
child.debugWrite(
to: &target,
includeTrivia: includeTrivia,
converter: converter,
mark: mark,
indentString: childIndentString
)
}
}
}
/// Protocol for the enums nested inside ``Syntax`` nodes that enumerate all the
/// possible types a child node might have.
public protocol SyntaxChildChoices: SyntaxProtocol {}
extension SyntaxChildChoices {
/// Checks if the current ``SyntaxChildChoices`` instance can be cast to a given specialized syntax type.
///
/// - Returns: `true` if the node can be cast, `false` otherwise.
///
/// - Note: This method is marked as deprecated because it is advised not to use it for unrelated casts.
@available(*, deprecated, message: "This cast will always fail")
public func `is`<S: SyntaxProtocol>(_ syntaxType: S.Type) -> Bool {
return self.as(syntaxType) != nil
}
/// Attempts to cast the current ``SyntaxChildChoices`` instance to a given specialized syntax type.
///
/// - Returns: An instance of the specialized syntax type, or `nil` if the cast fails.
///
/// - Note: This method is marked as deprecated because it is advised not to use it for unrelated casts.
@available(*, deprecated, message: "This cast will always fail")
public func `as`<S: SyntaxProtocol>(_ syntaxType: S.Type) -> S? {
return S.init(self)
}
/// Force-casts the current ``SyntaxChildChoices`` instance to a given specialized syntax type.
///
/// - Returns: An instance of the specialized syntax type.
///
/// - Warning: This function will crash if the cast is not possible. Use `as` for a safe attempt.
///
/// - Note: This method is marked as deprecated because it is advised not to use it for unrelated casts.
@available(*, deprecated, message: "This cast will always fail")
public func cast<S: SyntaxProtocol>(_ syntaxType: S.Type) -> S {
return self.as(S.self)!
}
}