Skip to content

Commit

Permalink
[GGE] Further adaptions to Rakudo master
Browse files Browse the repository at this point in the history
- Rewrote CodeString.emit to do string plumbing instead of using .subst --
  slightly more kosher, but still uses /\d/ and /\w/ for convenience.

- Changed a class-bound variable from 'my' to 'our'. (The alpha branch of
  Rakudo wrongly accepted the former variant.)

- Added an extra layer of parens in the parameter list of
  GGE::Perl6Regex.postcircumfix:<( )>

- Worked around the <-[\t]> bug. \T is a much better way to write this
  anyway.

- Put in a few stringifications where they were suddenly needed.

- Cured a case of sudden overinterpolation in a qq string.

- Replaced two well-placed Str.trans calls with a lot of Str.subst calls,
  since we don't have the former properly yet in Rakudo master.
  • Loading branch information
Carl Masak committed Apr 17, 2010
1 parent 5f369ef commit 29b6360
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
17 changes: 12 additions & 5 deletions lib/GGE/Exp.pm
Expand Up @@ -5,10 +5,17 @@ class CodeString {
has Str $!contents = '';
my $counter = 0;

method emit($string, *@args, *%kwargs) {
$!contents ~= $string\
.subst(/\%(\d)/, { @args[$0] // '...' }, :g)\
.subst(/\%(\w)/, { %kwargs{$0} // '...' }, :g);
method emit($string is copy, *@args, *%kwargs) {
while index($string, '%') -> $pos {
my $new;
given substr($string, $pos + 1, 1) {
when /\d/ { $new = @args[$_.Int] // '...' }
when /\w/ { $new = %kwargs{$_} // '...' }
default { die "Illegal subststr %" ~ $_ }
}
$string = $string.substr(0, $pos) ~ $new ~ $string.substr($pos + 2)
}
$!contents ~= $string;
}

method escape($string) {
Expand Down Expand Up @@ -51,7 +58,7 @@ sub NONE { 2 }
#>;

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

method structure($indent = 0) {
# RAKUDO: The below was originally written as a map, but there's
Expand Down
5 changes: 4 additions & 1 deletion lib/GGE/Perl6Regex.pm
Expand Up @@ -131,7 +131,10 @@ class GGE::Perl6Regex {
return self.bless(*, :$exp, :$binary);
}

method postcircumfix:<( )>($target, :$debug) {
# Why the double parens in the parameter list? Apparently, the
# postcircumfix:<( )> method gets called with a Capture, so we have to
# unpack that.
method postcircumfix:<( )>( ($target, :$debug) ) {
$!binary($target, :$debug);
}

Expand Down
14 changes: 8 additions & 6 deletions t/perl6regex/01-regex.t
Expand Up @@ -30,12 +30,12 @@ for @test-files -> $test-file {
}
next if $line ~~ /^ \#/;
$i++;
$line ~~ /^ (<-[\t]>*) \t+ (<-[\t]>+) \t+ (<-[\t]>+) \t+ (.*) $/
$line ~~ /^ (\T*) \t+ (\T+) \t+ (\T+) \t+ (.*) $/
or die "Unrecognized line format: $line";
my ($pattern, $target, $result, $description) = $0, $1, $2, $3;
$target = $target eq q[''] ?? '' !! backslash_escape($target);
$result = backslash_escape($result);
my $full-description = "[$test-file:$i] $description";
$target = $target eq q[''] ?? '' !! backslash_escape(~$target);
$result = backslash_escape(~$result);
my $full-description = "[$test-file" ~ ":$i] $description";
my $match;
my $failed = 1; # RAKUDO: Manual CATCH workaround
try {
Expand Down Expand Up @@ -75,8 +75,10 @@ sub match_perl6regex($pattern, $target) {
}

sub backslash_escape($string) {
return $string.trans(['\n', '\r', '\e', '\t', '\f'] =>
["\n", "\r", "\e", "\t", "\f"])\
# RAKUDO: No .trans again yet
#return $string.trans(['\n', '\r', '\e', '\t', '\f'] =>
# ["\n", "\r", "\e", "\t", "\f"])\
return $string.subst(/\\n/, "\n", :g).subst(/\\r/, "\r", :g).subst(/\\e/, "\e", :g).subst(/\\t/, "\t", :g).subst(/\\f/, "\f", :g)\
.subst(/'\\x' (<[0..9a..f]>**{2..4})/, { chr(:16($0)) }, :g);
}

Expand Down
8 changes: 5 additions & 3 deletions test-regex
Expand Up @@ -13,7 +13,9 @@ my GGE::Match $match
say $match ?? $match.dump_str('mob', ' ', '') !! "No match\n";

sub backslash_escape($string) {
return $string.trans(['\n', '\r', '\e', '\t', '\f'] =>
["\n", "\r", "\e", "\t", "\f"])\
.subst(/'\\x' (<[0..9a..f]>**{4})/, { chr(:16($0)) }, :g);
# RAKUDO: No .trans again yet
#return $string.trans(['\n', '\r', '\e', '\t', '\f'] =>
# ["\n", "\r", "\e", "\t", "\f"])\
return $string.subst(/\\n/, "\n", :g).subst(/\\r/, "\r", :g).subst(/\\e/, "\e", :g).subst(/\\t/, "\t", :g).subst(/\\f/, "\f", :g)\
.subst(/'\\x' (<[0..9a..f]>**{2..4})/, { chr(:16($0)) }, :g);
}

0 comments on commit 29b6360

Please sign in to comment.