使用 $/ 与在语法操作中使用任何其他变量并不完全相同
Using $/ is not exactly the same as using any other variable in grammar actions
理论上,according to the documentation,您可以在语法操作中为方法使用任何参数。
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
的意义上,可能),但我只想澄清这是根据规范还是错误。
这是因为 make
子例程是 Rakudo 中的少数情况之一,它实际上尝试从调用它的范围访问 $/
变量。这也是它的记录方式:
The sub form operates on the current $/
(来自 the documentation)
理论上,according to the documentation,您可以在语法操作中为方法使用任何参数。
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
的意义上,可能),但我只想澄清这是根据规范还是错误。
这是因为 make
子例程是 Rakudo 中的少数情况之一,它实际上尝试从调用它的范围访问 $/
变量。这也是它的记录方式:
The sub form operates on the current $/
(来自 the documentation)