如何将参数传递给 Perl 6 语法?
How can I pass arguments to a Perl 6 grammar?
在中,我提供了一个模糊模糊匹配问题的Perl 6 解决方案。我有这样的语法(虽然我可能在编辑 #3 后改进了它):
grammar NString {
regex n-chars { [<.ignore>* \w]**4 }
regex ignore { \s }
}
文字4
本身就是示例中目标字符串的长度。但下一个问题可能是其他长度。那么我怎样才能告诉语法我想要匹配多长时间呢?
虽然文档没有显示示例或使用 $args
参数,但我在 S05-grammar/example.t in roast.
中找到了一个
在 :args
中指定参数并为正则表达式提供适当的签名。在正则表达式中,访问代码块中的参数:
grammar NString {
regex n-chars ($length) { [<.ignore>* \w]**{ $length } }
regex ignore { \s }
}
class NString::Actions {
method n-chars ($/) {
put "Found $/";
}
}
my $string = 'The quick, brown butterfly';
loop {
state $from = 0;
my $match = NString.subparse(
$string,
:rule('n-chars'),
:actions(NString::Actions),
:c($from++),
:args( \(5) )
);
last unless ?$match;
}
虽然我仍然不确定传递参数的规则。这不起作用:
:args( 5 )
我得到:
Too few positionals passed; expected 2 arguments but got 1
这个有效:
:args( 5, )
不过想一晚上就够了。
在
grammar NString {
regex n-chars { [<.ignore>* \w]**4 }
regex ignore { \s }
}
文字4
本身就是示例中目标字符串的长度。但下一个问题可能是其他长度。那么我怎样才能告诉语法我想要匹配多长时间呢?
虽然文档没有显示示例或使用 $args
参数,但我在 S05-grammar/example.t in roast.
在 :args
中指定参数并为正则表达式提供适当的签名。在正则表达式中,访问代码块中的参数:
grammar NString {
regex n-chars ($length) { [<.ignore>* \w]**{ $length } }
regex ignore { \s }
}
class NString::Actions {
method n-chars ($/) {
put "Found $/";
}
}
my $string = 'The quick, brown butterfly';
loop {
state $from = 0;
my $match = NString.subparse(
$string,
:rule('n-chars'),
:actions(NString::Actions),
:c($from++),
:args( \(5) )
);
last unless ?$match;
}
虽然我仍然不确定传递参数的规则。这不起作用:
:args( 5 )
我得到:
Too few positionals passed; expected 2 arguments but got 1
这个有效:
:args( 5, )
不过想一晚上就够了。