1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

Fix indendation error messages

This commit is contained in:
xixixao 2014-01-26 05:25:13 +00:00
parent d687d52f9e
commit 104b4666fe
6 changed files with 37 additions and 17 deletions

View file

@ -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);
};

View file

@ -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');
};

View file

@ -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];

View file

@ -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

View file

@ -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

View file

@ -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
^^
'''