Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "Xtext/FAQ"
(→How do I load my model in a standalone Java application ?) |
|||
Line 3: | Line 3: | ||
== How do I load my model in a standalone Java application ? == | == 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: | + | Assuming you have a grammar Foo.xtext the Java code to load a corresponding model from a resource should look something like this: |
<source lang="java"> | <source lang="java"> | ||
Line 11: | Line 11: | ||
Resource resource = resourceSet.getResource(URI.createURI("platform:/resource/myproject/my.foo"), true); | Resource resource = resourceSet.getResource(URI.createURI("platform:/resource/myproject/my.foo"), true); | ||
Foo foo = (Foo) resource.getContents().get(0); | Foo foo = (Foo) resource.getContents().get(0); | ||
− | </source> | + | </source> |
= Workflow / Generator = | = Workflow / Generator = | ||
Line 28: | Line 28: | ||
== OK, but I didn't get these warnings in oAW Xtext ! == | == 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 [http://www.antlr.org/wiki/display/ANTLR3/Grammar+options 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. | + | Unlike in oAW Xtext the ANTLR grammar generated by TMF Xtext doesn't have [http://www.antlr.org/wiki/display/ANTLR3/Grammar+options 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: |
<source lang="xml"> | <source lang="xml"> | ||
− | + | <!-- Antlr Generator fragment --> | |
− | + | <fragment class="org.eclipse.xtext.generator.AntlrDelegatingFragment"/> | |
− | + | ||
− | + | ||
</source> | </source> | ||
+ | |||
+ | with: | ||
+ | |||
+ | <source lang="xml"> | ||
+ | <!-- Antlr Generator fragment --> | ||
+ | <fragment class="de.itemis.xtext.antlr.XtextAntlrGeneratorFragment"> | ||
+ | <options backtrack="true"/> | ||
+ | </fragment> | ||
+ | </source> | ||
+ | |||
+ | Further down in the workflow you will find another use of the <code>AntlrDelegatingFragment</code> fragment (used for content assist) you have to replace with: | ||
+ | |||
+ | <source lang="xml"> | ||
+ | <fragment class="de.itemis.xtext.antlr.XtextAntlrUiGeneratorFragment"> | ||
+ | <options backtrack="true"/> | ||
+ | </fragment> | ||
+ | </source> | ||
+ | |||
+ | Note that you can in both cases also specify <code>memoize="true"</code> as an additional option. | ||
== Why are generated packages from an imported grammar A duplicated in dependent grammar B ? == | == Why are generated packages from an imported grammar A duplicated in dependent grammar B ? == |
Revision as of 09:24, 22 July 2009
Contents
General
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/my.foo"), true); 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"/>
with:
<!-- Antlr Generator fragment --> <fragment class="de.itemis.xtext.antlr.XtextAntlrGeneratorFragment"> <options backtrack="true"/> </fragment>
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"/> </fragment>
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" genModels="platform:/resource/project/src/my/pack/SecretCompartments.genmodel"/>