solidity-parser-antlr icon indicating copy to clipboard operation
solidity-parser-antlr copied to clipboard

Returning original node from visitor

Open JisuPark opened this issue 6 years ago • 4 comments

First of all, thank you for the awesome project!

I suggest enhancing transformAST to return not only the parsed one but also the original one.

Similar to Javaparser project, if we want to support Transform and Generate the solidity code, returning original node is essential.

We could use the changed version to reconstruct specific granularity.

For example,

var source = "contract test { uint a; function c() public { uint f = 3; uint e = 1+4;}}"
var ast = parser.parse(source);
parser.visit(ast, {
  FunctionDefinition: (node) => {
    console.log(node.ctx.getText()); // "functionc()public{uintf=3;uinte=1+4;}"
  }
});

By the way, I'm still thinking about how to keep whitespace and newlines. Could you give me some advice? (It may be related to #32)

JisuPark avatar Aug 05 '18 22:08 JisuPark

@JisuPark hey! sorry about the late response.

I'm not sure what you mean by the original code, but I am wary of exposing such implementation details to the public interface. I believe adding a Concrete Syntax Tree modeled after the JS one should be the way to go here, but any feedback is appreciated.

federicobond avatar Aug 13 '18 00:08 federicobond

@federicobond Thanks for the reply!

Now I see your point. I thought current node arguments are too solid to develop a variety of visitor.

When I wanted to extract only function declaration in the source, it was hard because I had to reassemble from the argument node of the visitor. (Let me know if there is an easier way.)

So I suggested such tweak since it might help developers to create such visitor easier.

I think Concrete Syntax Tree can solve it easily. Let me know how I can help you to develop it.

Thanks

JisuPark avatar Aug 13 '18 07:08 JisuPark

@JisuPark can you provide a piece of code of what you would like to achieve with this library? Better if it is in the form of a pull request with a failing test case. Let’s forget about the implementation for now and understand the API.

federicobond avatar Aug 13 '18 13:08 federicobond

The getText method shown in the example looks pretty useful :)

yxliang01 avatar Nov 09 '19 20:11 yxliang01