为 Java 个 M3 节点添加注释

Adding annotations for Java M3 nodes

我正在尝试向 Java M3 节点添加多个注释。作为一个工作示例,这就是我正在尝试做的事情:

n = \number("5");
n = setAnnotations(n, ("modifiers" : [\static()]));
n = setAnnotations(n, ("modifiers" : getAnnotations(n ["modifiers"]+\public()));

导致以下错误:

|rascal://Driver|(1556,7,<59,57>,<59,64>): insert into collection not supported on value and Modifier
?[Advice](http://tutor.rascal-mpl.org/Errors/Static/UnsupportedOperation/UnsupportedOperation.html)

另外我试过:

n = \number("5");
n = setAnnotations(n, ("modifiers" : [\static()]));
n = setAnnotations(n, ("modifiers" : getAnnotations(n)["modifiers"]+[\public()]));

它不会崩溃但会生成如下注释:

number("5")[
  @modifiers=[
    [static()],
    public()
  ]
]

这不是我要找的。我也试过:

n = \number("5");
n = setAnnotations(n, ("modifiers" : [\static()]));
a = [\static(), [ m | m <- getAnnotations(n)["modifiers"] ] ];
n = setAnnotations(n, (modifiers : a));

出现以下错误:

|rascal://Driver|(1548,11,<59,49>,<59,60>): value is not enumerable
?[Advice](http://tutor.rascal-mpl.org/Errors/Static/NotEnumerable/NotEnumerable.html)

真的,我只是想要一些方法来将修饰符附加到修饰符注释中。我在这里做错了什么?谢谢。

如果您只想为值添加注释,有效的备选方案如下:

// as assignment statements which modify the variable `n`:
n@modifiers = [\static(), \public()]; // setting a new value for the modifiers annotation
n@modifiers += [\public()]; // short-hand to concatenate to the existing value of modifiers 
n@modifiers?[] += [\public()]; // in case sometimes there is no initial value to begin with we initialize with empty list.
// as expression to create a new value assigned to m with a different annotation than n had:
m = n[@modifiers=[\static(), \public()];
m = n[@modifiers=n@modifiers + [\public()]]; // using the previous value too

关于您看到的错误的一些说明:

  • getAnnotations returns 类型 map[str,value] 的值,因为值是顶级类型 Rascal 不知道有多少操作可以应用于它。这解释了为什么你不能连接或枚举。它需要模式匹配才能向下转换为更有用的类型,如 for (list[Modifier] modifiers := getAnnotations(n)["modifiers"], Modifier m <- modifiers) { ... }
  • 要以类型化的方式检索注释,只需使用 n@modifiers,由于 M3 库中某处的声明 anno list[Modifier] Declaration@modifiers;,其类型为 list[Modifier]