2016-05-24 126 views
1

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

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

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

+1

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

回答

2

好问题,要做到这一点的最好办法是引入一个变量,比如在模式sn,并用它来获取注释

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

作为一个方面说明:您使用的功能getTraversalContext但我强烈建议您避免使用它,因为它是实验性的,未来很可能会发生变化。承认:我们应该标记为这样。

+0

完美!奇迹般有效。 – Nicasso