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

Inconsistant NodeType.simpleName() #8540

Open
andrewstein opened this issue Dec 20, 2024 · 5 comments
Open

Inconsistant NodeType.simpleName() #8540

andrewstein opened this issue Dec 20, 2024 · 5 comments

Comments

@andrewstein
Copy link

Environment Information

Provide at least:

  • JRuby version: 9.4.9.0
  • Operating system: All

Expected and Actual Behaviors

A call to node.getNodeType().simpleName() should be consistent. For nodes of class BlockNode it returns "block" which is fine. However, nodes of class RestArgNode it return "res".

Looking at the implementation, it would seem that it strips the last four chars of the lower case of the enum NodeType.
Most NodeTypes end with "NODE", in our example BLOCKNODE. However, a couple of NodeTypes end with "ARG", in our example RESTARG, which leads to the truncated "res". (There is also a NodeType that ends with "VALUE" :-) ).

This can be fixed by adding "NODE" to all NodeTypes or by adding special handling in simpleName() of the few inconsistent NodeTypes.

@enebo
Copy link
Member

enebo commented Dec 20, 2024

@andrewstein it appears that simpleName is long dead code which was only added for SexpMaker (which no longer calls it). It is isolated enough that I will remove this method for JRuby 10 (we generally do not consider org.jruby.ast to be a public API space -- although if you have a need then let me know).

@andrewstein
Copy link
Author

Thank you @enebo for your reply.

My goal is to dump the AST out to a file. I cannot use the Node.toString(true, 0) for this since it is very incomplete. For example, for a StrNode it does not show the actual string. So I am rolling out my own. (If there is something already available I am not aware of it. I am aware of the ruby_parser gem and would be happy to use a Java equivalent if available.)

Node.toString(true, 0) uses a method node.getNodeName() which is protected, so I chose node.getNodeType().simpleName() instead. I guess I can roll my own equivalent of node.getNodeName() which is a simple mangling of the class name.

@enebo
Copy link
Member

enebo commented Dec 20, 2024

@andrewstein This may not help you since you are probably using Java only but we have bin/ast which does print out the AST but it is a mixture or Ruby and Java. It also probably does not print what you are looking for (it is largely something for us to see structure). You can look with jruby -S ast -e foo. It might be helpful for you to figure out how to do this using Java though.

I will also point out JRuby 10 will work with both the legacy parser (which you are looking at) and the prism parser (a cross-Ruby-implementation) which effectively is a foreign function callout to a C library which produces a blob we can turn into an AST. The AST it makes is not the same as the legacy one. That parser is fairly well documented though and it can also do syntax-level AST info. We do have some experimental bindings to call it through Chicory using web assembly so there is a pure-Java path...just not completed yet.

That is a lot of digest but I am not sure how long and for what you need this .

@enebo
Copy link
Member

enebo commented Dec 20, 2024

@andrewstein you can pop onto our matrix channel to chat too if you want: https://app.element.io/?updated=1.11.69#/room/#jruby:matrix.org

@andrewstein
Copy link
Author

Thank you @enebo I will have to look into all of this in two weeks time. Happy holidays :-)

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

No branches or pull requests

2 participants