Jakob Sachs
Jakob Sachs

Reputation: 715

Writing java-style If statements in XText results in an error

I'm writing a custom Xtext editor for my own DSL-Language and now want to add If-Statements to my language. The Statements look something like this:

if (TRUE) {
    (...)
}

But when I try adding them in, I get an error "A class may not be a super type of itself".

This is my code so far:

grammar XtextTest with org.eclipse.xtext.common.Terminals

generate xtextTest "http://www.my.xtext/Test"

Model:
    statements+=Statement*;

Statement:
    VariableAssignment |
    IfStatement;

IfStatement:
    'if' '(' BooleanExpression ')' '{' Statement '}';

BooleanExpression:
    'TRUE' | 'FALSE';

VariableAssignment:
    name=ID "=" INT ';';

How can I implement this? Or am I doing something obviously wrong?

Any help is appreciated ^^

Upvotes: 1

Views: 543

Answers (1)

Christian Dietrich
Christian Dietrich

Reputation: 11868

Assignments are a important thing in Xtext. if you just call rules without assigning them it influences the supertype hierarchy that is inferred. => it is better to change the grammar to

IfStatement:
   'if' '(' condition=BooleanExpression ')' '{' statement=Statement '}';

If you want to introduce a common supertype/subtype relationship don't use assignments

Number: Double | Long; 

Upvotes: 1

Related Questions