Exponent
Exponent

Reputation: 50

Parsing strings in Xtext

I'm currently attempting to write an Eclipse-based editor for an in-house language we use in the Company. The language is a collection of statements of the form:

{action}: {arguments}

...on its own line. The format of {arguments} depends on the type of {action} being performed. An example of a script fragment could look like:

banner: Some string with numbers and punctuation (23) in it!
# some comment
timeout: 42

My problem is parsing such a fragment. I've got comments and the timeout statement working, but I can't seem to create a rule to cover the banner statement. All of my attempts have resulted in Antlr "token definition unreachable" warnings, or the editor not being able to match input. I've tried the following rules for the banner statement:

Banner:
  'banner:' name=ANY_OTHER*;

and

Banner:
  'banner:' name=FF_STRING;
terminal FF_STRING : ('a'..'z'|'A'..'Z'|'0'..'9'|'.'|':'|' ')+;

...which gives me the antlr warnings. A hack I've come up with is to simple create a terminal which is identical to an SL_COMMENT, with 'banner:' instead of '#' at the beginning. The disadvantage is that I do not get syntax colouring, nor does 'banner' show up in the auto-completion list.

Any advice is welcome.

Upvotes: 0

Views: 2723

Answers (1)

Sebastian Zarnekow
Sebastian Zarnekow

Reputation: 6729

You could try to use a data type rule and a reduced set of terminal rules. Something like this could work:

Banner
  'Banner:' name=Value;
Value hidden(): 
  (ID | WS | INT | <any keyword from your grammar>)* LineBreak;
terminal LineBreak: '\r' '\n'? | '\n';
termianl WS: (' '|'\t') *

Upvotes: 4

Related Questions