working with xtext not as plugin

我只是一个虾纸丫 提交于 2019-12-13 05:25:47

问题


In general I have my dsl as plugin and I want to create a new app that use my dsl

so i tried to write this code:

JsonParser p = new JsonParser();
IParseResult r = p.parse(new StringReader("{}")); 
//once that work it will be the file data instead of {}

but when i do the parse the node model builder is null and the following line has exception: return doParse(ruleName, in, nodeModelBuilder.get(), 0);

and i'm not sure how to init nodeModelBuilder

i'm sure i missing some steps but i'm not quite familiar with the xtext process. thanks!


回答1:


You already read the following answer on Eclipse Forum. You need to create an IParser instance by injecting it. All dependencies gets also injected. The necessary bindings are described in your JsonRuntimeModule. Xtext uses Guice and theses Modules to glue everything together. This pattern is called Dependency Injection.


... I want to create a new app that use my dsl

So you want to use your Json DSL in standalone mode.

My suggestion:

  • Create a minimum Eclipse IApplication with CLI that reads and parses an input file. The advantage of an Eclipse IApplication is that you can easily deploy an headless version of your DSL runtime. [1]
  • Have a look at your JsonInjectorProvider and the ParseHelper [2] from Xtext's JUnit support for examples how to use your DSL and Xtext runtime in standalone mode.

[1] http://www.eclipsezone.com/eclipse/forums/t99762.html [2] org.eclipse.xtext.junit.util.ParseHelper




回答2:


You are not supposed to call parser directly. See: http://wiki.eclipse.org/Xtext/FAQ#How_do_I_load_my_model_in_a_standalone_Java_application.C2.A0.3F

The code should look like:

Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
Resource resource = resourceSet.getResource(new File("/../../some.json").toURI(), true);
Model modelRootElement = (Model) resource.getContents().get(0);

Replace MyDsl with 'JsonParser' or 'Json' or whatever is your DSL name. Look for class JsonStandaloneSetup or JsonParserStandaloneSetup in your DSL source code. This class is generated when you start the Xtext project (or when you run workflow for the first time, not sure now). Replace Model with whatever is your root element type. It must be EObject subclass.

The parsing/validation/buidling AST is done resource.getContents() command. Not very intuitive, I know. It is because you have to initialize context, all sorts of contexts i fact, Guice context, EMF context, and perhaps other, all encapsulated in the StandaloneSetup (and RuntimeModule). The context is similar to Spring Application Context.




回答3:


You need to use StandaloneSetup to run in standalone mode.

See this tutorial for help



来源:https://stackoverflow.com/questions/13722707/working-with-xtext-not-as-plugin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!