Search code examples
xtext

XText - How to check syntax violation in next line


I have a problem with xtext. Basicly I try to create a whitespace sensitive language. This is what my grammer should allow:

Title
Message
   Signal 1
   Signal 2
   Struct
      Signal 3
   Signal 4

And this should be not allowed.

    Title
Message
   Signal 1
      Signal 2
   Struct
      Signal 3
   Signal 4

So the problem is that Signal 1 is one level above Signal 2. But going to the next level is only allowed for structs.

I have no plan how to check wether the next line is on the same level or not. Can you give me a hint or a similar code snippet?

Thanks


Solution

  • In the newest xtext version xtext supports whitespace sensitive languages.

    There are two synthetic tokens, BEGIN and END which can increase or decrease the indentation.

        // The following synthetic tokens are used for the indentation-aware blocks
    terminal BEGIN:
        'synthetic:BEGIN'; // increase indentation
    terminal END:
        'synthetic:END'; // decrease indentation
    

    For an example watch the homeautomation example delivered with xtext.