0
votes

Sometime, when defining a Xtext grammar, I define production rules without infered type.

For example, when defining an expression language, the parenthesis can be used to deal with the operators precedence, but does not have to be a type in the metamodel.

For example the grammar bellow can be used to define the two following boolean expression "true && false || true" and "true && (false || true)".

Expr returns Expression:
    Or;


Or returns Expression:
    And (=> ({And.left=current} '||') right=And)*;

And returns Expression:
    Lit (=> ({CmdAnd.left=current} '&&') right=Lit)*;

Lit returns Expression:
    {BoolTrue} 'true' | 
    {BoolFalse} 'false' 
    '(' Expression ')';

In xtext, the formatting is done by dispaching the EMF model element to format methods, for instance:

def dispatch void format(And and, extension IFormattableDocument document) {

}

But in the case of the third production rule of Lit (the parenthesis), no type is defined on purpose, which prevents me to dispatch to this syntactic element. Consequently, I cannot find a way to define my formatting rule for this part of my syntax.

How to define my formatting rules for the parenthesis in such context?

1

1 Answers

0
votes

i think you have to do this on a higher level e.g.

@Inject extension DomainmodelGrammarAccess

def dispatch void format(Operation operation, extension IFormattableDocument document) {

    operation.allRegionsFor.keywords(XParenthesizedExpressionAccess.leftParenthesisKeyword_0).forEach [
        append[noSpace]
    ]
    operation.allRegionsFor.keywords(XParenthesizedExpressionAccess.rightParenthesisKeyword_2).forEach [
        prepend[noSpace]
    ]