Nicasso
Nicasso

Reputation: 265

Rascal AST visit access annotation

Ok, so I would like to access a node's annotations when I visit it. To give an example:

visit (myAST) {
  case someNode(str name): {
    // How do I now access the @src annotation of someNode here?
  }
};

I have already tried the following but this does not work:

visit (myAST) {
  case someNode(str name): {
    parents = getTraversalContext();

    iprintln(parents[0]); // This print shows someNode with the @src annotation.
    iprintln(parents[0]@src); // This gives the error: get-annotation not supported on value at...
  }
};

What am I doing wrong here? Is my approach wrong?

Upvotes: 1

Views: 365

Answers (1)

Paul Klint
Paul Klint

Reputation: 1406

Good question, the best way to do this is to introduce a variable, say sn in the pattern and to use that to fetch the annotation

visit (myAST) {
   case sn:someNode(str name): {
     // Access the @src annotation of someNode here by sn@src
   }
 };

As a side note: you use the function getTraversalContext but I strongly advice you to avoid it since it is experimental and will very likely change in the future. Admitted: we should have marked it as such.

Upvotes: 4

Related Questions