diff --git a/lib/ast.js b/lib/ast.js index 963692aef..23987f8b4 100644 --- a/lib/ast.js +++ b/lib/ast.js @@ -343,6 +343,7 @@ exports.Block = Block = (function(superclass){ }; prototype.makeReturn = function(it){ var that, ref$, key$, ref1$; + this.chomp(); if (that = (ref1$ = ref$ = this.lines)[key$ = ref1$.length - 1] != null ? ref$[key$] = ref$[key$].makeReturn(it) : void 8) { if (that instanceof Return && !that.it) { --this.lines.length; @@ -409,7 +410,7 @@ exports.Block = Block = (function(superclass){ }; prototype.compileExpressions = function(o, level){ var lines, i, that, code, last, i$, len$, node; - lines = this.lines; + lines = this.chomp().lines; i = -1; while (that = lines[++i]) { if (that.comment) { diff --git a/lib/grammar.js b/lib/grammar.js index b385ddd7f..f9ec2e1e2 100644 --- a/lib/grammar.js +++ b/lib/grammar.js @@ -93,7 +93,7 @@ bnf = { }) ], Block: [o('INDENT Lines DEDENT', function(){ - return $2.chomp(); + return $2; })], Expression: [ o('Chain', function(){ diff --git a/lib/parser.js b/lib/parser.js index f5ba567a3..2c8bdad0f 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -72,7 +72,7 @@ case 38:this.$ = yy.L(yylineno, yy.JS($$[$0], true, true)); break; case 39:this.$ = yy.L(yylineno, yy.Throw(yy.JS("Error('unimplemented')"))); break; -case 40:this.$ = $$[$0-1].chomp(); +case 40:this.$ = $$[$0-1]; break; case 41:this.$ = $$[$0].unwrap(); break; diff --git a/src/ast.co b/src/ast.co index e22bed817..3ff673e33 100644 --- a/src/ast.co +++ b/src/ast.co @@ -237,6 +237,7 @@ class exports.Block extends Node # **Block** does not return its entire body, rather it # ensures that the final line is returned. makeReturn: -> + @chomp! if @lines[*-1]?=makeReturn it --@lines.length if that instanceof Return and not that.it this @@ -278,7 +279,7 @@ class exports.Block extends Node # Compile to a comma-separated list of expressions. compileExpressions: (o, level) -> - {lines} = this; i = -1 + {lines} = @chomp!; i = -1 lines.splice i-- 1 if that.comment while lines[++i] lines.push Literal \void unless lines.length lines.0 <<< {@front}; lines[*-1] <<< {@void} diff --git a/src/grammar.co b/src/grammar.co index 2b1a2ffc1..856f01204 100644 --- a/src/grammar.co +++ b/src/grammar.co @@ -129,7 +129,7 @@ bnf = # An indented block of expressions. # Note that [Lexer](#lexer) rewrites some single-line forms into blocks. Block: - o 'INDENT Lines DEDENT' -> $2.chomp! + o 'INDENT Lines DEDENT' -> $2 ... # All the different types of expressions in our language. diff --git a/test/comment.co b/test/comment.co index 9a128d186..73dd4ac3b 100644 --- a/test/comment.co +++ b/test/comment.co @@ -131,10 +131,11 @@ obj = { * ^ */ }; -if (1) { +(function(){ /* no semicolon at end -> */ - 2; -} + 1; + return 2; +}); /* trailing top level comment */ ''', Coco.compile ''' /* leading block comments */ @@ -148,11 +149,12 @@ obj = { * ^ */ } -if 1 +-> /* no semicolon at end -> */ + 1 2 - /* trailing comments in a block */ - /* are removed */ + /* trailing block comments are */ + /* removed when returning value */ /* trailing top level comment */ ''', {+bare} diff --git a/test/function.co b/test/function.co index 2a7f2df70..f37418c67 100644 --- a/test/function.co +++ b/test/function.co @@ -378,6 +378,13 @@ eq 3 do eq 6 (a <- g 6; a) +# [#192](https://github.com/satyr/coco/issues/192) +eq '192' do + <- '081'replace /./g + -~it + /* ignore trailing */ + /* block comments */ + ### `function` new