Skip to content
Browse files

Fix heredoc despacing for real, now with a working test too

  • Loading branch information...
1 parent ae25334 commit 4d0228092c147023528b330dccbf6e8b1bff22c8 @sorear committed Jun 27, 2011
Showing with 83 additions and 3 deletions.
  1. +80 −0 src/niecza
  2. +3 −3 test2.pl
View
80 src/niecza
@@ -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: <
>;
View
6 test2.pl
@@ -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';

0 comments on commit 4d02280

Please sign in to comment.
Something went wrong with that request. Please try again.