Skip to content

Commit

Permalink
Coalesce sequences of regex literals
Browse files Browse the repository at this point in the history
When we have `/st/`, the compiler frontend currently produces two
RakuAST::Regex::Literal nodes. These were individually compiled also,
which breaks matching against the ligature st (with the appropriate
options set). Coalesce these as part of the compilation from RakuAST to
QAST, meaning that the same job doesn't have to be done by all those
synthetically constructing RakuAST in order to get correct matching.
  • Loading branch information
jnthn committed Jun 7, 2021
1 parent 203a83f commit f7244fc
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/Raku/ast/regex.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,35 @@ class RakuAST::Regex::Sequence is RakuAST::Regex {

method IMPL-REGEX-QAST(RakuAST::IMPL::QASTContext $context, %mods) {
my $concat := QAST::Regex.new(:rxtype<concat>);
my str $collect-literals := '';
for $!terms {
my $qast := $_.IMPL-REGEX-QAST($context, %mods);
if $qast {
$qast.backtrack('r') if %mods<r> && !$qast.backtrack;
$concat.push($qast);
if nqp::istype($_, RakuAST::Regex::Literal) {
$collect-literals := $collect-literals ~ $_.text;
}
else {
if $collect-literals ne '' {
$concat.push(self.IMPL-LIT($collect-literals, %mods));
$collect-literals := '';
}
my $qast := $_.IMPL-REGEX-QAST($context, %mods);
if $qast {
$qast.backtrack('r') if %mods<r> && !$qast.backtrack;
$concat.push($qast);
}
}
}
if $collect-literals ne '' {
$concat.push(self.IMPL-LIT($collect-literals, %mods));
}
$concat
}

method IMPL-LIT(str $text, %mods) {
self.IMPL-APPLY-LITERAL-MODS:
QAST::Regex.new( :rxtype<literal>, $text ),
%mods
}

method visit-children(Code $visitor) {
for $!terms {
$visitor($_);
Expand Down

0 comments on commit f7244fc

Please sign in to comment.