fixing empty catch clauses ... Issue #470

This commit is contained in:
Jeremy Ashkenas 2010-07-04 12:50:04 -04:00
parent 92878558c6
commit e281133f12
6 changed files with 124 additions and 110 deletions

View File

@ -332,8 +332,6 @@
Catch: [
o("CATCH Identifier Block", function() {
return [$2, $3];
}), o("CATCH Identifier TERMINATOR", function() {
return [$2, new Expressions()];
})
],
Throw: [

File diff suppressed because one or more lines are too long

View File

@ -196,17 +196,23 @@
Rewriter.prototype.addImplicitIndentation = function() {
return this.scanTokens((function(__this) {
var __func = function(prev, token, post, i) {
var idx, indent, insertion, outdent, parens, pre, starter, tok;
var _c, idx, indent, insertion, outdent, parens, pre, starter, tok;
if (token[0] === 'ELSE' && prev[0] !== 'OUTDENT') {
this.tokens.splice(i, 0, ['INDENT', 2, token[2]], ['OUTDENT', 2, token[2]]);
this.tokens.splice.apply(this.tokens, [i, 0].concat(this.indentation(token)));
return 2;
}
if (token[0] === 'CATCH' && this.tokens[i + 2][0] === 'TERMINATOR') {
this.tokens.splice.apply(this.tokens, [i + 2, 0].concat(this.indentation(token)));
return 4;
}
if (!(include(SINGLE_LINERS, token[0]) && post[0] !== 'INDENT' && !(token[0] === 'ELSE' && post[0] === 'IF'))) {
return 1;
}
starter = token[0];
indent = ['INDENT', 2, token[2]];
indent.generated = true;
_c = this.indentation(token);
indent = _c[0];
outdent = _c[1];
indent.generated = (outdent.generated = true);
this.tokens.splice(i + 1, 0, indent);
idx = i + 1;
parens = 0;
@ -216,8 +222,6 @@
pre = this.tokens[idx - 1];
if ((!tok || (include(SINGLE_CLOSERS, tok[0]) && tok[1] !== ';' && parens === 0) || (tok[0] === ')' && parens === 0)) && !(tok[0] === 'ELSE' && !('IF' === starter || 'THEN' === starter))) {
insertion = pre[0] === "," ? idx - 1 : idx;
outdent = ['OUTDENT', 2, token[2]];
outdent.generated = true;
this.tokens.splice(insertion, 0, outdent);
break;
}
@ -334,6 +338,9 @@
});
})(this));
};
Rewriter.prototype.indentation = function(token) {
return [['INDENT', 2, token[2]], ['OUTDENT', 2, token[2]]];
};
return Rewriter;
})();
BALANCED_PAIRS = [['(', ')'], ['[', ']'], ['{', '}'], ['INDENT', 'OUTDENT'], ['PARAM_START', 'PARAM_END'], ['CALL_START', 'CALL_END'], ['INDEX_START', 'INDEX_END']];

View File

@ -378,7 +378,6 @@ grammar: {
# A catch clause names its error and runs a block of code.
Catch: [
o "CATCH Identifier Block", -> [$2, $3]
o "CATCH Identifier TERMINATOR", -> [$2, new Expressions]
]
# Throw an exception object.

View File

@ -48,7 +48,7 @@ exports.Rewriter: class Rewriter
move: block @tokens[i - 1], @tokens[i], @tokens[i + 1], i
i: + move
true
# Massage newlines and indentations so that comments don't have to be
# correctly indented, or appear on a line of their own.
adjustComments: ->
@ -159,14 +159,17 @@ exports.Rewriter: class Rewriter
addImplicitIndentation: ->
@scanTokens (prev, token, post, i) =>
if token[0] is 'ELSE' and prev[0] isnt 'OUTDENT'
@tokens.splice i, 0, ['INDENT', 2, token[2]], ['OUTDENT', 2, token[2]]
@tokens.splice i, 0, @indentation(token)...
return 2
if token[0] is 'CATCH' and @tokens[i + 2][0] is 'TERMINATOR'
@tokens.splice i + 2, 0, @indentation(token)...
return 4
return 1 unless include(SINGLE_LINERS, token[0]) and
post[0] isnt 'INDENT' and
not (token[0] is 'ELSE' and post[0] is 'IF')
starter: token[0]
indent: ['INDENT', 2, token[2]]
indent.generated: true
[indent, outdent]: @indentation token
indent.generated: outdent.generated: true
@tokens.splice i + 1, 0, indent
idx: i + 1
parens: 0
@ -179,8 +182,6 @@ exports.Rewriter: class Rewriter
(tok[0] is ')' and parens is 0)) and
not (tok[0] is 'ELSE' and starter not in ['IF', 'THEN'])
insertion: if pre[0] is "," then idx - 1 else idx
outdent: ['OUTDENT', 2, token[2]]
outdent.generated: true
@tokens.splice insertion, 0, outdent
break
parens: + 1 if tok[0] is '('
@ -257,6 +258,10 @@ exports.Rewriter: class Rewriter
else
return 1
# Generate the indentation tokens, based on another token on the same line.
indentation: (token) ->
[['INDENT', 2, token[2]], ['OUTDENT', 2, token[2]]]
# Constants
# ---------

View File

@ -28,4 +28,11 @@ ok result is 5
try
# nothing
catch err
#nothing
# nothing
try
# nothing
finally
# nothing
ok yes