我想拥有一个语法,它是否存在空格...我想匹配:
this ' <foo> <bar> <baz> '
and also this '<foo><bar><baz>'
Run Code Online (Sandbox Code Playgroud)
这有效:
token TOP { \s* <foo> \s* <bar> \s* <baz> \s* }
Run Code Online (Sandbox Code Playgroud)
但是,在阅读了有关:sigspace, <。ws >和规则的所有信息之后,我可以想象有一种方法可以避免重复的* \ s。(即,如何在per6语法中匹配十六进制数组)
请问有人可以告诉我在perl6语法中是否还有更好的方法?
注意 这不能通过简单地将令牌声明符更改为rule来解决 -当我尝试这种方法时,解析字符串中要么匹配空格,要么不匹配空格(但不能同时匹配两个)。
在将一种音乐语言翻译成另一种音乐语言(ABC 到 Alda)作为学习 Raku DSL 能力的借口的过程中,我注意到似乎没有办法终止.parse! 这是我缩短的演示代码:
#!/home/hsmyers/rakudo741/bin/perl6
use v6d;
# use Grammar::Debugger;
use Grammar::Tracer;
my $test-n01 = q:to/EOS/;
a b c d e f g
A B C D E F G
EOS
grammar test {
token TOP { <score>+ }
token score {
<.ws>?
[
| <uc>
| <lc>
]+
<.ws>?
}
token uc { <[A..G]> }
token lc { <[a..g]> }
}
test.parse($test-n01).say;
Run Code Online (Sandbox Code Playgroud)
Grammer::Tracer 显示的最后一部分说明了我的问题。
| score
| | uc
| | * MATCH "G" …Run Code Online (Sandbox Code Playgroud)