-
Notifications
You must be signed in to change notification settings - Fork 441
/
Copy pathSimpleLookupQueries.swift
176 lines (155 loc) · 6.02 KB
/
SimpleLookupQueries.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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2024 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
//
//===----------------------------------------------------------------------===//
import Foundation
import SwiftSyntax
extension SyntaxProtocol {
/// Given syntax node position, returns all available labeled statements.
@_spi(Compiler) @_spi(Testing) public func lookupLabeledStmts() -> [LabeledStmtSyntax] {
return lookupLabeledStmts(at: self)
}
/// Given syntax node position, returns the current switch case and it's fallthrough destination.
@_spi(Compiler) @_spi(Testing) public func lookupFallthroughSourceAndDest()
-> (source: SwitchCaseSyntax?, destination: SwitchCaseSyntax?)
{
return lookupFallthroughSourceAndDestination(at: self)
}
/// Given syntax node position, returns the closest ancestor catch node.
@_spi(Compiler) @_spi(Testing) public func lookupCatchNode() -> Syntax? {
return lookupCatchNodeHelper(at: Syntax(self), traversedCatchClause: false)
}
// MARK: - lookupLabeledStmts
/// Given syntax node position, returns all available labeled statements.
private func lookupLabeledStmts(at syntax: SyntaxProtocol) -> [LabeledStmtSyntax] {
return walkParentTreeUpToFunctionBoundary(
at: syntax.parent,
collect: LabeledStmtSyntax.self
)
}
// MARK: - lookupFallthroughSourceAndDest
/// Given syntax node position, returns the current switch case and it's fallthrough destination.
private func lookupFallthroughSourceAndDestination(at syntax: SyntaxProtocol)
-> (SwitchCaseSyntax?, SwitchCaseSyntax?)
{
guard
let originalSwitchCase = walkParentTreeUpToFunctionBoundary(
at: Syntax(syntax),
collect: SwitchCaseSyntax.self
)
else {
return (nil, nil)
}
let nextSwitchCase = lookupNextSwitchCase(at: originalSwitchCase)
return (originalSwitchCase, nextSwitchCase)
}
/// Given a switch case, returns the case that follows according to the parent.
private func lookupNextSwitchCase(at switchCaseSyntax: SwitchCaseSyntax) -> SwitchCaseSyntax? {
guard let switchCaseListSyntax = switchCaseSyntax.parent?.as(SwitchCaseListSyntax.self) else { return nil }
var visitedOriginalCase = false
for child in switchCaseListSyntax.children(viewMode: .sourceAccurate) {
if let thisCase = child.as(SwitchCaseSyntax.self) {
if thisCase.id == switchCaseSyntax.id {
visitedOriginalCase = true
} else if visitedOriginalCase {
return thisCase
}
}
}
return nil
}
// MARK: - lookupCatchNode
/// Given syntax node location, finds where an error could be caught. If set to `true`, `traverseCatchClause`lookup will skip the next do statement.
private func lookupCatchNodeHelper(at syntax: Syntax?, traversedCatchClause: Bool) -> Syntax? {
guard let syntax else { return nil }
switch syntax.as(SyntaxEnum.self) {
case .doStmt:
if traversedCatchClause {
return lookupCatchNodeHelper(at: syntax.parent, traversedCatchClause: false)
} else {
return syntax
}
case .catchClause:
return lookupCatchNodeHelper(at: syntax.parent, traversedCatchClause: true)
case .tryExpr(let tryExpr):
if tryExpr.questionOrExclamationMark != nil {
return syntax
} else {
return lookupCatchNodeHelper(at: syntax.parent, traversedCatchClause: traversedCatchClause)
}
case .functionDecl, .accessorDecl, .initializerDecl, .deinitializerDecl, .closureExpr:
return syntax
case .exprList(let exprList):
if let tryExpr = exprList.first?.as(TryExprSyntax.self), tryExpr.questionOrExclamationMark != nil {
return Syntax(tryExpr)
}
return lookupCatchNodeHelper(at: syntax.parent, traversedCatchClause: traversedCatchClause)
default:
return lookupCatchNodeHelper(at: syntax.parent, traversedCatchClause: traversedCatchClause)
}
}
// MARK: - walkParentTree helper methods
/// Callect the first syntax node matching the collection type up to a function boundary.
private func walkParentTreeUpToFunctionBoundary<T: SyntaxProtocol>(
at syntax: Syntax?,
collect: T.Type
) -> T? {
walkParentTreeUpToFunctionBoundary(at: syntax, collect: collect, stopWithFirstMatch: true).first
}
/// Callect syntax nodes matching the collection type up to a function boundary.
private func walkParentTreeUpToFunctionBoundary<T: SyntaxProtocol>(
at syntax: Syntax?,
collect: T.Type,
stopWithFirstMatch: Bool = false
) -> [T] {
walkParentTree(
upTo: [
MemberBlockSyntax.self,
FunctionDeclSyntax.self,
InitializerDeclSyntax.self,
DeinitializerDeclSyntax.self,
AccessorDeclSyntax.self,
ClosureExprSyntax.self,
],
at: syntax,
collect: collect,
stopWithFirstMatch: stopWithFirstMatch
)
}
/// Callect syntax nodes matching the collection type up until encountering one of the specified syntax nodes.
private func walkParentTree<T: SyntaxProtocol>(
upTo stopAt: [SyntaxProtocol.Type],
at syntax: Syntax?,
collect: T.Type,
stopWithFirstMatch: Bool = false
) -> [T] {
guard let syntax, !stopAt.contains(where: { syntax.is($0) }) else { return [] }
if let matchedSyntax = syntax.as(T.self) {
if stopWithFirstMatch {
return [matchedSyntax]
} else {
return [matchedSyntax]
+ walkParentTree(
upTo: stopAt,
at: syntax.parent,
collect: collect,
stopWithFirstMatch: stopWithFirstMatch
)
}
} else {
return walkParentTree(
upTo: stopAt,
at: syntax.parent,
collect: collect,
stopWithFirstMatch: stopWithFirstMatch
)
}
}
}