You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by yxudvzfiu <lu...@ethz.ch> on 2016/09/29 14:48:44 UTC

Parse Syntax-incorrect Groovy Code to AST (withoud syntax error thrown)

Hello

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 =
astBuilder.buildFromString(/*CompilePhase.CONVERSION,*/groovySourceCode);
----------------------------------------------------
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 :)
________________________________________
Note:

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.GroovyClassLoader();
groovy.lang.GroovyCodeSource codeSource = new
groovy.lang.GroovyCodeSource(groovySourceCode,
		scriptClassName + ".groovy", "/groovy/script");

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




--
View this message in context: http://groovy.329449.n5.nabble.com/Parse-Syntax-incorrect-Groovy-Code-to-AST-withoud-syntax-error-thrown-tp5735669.html
Sent from the Groovy Users mailing list archive at Nabble.com.