Generator templates from large existing text or XML files


I try to solve a very specific problem with MPS. I have a number of XML and other (text) files here. Some of them are quite large. The plan is to refactor these files into MPS generator templates. After they have been parameterized using a (to be developed) MPS language more of these files can be generated based on the templates, the language, and some additional input.

However, it seems next to impossible to import the files into a MPS textGen generator template. The only way to import something in an useful way is the "import as Java" option - but my files are not Java code. The other option is to type them into the editor. But we are talking about a lot of XML and text here!

What would you recommend? How can I get these files into MPS without actually retyping them manually?
I agree that this would be a common (perhaps very common) use case for people getting started using MPS. It is a typical use case for something like the StringTemplate tool by Terrence Parr, the main author of ANTLR.

I have already begun thinking about how to implement a StringTemplate-like tool using MPS, but as far as I'm aware (and I could easily be wrong on this, as I'm a newbie myself) there is not yet an equivalent character-based text language for MPS, which would allow easy import and manipulation of raw text documents (e.g. to add node macros or property macros to).

My best guess for you would be to either:

a) resort to copy-paste of text into either a TextGen or GText document (see also the tutorial videos for TextGen and GText). However, I have a feeling this would not be as easy as it sounds, since it probably won't handle newlines very well.

b) perhaps try a), and if there are problems (such as pasting in newlines), then extend either the TextGen or GText languages to add a new type of node which will correctly accept such special characters (the idea would be similar to the difference between a text-box and a text-area in HTML or other UI components such as in Swing). This approach might possibly work if a) doesn't work by itself. There are many useful techniques you can use to modify/extend existing languages, and from what I've seen, it should at least be possible to extend TextGen or GText to handle this new capability. However, it may require a fairly steep initial learning curve to figure out the best way to make the necessary modifications.

c) start from scratch and create your own character-based text language that will allow you to import raw text files, and project each character on the screen as an individual element of a (flat) AST. This will allow you to select any text within the AST (as a bunch of sibling nodes) and wrap them with a node macro or a property macro to parameterize your template very similar to how StringTemplate works. You may need to do some special tweaks to get this idea to work, but they would be quite straightforward, compared to option b) which would require working within the existing boundaries of the TextGen or GText languages.

Personally, I would go for option c), but I would understand if you saw that as 'too much work' for such a simple use case as you present. However, consider that this type of tool would be extremely re-usable after it is built, and you could customize it in many different ways to solve similar problems as you proposed.

Just some ideas from a fellow newbie.
Hi everyone,

I am now facing a similar problem. I have a few non-Java files that I would like to build with the MPS textGen. The code in the files is mostly constant, only a few attributes need to be changed according to the elements in the input language model. It seems to me like there is no mechanism in MPS to do this easily, like the StringTemplate in antlr, as wct was saying.

I see that this thread is two years old now. Are there any new additions to MPS that would facilitate this kind of use case?

Thank you,
I would suggest to look at the TextOutput language from the NYoSh project. We use TextOutput to generate text with the generator aspect. It works well when the output consists of mostly fixed text and you need to change or or two pieces here and there.  When using TextOutput, your output looks much more like what will be generated than it would with TextGen.

TextOutput is available as a plugin here:
The latest version (1.2.1) has not propagated through the MPS plugin repo yet, but you can get it here:
Uau, this plugin comes in very handy for my current projects. I was not aware of it so far. Thank you!

You can generate text strings in your application at run time by using Visual Studio runtime text templates. The computer where the application executes does not have to have Visual Studio.

Please sign in to leave a comment.