kotlinx / ast

Generic AST parsing library for kotlin multiplatform
Apache License 2.0
317 stars 22 forks source link

Running at command line or as gradle task #44

Open saintjab opened 3 years ago

saintjab commented 3 years ago

Hi, thanks for this tool. This is really a question and not an issue as am not conversant with ASTs. Is this something that can be run from the command line or as gradle task? If possible (gradle better), could you give me some pointers? At the moment am running this as a unit test and printing the output to a file to be checked later. Ideally, I want to create a gradle task pointing to a dir to load all files in there and print this AST output to a file for analysis later on.

drieks commented 3 years ago

Hi @saintjab,

the unittest for kotlinx.ast are implemented in this way, a normal unittest that will read a file, parse the ast and then it will dump it.

You can find this test files here: https://github.com/kotlinx/ast/tree/master/grammar-kotlin-parser-test/src/commonMain/resources/testdata

.kt.txt is the given test data .raw.ast.txt the the raw ast as returned by antlr .raw.info.txt contains line numbers .summary.ast.txt the ast after calling summary *.summary.info.txt line numbers after calling sumary

The test is implemented here: https://github.com/kotlinx/ast/blob/master/grammar-kotlin-parser-antlr-java/src/test/kotlin/kotlinx/ast/grammar/kotlin/target/antlr/java/test/KotlinGrammarAntlrJavaParserTestDataTest.kt But you have to look into the base classes if you want to see how it is working. Different antlr version are tested, so there is one test per parser (antlr java, antlr kotlin, antlr optimized) and grammar (for example the kotlin parser)

It is possible to used it from gradle, but you have to implement everything ourselves. I'm using a kotlin main function to call my private code parser project from the command line. I think this is the easiest way.