【问题标题】:Rascal AST visit access annotationRascal AST 访问访问注释
【发布时间】:2016-05-24 07:15:02
【问题描述】:

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

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

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

【问题讨论】:

  • 如果您将parents[0] 与声明注释的类型或node 匹配,则第二种解决方案将起作用。这是因为getAnnotation 函数只为node 定义,而不为value 定义。然而,保罗的解决方案比使用getTraversalContext

标签: rascal


【解决方案1】:

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

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

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

【讨论】:

  • 完美!像魅力一样工作。
猜你喜欢
  • 1970-01-01
  • 2015-09-03
  • 2010-09-24
  • 2019-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多