Skip to content

[SwiftSyntax] Add SyntaxRewriter.visitAny(_:) #15212

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 3 commits into from
Mar 27, 2018

Conversation

harlanhaskins
Copy link
Contributor

This patch makes it so clients can override visit(_ node: Syntax) -> Syntax in SyntaxRewriter and SyntaxVisitor subclasses, if they choose to override the global visitation behavior.

@harlanhaskins harlanhaskins requested a review from nkcsgexi March 13, 2018 18:31
@harlanhaskins
Copy link
Contributor Author

@swift-ci please smoke test

@nkcsgexi
Copy link
Contributor

hmm, this will make visitPre and visitPost weird after clients rewriting visit(). Can we add another open function instead of making this one open?

@harlanhaskins
Copy link
Contributor Author

Ah, yes you're right - this would make it so visitPost didn't happen after visit(_:).

Something like open func visitAny(_ syntax: Syntax) -> Syntax?

@nkcsgexi
Copy link
Contributor

sounds good!

@harlanhaskins harlanhaskins force-pushed the open-sesame branch 2 times, most recently from 09db2e5 to e8b0bdb Compare March 13, 2018 19:34
@harlanhaskins
Copy link
Contributor Author

@swift-ci please smoke test


// Visit this node using the global visitor first
let newNode = visitAny(node)
switch newNode.raw.kind {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this two-phase transformation may complicate clients' codebase. Can we force users to choose either using visitAny or visit(_ node: SpecializedSyntax)? meaning if visitAny changed a node, we don't call visit(_ node: SpecializedSyntax) any more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense, I'll do that!

@harlanhaskins harlanhaskins force-pushed the open-sesame branch 2 times, most recently from dccecde to 91a27ea Compare March 13, 2018 20:06
// If the global visitor returned non-nil, visit the rewritten node's
// children and skip specialized dispatch.
if let newNode = visitAny(node) {
return visitChildren(newNode)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, sorry about being a little nit-picky here. Whether visitingChildren should be up to the overriding function, right? like other specialized visit functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yes you're right. Thanks for catching that!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The more I think about it, though, the more I wonder if we should always call visitChildren after the specialized visitor has been called.

For example, if I'm writing a transform that, say, lays out function call arguments a certain way, I shouldn't have to manually iterate over all subexpressions and visit them, no?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, i think it is a valid assumption that in most cases users will visit a node's children even though the node itself has been updated. @rintaro what do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, we should separate Visitor and Walker like ASTVisitor / ASTWalker.

@harlanhaskins harlanhaskins changed the title [SwiftSyntax] Make SyntaxRewriter and SyntaxVisitor visit(_:) methods overridable [SwiftSyntax] Add SyntaxRewriter.visitAny(_:) Mar 13, 2018
@nkcsgexi
Copy link
Contributor

@swift-ci please smoke test and merge

@nkcsgexi
Copy link
Contributor

@swift-ci please smoke test

2 similar comments
@harlanhaskins
Copy link
Contributor Author

@swift-ci please smoke test

@harlanhaskins
Copy link
Contributor Author

@swift-ci please smoke test

@harlanhaskins
Copy link
Contributor Author

@swift-ci please smoke test

@harlanhaskins
Copy link
Contributor Author

@swift-ci please smoke test

1 similar comment
@harlanhaskins
Copy link
Contributor Author

@swift-ci please smoke test

@harlanhaskins
Copy link
Contributor Author

@swift-ci please smoke test

@harlanhaskins harlanhaskins merged commit bdfa6cd into swiftlang:master Mar 27, 2018
@harlanhaskins harlanhaskins deleted the open-sesame branch March 27, 2018 14:55
maldahleh pushed a commit to maldahleh/swift that referenced this pull request Oct 26, 2020
* [SwiftSyntax] Add SyntaxRewriter.visitAny(_:)

This function, when overridden, allows Syntax rewriters to perform custom dynamic visitation behavior. If a user wanted to, say, store a series of transformations accessible by metatype, they can override visitAny and do their own runtime dispatch.

If a non-nil result is returned from visitAny, the original specialized visitors are skipped.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants