@@ -14,40 +14,27 @@ import Foundation
1414import SwiftSyntax
1515
1616extension SyntaxProtocol {
17- /// Scope at the syntax node. Could be inherited from parent or introduced at the node.
18- var scope : Scope ? {
19- switch self . syntaxNodeType {
20- case is SourceFileSyntax . Type :
21- FileScope ( syntax: self )
22- default :
23- parent? . scope
24- }
17+ /// Given syntax node position, returns all available labeled statements.
18+ @_spi ( Compiler) @_spi ( Testing) public func lookupLabeledStmts( ) -> [ LabeledStmtSyntax ] {
19+ return lookupLabeledStmts ( at: self )
2520 }
26- }
27-
28- /// Provide common functionality for specialized scope implementatations.
29- protocol Scope {
30- /// The parent of this scope.
31- var parent : Scope ? { get }
3221
33- /// Syntax node that introduces this protocol.
34- var sourceSyntax : SyntaxProtocol { get }
35- }
22+ /// Given syntax node position, returns the current switch case and it's fallthrough destination.
23+ @_spi ( Compiler) @_spi ( Testing) public func lookupFallthroughSourceAndDest( )
24+ -> ( source: SwitchCaseSyntax ? , destination: SwitchCaseSyntax ? )
25+ {
26+ return lookupFallthroughSourceAndDestination ( at: self )
27+ }
3628
37- extension Scope {
38- /// Recursively walks up syntax tree and finds the closest scope other than this scope.
39- func getParentScope( forSyntax syntax: SyntaxProtocol ? ) -> Scope ? {
40- if let lookedUpScope = syntax? . scope, lookedUpScope. sourceSyntax. id == syntax? . id {
41- return getParentScope ( forSyntax: sourceSyntax. parent)
42- } else {
43- return syntax? . scope
44- }
29+ /// Given syntax node position, returns the closest ancestor catch node.
30+ @_spi ( Compiler) @_spi ( Testing) public func lookupCatchNode( ) -> Syntax ? {
31+ return lookupCatchNodeHelper ( at: Syntax ( self ) , traversedCatchClause: false )
4532 }
4633
4734 // MARK: - lookupLabeledStmts
4835
4936 /// Given syntax node position, returns all available labeled statements.
50- func lookupLabeledStmts( at syntax: SyntaxProtocol ) -> [ LabeledStmtSyntax ] {
37+ private func lookupLabeledStmts( at syntax: SyntaxProtocol ) -> [ LabeledStmtSyntax ] {
5138 return walkParentTreeUpToFunctionBoundary (
5239 at: syntax. parent,
5340 collect: LabeledStmtSyntax . self
@@ -57,7 +44,9 @@ extension Scope {
5744 // MARK: - lookupFallthroughSourceAndDest
5845
5946 /// Given syntax node position, returns the current switch case and it's fallthrough destination.
60- func lookupFallthroughSourceAndDestination( at syntax: SyntaxProtocol ) -> ( SwitchCaseSyntax ? , SwitchCaseSyntax ? ) {
47+ private func lookupFallthroughSourceAndDestination( at syntax: SyntaxProtocol )
48+ -> ( SwitchCaseSyntax ? , SwitchCaseSyntax ? )
49+ {
6150 guard
6251 let originalSwitchCase = walkParentTreeUpToFunctionBoundary (
6352 at: Syntax ( syntax) ,
@@ -93,11 +82,6 @@ extension Scope {
9382
9483 // MARK: - lookupCatchNode
9584
96- /// Given syntax node position, returns the closest ancestor catch node.
97- func lookupCatchNode( at syntax: Syntax ) -> Syntax ? {
98- return lookupCatchNodeHelper ( at: syntax, traversedCatchClause: false )
99- }
100-
10185 /// Given syntax node location, finds where an error could be caught. If set to `true`, `traverseCatchClause`lookup will skip the next do statement.
10286 private func lookupCatchNodeHelper( at syntax: Syntax ? , traversedCatchClause: Bool ) -> Syntax ? {
10387 guard let syntax else { return nil }
@@ -117,23 +101,30 @@ extension Scope {
117101 } else {
118102 return lookupCatchNodeHelper ( at: syntax. parent, traversedCatchClause: traversedCatchClause)
119103 }
120- case . functionDecl, . accessorDecl, . initializerDecl:
104+ case . functionDecl, . accessorDecl, . initializerDecl, . deinitializerDecl , . closureExpr :
121105 return syntax
106+ case . exprList( let exprList) :
107+ if let tryExpr = exprList. first? . as ( TryExprSyntax . self) , tryExpr. questionOrExclamationMark != nil {
108+ return Syntax ( tryExpr)
109+ }
110+ return lookupCatchNodeHelper ( at: syntax. parent, traversedCatchClause: traversedCatchClause)
122111 default :
123112 return lookupCatchNodeHelper ( at: syntax. parent, traversedCatchClause: traversedCatchClause)
124113 }
125114 }
126115
116+ // MARK: - walkParentTree helper methods
117+
127118 /// Callect the first syntax node matching the collection type up to a function boundary.
128- func walkParentTreeUpToFunctionBoundary< T: SyntaxProtocol > (
119+ private func walkParentTreeUpToFunctionBoundary< T: SyntaxProtocol > (
129120 at syntax: Syntax ? ,
130121 collect: T . Type
131122 ) -> T ? {
132123 walkParentTreeUpToFunctionBoundary ( at: syntax, collect: collect, stopWithFirstMatch: true ) . first
133124 }
134125
135126 /// Callect syntax nodes matching the collection type up to a function boundary.
136- func walkParentTreeUpToFunctionBoundary< T: SyntaxProtocol > (
127+ private func walkParentTreeUpToFunctionBoundary< T: SyntaxProtocol > (
137128 at syntax: Syntax ? ,
138129 collect: T . Type ,
139130 stopWithFirstMatch: Bool = false
@@ -154,7 +145,7 @@ extension Scope {
154145 }
155146
156147 /// Callect syntax nodes matching the collection type up until encountering one of the specified syntax nodes.
157- func walkParentTree< T: SyntaxProtocol > (
148+ private func walkParentTree< T: SyntaxProtocol > (
158149 upTo stopAt: [ SyntaxProtocol . Type ] ,
159150 at syntax: Syntax ? ,
160151 collect: T . Type ,
0 commit comments