Perl 6规则如何将参数传递给另一个规则?
问题描述:
在How can I pass arguments to a Perl 6 grammar?我作为submerse的一部分传递了一个规则的参数。我想知道如何在语法中完全做到这一点。假设我有这样的事情:Perl 6规则如何将参数传递给另一个规则?
grammar TryIt {
rule TOP { \d+ <stuff> }
rule stuff ($count) { <[ \S A..Z ]>{$count} }
}
my $match = TryIt.parse: "123 ABCD";
say $match;
我如何通过从TOP
到stuff
参数?我没有找到任何例子,并且关于附近人物的各种猜测都没有奏效。
答
这些形式都工作:
<stuff(...)>
<stuff: ...>
不知道他们的用户文档中会上市,但S05:的正则表达式和规则设计文件has a section on them。
注意,一个规则的内部,像$0
和$/
反向引用只能保证的序列点之后可用,e.g块的开口支架。一个空块{}
的作品。
实施例:
grammar TryIt {
token TOP { (\d+) \s {} <stuff($0)> .* }
token stuff ($count) { <[ A..Z ]> ** {$count} }
}
say TryIt.subparse: "3 ABCDEFG";
输出:
「3 ABCDEFG」
0 => 「3」
stuff => 「ABC」