Revert "coco b8039b9: merged @closeOpenCalls and @closeOpenIndexes into @closeOpenPairs"

This reverts commit a151ceccc6.
This commit is contained in:
Jeremy Ashkenas 2010-11-09 08:20:09 -05:00
parent 9c76c3ef8e
commit 611174b0af
2 changed files with 55 additions and 41 deletions

View File

@ -16,7 +16,8 @@
this.tokens = _arg; this.tokens = _arg;
this.removeLeadingNewlines(); this.removeLeadingNewlines();
this.removeMidExpressionNewlines(); this.removeMidExpressionNewlines();
this.closeOpenPairs(); this.closeOpenCalls();
this.closeOpenIndexes();
this.addImplicitIndentation(); this.addImplicitIndentation();
this.tagPostfixConditionals(); this.tagPostfixConditionals();
this.addImplicitBraces(); this.addImplicitBraces();
@ -76,33 +77,37 @@
return 0; return 0;
}); });
}; };
exports.Rewriter.prototype.closeOpenPairs = function() { exports.Rewriter.prototype.closeOpenCalls = function() {
var stack, token, _i, _len, _ref; var action, condition;
stack = []; condition = function(token, i) {
_ref = this.tokens; var _ref;
for (_i = 0, _len = _ref.length; _i < _len; _i++) { return ((_ref = token[0]) === ')' || _ref === 'CALL_END') || token[0] === 'OUTDENT' && this.tag(i - 1) === ')';
token = _ref[_i]; };
switch (token[0]) { action = function(token, i) {
case 'CALL_START': return this.tokens[token[0] === 'OUTDENT' ? i - 1 : i][0] = 'CALL_END';
case 'INDEX_START': };
case '(': return this.scanTokens(function(token, i) {
case '[': if (token[0] === 'CALL_START') {
stack.push(token[0]); this.detectEnd(i + 1, condition, action);
break;
case 'CALL_END':
case 'INDEX_END':
case ')':
case ']':
switch (stack.pop()) {
case 'CALL_START':
token[0] = 'CALL_END';
break;
case 'INDEX_START':
token[0] = 'INDEX_END';
}
} }
} return 1;
return this; });
};
exports.Rewriter.prototype.closeOpenIndexes = function() {
var action, condition;
condition = function(token, i) {
var _ref;
return (_ref = token[0]) === ']' || _ref === 'INDEX_END';
};
action = function(token, i) {
return token[0] = 'INDEX_END';
};
return this.scanTokens(function(token, i) {
if (token[0] === 'INDEX_START') {
this.detectEnd(i + 1, condition, action);
}
return 1;
});
}; };
exports.Rewriter.prototype.addImplicitBraces = function() { exports.Rewriter.prototype.addImplicitBraces = function() {
var action, condition, stack, start; var action, condition, stack, start;

View File

@ -20,7 +20,8 @@ class exports.Rewriter
rewrite: (@tokens) -> rewrite: (@tokens) ->
@removeLeadingNewlines() @removeLeadingNewlines()
@removeMidExpressionNewlines() @removeMidExpressionNewlines()
@closeOpenPairs() @closeOpenCalls()
@closeOpenIndexes()
@addImplicitIndentation() @addImplicitIndentation()
@tagPostfixConditionals() @tagPostfixConditionals()
@addImplicitBraces() @addImplicitBraces()
@ -67,19 +68,27 @@ class exports.Rewriter
tokens.splice i, 1 tokens.splice i, 1
0 0
# The lexer has tagged the opening parenthesis of a method call, or the open # The lexer has tagged the opening parenthesis of a method call. Match it with
# bracket of an indexing operation. Match it with its paired close. # its paired close. We have the mis-nested outdent case included here for
closeOpenPairs: -> # calls that close on the same line, just before their outdent.
stack = [] closeOpenCalls: ->
for token in @tokens condition = (token, i) ->
switch token[0] token[0] in [')', 'CALL_END'] or
when 'CALL_START', 'INDEX_START', '(', '[' token[0] is 'OUTDENT' and @tag(i - 1) is ')'
stack.push token[0] action = (token, i) ->
when 'CALL_END', 'INDEX_END', ')', ']' @tokens[if token[0] is 'OUTDENT' then i - 1 else i][0] = 'CALL_END'
switch stack.pop() @scanTokens (token, i) ->
when 'CALL_START' then token[0] = 'CALL_END' @detectEnd i + 1, condition, action if token[0] is 'CALL_START'
when 'INDEX_START' then token[0] = 'INDEX_END' 1
this
# The lexer has tagged the opening parenthesis of an indexing operation call.
# Match it with its paired close.
closeOpenIndexes: ->
condition = (token, i) -> token[0] in [']', 'INDEX_END']
action = (token, i) -> token[0] = 'INDEX_END'
@scanTokens (token, i) ->
@detectEnd i + 1, condition, action if token[0] is 'INDEX_START'
1
# Object literals may be written with implicit braces, for simple cases. # Object literals may be written with implicit braces, for simple cases.
# Insert the missing braces here, so that the parser doesn't have to. # Insert the missing braces here, so that the parser doesn't have to.