Child pages
  • Textual Modeling with Xtext

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

  1. Install the Xtext Antlr Runtime Feature from http://download.itemis.de/updates/ (click HelpInstall New Software... and enter the URL into the topmost text field; once Eclipse has downloaded the list of software available under that URL, select the feature from the list)
  2. File → New → Project... → Xtext → Xtext Project From Existing Meta Models → Next → Add... → turing.genmodel → OK
  3. Select your top-level model element as "Entry rule", e.g. TuringMachine → Next → enter the following values:
    • Project name: de.cau.cs.rtprak.login.turing.text (like in previous tutorials, replace "login" by your login name)
    • Location: your repository path
    • Name: de.cau.cs.rtprak.login.turing.text.Turing
    • Extensions: tuxt (the file extension under which your text files will be stored)
    • Uncheck Create SDK feature project (not required)
  4. Finish → an editor is opened with a predefined grammar (Turing.xtext). The fourth line should contain an import statement followed by the namespace URI of your meta model, which is probably marked with an error. The problem here is that the meta model is not loaded in the Eclipse instance you are using for development. Fix this problem by replacing the URI by the workspace path of your Turing Machines ecore model, e.g. "platform:/resource/de.cau.cs.rtprak.login.turingmodel/model/turing.ecore"
  5. The Turing.xtext file is located in the package de.cau.cs.rtprak.login.turing.text together with a Modeling Workflow Engine file, which can be used to configure code generation. Right-click GenerateTuring.mwe2 and select Run As → MWE2 Workflow. If you get an error message like "*ATTENTION* It is recommended to use the ANTLR 3 parser generator" in the console, type n and install Antlr from the update site.
  6. Now a great amount of Java code should have been generated. Add the new plugins to your Eclipse run configuration and start it.
  7. In the new Eclipse instance: File → New → File and name it test.tuxt (when asked to add the Xtext nature, hit Yes).
  8. Use ctrl + space for getting content assist to create a valid model following the predefined grammar. The syntax will probably look similar to this:

    No Format
    TuringMachine {
        states {
            State blub {
            },
            State blah {
                outgoing {
                    Transition {
                        target blub
                    }
                }
            }
        }
    }

...

Xtext supports automatic formatting, which is available in the text editor with right-click → Format or ctrl+shift+F. However, the formatter must be configured in order to generate good results. Write a formatter configuration that fits well to your syntax by editing the generated file TuringFormatter in the formatting subpackage. Learn how this is done by reading the Xtext reference documentation → documentation → Runtime Concepts → Concepts → Formatting.

Validation

The generated code includes some automatic validation of models with respect to syntactic issues. If the token sequence in a text file does not conform to the grammar, error markers are shown at appropriate points in the text. However, this should be augmented by semantic validation by checking high-level properties of the model. Implement such a semantic validation by editing the generated file TuringJavaValidator in the validation subpackage. Learn how this is done by reading the Xtext reference documentation → documentation → Runtime Concepts → Concepts → Validation Custom Validation. You should implement at least the following checks:

...

The generated code includes a parser for text files in your syntax. This parser is simply used with the same interface as for any other EMF models: resource sets (see the Xtext reference documentation → Integration with EMF and Other EMF Editors). Your final task for this tutorial is to reuse the Turing controller you implemented in the EMF tutorial for simulating Turing Machine models, this time applying it to .tuxt text files. However, instead of using a fixed path to your model file, we will now use a menu contribution in order to set the file path dynamically. This contribution will be put into the popup menu of the Navigator / Project Explorer view by configuring a visibility expression that tests the currently selected (right-clicked) elements. The contribution shall only be visible if the selected files have the extension tuxt, which is assigned to our mighty textual syntax.

...