Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
enforce invariant of correct # of NFA fates
The mergesubstates method now always guarantees that states[0] has a
size reflecting the number of fates used by this NFA.  Previously it
relied on its caller to set the fates list, and since /@array/ didn't
do that (oops), the fate count was off, resulting in reallocations
in the NFA engine.  Since alternatives just use the fate numbers
directly, they no longer have to set the fates.  Protoregexes, on
the other hand, still override the fate number with rxname, but now
do so by setting it after mergesubstates sets it to the number.
  • Loading branch information
TimToady committed Nov 29, 2014
1 parent 3254b59 commit 0cf0677
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
4 changes: 1 addition & 3 deletions src/QRegex/Cursor.nqp
Expand Up @@ -341,8 +341,8 @@ role NQPCursorRole is export {
if nqp::existskey(%protorx, $name) {
for %protorx{$name} -> $rxname {
$fate := $fate + 1;
@fates[$fate] := $rxname;
$nfa.mergesubrule($start, 0, $fate, self, $rxname);
@fates[$fate] := $rxname; # override default fate #
}
}
$nfa;
Expand Down Expand Up @@ -381,11 +381,9 @@ role NQPCursorRole is export {

method !alt_nfa($regex, str $name) {
my $nfa := QRegex::NFA.new;
my @fates := $nfa.states[0];
my int $start := 1;
my int $fate := 0;
for $regex.ALT_NFA($name) {
@fates[$fate] := $fate;
$nfa.mergesubstates($start, 0, $fate, $_, self);
$fate++;
}
Expand Down
5 changes: 3 additions & 2 deletions src/QRegex/NFA.nqp
Expand Up @@ -429,6 +429,7 @@ class QRegex::NFA {

method mergesubstates($start, $to, $fate, @substates, $cursor, %seen?) {
# nqp::say("mergesubstates start $start to $to fate $fate") if $nfadeb;
$!states[0][$fate] := $fate; # overridden by !protoregex_nfa
if @substates {
# create an empty end state for the subrule's NFA
my int $substart := self.addstate();
Expand Down Expand Up @@ -492,7 +493,7 @@ class QRegex::NFA {
# my $t1 := nqp::time_n();
# $nfatime := $nfatime + $t1 - $t0;
# if nqp::chars($target) == $offset {
# nqp::printfh(nqp::getstderr(), "EOS in proto at $offset nfa " ~ $nfatime ~ " etc " ~ $etctime ~ "\n");
# nqp::printfh(nqp::getstderr(), "EOS in proto at $offset " ~ $nfatime ~ " / " ~ $etctime ~ " " ~ ($nfatime / ($etctime + $nfatime)) ~ "\n");
# }
# $lastnfatime := $t1;
$result;
Expand All @@ -510,7 +511,7 @@ class QRegex::NFA {
# my $t1 := nqp::time_n();
# $nfatime := $nfatime + $t1 - $t0;
# if nqp::chars($target) == $offset {
# nqp::printfh(nqp::getstderr(), "EOS in alt at $offset nfa " ~ $nfatime ~ " etc " ~ $etctime ~ "\n");
# nqp::printfh(nqp::getstderr(), "EOS in alt at $offset " ~ $nfatime ~ " / " ~ $etctime ~ " " ~ ($nfatime / ($etctime + $nfatime)) ~ "\n");
# }
# $lastnfatime := $t1;
$result;
Expand Down

0 comments on commit 0cf0677

Please sign in to comment.