Raven
Raven

Reputation: 3526

How to refer to an underlying element for the error method

I am looking for a possibitlity to refer to an element which is a bit deeper in the grammar in the error function.
My grammar snippet looks like that:

Declaration:
    name = ID "=" decCon=DecContent
;

    DecContent:
        singleContent=VarContent (op+=OPERATOR nextCon+=VarContent)*
    ;

        VarContent:
            (unOP=("+"|"-"))? num = NUMBER
            | string = STRING
            | (unOP=("+"|"-"))? reference = [Declaration]
            | arrayContent = ArrayLiteral
            | embraced = "(" embrCon=DecContent ")"
        ;

Now I want the error mehtod not to refer to "decCon" (eINSTANCE.declaration_decCon) but to, let's say, "string" in the rule "VarContent".
How would I manage this?
Do I have to implement custom scopes for that?

Greeting Krzmbrzl

Upvotes: 0

Views: 67

Answers (1)

Joko
Joko

Reputation: 590

I assume that you implemented a check method in you validation class. Ok, instead of implement a method

@Check
def checkDeclaration(Declaration d) {
    //...
}

you should implement a method

@Check
def checkVarContent(VarContent v) {
    //...
}

where you iterate upwards (el.eContainer() ...) until you are at the declaration level to perform the check. Then you can assign the error marker to the element you want.

Edit: Because an Xtext grammar describes an EMF meta model, Xtext generates a regular EMF meta model from your grammar and the AST one works with is an instance of this model. In this case, e.g., VarContent, Declaration, ... are model classes wich derive from Eobject. From the EMF concept the meta model also has a containment hierarchy. The method EObject EObject.getEContainer() returns the parent element of a model element. With some model knowledge one can cast the returned Eobject to the concrete class and then use it.

Upvotes: 1

Related Questions