Skip to content
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

[SR-6902] libSyntax visitors need another entry point #49451

Closed
dabrahams opened this issue Feb 2, 2018 · 2 comments
Closed

[SR-6902] libSyntax visitors need another entry point #49451

dabrahams opened this issue Feb 2, 2018 · 2 comments
Assignees
Labels
feature A feature request or implementation † libswiftSyntax † Area → compiler: the once-integrated C++ Syntax library succeeded by SwiftSyntax source tooling Area: IDE support, SourceKit, and other source tooling

Comments

@dabrahams
Copy link
Collaborator

dabrahams commented Feb 2, 2018

Previous ID SR-6902
Radar None
Original Reporter @dabrahams
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Source Tooling
Labels Bug, Syntax
Assignee @nkcsgexi
Priority Medium

md5: b04ec2903e11276fa4ebf4b2335e6c9c

Issue Description:

I want to process tokens and keep track of the trail of nodes to the root of the parse tree. I end up with something like this:

typealias Node = Syntax.Type

final class Rewriter : SyntaxRewriter {
    var tokens: [(String, ancestors: [Node])] = []
    var ancestors: [Node] = []

    override func visit(_ token: TokenSyntax) -> Syntax {
         tokens.append((token.text, ancestors: ancestors))
         return super.visit(token)
    }

    override func visit(_ node: SwiftSyntax.UnknownDeclSyntax) -> DeclSyntax {
        ancestors.append(type(of: node))
        let r = super.visit(node)
        ancestors.removeLast()
        return r
    }

    override func visit(_ node: SwiftSyntax.UnknownExprSyntax) -> ExprSyntax {
        ancestors.append(type(of: node))
        let r = super.visit(node)
        ancestors.removeLast()
        return r
    }

    … ETC FOR EVERY OTHER SYNTAX NODE…
}

while this is doable, the boilerplate repetition is regrettable to say the least. There should be an entry point that allows me to do it with a single override.

@nkcsgexi
Copy link
Member

nkcsgexi commented Feb 2, 2018

Added preVisit and postVisit open functions in #14365 . We can override this function to specify generalized visiting logic.

@nkcsgexi
Copy link
Member

nkcsgexi commented Feb 3, 2018

Merged the above PR. @dabrahams please let me know if the preVisit and postVisit functions are sufficient for your scenario.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added source tooling Area: IDE support, SourceKit, and other source tooling feature A feature request or implementation and removed bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. labels Apr 5, 2023
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A feature request or implementation † libswiftSyntax † Area → compiler: the once-integrated C++ Syntax library succeeded by SwiftSyntax source tooling Area: IDE support, SourceKit, and other source tooling
Projects
None yet
Development

No branches or pull requests

3 participants