-
Notifications
You must be signed in to change notification settings - Fork 441
/
Copy pathRawSyntaxNodeProtocol.swift
227 lines (197 loc) · 6.23 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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 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 {
/// 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?<T: RawSyntaxNodeProtocol>(_ other: T)
}
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)
}
var description: String {
raw.description
}
func write<Target>(to target: inout Target) where Target : TextOutputStream {
raw.write(to: &target)
}
var isEmpty: Bool {
return raw.byteLength == 0
}
}
/// `RawSyntax` itself conforms to `RawSyntaxNodeProtocol`.
extension RawSyntax: RawSyntaxNodeProtocol {
public static func isKindOf(_ raw: RawSyntax) -> Bool {
return true
}
public var raw: RawSyntax { self }
init(raw: RawSyntax) {
self = raw
}
public init<T: RawSyntaxNodeProtocol>(_ other: T) {
self.init(raw: other.raw)
}
}
@_spi(RawSyntax)
public struct RawTokenSyntax: RawSyntaxNodeProtocol {
public typealias SyntaxType = TokenSyntax
@_spi(RawSyntax)
public var tokenView: RawSyntaxTokenView {
return raw.tokenView!
}
public static func isKindOf(_ raw: RawSyntax) -> Bool {
return raw.kind == .token
}
public var raw: RawSyntax
init(raw: RawSyntax) {
assert(Self.isKindOf(raw))
self.raw = raw
}
public init?<Node: RawSyntaxNodeProtocol>(_ other: Node) {
guard Self.isKindOf(other.raw) else { return nil }
self.init(raw: other.raw)
}
public var tokenKind: RawTokenKind {
return tokenView.rawKind
}
public var tokenText: SyntaxText {
return tokenView.rawText
}
public var byteLength: Int {
return raw.byteLength
}
public var presence: SourcePresence {
tokenView.presence
}
public var isMissing: Bool {
presence == .missing
}
public var leadingTriviaByteLength: Int {
return tokenView.leadingTriviaByteLength
}
public var leadingTriviaPieces: [RawTriviaPiece] {
tokenView.leadingRawTriviaPieces
}
public var trailingTriviaByteLength: Int {
return tokenView.trailingTriviaByteLength
}
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,
hasLexerError: Bool,
arena: __shared SyntaxArena
) {
let raw = RawSyntax.parsedToken(
kind: kind,
wholeText: wholeText,
textRange: textRange,
presence: presence,
arena: arena,
hasLexerError: hasLexerError
)
self = RawTokenSyntax(raw: 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,
arena: __shared SyntaxArena
) {
if leadingTriviaPieces.isEmpty && trailingTriviaPieces.isEmpty {
// Create it via `RawSyntax.parsedToken()`.
self.init(
kind: kind,
wholeText: text,
textRange: 0 ..< text.count,
presence: presence,
hasLexerError: false,
arena: arena
)
} else {
// Create it via `RawSyntax.makeMaterializedToken()`.
self.init(
materialized: kind,
text: text,
leadingTriviaPieces: leadingTriviaPieces,
trailingTriviaPieces: trailingTriviaPieces,
presence: presence,
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,
arena: __shared SyntaxArena
) {
let raw = RawSyntax.makeMaterializedToken(
kind: kind,
text: text,
leadingTriviaPieceCount: leadingTriviaPieces.count,
trailingTriviaPieceCount: trailingTriviaPieces.count,
presence: presence,
arena: arena,
initializingLeadingTriviaWith: { buffer in
_ = buffer.initialize(from: leadingTriviaPieces)
}, initializingTrailingTriviaWith: { buffer in
_ = buffer.initialize(from: trailingTriviaPieces)
})
self = RawTokenSyntax(raw: 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,
arena: __shared SyntaxArena
) {
// FIXME: Allow creating a `RawSyntax.parsedToken()` with a string literal
// for text. Currently it asserts that the string buffer is not contained
// within the `arena`.
self.init(
materialized: kind,
text: text ?? kind.defaultText ?? "",
leadingTriviaPieces: [],
trailingTriviaPieces: [],
presence: .missing,
arena: arena
)
}
}