Skip to content

Commit

Permalink
Initial protoregex nfa generation and merging.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmichaud committed Aug 15, 2011
1 parent 8c346b0 commit 94795ee
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
29 changes: 28 additions & 1 deletion src/QRegex/Cursor.nqp
Expand Up @@ -92,7 +92,34 @@ role NQPCursorRole {
}

method !protoregex($name) {
pir::say($name);
_dumper(self."!protoregex_nfa"($name));
}

method !protoregex_nfa($name) {
my %protorx := self."!protoregex_table"();
my $nfa := QRegex::NFA.new;
my @fates := $nfa.states[0];
my $start := 1;
my $fate := 0;
my $prefix := $name ~ ':sym<';
my $prefixchars := nqp::chars($prefix);
for %protorx {
my $rxname := $_.key;
if nqp::substr($rxname, 0, $prefixchars) eq $prefix {
$fate := $fate + 1;
$nfa.mergesubrule($start, $fate, self, $rxname);
}
}
$nfa;
}

method !protoregex_table() {
my %protorx;
for self.HOW.methods(self, :local(0)) -> $meth {
my $methname := ~$meth;
%protorx{$methname} := $meth if nqp::index($methname, ':sym<') >0;
}
%protorx;
}

method !BACKREF($name) {
Expand Down
34 changes: 33 additions & 1 deletion src/QRegex/NFA.nqp
Expand Up @@ -113,12 +113,44 @@ class QRegex::NFA {
$past;
}

method mergesubrule($start, $fate, $cursor, $name) {
nqp::say("adding $name");
my $subrule := $cursor.HOW.find_method($cursor, $name);
my @substates := $subrule.nqpattr('nfa') if $subrule;
if @substates {
# append a clone of the new states to our states
my $substart := nqp::elems($!states);
for @substates { nqp::push($!states, nqp::clone($_)) }
my $subend := nqp::elems($!states);
# Go through all of the newly added states, and
# apply $substart offset to target states
# adjust fate edges to be $fate
# append any subrules
my $i := $substart;
while $i < $subend {
my $substate := $!states[$i];
my $j := 0;
my $k := nqp::elems($substate);
while $j < $k {
$substate[$j+2] := $substate[$j+2] + $substart;
$substate[$j+1] := $fate if $substate[$j] == $EDGE_FATE;
$j := $j + 3;
}
$i := $i + 1;
}
self.addedge($start, $substart+1, $EDGE_EPSILON, 0);
}
else {
self.addedge($start, 0, $EDGE_FATE, $fate);
}
}

method __dump($dumper, $label) {
my $subindent := $dumper.'newIndent'();
print('[');
my $st := 0;
for $!states {
print(pir::sprintf__SsP("\n%03d: [%s]", [$st, nqp::join(', ', $_)]));
print(nqp::sprintf("\n%03d: [%s]", [$st, nqp::join(', ', $_)]));
$st := $st + 1;
}
$dumper.deleteIndent();
Expand Down

0 comments on commit 94795ee

Please sign in to comment.