在语法操作中使用$ /与使用任何其他变量并不完全相同

jjm*_*elo 8 grammar perl6

从理论上讲,根据文档,您可以在语法操作中为方法使用任何参数。

grammar G {
    token TOP { \w+ }
}

class Action-Arg {
    method TOP ($match) { $match.make: ~$match }
}

class Action {
    method TOP ($/) { make ~$/ }
}

class Action-Fails {
    method TOP ($match) { make ~$match }
}

say G.parse( "zipi", actions => Action-Arg );
say G.parse( "zape", actions => Action );
say G.parse( "pantuflo", actions => Action-Fails );
Run Code Online (Sandbox Code Playgroud)

但是,两个第一个版本按预期工作。但是第三个(将是第二个的直接翻译)以

Cannot bind attributes in a Nil type object
  in method TOP at match-and-match.p6 line 19
  in regex TOP at match-and-match.p6 line 7
  in block <unit> at match-and-match.p6 line 24
Run Code Online (Sandbox Code Playgroud)

可能存在一些特殊的语法(make实际上$/.make可能是在某种意义上),但是我只是想澄清一下这是否符合规范或是一个错误。

Eli*_*sen 8

这是因为make子例程是Rakudo中罕见的情况之一,在该情况下,它实际上尝试$/从调用它的作用域访问变量。这也是如何记录的:

子表格在当前$ /上运行

(来自文档

  • 您传递给make的东西就是您想要的东西。它尝试将其绑定到假定在调用make的作用域中的$ /中存在的Match对象。原始问题的“ Action-Fails”示例没有一个作为Match对象的$ /:而是找到了一个N​​il,然后死了,因为它无法绑定任何东西。 (2认同)