Mar*_*rty 9 perl6 regexp-grammars
在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 >= 0, we found what we were looking for
if $cur.pos >= 0 {
$cur."!cursor_pass"(self.pos, 'SOLscan')
}
self
}
token line {
{ $SOLpos = self.pos ; say '$SOLpos = ' ~ $SOLpos }
[
|| <word> <ws> 'two' { say 'matched two' } <SOLscan \w+> <ws> <word>
|| <word>+ %% <ws> { say 'matched words' }
]
\n
}
token word { \S+ }
token ws { \h+ }
}
my $mo = G.subparse: q:to/END/ ;
hello world
one two three
END
Run Code Online (Sandbox Code Playgroud)
实际上,这段代码产生:
$ ./h.pl
$SOLpos = 0
matched words
$SOLpos = 12
matched two
Too many positionals passed; expected 1 argument but got 2
in method SOLscan at ./h.pl line 14
in regex line at ./h.pl line 32
in regex TOP at ./h.pl line 7
in block <unit> at ./h.pl line 41
$
Run Code Online (Sandbox Code Playgroud)
第14行是$cur.from($SOLpos).如果注释掉,第15行会产生相同的错误.似乎.pos和.from只读...(也许:-)
有什么想法适当的咒语是什么?注意,任何提出的解决方案都可以与我在这里完成的工作相去甚远 - 我真正想要做的就是理解应该如何使用该机制.
| 归档时间: |
|
| 查看次数: |
148 次 |
| 最近记录: |