-
Notifications
You must be signed in to change notification settings - Fork 441
/
Copy pathRawSyntaxNodeProtocol.swift
283 lines (248 loc) · 7.63 KB
/
RawSyntaxNodeProtocol.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
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
/// All typed raw syntax nodes conform to this protocol.
/// `RawXXXSyntax` is a typed wrappeer of ``RawSyntax``.
@_spi(RawSyntax)
public protocol RawSyntaxNodeProtocol: CustomStringConvertible, TextOutputStreamable, Sendable {
/// Returns `true` if `raw` can be cast to this concrete raw syntax type.
static func isKindOf(_ raw: RawSyntax) -> Bool
/// Type erased raw syntax.
var raw: RawSyntax { get }
/// Create the typed raw syntax if `other` can be cast to `Self`
init?(_ other: some RawSyntaxNodeProtocol)
}
public extension RawSyntaxNodeProtocol {
/// Cast to the specified raw syntax type if possible.
func `as`<Node: RawSyntaxNodeProtocol>(_: Node.Type) -> Node? {
Node(self)
}
/// Check if this instance can be cast to the specified syntax type.
func `is`<Node: RawSyntaxNodeProtocol>(_: Node.Type) -> Bool {
Node.isKindOf(self.raw)
}
func cast<S: RawSyntaxNodeProtocol>(_ syntaxType: S.Type) -> S {
return self.as(S.self)!
}
var description: String {
raw.description
}
func write(to target: inout some TextOutputStream) {
raw.write(to: &target)
}
var isEmpty: Bool {
return raw.byteLength == 0
}
/// Whether the tree contained by this layout has any
/// - missing nodes or
/// - unexpected nodes or
/// - tokens with a ``TokenDiagnostic`` of severity `error`
var hasError: Bool {
return raw.recursiveFlags.contains(.hasError)
}
}
/// ``RawSyntax`` itself conforms to `RawSyntaxNodeProtocol`.
extension RawSyntax: RawSyntaxNodeProtocol {
@_spi(RawSyntax)
public static func isKindOf(_ raw: RawSyntax) -> Bool {
return true
}
@_spi(RawSyntax)
public var raw: RawSyntax { self }
init(raw: RawSyntax) {
self = raw
}
@_spi(RawSyntax)
public init(_ other: some RawSyntaxNodeProtocol) {
self.init(raw: other.raw)
}
}
#if swift(<5.8)
// Cherry-pick this function from SE-0370
extension Slice {
@_spi(RawSyntax)
@inlinable
public func initialize<S>(
from source: S
) -> (unwritten: S.Iterator, index: Index)
where S: Sequence, Base == UnsafeMutableBufferPointer<S.Element> {
let buffer = Base(rebasing: self)
let (iterator, index) = buffer.initialize(from: source)
let distance = buffer.distance(from: buffer.startIndex, to: index)
return (iterator, startIndex.advanced(by: distance))
}
}
#endif
@_spi(RawSyntax)
public struct RawTokenSyntax: RawSyntaxNodeProtocol {
@_spi(RawSyntax)
public typealias SyntaxType = TokenSyntax
@_spi(RawSyntax)
public var tokenView: RawSyntaxTokenView {
return raw.tokenView!
}
@_spi(RawSyntax)
public static func isKindOf(_ raw: RawSyntax) -> Bool {
return raw.kind == .token
}
@_spi(RawSyntax)
public var raw: RawSyntax
init(raw: RawSyntax) {
precondition(Self.isKindOf(raw))
self.raw = raw
}
private init(unchecked raw: RawSyntax) {
self.raw = raw
}
@_spi(RawSyntax)
public init?(_ other: some RawSyntaxNodeProtocol) {
guard Self.isKindOf(other.raw) else { return nil }
self.init(unchecked: other.raw)
}
@_spi(RawSyntax)
public var tokenKind: RawTokenKind {
return tokenView.rawKind
}
@_spi(RawSyntax)
public var tokenText: SyntaxText {
return tokenView.rawText
}
@_spi(RawSyntax)
public var byteLength: Int {
return raw.byteLength
}
@_spi(RawSyntax)
public var presence: SourcePresence {
tokenView.presence
}
@_spi(RawSyntax)
public var isMissing: Bool {
presence == .missing
}
@_spi(RawSyntax)
public var leadingTriviaByteLength: Int {
return tokenView.leadingTriviaByteLength
}
@_spi(RawSyntax)
public var leadingTriviaPieces: [RawTriviaPiece] {
tokenView.leadingRawTriviaPieces
}
@_spi(RawSyntax)
public var trailingTriviaByteLength: Int {
return tokenView.trailingTriviaByteLength
}
@_spi(RawSyntax)
public var trailingTriviaPieces: [RawTriviaPiece] {
tokenView.trailingRawTriviaPieces
}
/// Creates a ``RawTokenSyntax``. `wholeText` must be managed by the same
/// `arena`. `textRange` is a range of the token text in `wholeText`.
public init(
kind: RawTokenKind,
wholeText: SyntaxText,
textRange: Range<SyntaxText.Index>,
presence: SourcePresence,
tokenDiagnostic: TokenDiagnostic?,
arena: __shared ParsingSyntaxArena
) {
let raw = RawSyntax.parsedToken(
kind: kind,
wholeText: wholeText,
textRange: textRange,
presence: presence,
tokenDiagnostic: tokenDiagnostic,
arena: arena
)
self = RawTokenSyntax(unchecked: raw)
}
/// Creates a ``RawTokenSyntax``. `text` and trivia must be managed by the same
/// `arena`.
public init(
kind: RawTokenKind,
text: SyntaxText,
leadingTriviaPieces: [RawTriviaPiece] = [],
trailingTriviaPieces: [RawTriviaPiece] = [],
presence: SourcePresence,
tokenDiagnostic: TokenDiagnostic? = nil,
arena: __shared ParsingSyntaxArena
) {
if leadingTriviaPieces.isEmpty && trailingTriviaPieces.isEmpty {
// Create it via `RawSyntax.parsedToken()`.
self.init(
kind: kind,
wholeText: text,
textRange: 0..<text.count,
presence: presence,
tokenDiagnostic: tokenDiagnostic,
arena: arena
)
} else {
// Create it via `RawSyntax.makeMaterializedToken()`.
self.init(
materialized: kind,
text: text,
leadingTriviaPieces: leadingTriviaPieces,
trailingTriviaPieces: trailingTriviaPieces,
presence: presence,
tokenDiagnostic: tokenDiagnostic,
arena: arena
)
}
}
/// Creates a `MaterializedToken`. Trivia must be managed by the same `arena`.
fileprivate init(
materialized kind: RawTokenKind,
text: SyntaxText,
leadingTriviaPieces: [RawTriviaPiece],
trailingTriviaPieces: [RawTriviaPiece],
presence: SourcePresence,
tokenDiagnostic: TokenDiagnostic?,
arena: __shared SyntaxArena
) {
let raw = RawSyntax.makeMaterializedToken(
kind: kind,
text: text,
leadingTriviaPieceCount: leadingTriviaPieces.count,
trailingTriviaPieceCount: trailingTriviaPieces.count,
presence: presence,
tokenDiagnostic: tokenDiagnostic,
arena: arena,
initializingLeadingTriviaWith: { buffer in
_ = buffer.initialize(from: leadingTriviaPieces)
},
initializingTrailingTriviaWith: { buffer in
_ = buffer.initialize(from: trailingTriviaPieces)
}
)
self = RawTokenSyntax(unchecked: raw)
}
/// Creates a missing ``TokenSyntax`` with the specified kind.
/// If `text` is passed, it will be used to represent the missing token's text.
/// If `text` is `nil`, the `kind`'s default text will be used.
/// If that is also `nil`, the token will have empty text.
public init(
missing kind: RawTokenKind,
text: SyntaxText? = nil,
leadingTriviaPieces: [RawTriviaPiece] = [],
trailingTriviaPieces: [RawTriviaPiece] = [],
arena: __shared SyntaxArena
) {
self.init(
materialized: kind,
text: text ?? kind.defaultText ?? "",
leadingTriviaPieces: leadingTriviaPieces,
trailingTriviaPieces: trailingTriviaPieces,
presence: .missing,
tokenDiagnostic: nil,
arena: arena
)
}
}