From 29b63609b710e7b4e44274682bf07b1d9fd32efc Mon Sep 17 00:00:00 2001 From: Carl Masak Date: Sat, 17 Apr 2010 16:08:49 +0200 Subject: [PATCH] [GGE] Further adaptions to Rakudo master - 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. --- lib/GGE/Exp.pm | 17 ++++++++++++----- lib/GGE/Perl6Regex.pm | 5 ++++- t/perl6regex/01-regex.t | 14 ++++++++------ test-regex | 8 +++++--- 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/lib/GGE/Exp.pm b/lib/GGE/Exp.pm index 50c3e5b..06f7db5 100644 --- a/lib/GGE/Exp.pm +++ b/lib/GGE/Exp.pm @@ -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) { @@ -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 diff --git a/lib/GGE/Perl6Regex.pm b/lib/GGE/Perl6Regex.pm index eda5bc8..8a27ca6 100644 --- a/lib/GGE/Perl6Regex.pm +++ b/lib/GGE/Perl6Regex.pm @@ -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); } diff --git a/t/perl6regex/01-regex.t b/t/perl6regex/01-regex.t index 5c51dd3..0aac650 100644 --- a/t/perl6regex/01-regex.t +++ b/t/perl6regex/01-regex.t @@ -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 { @@ -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); } diff --git a/test-regex b/test-regex index ac945f0..0e76686 100755 --- a/test-regex +++ b/test-regex @@ -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); }