What parameters should be used for AbstractDeclarativeValidator.warning and error?

StackOverflow https://stackoverflow.com/questions/17026243

  •  31-05-2022
  •  | 
  •  

Domanda

I have a working grammar on xtext, and am starting the validation of the code.

For this, I added a method in the validator xtext created for me.

Of course, when an expression isn't valid, I want to be able to give a warning on the given AST node.

I attempted the obvious:

@Check
public void testCheck(Expression_Multiplication m){
    if(!(m.getLeft() instanceof Expression_Number)){
        warning("Multiplication should be on numbers.",m.getLeft());
    }
    if(!(m.getRight() instanceof Expression_Number)){
        warning("Multiplication should be on numbers.",m.getRight());
    }
}

Without success, as Expression_Number extends EObject, but is not an EStructuralFeature.

warning(String message, EStructuralFeature feature)

There are many other prototypes for warning, but none that takes just a String and a Eobject. Using null or various values extracted from eContainingFeature logs an error, and sometimes shows the warning at the correct place anyway. Searching for examples, I found that the values were often coming from the statics fields of a class called Literals or ***Package, the one generated in the project contains EStructuralFeatures, but I have no idea of which one to use, or why I would need one of these.

So the question is:

How can I place a warning on a given AST element ?

È stato utile?

Soluzione 2

I ended up using

private void warning(String text, EObject badAstNode){
    // The -1 seems to come from a static member somewhere. Probably cleaner to
    // name it, but I couldn't find it again.
    warning(text,badAstNode,null,-1);
}

I have no idea about whether this is supposed to be the right way, but it seemed to work in the various cases I used it, and requires a minimal amount of state to be kept.

Altri suggerimenti

The EStructuralFeature is the property of your AST. You'll find a generated EPackage class, which contains constants.

I guess in your case it is something like:

MyDslPackage.Literals.EXPRESSION_MULTIPLICATION__LEFT

and

MyDslPackage.Literals.EXPRESSION_MULTIPLICATION__RIGHT
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top