inab / python-groovy-parser

Repo with a proof of concept of Groovy parser
Apache License 2.0
0 stars 0 forks source link

Minimal example? #2

Open Regenhardt opened 10 months ago

Regenhardt commented 10 months ago

Is there a minimal example of how to read a groovy file and maybe just print out the first class or method in it or something? I find it very complicated to look at the test programs provided in the readme.

jmfernandez commented 9 months ago

Hi, @Regenhardt . It is true that it is a bit difficult to capture some elements, due the Groovy grammar is quite open when you are expressing several concepts. So, this library can be considered "low level", as it provides the properly parsed syntactic tree, but not the abstracted concepts like process or class declarations.

As my main motivation to write this library was to properly capture features from Nextflow workflows (they are written in Groovy), the example I can provide is next one https://github.com/inab/WfExS-backend/blob/main/wfexs_backend/utils/groovy_parsing.py , which is an evolution from the example nested in this repository. There you can see I have had to consider several possible cases in order to properly capture the declarations, as a "process" declaration in Nextflow is really calling a method called "process" with several parameters, in order to register it in the Nextflow core.

So, if you want to capture a class declaration, you have to have a look first at the grammar, https://github.com/inab/python-groovy-parser/blob/19cad563f2b3dd3bede85d5883d9810920fd6e81/groovy_parser/GROOVY_3_0_X/master_groovy_parser.g#L69 . So, you need to find a RuleNode whose first element is a LeafNode with "leaf" == "KEYWORD" and "value" == "class" .

Next week I'm trying to put an agnostic example here.