removing bound functions from the Rewriter.

This commit is contained in:
Jeremy Ashkenas 2010-08-11 21:07:47 -04:00
parent 398ec3be5a
commit ac752a46bc
2 changed files with 30 additions and 32 deletions

View File

@ -1,8 +1,6 @@
(function() {
var BALANCED_PAIRS, EXPRESSION_CLOSE, EXPRESSION_END, EXPRESSION_START, IMPLICIT_BLOCK, IMPLICIT_CALL, IMPLICIT_END, IMPLICIT_FUNC, INVERSES, Rewriter, SINGLE_CLOSERS, SINGLE_LINERS, _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, helpers, include, pair;
var __bind = function(func, context) {
return function(){ return func.apply(context, arguments); };
}, __hasProp = Object.prototype.hasOwnProperty;
var __hasProp = Object.prototype.hasOwnProperty;
if ((typeof process !== "undefined" && process !== null)) {
_a = require('./helpers');
helpers = _a.helpers;
@ -35,7 +33,7 @@
if (!(this.tokens[i])) {
break;
}
move = block(this.tokens[i], i);
move = block.call(this, this.tokens[i], i);
i += move;
}
return true;
@ -62,7 +60,7 @@
return i - 1;
};
Rewriter.prototype.adjustComments = function() {
return this.scanTokens(__bind(function(token, i) {
return this.scanTokens(function(token, i) {
var _c, _d, after, before, post, prev;
if (!(token[0] === 'HERECOMMENT')) {
return 1;
@ -91,7 +89,7 @@
return 2;
}
return 1;
}, this));
});
};
Rewriter.prototype.removeLeadingNewlines = function() {
var _c;
@ -102,16 +100,16 @@
return _c;
};
Rewriter.prototype.removeMidExpressionNewlines = function() {
return this.scanTokens(__bind(function(token, i) {
return this.scanTokens(function(token, i) {
if (!(include(EXPRESSION_CLOSE, this.tag(i + 1)) && token[0] === 'TERMINATOR')) {
return 1;
}
this.tokens.splice(i, 1);
return 0;
}, this));
});
};
Rewriter.prototype.closeOpenCalls = function() {
return this.scanTokens(__bind(function(token, i) {
return this.scanTokens(function(token, i) {
var action, condition;
if (token[0] === 'CALL_START') {
condition = function(token, i) {
@ -124,10 +122,10 @@
this.detectEnd(i + 1, condition, action);
}
return 1;
}, this));
});
};
Rewriter.prototype.closeOpenIndexes = function() {
return this.scanTokens(__bind(function(token, i) {
return this.scanTokens(function(token, i) {
var action, condition;
if (token[0] === 'INDEX_START') {
condition = function(token, i) {
@ -140,12 +138,12 @@
this.detectEnd(i + 1, condition, action);
}
return 1;
}, this));
});
};
Rewriter.prototype.addImplicitBraces = function() {
var stack;
stack = [];
return this.scanTokens(__bind(function(token, i) {
return this.scanTokens(function(token, i) {
var action, condition, idx, last;
if (include(EXPRESSION_START, token[0])) {
stack.push((token[0] === 'INDENT' && (this.tag(i - 1) === '{')) ? '{' : token[0]);
@ -176,10 +174,10 @@
return 2;
}
return 1;
}, this));
});
};
Rewriter.prototype.addImplicitParentheses = function() {
return this.scanTokens(__bind(function(token, i) {
return this.scanTokens(function(token, i) {
var _c, action, condition, prev;
prev = this.tokens[i - 1];
if (prev && prev.spaced && include(IMPLICIT_FUNC, prev[0]) && include(IMPLICIT_CALL, token[0]) && !(token[0] === '!' && (('IN' === (_c = this.tag(i + 1)) || 'OF' === _c)))) {
@ -196,10 +194,10 @@
return 2;
}
return 1;
}, this));
});
};
Rewriter.prototype.addImplicitIndentation = function() {
return this.scanTokens(__bind(function(token, i) {
return this.scanTokens(function(token, i) {
var _c, action, condition, indent, outdent, starter;
if (token[0] === 'ELSE' && this.tag(i - 1) !== 'OUTDENT') {
this.tokens.splice.apply(this.tokens, [i, 0].concat(this.indentation(token)));
@ -231,13 +229,13 @@
return 2;
}
return 1;
}, this));
});
};
Rewriter.prototype.ensureBalance = function(pairs) {
var _c, _d, key, levels, line, open, openLine, unclosed, value;
levels = {};
openLine = {};
this.scanTokens(__bind(function(token, i) {
this.scanTokens(function(token, i) {
var _c, _d, _e, _f, close, open, pair;
_d = pairs;
for (_c = 0, _e = _d.length; _c < _e; _c++) {
@ -260,7 +258,7 @@
}
}
return 1;
}, this));
});
unclosed = (function() {
_c = []; _d = levels;
for (key in _d) {
@ -288,7 +286,7 @@
val = _c[key];
(debt[key] = 0);
}
return this.scanTokens(__bind(function(token, i) {
return this.scanTokens(function(token, i) {
var inv, match, mtag, oppos, tag;
tag = token[0];
inv = INVERSES[token[0]];
@ -320,7 +318,7 @@
} else {
return 1;
}
}, this));
});
};
Rewriter.prototype.indentation = function(token) {
return [['INDENT', 2, token[2]], ['OUTDENT', 2, token[2]]];

View File

@ -50,7 +50,7 @@ exports.Rewriter = class Rewriter
i = 0
loop
break unless @tokens[i]
move = block @tokens[i], i
move = block.call this, @tokens[i], i
i += move
true
@ -68,7 +68,7 @@ exports.Rewriter = class Rewriter
# Massage newlines and indentations so that comments don't have to be
# correctly indented, or appear on a line of their own.
adjustComments: ->
@scanTokens (token, i) =>
@scanTokens (token, i) ->
return 1 unless token[0] is 'HERECOMMENT'
[before, prev, post, after] = [@tokens[i - 2], @tokens[i - 1], @tokens[i + 1], @tokens[i + 2]]
if after and after[0] is 'INDENT'
@ -95,7 +95,7 @@ exports.Rewriter = class Rewriter
# Some blocks occur in the middle of expressions -- when we're expecting
# this, remove their trailing newlines.
removeMidExpressionNewlines: ->
@scanTokens (token, i) =>
@scanTokens (token, i) ->
return 1 unless include(EXPRESSION_CLOSE, @tag(i + 1)) and token[0] is 'TERMINATOR'
@tokens.splice i, 1
return 0
@ -103,7 +103,7 @@ exports.Rewriter = class Rewriter
# The lexer has tagged the opening parenthesis of a method call. Match it with
# its paired close.
closeOpenCalls: ->
@scanTokens (token, i) =>
@scanTokens (token, i) ->
if token[0] is 'CALL_START'
condition = (token, i) -> token[0] in [')', 'CALL_END']
action = (token, i) -> token[0] = 'CALL_END'
@ -113,7 +113,7 @@ exports.Rewriter = class Rewriter
# The lexer has tagged the opening parenthesis of an indexing operation call.
# Match it with its paired close.
closeOpenIndexes: ->
@scanTokens (token, i) =>
@scanTokens (token, i) ->
if token[0] is 'INDEX_START'
condition = (token, i) -> token[0] in [']', 'INDEX_END']
action = (token, i) -> token[0] = 'INDEX_END'
@ -124,7 +124,7 @@ exports.Rewriter = class Rewriter
# Insert the missing braces here, so that the parser doesn't have to.
addImplicitBraces: ->
stack = []
@scanTokens (token, i) =>
@scanTokens (token, i) ->
if include EXPRESSION_START, token[0]
stack.push(if (token[0] is 'INDENT' and (@tag(i - 1) is '{')) then '{' else token[0])
if include EXPRESSION_END, token[0]
@ -150,7 +150,7 @@ exports.Rewriter = class Rewriter
# Insert the implicit parentheses here, so that the parser doesn't have to
# deal with them.
addImplicitParentheses: ->
@scanTokens (token, i) =>
@scanTokens (token, i) ->
prev = @tokens[i - 1]
if prev and prev.spaced and include(IMPLICIT_FUNC, prev[0]) and include(IMPLICIT_CALL, token[0]) and
not (token[0] is '!' and (@tag(i + 1) in ['IN', 'OF']))
@ -171,7 +171,7 @@ exports.Rewriter = class Rewriter
# blocks, so it doesn't need to. ')' can close a single-line block,
# but we need to make sure it's balanced.
addImplicitIndentation: ->
@scanTokens (token, i) =>
@scanTokens (token, i) ->
if token[0] is 'ELSE' and @tag(i - 1) isnt 'OUTDENT'
@tokens.splice i, 0, @indentation(token)...
return 2
@ -201,7 +201,7 @@ exports.Rewriter = class Rewriter
ensureBalance: (pairs) ->
levels = {}
openLine = {}
@scanTokens (token, i) =>
@scanTokens (token, i) ->
for pair in pairs
[open, close] = pair
levels[open] or= 0
@ -237,7 +237,7 @@ exports.Rewriter = class Rewriter
stack = []
debt = {}
(debt[key] = 0) for key, val of INVERSES
@scanTokens (token, i) =>
@scanTokens (token, i) ->
tag = token[0]
inv = INVERSES[token[0]]
if include EXPRESSION_START, tag