Rascal AST 访问访问注解

Rascal AST visit access annotation

好的,所以我想在访问节点时访问它的注释。举个例子:

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

我已经尝试了以下方法,但这不起作用:

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...
  }
};

我在这里做错了什么?我的方法错了吗?

问得好,最好的方法是引入一个变量,比如在模式中 sn 并使用它来获取注释

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

附带说明:您使用 getTraversalContext 函数,但我强烈建议您避免使用它,因为它是实验性的,将来很可能会发生变化。承认:我们应该这样标记它。