取反命名正则表达式,或 Raku 中的字符 Class 插值
Negated Named Regex, or Character Class Interpolation in Raku
我正在尝试解析带引号的字符串。像这样:
say '"in quotes"' ~~ / '"' <-[ " ]> * '"'/;
(来自https://docs.raku.org/language/regexes "枚举字符类和范围")
但是...我想要不止一种类型的报价。像这样的东西组成了不起作用的语法:
token attribute_value { <quote> ($<-quote>) $<quote> };
token quote { <["']> };
我发现这个讨论是另一种方法,但它似乎没有任何进展:https://github.com/Raku/problem-solving/issues/97。
有没有办法做这种事情?谢谢!
更新 1
我无法让@user0721090601 的“多令牌”解决方案起作用。我的第一次尝试成功了:
$ ./multi-token.raku
No such method 'quoted_string' for invocant of type 'QuotedString'
in block <unit> at ./multi-token.raku line 16
在做了一些研究之后我添加了 proto token quoted_string {*}
:
#!/usr/bin/env raku
use Grammar::Tracer;
grammar QuotedString {
proto token quoted_string {*}
multi token quoted_string:sym<'> { <sym> ~ <sym> <-[']> }
multi token quoted_string:sym<"> { <sym> ~ <sym> <-["]> }
token quote { <["']> }
}
my $string = '"foo"';
my $quoted-string = QuotedString.parse($string, :rule<quoted_string>);
say $quoted-string;
$ ./multi-token.raku
quoted_string
* FAIL
(Any)
我还在学习 Raku,所以我可能做错了什么。
更新 2
哦!感谢@raiph 指出这一点。我忘了在 <-[']>
和 <-["]>
上加上量词。这就是我不假思索 copy/pasting 得到的!当你做对时找到作品:
#!/usr/bin/env raku
use Grammar::Tracer;
grammar QuotedString {
proto token quoted_string (|) {*}
multi token quoted_string:sym<'> { <sym> ~ <sym> <-[']>+ }
multi token quoted_string:sym<"> { <sym> ~ <sym> <-["]>+ }
token quote { <["']> }
}
my $string = '"foo"';
my $quoted-string = QuotedString.parse($string, :rule<quoted_string>);
say $quoted-string;
更新 3
只是为了向这个鞠躬...
#!/usr/bin/env raku
grammar NegativeLookahead {
token quoted_string { <quote> $<string>=([<!quote> .]+) $<quote> }
token quote { <["']> }
}
grammar MultiToken {
proto token quoted_string (|) {*}
multi token quoted_string:sym<'> { <sym> ~ <sym> $<string>=(<-[']>+) }
multi token quoted_string:sym<"> { <sym> ~ <sym> $<string>=(<-["]>+) }
}
use Bench;
my $string = "'foo'";
my $bench = Bench.new;
$bench.cmpthese(10000, {
negative-lookahead =>
sub { NegativeLookahead.parse($string, :rule<quoted_string>); },
multi-token =>
sub { MultiToken.parse($string, :rule<quoted_string>); },
});
$ ./bench.raku
Benchmark:
Timing 10000 iterations of multi-token, negative-lookahead...
multi-token: 0.779 wallclock secs (0.759 usr 0.033 sys 0.792 cpu) @ 12838.058/s (n=10000)
negative-lookahead: 0.912 wallclock secs (0.861 usr 0.048 sys 0.909 cpu) @ 10967.522/s (n=10000)
O--------------------O---------O-------------O--------------------O
| | Rate | multi-token | negative-lookahead |
O====================O=========O=============O====================O
| multi-token | 12838/s | -- | -20% |
| negative-lookahead | 10968/s | 25% | -- |
O--------------------O---------O-------------O--------------------O
我将采用“多令牌”解决方案。
谢谢大家!
您可以采用几种不同的方法——哪种方法最好可能取决于您所采用的结构的其余部分。
但首先要观察您当前的解决方案,以及为什么向其他人开放它不会以这种方式起作用。考虑字符串 'value"
。应该解析吗?你布置的结构实际上会匹配它!这是因为 each <quote>
令牌将匹配 单引号或双引号。
处理内心
最简单的解决方案是让你的内部成为一个非贪婪的通配符:
<quote> (.*?) <quote>
这将在您再次达到报价时立即停止比赛。另请注意使用波浪号的替代语法,使两个终端位更靠近:
<quote> ~ <quote> (.*?)
您最初的尝试是想使用一种不匹配。这确实以断言 <!quote>
的形式存在,如果找到 <quote>
它将失败(它不必只是一个字符,任何复杂的东西)。但是,它不会消耗,因此您需要单独提供它。例如
[<!quote> .]*
将检查某物不是引号,然后使用下一个字符。
最后,您可以使用这两种方法中的任何一种,并使用在内部处理的 <content>
令牌。如果您打算以后做更复杂的事情(例如转义字符),这实际上是一个很好的方法。
避免不匹配
如我所述,您的解决方案将解析不匹配的引号。所以我们需要有一种方法来确保我们正在(不)匹配的报价与开始报价相同。一种方法是使用 multi token
:
proto token attribute_value (|) { * }
multi token attribute_value:sym<'> { <sym> ~ <sym> <-[']> }
multi token attribute_value:sym<"> { <sym> ~ <sym> <-["]> }
(不需要使用实际的令牌 <sym>
,如果需要,您可以将其写成 { \' <-[']> \'}
)。
另一种方法是传递参数(按字面意思,或通过动态变量)。例如,您可以将 attribute_value
写成
token attribute_value {
$<start-quote>=<quote> # your actual start quote
:my $*end-quote; # define the variable in the regex scope
{ $*end-quote = ... } # determine the requisite end quote (e.g. ” for “)
<attribute_value_contents> # handle actual content
$*end-quote # fancy end quote
}
token attribute_value_contents {
# We have access to $*end-quote here, so we can use
# either of the techniques we've described before
# (a) using a look ahead
[<!before $*end-quote> .]*
# (b) being lazy (the easier)
.*?
# (c) using another token (described below)
<attr_value_content_char>+
}
我提到最后一个,因为如果您最终决定允许转义字符,您甚至可以进一步委托。例如,您可以执行
proto token attr_value_content_char (|) { * }
multi token attr_value_content_char:sym<escaped> { \ $*end-quote }
multi token attr_value_content_char:sym<literal> { . <?{ $/ ne $*end-quote }> }
但是如果这对你正在做的事情来说太过分了,嗯:-)
无论如何,我可能没有想到其他人能想到的其他方法,但希望这些方法能让您走上正确的道路。 (另外部分代码未经测试,所以可能存在轻微错误,对此深表歉意)
假设您只想再次匹配相同的引号字符。
token attribute-value { <string> }
token string {
# match <quote> and expect to end with "$<quote>"
<quote> ~ "$<quote>"
[
# update match structure in $/ otherwise "$<quote>" won't work
{}
<!before "$<quote>"> # next character isn't the same as $<quote>
. # any character
]* # any number of times
}
token quote { <["']> }
对于任何更复杂的事情,请使用之前答案中的 $*end-quote
动态变量。
我正在尝试解析带引号的字符串。像这样:
say '"in quotes"' ~~ / '"' <-[ " ]> * '"'/;
(来自https://docs.raku.org/language/regexes "枚举字符类和范围") 但是...我想要不止一种类型的报价。像这样的东西组成了不起作用的语法:
token attribute_value { <quote> ($<-quote>) $<quote> };
token quote { <["']> };
我发现这个讨论是另一种方法,但它似乎没有任何进展:https://github.com/Raku/problem-solving/issues/97。 有没有办法做这种事情?谢谢!
更新 1
我无法让@user0721090601 的“多令牌”解决方案起作用。我的第一次尝试成功了:
$ ./multi-token.raku
No such method 'quoted_string' for invocant of type 'QuotedString'
in block <unit> at ./multi-token.raku line 16
在做了一些研究之后我添加了 proto token quoted_string {*}
:
#!/usr/bin/env raku
use Grammar::Tracer;
grammar QuotedString {
proto token quoted_string {*}
multi token quoted_string:sym<'> { <sym> ~ <sym> <-[']> }
multi token quoted_string:sym<"> { <sym> ~ <sym> <-["]> }
token quote { <["']> }
}
my $string = '"foo"';
my $quoted-string = QuotedString.parse($string, :rule<quoted_string>);
say $quoted-string;
$ ./multi-token.raku
quoted_string
* FAIL
(Any)
我还在学习 Raku,所以我可能做错了什么。
更新 2
哦!感谢@raiph 指出这一点。我忘了在 <-[']>
和 <-["]>
上加上量词。这就是我不假思索 copy/pasting 得到的!当你做对时找到作品:
#!/usr/bin/env raku
use Grammar::Tracer;
grammar QuotedString {
proto token quoted_string (|) {*}
multi token quoted_string:sym<'> { <sym> ~ <sym> <-[']>+ }
multi token quoted_string:sym<"> { <sym> ~ <sym> <-["]>+ }
token quote { <["']> }
}
my $string = '"foo"';
my $quoted-string = QuotedString.parse($string, :rule<quoted_string>);
say $quoted-string;
更新 3
只是为了向这个鞠躬...
#!/usr/bin/env raku
grammar NegativeLookahead {
token quoted_string { <quote> $<string>=([<!quote> .]+) $<quote> }
token quote { <["']> }
}
grammar MultiToken {
proto token quoted_string (|) {*}
multi token quoted_string:sym<'> { <sym> ~ <sym> $<string>=(<-[']>+) }
multi token quoted_string:sym<"> { <sym> ~ <sym> $<string>=(<-["]>+) }
}
use Bench;
my $string = "'foo'";
my $bench = Bench.new;
$bench.cmpthese(10000, {
negative-lookahead =>
sub { NegativeLookahead.parse($string, :rule<quoted_string>); },
multi-token =>
sub { MultiToken.parse($string, :rule<quoted_string>); },
});
$ ./bench.raku
Benchmark:
Timing 10000 iterations of multi-token, negative-lookahead...
multi-token: 0.779 wallclock secs (0.759 usr 0.033 sys 0.792 cpu) @ 12838.058/s (n=10000)
negative-lookahead: 0.912 wallclock secs (0.861 usr 0.048 sys 0.909 cpu) @ 10967.522/s (n=10000)
O--------------------O---------O-------------O--------------------O
| | Rate | multi-token | negative-lookahead |
O====================O=========O=============O====================O
| multi-token | 12838/s | -- | -20% |
| negative-lookahead | 10968/s | 25% | -- |
O--------------------O---------O-------------O--------------------O
我将采用“多令牌”解决方案。 谢谢大家!
您可以采用几种不同的方法——哪种方法最好可能取决于您所采用的结构的其余部分。
但首先要观察您当前的解决方案,以及为什么向其他人开放它不会以这种方式起作用。考虑字符串 'value"
。应该解析吗?你布置的结构实际上会匹配它!这是因为 each <quote>
令牌将匹配 单引号或双引号。
处理内心
最简单的解决方案是让你的内部成为一个非贪婪的通配符:
<quote> (.*?) <quote>
这将在您再次达到报价时立即停止比赛。另请注意使用波浪号的替代语法,使两个终端位更靠近:
<quote> ~ <quote> (.*?)
您最初的尝试是想使用一种不匹配。这确实以断言 <!quote>
的形式存在,如果找到 <quote>
它将失败(它不必只是一个字符,任何复杂的东西)。但是,它不会消耗,因此您需要单独提供它。例如
[<!quote> .]*
将检查某物不是引号,然后使用下一个字符。
最后,您可以使用这两种方法中的任何一种,并使用在内部处理的 <content>
令牌。如果您打算以后做更复杂的事情(例如转义字符),这实际上是一个很好的方法。
避免不匹配
如我所述,您的解决方案将解析不匹配的引号。所以我们需要有一种方法来确保我们正在(不)匹配的报价与开始报价相同。一种方法是使用 multi token
:
proto token attribute_value (|) { * }
multi token attribute_value:sym<'> { <sym> ~ <sym> <-[']> }
multi token attribute_value:sym<"> { <sym> ~ <sym> <-["]> }
(不需要使用实际的令牌 <sym>
,如果需要,您可以将其写成 { \' <-[']> \'}
)。
另一种方法是传递参数(按字面意思,或通过动态变量)。例如,您可以将 attribute_value
写成
token attribute_value {
$<start-quote>=<quote> # your actual start quote
:my $*end-quote; # define the variable in the regex scope
{ $*end-quote = ... } # determine the requisite end quote (e.g. ” for “)
<attribute_value_contents> # handle actual content
$*end-quote # fancy end quote
}
token attribute_value_contents {
# We have access to $*end-quote here, so we can use
# either of the techniques we've described before
# (a) using a look ahead
[<!before $*end-quote> .]*
# (b) being lazy (the easier)
.*?
# (c) using another token (described below)
<attr_value_content_char>+
}
我提到最后一个,因为如果您最终决定允许转义字符,您甚至可以进一步委托。例如,您可以执行
proto token attr_value_content_char (|) { * }
multi token attr_value_content_char:sym<escaped> { \ $*end-quote }
multi token attr_value_content_char:sym<literal> { . <?{ $/ ne $*end-quote }> }
但是如果这对你正在做的事情来说太过分了,嗯:-)
无论如何,我可能没有想到其他人能想到的其他方法,但希望这些方法能让您走上正确的道路。 (另外部分代码未经测试,所以可能存在轻微错误,对此深表歉意)
假设您只想再次匹配相同的引号字符。
token attribute-value { <string> }
token string {
# match <quote> and expect to end with "$<quote>"
<quote> ~ "$<quote>"
[
# update match structure in $/ otherwise "$<quote>" won't work
{}
<!before "$<quote>"> # next character isn't the same as $<quote>
. # any character
]* # any number of times
}
token quote { <["']> }
对于任何更复杂的事情,请使用之前答案中的 $*end-quote
动态变量。