Pankesh Patel
Pankesh Patel

Reputation: 1302

providing automization with Xtext and Java program

In my current project, I am using xText editor for writing my dsl specifications (i.e., voc.mydsl, arch.mydsl, and network.mydsl). I like the xText editor because of its code-completion and other functionalities.

However, I have a separate Java program. This java program takes text files (i.e., voc.txt, arch.txt, network.txt) as inputs, parse these files using 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 XText editor (voc.mydsl, arch.mydsl, and network.mydsl).
(2) I copy-paste these specification into three text files (i.e., voc.txt, arch.txt, network.txt).
(3) Finally, I run the Java program to parse these .txt files and generate code.

Is there any way that I can automize (performed in a single click) all the above three steps? Let me know if you need any detail.

Upvotes: 4

Views: 301

Answers (1)

A.H.
A.H.

Reputation: 66213

You could write a "special" generator for your DSL. XText will call this generator whenever you edit and save a *.mydsl file. What you actually do in this "Generator" thing is of no interest to Xtext. So your MydslGenerator.xtend generator could 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
    }
}

In the last step you can call your "other" program either by calling its main method directly from Eclipse or by invoking a new JVM. The later is only advisable if the other generator works quickly because it is called whenever you save a *.mydsl file. The first method is only advisable when the other program has no memory leaks and has not to many jar dependencies.

Upvotes: 3

Related Questions