solidity-parser-antlr
solidity-parser-antlr copied to clipboard
[Question] automatically generating typing from concrete output or the test cases?
This issue is to discuss the possibility of having the typing for the output of the parser automatically generated. Currently, the typing file at https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/solidity-parser-antlr/index.d.ts is more or less manually written and is very incomplete. Since the output is very structured, I feel like this is doable. However, I haven't figured out a way yet. Any thoughts?
Hi @yxliang01, thanks for bringing this up! I don't see a simple way of generating that sort of type information automatically, but I am more than willing to bring the type definitions into this repo so that they can be updated with less bureaucracy and kept up-to-date with any implementation changes.
Could you open a pull request to begin that effort?
@federicobond Sure. It sounds good. I will do it tomorrow. But, since you are aware of this typing thing going on, would you think it's a good idea to bring the typescript tool chain to this project and we simply annotate the types incrementally? This way might some make this library even less error-prone and we sometimes might get benefit from the typescript compiler's type inference. What do you think?
I think it's a good idea but I would like to do it in two phases. First, bring the type definitions as an independent file, then consider moving the codebase to TypeScript.
@federicobond Please see https://github.com/federicobond/solidity-parser-antlr/issues/48 and the created PR as the begin of this effort.
I had a deeper look at the ANTLR grammar and feel like it doesn't really define the structural constraint of the Solidity language. So, it indeed doesn't look like we can just "convert" the grammar to TS types. But, now I see a possibility that we might be able to convert from the quite complete test at https://raw.githubusercontent.com/federicobond/solidity-parser-antlr/master/test/ast.js (Thanks for the good testing :) ). Changing the issue title now.
@LogvinovLeon Would you like to describe how you created these types previously? Was it automatically generated?
Were you simply converting from https://github.com/federicobond/solidity-parser-antlr/blob/5cbebf9ee879500bc76db52fde77c33c37a266d0/src/lib/SolidityParser.js#L689 ? Since I see some of the types I can't see the output statements of them in this library codebase (e.g. Expression
, SimpleStatement
) and looks like they are the intermediate types returned by the ANTLR parser. Correct me if wrong.
I did parse a rather complex contract and iterated over the generated AST to generate those. Didn't define all the properties there as I didn't need them at the time for my use-case.
Since I see some of the types I can't see the output statements of them in this library codebase (e.g.
Expression
,SimpleStatement
) and looks like they are the intermediate types returned by the ANTLR parser. Correct me if wrong.
@yxliang01 that's correct. ANTLR outputs a parse tree (which is a version of a concrete syntax tree). Most of the code in this lib is dedicated to transforming that parse tree into an AST.
@federicobond Are you also confirming that Expression
and SimpleStatement
are not part of the output AST of this library? If so, I will fix it soon.
I think so, yes.