在perl6语法,如所解释这里(注意,不能保证设计文件被向上最新作为实现完成)时,如果开度角托架之后是一个标识符,则构建体是一个子规则的呼叫时,方法或功能.
如果标识符后面的字符是开头,那么它是对方法或函数的调用,例如:<foo('bar')>
.正如在页面下方进一步解释的那样,如果标识符后面的第一个字符是空格,那么直到结束角度的其余字符串将被解释为方法的正则表达式参数 - 引用:
<foo bar>
Run Code Online (Sandbox Code Playgroud)
或多或少相当于
<foo(/bar/)>
Run Code Online (Sandbox Code Playgroud)
使用此功能的正确方法是什么?在我的例子中,我正在解析面向行的数据,而我正在尝试声明一条规则,该规则将对正在解析的当前行进行单独搜索:
#!/usr/bin/env perl6
# use Grammar::Tracer ;
grammar G {
my $SOLpos = -1 ; # Start-of-line pos
regex TOP { <line>+ }
method SOLscan($regex) {
# Start a new cursor
my $cur = self."!cursor_start_cur"() ;
# Set pos and from to start of the current line
$cur.from($SOLpos) ;
$cur.pos($SOLpos) ;
# Run the given regex on the cursor
$cur = $regex($cur) ;
# If pos is >= …
Run Code Online (Sandbox Code Playgroud) 要从sub返回的列表中保存2个值并抛出第三个值,可以;
(my $first, my $second) = (1, 2, 3);
print $first, "\n";
print $second, "\n";
exit 0;
Run Code Online (Sandbox Code Playgroud)
并且它按预期工作(在perl5和perl6中).如果你只想要第一个;
(my $first) = (1, 2, 3);
print $first, "\n";
exit 0;
Run Code Online (Sandbox Code Playgroud)
...你得到了整个清单.这似乎违反直觉 - 为什么不一致?
要了解perl6如何解析您的代码,您可以使用以下--target
选项:
$ perl6 --target=parse -e '"Hello World".say'
- statementlist: "Hello World".say
- statement: 1 matches
- EXPR: .say
- 0: "Hello World"
- value: "Hello World"
- quote: "Hello World"
- nibble: Hello World
- OPER: .say
- sym: .
- dottyop: say
- methodop: say
- longname: say
- name: say
- identifier: say
- O: <object>
- dotty: .say
- sym: .
- dottyop: say
- methodop: say
- longname: say
- name: say
- identifier: …
Run Code Online (Sandbox Code Playgroud) 使用正则表达式,标记或规则,可以像这样定义变量;
token directive {
:my $foo = "in command";
<command> <subject> <value>?
}
Run Code Online (Sandbox Code Playgroud)
在这里的语言文档中没有任何关于它的内容,而在S05 - Regexes和Rules中引用的内容很少;
任何语法正则表达式实际上只是一种方法,你可以使用冒号后跟任何由Perl 6语法解析的范围声明符来声明变量,包括my,my,state和constant.(作为准声明符,temp和let也被识别.)单个语句(通过终止分号或行结束右括号)被解析为普通的Perl 6代码:
token prove-nondeterministic-parsing {
:my $threshold = rand;
'maybe' \s+ <it($threshold)>
}
Run Code Online (Sandbox Code Playgroud)
我在语法中得到的regexen与类中的方法非常相似; 我知道你可以在一个规则内的任何地方启动一个块,如果解析成功到达那个点,那么块将被执行 - 但我不明白这个东西到底是什么.
有人可以清楚地定义它的范围; 解释它需要满足什么,并给出典型的用例?