Skip to main content
Jump to: navigation, search



How do I load my model in a standalone Java application ?

Assuming you have a grammar Foo.xtext the Java code to load a corresponding model from a resource should look something like this:

Injector injector = new FooStandaloneSetup().createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
Resource resource = resourceSet.getResource(URI.createURI("platform:/resource/myproject/"), true);
Foo foo = (Foo) resource.getContents().get(0);

To load a model from a String or an InputStream replace the last two lines with:

Resource resource = resourceSet.createResource(URI.createURI("platform:/resource/myproject/"));
InputStream in = new ByteArrayInputStream("foo model goes here".getBytes());
resource.load(in, resourceSet.getLoadOptions());
Foo foo = (Foo) resource.getContents().get(0);

Workflow / Generator

Why do I get warnings like "warning(200): InternalFoo.g:42:3: Decision can match ..." when running the generator ?

Here's an example of the full error message:

warning(200): InternalFoo.g:42:3: Decision can match input such as "FOO" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input

These warnings are generated by the ANTLR code generator. Based on the rules in your grammar the ANTLR generated parser cannot disambiguate what rules to apply for a given input. You should try to refactor your grammar (see example) or you can enable backtracking for your parser (see next question).

OK, but I didn't get these warnings in oAW Xtext !

Unlike in oAW Xtext the ANTLR grammar generated by TMF Xtext doesn't have backtracking enabled by default. To enable backtracking you have to add a nested element <options backtrack="true"/> to the ANTLR generator fragments in your Xtext project's MWE workflow. So replace:

<!-- Antlr Generator fragment -->
<fragment class="org.eclipse.xtext.generator.AntlrDelegatingFragment"/>


<!-- Antlr Generator fragment -->
<fragment class="de.itemis.xtext.antlr.XtextAntlrGeneratorFragment">
   <options backtrack="true"/>

Further down in the workflow you will find another use of the AntlrDelegatingFragment fragment (used for content assist) you have to replace with:

<fragment class="de.itemis.xtext.antlr.XtextAntlrUiGeneratorFragment">
   <options backtrack="true"/>

Note that you can in both cases also specify memoize="true" as an additional option.

Why are generated packages from an imported grammar A duplicated in dependent grammar B ?

In addition to the import statement in B.xtext you must also configure your GenerateB.mwe workflow to let it know about the corresponding GenModels of grammar A. You do this by setting the genModels attribute of the EcoreGeneratorFragment:

   <fragment class="org.eclipse.xtext.generator.ecore.EcoreGeneratorFragment"

How can I control the Xtext meta model inference ?

The typical use case is to let Xtext automatically infer a meta model corresponding to the Xtext grammar. Quite often this meta model is exactly what you want. If you on the other hand want to make some small changes to the inferred meta model (e.g. set attribute default values, add operations, features, or enumeration literals, etc.) you must implement a post processor. Please refer to the relevant documentation and the following example for more details.

The following example shows how to add an enumeration literal (here NULL) to an enumeration (here VisibilityModifier) and sets it as the default value for all attributes of that type.

import ecore;
process(xtext::GeneratedMetamodel this) :
process(EPackage this) :
process(EClassifier this) :
process(EClass this) :
process(EEnum this) :
     if name == 'VisibilityModifier' then eLiterals.add(newLiteral('null', 'NULL', eLiterals.size))
create EEnumLiteral newLiteral(String literal, String name, int value) :
     setLiteral(literal) -> setName(name) -> setValue(value)
process(EStructuralFeature this) :
process(EAttribute this) :
     if == 'VisibilityModifier' then setDefaultValueLiteral('NULL')

Back to the top