0

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

Raven
  • 2,951
  • 2
  • 26
  • 42

1 Answers1

1

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.

Joko
  • 600
  • 3
  • 15
  • First of all thank you for your reply... It looks really promising to me :) But could you please explain to me what el.eContainer() does and how I have to use it? (Sry but I'm just starting out with validation so I'm not quite familiar with Xtend and it's methods) – Raven May 19 '15 at 16:54
  • 1
    Look into my answer again, I added some stuff. – Joko May 19 '15 at 18:04