-
Notifications
You must be signed in to change notification settings - Fork 473
🚥 Introduce a new Lexical query lookupControlStructure to the SwiftLexicalLookup Library
#3216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,43 @@ | |
| import SwiftSyntax | ||
|
|
||
| extension SyntaxProtocol { | ||
|
|
||
| @_spi(Experimental) public func lookupControlStructure() -> Syntax? { | ||
| let syntax = Syntax(self) | ||
|
|
||
| switch syntax.as(SyntaxEnum.self) { | ||
| case .throwStmt, .returnStmt: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The target of
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, |
||
| return lookupEnclosingControlStructure(for: [ | ||
| FunctionDeclSyntax.self, | ||
| InitializerDeclSyntax.self, | ||
| DeinitializerDeclSyntax.self, | ||
| AccessorDeclSyntax.self, | ||
| ClosureExprSyntax.self, | ||
| ]) | ||
| case .breakStmt, .continueStmt: | ||
| return lookupEnclosingControlStructure(for: [ | ||
|
Comment on lines
+29
to
+30
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, OUTER: do {
for val in values {
if val.condition {
break OUTER
}
}
}In the actual compiler, the rule is implemented in |
||
| WhileStmtSyntax.self, | ||
| RepeatStmtSyntax.self, | ||
| ForStmtSyntax.self, | ||
| ]) | ||
| default: | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| private func lookupEnclosingControlStructure(for types: [SyntaxProtocol.Type]) -> Syntax? { | ||
| var nextSyntax: Syntax? = Syntax(self).parent | ||
|
|
||
| while let currentSyntax = nextSyntax { | ||
| if types.contains(where: { currentSyntax.is($0) }) { | ||
| return currentSyntax | ||
| } | ||
| nextSyntax = currentSyntax.parent | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| /// Returns all labeled statements available at a particular syntax node. | ||
| /// | ||
| /// - Returns: Available labeled statements at a particular syntax node | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't feel comfortable with having this method on
SyntaxProtocol.Instead, how about making
And make
break,continue,return,throw, andfallthroughconform to it?