Thursday, August 6, 2009

Setting up guice for xtext/TMF and parsing rules for validation

I am by no means a Guice expert, but here is a small code snippet which works for me, to setup TMF/xtext with guice. The main method may contain simple @Inject annotations to get your correct implementations injected.

public static void main(String[] args) {
BausteineStandaloneSetup setup = new BausteineStandaloneSetup();
Injector injector = setup.createInjector();
Main main = injector.getInstance(Main.class);

I currently use this to preparse arbitrary files which are not in my DSL and transform them. I do not want to rewrite particular checks for Rules, e.g. to verify if an ID is in the correct format, hence I just get the Parser injected and invoke the rule checking with a StringInputStream.
This looks roughly like this:

private IAntlrParser parser;
private BausteineGrammarAccess access;
InputStream inputStream = new StringInputStream("baustein " + name);
IParseResult result = parser.parse(access.getBausteinDefRule().getName(),inputStream);

There might be a more efficient way to do this, but currently I am quite happy with the solution. I guess I could try to use the lexer instead of the parser if I only want to check for terminals, but I need to extend the checks anyway.

1 comment:

  1. I just wanted to comment your blog and say that I really enjoyed reading your blog post here.
    It was very informative and I also digg the way you write!I also provide this service u can visit my site.

    software validation