-
Notifications
You must be signed in to change notification settings - Fork 440
[SwiftLexicalScopes][GSoC] Add simple scope queries and initial name lookup API structure #2696
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9f2fab0
Add SwiftLexicalScopes library
MAJKFL d4fb767
Move `ancestorOrSelf` to SwiftSyntax module
MAJKFL 211cb6f
Add suggested changes
MAJKFL a00c922
Limit access of LiexicalScope lookup methods
MAJKFL db7fd99
Simplify `lookupLabeledStmtsHelper`.
MAJKFL 74ddcc4
Extract `walkParentTree` helper method. Adjust query methods.
MAJKFL b85dced
Remove name lookup method placeholder and test harness. Add missing c…
MAJKFL 8dddaa3
Remove `Scope` related code. Rename `SwiftLexicalScopes` to `SwiftLei…
MAJKFL 0dc27c9
Add documentation. Move `lookupFallthroughSourceAndDest` to `FallThro…
MAJKFL 0059f2f
Merge branch 'main' into main
MAJKFL 08297ae
Formatting
MAJKFL b4a9b3f
Add `SwiftLexicalLookup` to `.spi.yml` file.
MAJKFL 67e2608
Remove superfluous code. Add an entry to the release notes. Simplify …
MAJKFL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,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() | ||||||
DougGregor marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
-> (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 | ||||||
) | ||||||
} | ||||||
MAJKFL marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
// 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. | ||||||
MAJKFL marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
private func lookupCatchNodeHelper(at syntax: Syntax?, traversedCatchClause: Bool) -> Syntax? { | ||||||
MAJKFL marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
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?, | ||||||
ahoppen marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
collect: T.Type, | ||||||
stopWithFirstMatch: Bool = false | ||||||
) -> [T] { | ||||||
walkParentTree( | ||||||
upTo: [ | ||||||
MemberBlockSyntax.self, | ||||||
FunctionDeclSyntax.self, | ||||||
InitializerDeclSyntax.self, | ||||||
DeinitializerDeclSyntax.self, | ||||||
AccessorDeclSyntax.self, | ||||||
ClosureExprSyntax.self, | ||||||
MAJKFL marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
], | ||||||
at: syntax, | ||||||
collect: collect, | ||||||
stopWithFirstMatch: stopWithFirstMatch | ||||||
) | ||||||
} | ||||||
|
||||||
/// Callect syntax nodes matching the collection type up until encountering one of the specified syntax nodes. | ||||||
MAJKFL marked this conversation as resolved.
Show resolved
Hide resolved
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.
Suggested change
|
||||||
private func walkParentTree<T: SyntaxProtocol>( | ||||||
upTo stopAt: [SyntaxProtocol.Type], | ||||||
at syntax: Syntax?, | ||||||
DougGregor marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
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 | ||||||
) | ||||||
DougGregor marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} | ||||||
} else { | ||||||
return walkParentTree( | ||||||
upTo: stopAt, | ||||||
at: syntax.parent, | ||||||
collect: collect, | ||||||
stopWithFirstMatch: stopWithFirstMatch | ||||||
) | ||||||
} | ||||||
} | ||||||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
@_spi(Testing) import SwiftLexicalLookup | ||
import SwiftParser | ||
import SwiftSyntax | ||
import XCTest | ||
import _SwiftSyntaxTestSupport | ||
|
||
/// Parse `source` and check if the method passed as `methodUnderTest` produces the same results as indicated in `expected`. | ||
/// | ||
/// The `methodUnderTest` provides test inputs taken from the `expected` dictionary. The closure should return result produced by the tested method as an array with the same ordering. | ||
/// | ||
/// - Parameters: | ||
/// - methodUnderTest: Closure with the tested method. Provides test argument from `expected` to the tested function. Should return method result as an array. | ||
/// - expected: A dictionary with parameter markers as keys and expected results as marker arrays ordered as returned by the test method. | ||
MAJKFL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func assertLexicalScopeQuery( | ||
source: String, | ||
methodUnderTest: (SyntaxProtocol) -> ([SyntaxProtocol?]), | ||
MAJKFL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expected: [String: [String?]] | ||
) { | ||
// Extract markers | ||
let (markerDict, textWithoutMarkers) = extractMarkers(source) | ||
|
||
// Parse the test source | ||
var parser = Parser(textWithoutMarkers) | ||
let sourceFileSyntax = SourceFileSyntax.parse(from: &parser) | ||
|
||
// Iterate through the expected results | ||
for (marker, expectedMarkers) in expected { | ||
// Extract a test argument | ||
guard let position = markerDict[marker], | ||
let testArgument = sourceFileSyntax.token(at: AbsolutePosition(utf8Offset: position)) | ||
else { | ||
XCTFail("Could not find token at location \(marker)") | ||
continue | ||
} | ||
|
||
// Execute the tested method | ||
let result = methodUnderTest(testArgument) | ||
|
||
// Extract the expected results for the test argument | ||
let expectedValues: [SyntaxProtocol?] = expectedMarkers.map { expectedMarker in | ||
guard let expectedMarker else { return nil } | ||
|
||
guard let expectedPosition = markerDict[expectedMarker], | ||
let expectedToken = sourceFileSyntax.token(at: AbsolutePosition(utf8Offset: expectedPosition)) | ||
MAJKFL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else { | ||
XCTFail("Could not find token at location \(marker)") | ||
return nil | ||
} | ||
|
||
return expectedToken | ||
} | ||
|
||
// Compare number of actual results to the number of expected results | ||
if result.count != expectedValues.count { | ||
XCTFail( | ||
"For marker \(marker), actual number of elements: \(result.count) doesn't match the expected: \(expectedValues.count)" | ||
) | ||
} | ||
|
||
// Assert validity of the output | ||
for (actual, expected) in zip(result, expectedValues) { | ||
if actual == nil && expected == nil { continue } | ||
|
||
XCTAssert( | ||
actual?.firstToken(viewMode: .sourceAccurate)?.id == expected?.id, | ||
"For marker \(marker), actual result: \(actual?.firstToken(viewMode: .sourceAccurate) ?? "nil") doesn't match expected value: \(expected?.firstToken(viewMode: .sourceAccurate) ?? "nil")" | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.