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

Implement Solidity AST printer #2165

Closed
axic opened this issue Apr 25, 2017 · 15 comments
Closed

Implement Solidity AST printer #2165

axic opened this issue Apr 25, 2017 · 15 comments
Labels
stale The issue/PR was marked as stale because it has been open for too long.

Comments

@axic
Copy link
Member

axic commented Apr 25, 2017

This would take the internal AST structure and return a "Solidity source code output" which can be parsed by the parser.

This can also be used by the "import AST from JSON" feature (see #2096) to create the hash of the sourcecode that is part of the onchain metadata.

@chriseth
Copy link
Contributor

chriseth commented Oct 6, 2017

What about just including the hash of the source code into the AST?

@axic axic added feature and removed enhancement labels Oct 6, 2017
@yxliang01
Copy link

yxliang01 commented Jul 12, 2018

May I know how this feature is going? I think this would be useful. I realize that there are some source code files look related.

@yxliang01
Copy link

I think this is implemented already. The AST printer can be used via CLI argument like --ast-json.

@chriseth
Copy link
Contributor

@axic could you clarify the meaning of this issue, please?

@axic
Copy link
Member Author

axic commented Jul 28, 2018

It says it in the description:

To print parseable Solidity source code.

@yxliang01
Copy link

@axic Did you mean being able to convert AST to Solidity source code?

@axic
Copy link
Member Author

axic commented Jan 12, 2019

Did you mean being able to convert AST to Solidity source code?

Yes.

@tharindu1992
Copy link

Any update here? I think this is very useful.

@tharindu1992
Copy link

@axic Did you mean being able to convert AST to Solidity source code?

Any updates? I am trying to do this.

@cameel
Copy link
Member

cameel commented Feb 18, 2022

Reposting from #5802 (which has been closed as a duplicate):

@ryanfalzon

Has there been any updates on this? I need to make use of this feature.

@cameel

No, the compiler can parse an AST from JSON but currently does not offer a feature to synthesize code back from that. There are external tools that can do that though. Check out solc-typed-ast.

@cameel
Copy link
Member

cameel commented Feb 18, 2022

@axic @chriseth Given that there are now tools that can do this, are we still planning to implement this?

Also pinging @blitz-1306, who might be interested in this topic.

@blitz-1306
Copy link

blitz-1306 commented Feb 22, 2022

Hello everyone. As I've been referenced here, I might share a few thoughts how this is done in solc-typed-ast. The most of the logic is concentrated in following directory: https://github.com/ConsenSys/solc-typed-ast/tree/master/src/ast/writing (mostly in ast_mapping.ts and yul_mapping.ts).

Writing AST to source code is mostly trivial task, aside from few considerations:

  • Version compatibility - produced source code may differ for various compiler versions. We slightly (not completely) compensate that by allowing to pass target compiler version and checking if we need to adjust output to fit into requirements in the most cases.
  • Source mapping - for some of dependent packages like Scribble, there is a need to map produced source code fragments back to AST nodes. We produce complete map node -> string, but I guess there should be a way to have a workaround with nodeId -> string (as this is still mappable back and node ids are unique within the tree).
  • Indentation / Whitespace - AST representation removes any information about whitespace and indentation. First minimal thing is to separate identifiers by at least a single whitespace. For better readability we introduced simplified formatting components that allows to regulate indentation. It may also worth to postpone such functionality to linters.

For us, AST-to-source conversion been beneficial for the following reasons:

Ofc, I do not think that all of the points above are related to the current scope of "just writing source from an AST". My intention here is to share some experience and ideas, also indicate some caveats of the feature alongside with possible benefits.

Regards.

@blitz-1306
Copy link

Pinging @cd1m0, who is actively developing Scribble at the moment (in case if there would be any additional input).

@github-actions
Copy link

This issue has been marked as stale due to inactivity for the last 90 days.
It will be automatically closed in 7 days.

@github-actions github-actions bot added the stale The issue/PR was marked as stale because it has been open for too long. label Feb 12, 2023
@ekpyron
Copy link
Member

ekpyron commented Feb 13, 2023

Just seeing the stale notification here and will even manually close this, since we won't be doing this ourselves - however, in case this pops up for anyone, I recently had a call with Certora about them having released an open source tool that does just this even with potential AST mutations. Without spending much time on looking it up, this may have been it: https://github.com/Certora/gambit or a related repo there.

@ekpyron ekpyron closed this as completed Feb 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale The issue/PR was marked as stale because it has been open for too long.
Projects
None yet
Development

No branches or pull requests

8 participants