14

I'm trying to build a parser for Solidity, and it would help if I could build a working version off of the existing parser. I've tried looking at the source code on the official git, but I haven't worked on a project of this magnitude before and it's confusing. Could some one tell me how I could isolate the parsing and AST modules so I can read a script and build an AST from it?

(I understand the scope might be a little too broad for this question, but I'd really appreciate any pointers)

hrishioa
  • 479
  • 3
  • 7

5 Answers5

10

If you use solc --ast, the compiler will output a json-representation of the AST (this is also included in the npm version of the compiler). Is that enough for your purposes? If not, please come to gitter to discuss.

q9f
  • 32,913
  • 47
  • 156
  • 395
chriseth
  • 3,392
  • 1
  • 20
  • 17
3

In addition to chriseth's answer, there's also the solidity-parser project which is useful if you A) are developing in a Javascript context, or B) don't want the parser to error if there are unresolved imports (unfortunately solc --ast will error if there are imports it cannot find).

Tim Coulter
  • 1,049
  • 1
  • 7
  • 9
2

In addition to Tim's answer there's also a python-solidity-parser based on the ANTLR syntax and AST format used in solidity-parser-antlr that you can use if you want to avoid depending on the compiler. Note: compiler AST is the truth.

tintin
  • 275
  • 3
  • 8
1

Use flag --ast-compact-json, which gives the "AST of all source files in a compact JSON format."

solc --ast-compact-json contract.sol