groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From yxudvzfiu <>
Subject Parse Syntax-incorrect Groovy Code to AST (withoud syntax error thrown)
Date Thu, 29 Sep 2016 14:48:44 GMT

I try to build a very simple code-completion for an editor. To achieve this:

I would like to parse syntax-*incorrect* groovy code into an AST.* Is there
an easy way do this?*
It would be perfectly fine to get a "syntactic-incorrect" AST (since the
code has syntax-errors), but sth. looking like an AST would be great.

I know that i can parse groovy code very simple by:
AstBuilder astBuilder = new AstBuilder();
List<ASTNode> astNodes =
but if there is a syntax-error in the groovySourceCode, i just get a
null-pointer in the astNodes (and a syntax-error exception).

I looked also into the groovy-eclipse project, but i honestly did not find
out how they handle this issue (they use AST nodes in the classes who
compute the suggestions, so i expect they somehow managed to parse the
groovy code into an AST even if the groovy code has syntax errors - or they
solve it totally different?)

Thanks for your answers/suggestions :)

I also tried to go deeper and set the Tolerance higher, but this didn't help
( i also didn't expect it to help ;))

String scriptClassName = "script" + System.currentTimeMillis();
groovy.lang.GroovyClassLoader classLoader = new
groovy.lang.GroovyCodeSource codeSource = new
		scriptClassName + ".groovy", "/groovy/script");

CompilerConfiguration config = CompilerConfiguration.DEFAULT;
CompilationUnit cu = new CompilationUnit(config, null, classLoader);
cu.addSource(codeSource.getName(), groovySourceCode);

View this message in context:
Sent from the Groovy Users mailing list archive at

View raw message