Automation with Xtext and Java

In my current project, I use the xText editor to write dsl specifications (i.e. voc.mydsl, arch.mydsl and network.mydsl). I like the xText editor because of its code completion and other functions.

However, I have a separate Java program. This java program accepts text files (i.e., voc.txt, arch.txt, network.txt) as inputs, parses these files with an ANTLR parser, and generates code using StringTemplate files.

Now, my problem is that currently I have to follow these steps manually:
(1) I write dsl specifications in the XText editor (voc.mydsl, arch.mydsl and network.mydsl).
(2) I will copy these specifications into three text files (i.e. voc.txt, arch.txt, network.txt).
(3) Finally, I run a Java program to parse these .txt files and generate the code.

Is there a way that I can automate (with one click) all three of the above steps? Let me know if you need any part.

+4
source share
1 answer

You can write a β€œspecial” generator for your DSL. XText will call this generator whenever you edit and save the * .mydsl file. What you are actually doing in this "Generator" thing is not of interest to Xtext. So your MydslGenerator.xtend generator might look like this:

 // whereever Xtext generates your empty version of this file package mydsl.xtext.generator // add imports @Singleton class MydslGenerator implements IGenerator { override void doGenerate(Resource resource, IFileSystemAccess fsa) { // calculate new filename val newFilename= resource.URI.lastSegment.replaceAll(".mydsl", ".txt") // get text representation of parsed model val textContent = resource.contents.map[NodeModelUtils::getNode(it).text].join // write text content to new file fsa.generateFile(newFilename, textContent); // TODO: call ANTLR parser on new file here } } 

At the last stage, you can call your "other" program either by calling its main method directly from Eclipse, or by calling a new JVM. Later it is recommended only if another generator is fast, because it is called whenever you save the * .mydsl file. The first method is recommended only when another program has no memory leaks and does not have many jar dependencies.

+3
source

All Articles