如何将参数传递给Perl 6语法? [英] How can I pass arguments to a Perl 6 grammar?
问题描述
在编辑距离:忽略开始/结束中,我为模糊模糊匹配问题提供了Perl 6解决方案.我有一个这样的语法(尽管也许我在Edit#3之后已经改进了它):
In Edit distance: Ignore start/end, I offered a Perl 6 solution to a fuzzy fuzzy matching problem. I had a grammar like this (although maybe I've improved it after Edit #3):
grammar NString {
regex n-chars { [<.ignore>* \w]**4 }
regex ignore { \s }
}
文字4
本身就是示例中目标字符串的长度.但是下一个问题可能是其他长度.那么我怎样才能告诉语法我要匹配多长时间呢?
The literal 4
itself was the length of the target string in the example. But the next problem might be some other length. So how can I tell the grammar how long I want that match to be?
推荐答案
Although the docs don't show an example or using the $args
parameter, I found one in S05-grammar/example.t in roast.
在:args
中指定参数,并为正则表达式赋予适当的签名.在正则表达式中,访问代码块中的参数:
Specify the arguments in :args
and give the regex an appropriate signature. Inside the regex, access the arguments in a code block:
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;
}
我仍然不确定传递参数的规则.这不起作用:
I'm still not sure about the rules for passing the arguments though. This doesn't work:
:args( 5 )
我得到:
通过的位置太少;预期有2个参数,但有1个
Too few positionals passed; expected 2 arguments but got 1
这有效:
:args( 5, )
但这足以让我们考虑一个晚上.
But that's enough thinking about this for one night.
这篇关于如何将参数传递给Perl 6语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!