-
Notifications
You must be signed in to change notification settings - Fork 441
/
Copy pathRawSyntaxTokenView.swift
202 lines (182 loc) · 6.49 KB
/
RawSyntaxTokenView.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
//===--- RawSyntaxTokenView.swift -----------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
extension RawSyntax {
/// A view into the `RawSyntax` that exposes functionality that's specific to tokens.
/// The token's payload must be a token, otherwise this traps.
var tokenView: RawSyntaxTokenView? {
switch raw.payload {
case .parsedToken, .materializedToken:
return RawSyntaxTokenView(raw: self)
case .layout(_):
return nil
}
}
}
/// A view into `RawSyntax` that exposes functionality that only applies to tokens.
struct RawSyntaxTokenView {
let raw: RawSyntax
fileprivate init(raw: RawSyntax) {
self.raw = raw
switch raw.payload {
case .parsedToken, .materializedToken:
break
case .layout(_):
preconditionFailure("RawSyntax must be a token")
}
}
/// Token kind of this node.
var rawKind: RawTokenKind {
switch raw.rawData.payload {
case .materializedToken(let dat):
return dat.tokenKind
case .parsedToken(let dat):
return dat.tokenKind
case .layout(_):
preconditionFailure("'tokenKind' is not available for non-token node")
}
}
/// Token text of this node.
var rawText: SyntaxText {
switch raw.rawData.payload {
case .parsedToken(let dat):
return dat.tokenText
case .materializedToken(let dat):
return dat.tokenText
case .layout(_):
preconditionFailure("'tokenText' is not available for non-token node")
}
}
/// The UTF-8 byte length of the leading trivia.
var leadingTriviaByteLength: Int {
switch raw.rawData.payload {
case .parsedToken(let dat):
return dat.leadingTriviaText.count
case .materializedToken(let dat):
return dat.leadingTrivia.reduce(0) { $0 + $1.byteLength }
case .layout(_):
preconditionFailure("'tokenLeadingTriviaByteLength' is not available for non-token node")
}
}
/// The UTF-8 byte length of the trailing trivia.
var trailingTriviaByteLength: Int {
switch raw.rawData.payload {
case .parsedToken(let dat):
return dat.trailingTriviaText.count
case .materializedToken(let dat):
return dat.trailingTrivia.reduce(0) { $0 + $1.byteLength }
case .layout(_):
preconditionFailure("'tokenTrailingTriviaByteLength' is not available for non-token node")
}
}
var leadingRawTriviaPieces: [RawTriviaPiece] {
switch raw.rawData.payload {
case .parsedToken(let dat):
return raw.arena.parseTrivia(source: dat.leadingTriviaText, position: .leading)
case .materializedToken(let dat):
return Array(dat.leadingTrivia)
case .layout(_):
preconditionFailure("'tokenLeadingRawTriviaPieces' is called on non-token raw syntax")
}
}
var trailingRawTriviaPieces: [RawTriviaPiece] {
switch raw.rawData.payload {
case .parsedToken(let dat):
return raw.arena.parseTrivia(source: dat.trailingTriviaText, position: .trailing)
case .materializedToken(let dat):
return Array(dat.trailingTrivia)
case .layout(_):
preconditionFailure("'tokenTrailingRawTriviaPieces' is called on non-token raw syntax")
}
}
/// Returns the leading `Trivia` length.
var leadingTriviaLength: SourceLength {
return SourceLength(utf8Length: leadingTriviaByteLength)
}
/// Returns the trailing `Trivia` length.
var trailingTriviaLength: SourceLength {
return SourceLength(utf8Length: trailingTriviaByteLength)
}
/// Returns the leading `Trivia`.
func formLeadingTrivia() -> Trivia {
return Trivia(pieces: leadingRawTriviaPieces.map({ TriviaPiece(raw: $0) }))
}
/// Returns the trailing `Trivia`.
/// - Returns: nil if called on a layout node.
func formTrailingTrivia() -> Trivia {
return Trivia(pieces: trailingRawTriviaPieces.map({ TriviaPiece(raw: $0) }))
}
/// Calls `body` with the token text. The token text value must not escape the closure.
func withUnsafeTokenText<Result>(
_ body: (SyntaxText?) -> Result
) -> Result {
body(rawText)
}
/// Returns a `RawSyntax` node with the same source text but with the token
/// kind changed to `newValue`.
func withKind(_ newValue: TokenKind) -> RawSyntax {
switch raw.rawData.payload {
case .parsedToken(_):
// The wholeText can't be continuous anymore. Make a materialized token.
return .makeMaterializedToken(
kind: newValue,
leadingTrivia: formLeadingTrivia(),
trailingTrivia: formTrailingTrivia(),
arena: raw.arena)
case .materializedToken(var payload):
let decomposed = newValue.decomposeToRaw()
let rawKind = decomposed.rawKind
let text: SyntaxText = (decomposed.string.map({raw.arena.intern($0)}) ??
decomposed.rawKind.defaultText ??
"")
payload.tokenKind = rawKind
payload.tokenText = text
return RawSyntax(arena: raw.arena, payload: .materializedToken(payload))
default:
preconditionFailure("'withTokenKind()' is called on non-token raw syntax")
}
}
/// The length of the token without leading or trailing trivia, assuming this
/// is a token node.
var textByteLength: Int {
switch raw.rawData.payload {
case .parsedToken(let dat):
return dat.tokenText.count
case .materializedToken(let dat):
return dat.tokenText.count
case .layout(_):
preconditionFailure("'tokenTextByteLength' is not available for non-token node")
}
}
var contentLength: SourceLength {
SourceLength(utf8Length: textByteLength)
}
func formKind() -> TokenKind {
switch raw.rawData.payload {
case .parsedToken(let dat):
return TokenKind.fromRaw(kind: dat.tokenKind, text: dat.tokenText)
case .materializedToken(let dat):
return TokenKind.fromRaw(kind: dat.tokenKind, text: dat.tokenText)
case .layout(_):
preconditionFailure("Must be invoked on a token")
}
}
var presence: SourcePresence {
switch raw.rawData.payload {
case .parsedToken(let dat):
return dat.presence
case .materializedToken(let dat):
return dat.presence
case .layout(_):
preconditionFailure("'presence' is a token-only property")
}
}
}