diff --git a/lib/coffee-script/coffee-script.js b/lib/coffee-script/coffee-script.js index 1b290b1c..80239e10 100644 --- a/lib/coffee-script/coffee-script.js +++ b/lib/coffee-script/coffee-script.js @@ -233,11 +233,11 @@ parser.yy = require('./nodes'); parser.yy.parseError = function(message, _arg) { - var errorLoc, errorText, errorToken, ignored, token, tokens, _ref; + var errorLoc, errorTag, errorText, errorToken, token, tokens, _ref; token = _arg.token; _ref = parser.lexer, errorToken = _ref.errorToken, tokens = _ref.tokens; - ignored = errorToken[0], errorText = errorToken[1], errorLoc = errorToken[2]; - errorText = errorToken === tokens[tokens.length - 1] ? 'end of input' : helpers.nameWhitespaceCharacter(errorText); + errorTag = errorToken[0], errorText = errorToken[1], errorLoc = errorToken[2]; + errorText = errorToken === tokens[tokens.length - 1] ? 'end of input' : errorTag === 'INDENT' || errorTag === 'OUTDENT' ? 'indentation' : helpers.nameWhitespaceCharacter(errorText); return helpers.throwSyntaxError("unexpected " + errorText, errorLoc); }; diff --git a/lib/coffee-script/nodes.js b/lib/coffee-script/nodes.js index 035f61c1..42b8064b 100644 --- a/lib/coffee-script/nodes.js +++ b/lib/coffee-script/nodes.js @@ -2153,7 +2153,6 @@ Expansion.prototype.isComplex = NO; Expansion.prototype.compileNode = function(o) { - throw new Error; return this.error('Expansion must be used inside a destructuring assignment or parameter list'); }; diff --git a/lib/coffee-script/rewriter.js b/lib/coffee-script/rewriter.js index 6915b736..22e2bb46 100644 --- a/lib/coffee-script/rewriter.js +++ b/lib/coffee-script/rewriter.js @@ -384,7 +384,7 @@ } if (__indexOf.call(SINGLE_LINERS, tag) >= 0 && this.tag(i + 1) !== 'INDENT' && !(tag === 'ELSE' && this.tag(i + 1) === 'IF')) { starter = tag; - _ref2 = this.indentation(true), indent = _ref2[0], outdent = _ref2[1]; + _ref2 = this.indentation(tokens[i]), indent = _ref2[0], outdent = _ref2[1]; if (starter === 'THEN') { indent.fromThen = true; } @@ -423,17 +423,14 @@ }); }; - Rewriter.prototype.indentation = function(implicit) { + Rewriter.prototype.indentation = function(origin) { var indent, outdent; - if (implicit == null) { - implicit = false; - } indent = ['INDENT', 2]; outdent = ['OUTDENT', 2]; - if (implicit) { + if (origin) { indent.generated = outdent.generated = true; - } - if (!implicit) { + indent.origin = outdent.origin = origin; + } else { indent.explicit = outdent.explicit = true; } return [indent, outdent]; diff --git a/src/coffee-script.coffee b/src/coffee-script.coffee index 7d1b1c23..559338c1 100644 --- a/src/coffee-script.coffee +++ b/src/coffee-script.coffee @@ -202,10 +202,12 @@ parser.yy.parseError = (message, {token}) -> # Disregard the token, we take its value directly from the lexer in case # the error is caused by a generated token which might refer to its origin. {errorToken, tokens} = parser.lexer - [ignored, errorText, errorLoc] = errorToken + [errorTag, errorText, errorLoc] = errorToken errorText = if errorToken is tokens[tokens.length - 1] 'end of input' + else if errorTag in ['INDENT', 'OUTDENT'] + 'indentation' else helpers.nameWhitespaceCharacter errorText diff --git a/src/rewriter.coffee b/src/rewriter.coffee index 4be00ff2..a23beea5 100644 --- a/src/rewriter.coffee +++ b/src/rewriter.coffee @@ -392,7 +392,7 @@ class exports.Rewriter if tag in SINGLE_LINERS and @tag(i + 1) isnt 'INDENT' and not (tag is 'ELSE' and @tag(i + 1) is 'IF') starter = tag - [indent, outdent] = @indentation yes + [indent, outdent] = @indentation tokens[i] indent.fromThen = true if starter is 'THEN' tokens.splice i + 1, 0, indent @detectEnd i + 2, condition, action @@ -422,11 +422,14 @@ class exports.Rewriter return 1 # Generate the indentation tokens, based on another token on the same line. - indentation: (implicit = no) -> + indentation: (origin) -> indent = ['INDENT', 2] outdent = ['OUTDENT', 2] - indent.generated = outdent.generated = yes if implicit - indent.explicit = outdent.explicit = yes if not implicit + if origin + indent.generated = outdent.generated = yes + indent.origin = outdent.origin = origin + else + indent.explicit = outdent.explicit = yes [indent, outdent] generate: generate diff --git a/test/error_messages.coffee b/test/error_messages.coffee index e6d084b4..fc736ca6 100644 --- a/test/error_messages.coffee +++ b/test/error_messages.coffee @@ -99,3 +99,22 @@ test "#1096: unexpected generated tokens", -> for i in [1]: ^ ''' + +test "#3325: implicit indentation errors", -> + assertErrorFormat ''' + i for i in a then i + ''', ''' + [stdin]:1:14: error: unexpected then + i for i in a then i + ^^^^ + ''' + +test "explicit indentation errors", -> + assertErrorFormat ''' + a = b + c + ''', ''' + [stdin]:2:1: error: unexpected indentation + c + ^^ + '''