Skip to content

Commit

Permalink
Fix heredoc despacing for real, now with a working test too
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed Jun 27, 2011
1 parent ae25334 commit 4d02280
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
80 changes: 80 additions & 0 deletions src/niecza
Expand Up @@ -23,6 +23,86 @@ use RxOp;
use Sig;
use STD;

augment class NieczaActions {
sub mkstringycat($/, *@strings) {
my @a;
for @strings -> $s {
my $i = ($s !~~ Op) ?? ::Op::StringLiteral.new(|node($/),
text => $s) !! $s;

# this *might* belong in an optimization pass
if @a && @a[*-1] ~~ ::Op::StringLiteral &&
$i ~~ ::Op::StringLiteral {
@a[*-1] = ::Op::StringLiteral.new(|node($/),
text => (@a[*-1].text ~ $i.text));
} else {
push @a, $i;
}
}
if @a == 0 {
return ::Op::StringLiteral.new(|node($/), text => "");
} elsif @a == 1 {
return (@a[0] ~~ ::Op::StringLiteral) ?? @a[0] !!
mkcall($/, '&prefix:<~>', @a[0]);
} else {
return mkcall($/, '&infix:<~>', @a);
}
}
method process_nibble($/, @bits, $prefix?) {
my @acc;
for @bits -> $n {
my $ast = $n.ast;

if $ast ~~ CClass {
$n.CURSOR.sorry("Cannot use a character class in a string");
$ast = "";
}

if $ast !~~ Op && defined($prefix) && $prefix ne "" {
my $start_nl = !$n.from || "\r\n".index(
substr($n.orig, $n.from-1, 1)).defined;
$ast = $ast.split(/ ^^ [ <?{ $start_nl }> || <?after <[\r\n]> > ]
<before \h>[ $prefix || \h+ ]/).join("");
}

push @acc, $ast;
}

my $post = $/.CURSOR.postprocessor;
make mkstringycat($/, @acc);

if $post eq 'null' {
# already OK
}
# actually quotewords is a bit trickier than this...
elsif $post eq 'words' || $post eq 'quotewords' {
my $sl = $/.ast;
if !$sl.^isa(::Op::StringLiteral) {
make ::Op::CallMethod.new(|node($/), :name<words>, receiver => $sl);
}
else {
my @tok = $sl.text.words;
@tok = map { ::Op::StringLiteral.new(|node($/), text => $_) }, @tok;

make ((@tok == 1) ?? @tok[0] !! ::Op::Paren.new(|node($/),
inside => ::Op::SimpleParcel.new(|node($/), items => @tok)));
}
}
elsif $post eq 'path' {
# TODO could stand to be a lot fancier.
make ::Op::CallMethod(|node($/), receiver => $/.ast, :name<IO>);
}
elsif $post eq 'run' {
make mkcall($/, 'rungather', $/.ast);
}
else {
$/.CURSOR.sorry("Unhandled postprocessor $post");
}

$/.ast;
}
}

CgOp._register_ops: <
>;

Expand Down
6 changes: 3 additions & 3 deletions test2.pl
Expand Up @@ -129,9 +129,9 @@
{
my $x = 'Bar';
my $in = qq:to [A] ;
Foo $x
A
is $in.substr(0,8), ' Foo Bar', "spaces preserved after heredoc interpolation";
$x Foo
A
is $in.substr(0,8), 'Bar Foo', "spaces preserved after heredoc interpolation";
}
#is $?ORIG.substr(0,5), '# vim', '$?ORIG works';
Expand Down

0 comments on commit 4d02280

Please sign in to comment.