-
Notifications
You must be signed in to change notification settings - Fork 209
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
Add AST Visitor #1975
Add AST Visitor #1975
Conversation
-1. This visitor is a bloat to RBS’s infrastructure.
An (not necessarily the) idiomatic design for Ruby is the simple enumerator. # https://github.com/ruby/rbs/blob/v3.5.2/sig/parser.rbs#L67-L69
_buffer, _directives, declarations = RBS::Parser.parse_signature RBS_STRING
declarations.each do|node|
case node
when RBS::AST::Declarations::Class
puts "Visiting class: #{node.name}"
# node.each_member / each_decl …
# …
end
end Not to mention that Ruby doesn’t have abstract classes.
|
I don't think providing a visitor is a bad idea, while I understand technically tree walking can be easily implemented with the pattern matching and recursive calls. I know there are some features that can be easily implemented on the top of visitor, without boring pattern matching enumeration. One concern I have is requiring |
This means the visitor class itself will be the boring pattern matching enumeration. |
Co-authored-by: Alexandre Terrasa <alexandre.terrasa@shopify.com> Co-authored-by: Ufuk Kayserilioglu <ufuk.kayserilioglu@shopify.com>
As shown by your own example, the simple enumerator doesn't absolve you to write the burdensome visit logic hidden in your The goal of the visitor is exactly to not have you write this logic yourself and focus on what you want to do with the nodes.
Thank you for being so pedantic. By abstract I meant the base visitor doesn't provide anything more than the visit logic, it's up to the subclass to do something with each node. This pattern tremendously simplifies the implementation of any tree walker and keeps things nicely organized in classes and methods rather than a long
Good idea, I moved the logic in |
Fair. Not all tree walkers care about every class and method, but most probably do. |
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.
Thanks! 🎉
The
Visitor
class implements the Visitor pattern for traversing the RBS Abstract Syntax Tree (AST).It provides methods to visit each type of node in the AST, allowing for custom processing of each node type.
This class is designed to be subclassed, with specific visit methods overridden to implement custom behavior for different node types.
Example usage:
I needed something like this and couldn't find anything in the repo other than the
EnvironmentWalker
that is way to specialized for my purpose.