我正在尝试在Perl中为Mustache编写一个tokenizer。我可以轻松处理大部分这样的令牌:
#!/usr/bin/perl
use strict;
use warnings;
my $comment = qr/ \G \{\{ ! (?<comment> .+? ) }} /xs;
my $variable = qr/ \G \{\{ (?<variable> .+? ) }} /xs;
my $text = qr/ \G (?<text> .+? ) (?= \{\{ | \z ) /xs;
my $tokens = qr/ $comment | $variable | $text /x;
my $s = do { local $/; <DATA> };
while ($s =~ /$tokens/g) {
my ($type) = keys %+;
(my $contents = $+{$type}) =~ s/\n/\\n/;
print "type [$type] contents [$contents]\n";
}
__DATA__
{{!this is a comment}}
Hi {{name}}, I like {{thing}}.
但是我遇到了Set Delimiters指令的问题:
#!/usr/bin/perl
use strict;
use warnings;
my $delimiters = qr/ \G \{\{ (?<start> .+? ) = [ ] = (?<end> .+?) }} /xs;
my $comment = qr/ \G \{\{ ! (?<comment> .+? ) }} /xs;
my $variable = qr/ \G \{\{ (?<variable> .+? ) }} /xs;
my $text = qr/ \G (?<text> .+? ) (?= \{\{ | \z ) /xs;
my $tokens = qr/ $comment | $delimiters | $variable | $text /x;
my $s = do { local $/; <DATA> };
while ($s =~ /$tokens/g) {
for my $type (keys %+) {
(my $contents = $+{$type}) =~ s/\n/\\n/;
print "type [$type] contents [$contents]\n";
}
}
__DATA__
{{!this is a comment}}
Hi {{name}}, I like {{thing}}.
{{(= =)}}
如果我将其更改为
my $delimiters = qr/ \G \{\{ (?<start> [^{]+? ) = [ ] = (?<end> .+?) }} /xs;
它工作正常,但Set Delimiters指令的要点是更改分隔符,因此代码将看起来像
my $variable = qr/ \G $start (?<variable> .+? ) $end /xs;
说{{{== ==}}}
完全有效(即将分隔符更改为{=
和=}
)。我想要的,但也许不是我需要的,是能够说出(?:not starting string)+?
之类的东西。我想我只是不得不放弃对它的清理并将代码放入正则表达式以迫使它只匹配我想要的东西。我试图避免这种情况有四个原因:
(?{CODE})
并返回特殊值。(?(condition)yes-pattern|no-pattern)
)。只是为了清楚(我希望),我试图匹配一个恒定长度的起始分隔符,后跟最短的字符串,允许匹配,并且不包含起始分隔符,后跟一个空格,后跟一个等号,后跟允许匹配以结束分隔符结束的最短字符串。
答案 0 :(得分:3)
使用负前瞻断言。像这样:
my $variable = qr/ \G $start (?<variable> (.(?!$end))+ ) $end /xs;
答案 1 :(得分:2)
对于那些好奇的人,接下来是用Perl 5.10风格编写的Mustache的完整标记器。现在我只需要编写解析器和渲染器。
#!/usr/bin/perl
use 5.010_000;
use strict;
use warnings;
sub gen_tokenizer {
my ($s, $e) = @_;
my ($start, $end) = map { quotemeta } $s, $e;
my $unescaped = "$s $e" eq "{{ }}" ?
qr/ \G \{{3} (?<unescaped> .+?) }{3} /xs :
qr{ \G $start & (?<unescaped> .+? ) $end }xs;
return qr{
$unescaped |
\G $start (?:
! (?<comment> .+? ) |
> (?<partial> .+? ) |
\# (?<enum_start> .+? ) |
/ (?<enum_stop> .+? ) |
(?<start> (?: . (?! $end ) )+? ) = [ ] = (?<end> .+? ) |
(?<variable> .+? )
) $end |
(?<text> .+? ) (?= $start | \z )
}xs;
}
my $template = do { local $/; <DATA> };
my $tokenizer = gen_tokenizer "{{", "}}";
while ($template =~ /$tokenizer/g) {
my @types = keys %+;
if (@types == 1) {
my $type = $types[0];
(my $contents = $+{$type}) =~ s/\n/\\n/g;
say "$type: [$contents]";
} else {
$tokenizer = gen_tokenizer $+{start}, $+{end};
say "set_delim: [$+{start} $+{end}]";
}
}
__DATA__
{{!this is a comment}}
{{{html header}}}
Hi {{name}}, I like {{thing}}.
{{(= =)}}
(#optional)
This will only print if optional is set
(/optional)
(&html footer)