3

I have a custom scripting language, that I am attempting to use XTEXT for syntax checking. It boils down to single line commands in the format

COMMAND:PARAMETERS

For the most part, xtext is working great. The only problem I have currently run into is how to handle wanted (or unwanted) white spaces. The language cannot have a space to begin a line, and there cannot be a space following the colon. As well, I need to allow white space in the parameters, as it could be a string of text, or something similar.

I have used a datatype to allow white space in the parameter:

UNQUOTED_STRING:
    (ID | INT | WS | '.' )+
;

This works, but has the side effect of allowing spaces throughout the line.

Does anyone know a way to limit where white spaces are allowed?

Thanks in advance for any advice!

Jeff Clare
  • 185
  • 1
  • 1
  • 8

1 Answers1

4

You can disallow whitespace globally for your grammar by using an empty set of hidden tokens, e.g.

grammar org.xyz.MyDsl with org.eclipse.xtext.common.Terminals hidden()

Then you can enable it at specific rules, e.g.

XParameter hidden(WS):
    'x' '=' value=ID
;

Note that this would allow linebreaks as well. If you don't want that you can either pass a custom terminal rule or overwrite the default WSrule.

Here is a more complete example (not perfect):

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals hidden()

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

Model:
    (commands+=Command '\r'? '\n')+
;

Command:
    SampleCommand
;

SampleCommand:
    command='get' ':' parameter=Parameter 
;

Parameter:
    '{' x=XParameter '}'
;

XParameter hidden(WS):
    'x' '=' value=ID
;

This will parse commands such as:

get:{x=TEST}
get:{ x =  TEST}

But will reject:

 get:{x=TEST}
get: {x=TEST}

Hope that gives you an idea. You can also do this the other way around by limiting the whitespace only for certain rules, e.g.

CommandList hidden():
    (commands+=Command '\r'? '\n')+
;

If that works better for your grammar.

Franz Becker
  • 705
  • 5
  • 8