Skip to content

Commit

Permalink
[GGE::Exp] moved enum types into classes
Browse files Browse the repository at this point in the history
The longer names make it clearer where they belong. Also, they're not enums
yet because I couldn't get named enums to work in this situation.
  • Loading branch information
Carl Masak committed May 30, 2010
1 parent 466b14f commit 8677a65
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 35 deletions.
57 changes: 31 additions & 26 deletions lib/GGE/Exp.pm
Expand Up @@ -37,29 +37,32 @@ role GGE::ShowContents {
}
}

# RAKUDO: Could name this one GGE::Exp::CUT or something, if enums
# with '::' in them worked, which they don't. [perl #71460]
our sub CUT_GROUP { -1 }
our sub CUT_RULE { -2 }
our sub CUT_MATCH { -3 }
#enum CUT (
# CUT_GROUP => -1,
# CUT_RULE => -2,
# CUT_MATCH => -3,
#);

our sub GREEDY { 0 }
our sub EAGER { 1 }
our sub NONE { 2 }
#enum GGE_BACKTRACK <
# GREEDY
# EAGER
# NONE
#>;

class GGE::Exp is GGE::Match {
our $group;

# RAKUDO: Should be just 'CutType' [RT #75454]
package GGE::Exp::CutType {
our sub CUT_GROUP { -1 }
our sub CUT_RULE { -2 }
our sub CUT_MATCH { -3 }
}
# RAKUDO: Enums don't work enough yet.
# our enum GGE::Exp::CutType (
# CUT_GROUP => -1,
# CUT_RULE => -2,
# CUT_MATCH => -3,
# );

# RAKUDO: Should be just 'Backtracking' [RT #75454]
package GGE::Exp::Backtracking {
our sub GREEDY { 0 }
our sub EAGER { 1 }
our sub NONE { 2 }
}
# RAKUDO: Enums don't work enough yet.
# our enum GGE::Exp::Backtracking <GREEDY EAGER NONE>;

method structure($indent = 0) {
# RAKUDO: The below was originally written as a map, but there's
# a bug somewhere in &map and lexical pads. The workaround
Expand Down Expand Up @@ -148,7 +151,7 @@ class GGE::Exp is GGE::Match {
}
when 'fail' {
local-return();
} ]], CUT_RULE);
} ]], GGE::Exp::CutType::CUT_RULE);
my $explabel = 'R';
$GGE::Exp::group = self;
my $exp = self.reduce;
Expand Down Expand Up @@ -180,7 +183,8 @@ class GGE::Exp is GGE::Match {
%hash<n> = $quant<max>;
%hash<N> = %hash<n> == Inf ?? '### ' !! '';
# RAKUDO: Waiting for named enums for this one
# my $bt = ($quant<backtrack> // GREEDY).name.lc;
# my $bt = ($quant<backtrack>
# // GGE::Exp::Backtracking::GREEDY).name.lc;
my $bt = 'no idea';
%hash<Q> = sprintf '%s..%s (%s)', %hash<m>, %hash<n>, $bt;
}
Expand Down Expand Up @@ -256,7 +260,7 @@ class GGE::Exp::Quant is GGE::Exp {
method contents() {
my ($min, $max, $bt) = map { self{$_} },
<min max backtrack>;
$bt //= GREEDY;
$bt //= GGE::Exp::Backtracking::GREEDY;
# RAKUDO: Named enums
# "{$bt.name.lc} $min..$max"
"no idea $min..$max"
Expand All @@ -277,7 +281,7 @@ class GGE::Exp::Quant is GGE::Exp {
%args<c> = 0;
%args<C> = '### ';
given self<backtrack> {
when EAGER() {
when GGE::Exp::Backtracking::EAGER() {
$code.emit( q[[
when '%L' { # quant %Q eager
push @gpad, 0;
Expand Down Expand Up @@ -308,7 +312,7 @@ class GGE::Exp::Quant is GGE::Exp {
goto('%1');
} ]], $replabel, $nextlabel, |%args);
}
when NONE() {
when GGE::Exp::Backtracking::NONE() {
# RAKUDO: Hash slices not implemented yet
# %args<c C> = $code.unique(), '';
%args<c> = $code.unique();
Expand Down Expand Up @@ -709,7 +713,7 @@ class GGE::Exp::CGroup is GGE::Exp::Group {

class GGE::Exp::Cut is GGE::Exp {
method reduce() {
if self<cutmark> > CUT_RULE() {
if self<cutmark> > GGE::Exp::CutType::CUT_RULE() {
my $group = $GGE::Exp::group;
if !$group<cutmark> {
$group<cutmark> = CodeString.unique();
Expand Down Expand Up @@ -802,7 +806,8 @@ class GGE::Exp::Subrule is GGE::Exp does GGE::ShowContents {
when '%L_cont' {
%4
goto('fail');
} ]], CUT_MATCH, $next, $captgen, $captsave, $captback, |%args);
} ]], GGE::Exp::CutType::CUT_MATCH, $next,
$captgen, $captsave, $captback, |%args);
}
}
}
20 changes: 11 additions & 9 deletions lib/GGE/Perl6Regex.pm
Expand Up @@ -471,23 +471,23 @@ class GGE::Perl6Regex {

$m.to = $mob.to;
if $mod2 eq ':?' {
$m<backtrack> = EAGER;
$m<backtrack> = GGE::Exp::Backtracking::EAGER;
$m.to += 2;
}
elsif $mod2 eq ':!' {
$m<backtrack> = GREEDY;
$m<backtrack> = GGE::Exp::Backtracking::GREEDY;
$m.to += 2;
}
elsif $mod1 eq '?' {
$m<backtrack> = EAGER;
$m<backtrack> = GGE::Exp::Backtracking::EAGER;
++$m.to;
}
elsif $mod1 eq '!' {
$m<backtrack> = GREEDY;
$m<backtrack> = GGE::Exp::Backtracking::GREEDY;
++$m.to;
}
elsif $mod1 eq ':' || $key eq ':' {
$m<backtrack> = NONE;
$m<backtrack> = GGE::Exp::Backtracking::NONE;
++$m.to;
}

Expand Down Expand Up @@ -652,7 +652,9 @@ class GGE::Perl6Regex {
$exp<sep> = perl6exp($sep, %pad);
}
%pad<isarray> = $isarray;
$exp<backtrack> //= %pad<ratchet> ?? NONE() !! GREEDY;
$exp<backtrack> //= %pad<ratchet>
?? GGE::Exp::Backtracking::NONE()
!! GGE::Exp::Backtracking::GREEDY;
return $exp;
}

Expand Down Expand Up @@ -730,9 +732,9 @@ class GGE::Perl6Regex {

multi sub perl6exp(GGE::Exp::Cut $exp is rw, %pad) {
$exp<cutmark> =
$exp.ast eq '::' ?? CUT_GROUP()
!! $exp.ast eq ':::' ?? CUT_RULE()
!! CUT_MATCH;
$exp.ast eq '::' ?? GGE::Exp::CutType::CUT_GROUP()
!! $exp.ast eq ':::' ?? GGE::Exp::CutType::CUT_RULE()
!! GGE::Exp::CutType::CUT_MATCH;
return $exp;
}

Expand Down

0 comments on commit 8677a65

Please sign in to comment.