首页 > 解决方案 > 使用 $/ 与在语法操作中使用任何其他变量并不完全相同

问题描述

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

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 );

但是,前两个版本按预期工作。但是第三个(这将是第二个的直接翻译)失败了

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

可能有一些特殊的语法正在发生(make实际上$/.make,可能),但我想澄清一下这是否符合规范或者是一个错误。

标签: grammarraku

解决方案


这是因为make子例程是 Rakudo 中很少见的情况之一,它实际上试图$/从调用它的范围内访问变量。这也是它的记录方式:

子表单对当前 $/ 进行操作

(来自文档


推荐阅读