diff --git a/lib/coffee-script/rewriter.js b/lib/coffee-script/rewriter.js index fdfeffc4..706f2c21 100644 --- a/lib/coffee-script/rewriter.js +++ b/lib/coffee-script/rewriter.js @@ -18,6 +18,7 @@ this.tagPostfixConditionals(); this.addImplicitBraces(); this.addImplicitParentheses(); + this.addLocationDataToGeneratedTokens(); return this.tokens; }; @@ -221,6 +222,31 @@ }); }; + Rewriter.prototype.addLocationDataToGeneratedTokens = function() { + return this.scanTokens(function(token, i, tokens) { + var prevToken; + if (token.generated && !token.locationData) { + if (i > 0) { + prevToken = tokens[i - 1]; + token.locationData = { + first_line: prevToken.locationData.last_line, + first_column: prevToken.locationData.last_column, + last_line: prevToken.locationData.last_line, + last_column: prevToken.locationData.last_column + }; + } else { + token.locationData = { + first_line: 0, + first_column: 0, + last_line: 0, + last_column: 0 + }; + } + } + return 1; + }); + }; + Rewriter.prototype.addImplicitIndentation = function() { var action, condition, indent, outdent, starter; starter = indent = outdent = null; diff --git a/src/rewriter.coffee b/src/rewriter.coffee index a21dd74c..897a8252 100644 --- a/src/rewriter.coffee +++ b/src/rewriter.coffee @@ -26,6 +26,7 @@ class exports.Rewriter @tagPostfixConditionals() @addImplicitBraces() @addImplicitParentheses() + @addLocationDataToGeneratedTokens() @tokens # Rewrite the token stream, looking one token ahead and behind. @@ -113,7 +114,7 @@ class exports.Rewriter [tag] = token sameLine = no if tag in LINEBREAKS return ( - (tag in ['TERMINATOR', 'OUTDENT'] or + (tag in ['TERMINATOR', 'OUTDENT'] or (tag in IMPLICIT_END and sameLine and not (i - startIndex is 1))) and ((!startsLine and @tag(i - 1) isnt ',') or not (two?[0] is ':' or one?[0] is '@' and three?[0] is ':'))) or @@ -190,6 +191,25 @@ class exports.Rewriter prev[0] = 'FUNC_EXIST' if prev[0] is '?' 2 + # Add location data to all tokens generated by the rewriter. + addLocationDataToGeneratedTokens: -> + @scanTokens (token, i, tokens) -> + if token.generated and not token.locationData + if i > 0 + prevToken = tokens[i-1] + token.locationData = + first_line: prevToken.locationData.last_line + first_column: prevToken.locationData.last_column + last_line: prevToken.locationData.last_line + last_column: prevToken.locationData.last_column + else + token.locationData = + first_line: 0 + first_column: 0 + last_line: 0 + last_column: 0 + return 1 + # Because our grammar is LALR(1), it can't handle some single-line # expressions that lack ending delimiters. The **Rewriter** adds the implicit # blocks, so it doesn't need to. ')' can close a single-line block,