2

I have a grammar of RQL adapted for my work and it differs from the Oracle grammar. The main difference is that string literals are not surrounded by double quotes.

Model:
    operands+=HoOperand ( (','|'&') HoOperand )*
;

And: '&' HoOperand;
Or: (';'|'|') HoOperand;

HoOperand:
    WSP* ( HigherOrderCall | Comparison ) WSP*
;

HigherOrderCall:
    LogicalOpAliases WSP* '(' ( HoOperand ( ',' HoOperand )* ) ')'
;

CompOps : ('!='|'='|'<'|'<='|'>'|'>=');
Comparison : Strval WSP* ( CompOps ) Strval;

Nchar: (ALPHA|DIGIT|'-'|'.'|'_'|'~'|'$'|':'|'*'|'+'|'?'|'/'|'@');
Pct_encoded: '%' XDIGIT XDIGIT;    
Strval:    (Nchar|Pct_encoded)+;
LogicalOpAliases: ('or'|'and'|'not');

terminal DIGIT:    ('0'..'9');
terminal XDIGIT: (DIGIT|'A'..'F');        
terminal ALPHA:    ('A'..'Z'|'a'..'z');
terminal WSP: (' '|'\t');

I've encountered such a problem: because of the difference noticed earlier entries like or=a and or(a=a) match the different rules, the first matches Comparison and the second matches HigherOrderCall. Accordingly to this the first or matches Strval and the second matches LogicalOpAliases in those rules they are the different terminals and I need lexer to backtrack.

I've already read a similar question and the answer to it. I tried to include LogicalOpAliases in Strval, it solved this issue partially (entries like anf=a are considered to be mistaken because of the lexer greediness, it tries to consume and and doesn't backtrack).

How can I resolve this problem?

And the second question is about highlighting. In the entry or= where or matches string literal or is highlighted. I tried to make rule LogicalOpAliases terminal rule, but highlighting disappeared at all. How can I resolve this?

Community
  • 1
  • 1

1 Answers1

1

Essentially, you have to fix your grammar.

Xtext grammars are not just parser specifications, but they are also used to infer the classes for the AST. You have to use assigned actions in the appropiate places, and be aware of the implications of direct rule calls like And: '&' HoOperand; to the inferred class hierarchy (the way the grammar is written, HoOperand would be a subclass of both And and Or).

Also, it would make sense to reduce clutter by defining WSP as a hidden token using hidden(WSP) instead of including it in every place where whitespace is allowed.

Bernhard Stadler
  • 1,725
  • 14
  • 24