-2

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

user281874
  • 53
  • 12

2 Answers2

1

The grammar you are trying to achieve is a python-like grammar, that is, a grammar that allows to define block scopes based in text indentation. Here you can find a discussion on this topic with some interesting links.

If you follow the links, you will find xtextadd, which is a draft implementation of this kinds of specifications, including a great (really, very complete) tutorial explaining every detail you should be aware of if you plan to implement the indentation-based-blocks feature. You can also find very helpful classes such as PythonTerminals.xtext and PythonesqueTokenSource.java. This is a very good starting point for your project.

The author of these contributions is martinbaker.

Miguel Jiménez
  • 1,276
  • 1
  • 16
  • 24
1

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.

user281874
  • 53
  • 12