NagyI
NagyI

Reputation: 5997

Xtext runtime compilation

I'm trying to understand Xtext and to find out if it fits my needs. I'm looking for a tool which allows me to create a DSL which can be compiled and evaluated in my application during execution.

Xtext seems like a really good tool because it provides many things for this (grammar editor, IDE integration, parser generator, etc). As i understood Xtext can generate the parser and the IDE extension for me but if i need a compiler i have to either write it by myself or create the Java mapping which results in generated Java source files i can compile.

What i don't see is if it's possible to embed Xtext's parser and generator into my application and compile scripts written in my DSL in runtime without precompiling them. The goal is to ship scripts with my application making post-release changes possible.

Upvotes: 0

Views: 597

Answers (1)

Sven Efftinge
Sven Efftinge

Reputation: 3095

Yes, this is totally possible and actually quite easy. All the core components, i.e. non-IDE features, are independent of Eclipse. There's example Java code in the documentation showing how to do this. It's basically a matter or

// creating the injector
Injector injector = new MyLanguageStandaloneSetup().createInjectorAndDoEMFRegistration()

// obtain a resource set
XtextResourceSet resourceSet = injector.get(XtextResourceSet.class);

// load file
Resource resource = resourceSet.getResource(URI.create("path/to/file.mylanguage"), true);

// obtain root AST element
MyModel model = (MyModel) resource.getContents().get(0);

Upvotes: 2

Related Questions