diff --git a/lib/lexer.js b/lib/lexer.js index 516fa83f..77dcddec 100644 --- a/lib/lexer.js +++ b/lib/lexer.js @@ -212,6 +212,7 @@ herecomment: true }); this.token('HERECOMMENT', comment.split(MULTILINER)); + this.token('TERMINATOR', '\n'); } else { lines = compact(match[1].replace(COMMENT_CLEANER, '').split(MULTILINER)); i = this.tokens.length - 1; diff --git a/lib/rewriter.js b/lib/rewriter.js index 9b6743e2..948dacaa 100644 --- a/lib/rewriter.js +++ b/lib/rewriter.js @@ -1,5 +1,5 @@ (function(){ - var BALANCED_PAIRS, EXPRESSION_CLOSE, EXPRESSION_END, EXPRESSION_START, IMPLICIT_BLOCK, IMPLICIT_CALL, IMPLICIT_END, IMPLICIT_FUNC, INVERSES, Rewriter, SINGLE_CLOSERS, SINGLE_LINERS, _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, helpers, include, pair; + var BALANCED_PAIRS, COMMENTS, EXPRESSION_CLOSE, EXPRESSION_END, EXPRESSION_START, IMPLICIT_BLOCK, IMPLICIT_CALL, IMPLICIT_END, IMPLICIT_FUNC, INVERSES, Rewriter, SINGLE_CLOSERS, SINGLE_LINERS, _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, helpers, include, pair; var __slice = Array.prototype.slice, __bind = function(func, obj, args) { return function() { return func.apply(obj || {}, args ? args.concat(__slice.call(arguments, 0)) : arguments); @@ -65,7 +65,7 @@ Rewriter.prototype.adjust_comments = function() { return this.scan_tokens(__bind(function(prev, token, post, i) { var _c, after, before; - if (!(token[0] === 'COMMENT')) { + if (!(include(COMMENTS, token[0]))) { return 1; } _c = [this.tokens[i - 2], this.tokens[i + 2]]; @@ -392,4 +392,6 @@ // The grammar can't disambiguate them, so we insert the implicit indentation. SINGLE_LINERS = ['ELSE', "->", "=>", 'TRY', 'FINALLY', 'THEN']; SINGLE_CLOSERS = ['TERMINATOR', 'CATCH', 'FINALLY', 'ELSE', 'OUTDENT', 'LEADING_WHEN']; + // Comment flavors. + COMMENTS = ['COMMENT', 'HERECOMMENT']; })(); diff --git a/src/lexer.coffee b/src/lexer.coffee index 980c482e..a8c9c44d 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -141,6 +141,7 @@ exports.Lexer: class Lexer if match[3] comment: @sanitize_heredoc match[3], {herecomment: true} @token 'HERECOMMENT', comment.split MULTILINER + @token 'TERMINATOR', '\n' else lines: compact match[1].replace(COMMENT_CLEANER, '').split MULTILINER i: @tokens.length - 1 diff --git a/src/rewriter.coffee b/src/rewriter.coffee index f6f4ea27..12be6660 100644 --- a/src/rewriter.coffee +++ b/src/rewriter.coffee @@ -53,7 +53,7 @@ exports.Rewriter: class Rewriter # correctly indented, or appear on a line of their own. adjust_comments: -> @scan_tokens (prev, token, post, i) => - return 1 unless token[0] is 'COMMENT' + return 1 unless include COMMENTS, token[0] [before, after]: [@tokens[i - 2], @tokens[i + 2]] if after and after[0] is 'INDENT' @tokens.splice i + 2, 1 @@ -292,3 +292,6 @@ IMPLICIT_END: ['IF', 'UNLESS', 'FOR', 'WHILE', 'UNTIL', 'TERMINATOR', 'INDENT' # The grammar can't disambiguate them, so we insert the implicit indentation. SINGLE_LINERS: ['ELSE', "->", "=>", 'TRY', 'FINALLY', 'THEN'] SINGLE_CLOSERS: ['TERMINATOR', 'CATCH', 'FINALLY', 'ELSE', 'OUTDENT', 'LEADING_WHEN'] + +# Comment flavors. +COMMENTS: ['COMMENT', 'HERECOMMENT']