0

I am trying to understand a xtext grammar I have found (below). I have two questions:

  • The XFeatureCall has return Type XExpression but this is overruled by {XFeatureCall} so I could set "returns XFeatureCall" as well?. Or is it actually necessary to do it this way?
  • Line 8 and 14 start with "=>". Are these "chosen predicates" or something else that did not come to my attention so far? I could not find this variation of chosen predicates in the xtext documentation. So I would appreciate clarification in its application.

xtext grammar:

StaticEquals:':=';
XFeatureCall returns XExpression:
    // Same as Xbase...
    {XFeatureCall}
    (declaringType=[JvmDeclaredType|StaticQualifier])?
    ('<' typeArguments+=JvmArgumentTypeReference (',' typeArguments+=JvmArgumentTypeReference)* '>')? 
    (feature=[JvmIdentifiableElement|IdOrSuper]|'class') 
    (=>explicitOperationCall?='(' 
        (
            featureCallArguments+=XShortClosure
          | featureCallArguments+=XExpression (',' featureCallArguments+=XExpression)*
        )? 
    ')')?
    =>featureCallArguments+=XClosure?
    // ... Except with this additional optional clause that allows static members to be set with := operator
    ({XAssignment.assignable = current} StaticEquals value = XAssignment)?;
moin moin
  • 2,263
  • 5
  • 32
  • 51

1 Answers1

0

First question: In fact in this case your rule returns a XFeatureCall but XFeatureCall has XExpression as its supertype. It is useful for example if you have:

SomeRule: (parts+=XFeatureCall)* (parts+=XOtherFeatureCall)*

Let XOtherFeatureCall also extend XExpression, and parts be a list of XExpressions.

Second question: it is a priority operator and means that what follows should be parsed now, even if there are other parsing solutions. See this classic example:

if a
  if b
    do;
  else
    doelse;

else could be parsed for the inner if or the outer if. Of course we want it in the inner if. Setting a rule such as:

=>'else' else=ElseExpression

forces the grammar to parse the else as soon as it finds it instead of returning to the outer rule that could consume a else too. So it solves an ambiguity.

Steph
  • 1,989
  • 14
  • 18