Convert an object to Xtext DSL

I defined a simple Xtext grammar that looks like this (simplified):

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals generate myDsl "http://www.xtext.org/example/mydsl/MyDsl" import "http://www.eclipse.org/emf/2002/Ecore" as ecore System: 'Define System' ( 'Define Components' '{' components+=Component+ '}' ) 'End' ; Component: 'Component' name=ID 'Value' value=Double ';' ; Double returns ecore::EDouble: '-'? INT? '.' INT ; 

The problem I like to solve is how can I convert a simple Java object to a valid xtext file?

To simplify my problem, let's say we create a list of components in Java:

 List<Component> components = new ArrayList<Component>(); components.add(new Component("FirstComponent", 1.0)); components.add(new Component("SecondComponent", 2.0)); components.add(new Component("ThirdComponent", 3.0)); 

The output file that I like to create should look like this:

 Define System Define Components { Component FirstComponent Value 1.0; Component SecondComponent Value 2.0; Component ThirdComponent Value 3.0; } End 

It is important that this file is checked using the xtext grammar so that it is valid. Hope you have some ideas for me. Here are some of mine, but so far I don’t know how to implement them:

Idea # 1: I know how to read and write a file. In my head, one solution might look like this: I have a list in my Java code, now I like to write a file that looks like the output file above. Subsequently, I like to read this file and check grammar errors. How can i do this?

Idea # 2: If I assume that I would create an xml file from Java code using JDOM, I would like to do the same in xtext. Just define the parent "Define System" that ends with "End" (see My output file), and then add the child element "Define Components {" that ends with "}" and then add children to it, for example. "Component FirstComponent Value 1.0;". Hope this does not confuse :-)

Idea # 3: I could use a pattern like the following and add children between the braces "{" ... "}":

 Define System Define Components { ... } End 

Btw: I've already tried binding Xtext with a StringTemplate code generator , but this is another problem. Hope you have some ideas.

+4
source share
2 answers

You can use Xtext serialization for this. Unlike the Java Serialization API, the Xtext implementation creates DSL.

The code will look like this:

 Injector injector = Guice.createInjector(new my.dsl.MyDslRuntimeModule()); Serializer serializer = injector.getInstance(Serializer.class); String s = serializer.serialize(eobj); 

where eobj is an instance of System .

If you wrote a formatter for your DSL, the result would also look good.

Associated Blog Entry: Deploy toString with Xtext Serializer

+4
source

Xtext provides you with AST for EMF. This AST has classes such as System and Component, along with their corresponding attributes, such as the Value Component attribute. These classes are available in the src-gen folder of your language project.

To instantiate these objects, you must use the factory class, also available in the same package.

To serialize such an AST, you can reuse the standard instrumental EMF by creating a resource and saving the contents. During serialization, the AST is checked.

 System system = ...; //Creating the AST programmatically ResourceSet set = new ResourceSetImpl(); Resource resource = set.createResource(URI.create...URI("filename")); //Initializing an EMF resource that represents a file resource.getContents.add(system); //adding your AST to the file resource resource.save(); 

A minor note: if you are not developing an Eclipse plug-in, you need to initialize the Xtext toolkit by calling the created static method "YourLanguage" StandaloneSetup.doSetup ().

For other program verification options, you can take a look at the ParseHelper and ValidatorTester classes used by the Xtext testing environment.

+2
source

Source: https://habr.com/ru/post/1432741/


All Articles