Permalink
Browse files

[GGE::Exp] allow a :stepwise option for debugging

This feature is meant to mimick TheDamian's Regexp::Grammars. The present
commit is only a first cut in that direction. More precisely, this commit
creates a somewhat similar output, but doesn't yet attempt to mimick the
CPAN module's output in exact detail.
  • Loading branch information...
masak committed Jul 31, 2010
1 parent 3eeca11 commit 147d1aff41863c7d08c028d3892d09c69fe551b3
Showing with 19 additions and 7 deletions.
  1. +17 −5 lib/GGE/Exp.pm
  2. +2 −2 lib/GGE/Grammar.pm
View
@@ -95,19 +95,19 @@ class GGE::Exp is GGE::Match {
$MATCH = $grammar;
}
if $name {
- $code.emit( q[[ method %0(:$debug) {
+ $code.emit( q[[ method %0(:$debug, :$stepwise) {
my $m = self;
]], $name );
}
else {
- $code.emit( q[[ sub ($m, :$debug) { ]] );
+ $code.emit( q[[ sub ($m, :$debug, :$stepwise) { ]] );
}
$code.emit( q[[
my $mob = %1.new($m);
my $target = $mob.target;
my $iscont = $mob.iscont;
my $mfrom;
- my $cpos = $mob.startpos;
+ my $cpos = $mob.startpos max 0;
my $pos;
my $rep;
my $lastpos = $target.chars;
@@ -123,6 +123,11 @@ class GGE::Exp is GGE::Match {
@cstack.push($label)
};
my &local-return = -> { @cstack.pop };
+ my &stepwise-say = -> *@_ {
+ prompt @_.join ~ ' ' ~ '.' x (58 - @_.join.chars) ~ ' '
+ if $stepwise;
+ };
+ stepwise-say "Calling rule '$name'";
loop {
given @cstack[*-1] {
when 'try_match' {
@@ -134,10 +139,15 @@ class GGE::Exp is GGE::Match {
when 'try_match_cont' {
if $cutmark <= %0 { goto('fail_cut'); break; }
++$cpos;
- if $iscont { goto('try_match'); break; }
+ if $iscont {
+ stepwise-say
+ "Backtrack. Resetting rule '$name' to position $cpos";
+ goto('try_match'); break;
+ }
goto('fail_rule');
}
when 'fail_rule' {
+ stepwise-say "Rule '$name' failed";
# $cutmark = %0 # XXX: Not needed yet
goto('fail_cut');
}
@@ -147,6 +157,7 @@ class GGE::Exp is GGE::Match {
return $mob;
}
when 'succeed' {
+ stepwise-say "Rule '$name' succeeded";
$mob.from = $mfrom;
$mob.to = $pos;
return $mob;
@@ -767,7 +778,8 @@ class GGE::Exp::Subrule is GGE::Exp does GGE::ShowContents {
unless $mob.can('%0') {
die "Unable to find regex '%0'";
}
- $captob = $captob.%0(%1); ]], $subname, $subarg, |%args);
+ $captob = $captob.%0(:$stepwise, %1); ]], $subname, $subarg,
+ |%args);
if self.hash-access('iszerowidth') {
my $test = self.hash-access('isnegated') ?? 'unless' !! 'if';
$code.emit( q[[
View
@@ -3,10 +3,10 @@ use v6;
use GGE::Match;
class GGE::Grammar is GGE::Match {
- method parse($target, :$debug) {
+ method parse($target, :$debug, :$stepwise) {
die "Cannot call .parse on a grammar with no TOP rule"
unless self.can('TOP');
my $m = self.new($target);
- $m.TOP(:$debug);
+ $m.TOP(:$debug, :$stepwise);
}
}

0 comments on commit 147d1af

Please sign in to comment.