Monday, August 24, 2009

xText Standalone Setup Parsing a DSL from a String without EMF Resource

I have been playing with xText, a very interesting framework for development of textual domain specific languages (DSLs).

My interest is around an (existing, legacy) expression language, and twofold: a) build a strong Eclipse editor, b) build an interpreter for a non-Eclipse application runtime. XText is really great for the first. Here is how to use the xText generated parser in a "standalone" non-Eclipse application, based on their generated Google Guice setup infrastructure, without having to go through the EMF ECore Resource XtextResource:

String t = "...";
Injector guiceInjector = new MyDSLStandaloneSetup().createInjectorAndDoEMFRegistration();
IParser parser = guiceInjector.getInstance(IAntlrParser.class);
IParseResult result = parser.parse(new StringInputStream(t));
List<SyntaxError> errors = result.getParseErrors();
Assert.assertTrue(errors.size() == 0);
EObject eRoot = result.getRootASTElement();
MyDSLRoot root = (MyDSLRoot) eRoot;

Filed a minor item on https://bugs.eclipse.org/bugs/show_bug.cgi?id=287413 to may be have them include something like this in the official xText documentation.

PS: I am not entirely sure that StringInputStream properly always converts all (Unicode) characters into bytes for an InputStream? (But that's another topic... the whole StringBufferInputStream deprecated story, the StringReader alternative which I guess would require that xText can feed the the Antlr Lexer a Reader instead of an InputStream... could it? That org.eclipse.xtext.util.StringInputStream is just a String.getBytes(), which "uses the platform's default encoding", is that safe?)