removing bound functions from the Rewriter.
This commit is contained in:
parent
398ec3be5a
commit
ac752a46bc
|
@ -1,8 +1,6 @@
|
||||||
(function() {
|
(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 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) {
|
var __hasProp = Object.prototype.hasOwnProperty;
|
||||||
return function(){ return func.apply(context, arguments); };
|
|
||||||
}, __hasProp = Object.prototype.hasOwnProperty;
|
|
||||||
if ((typeof process !== "undefined" && process !== null)) {
|
if ((typeof process !== "undefined" && process !== null)) {
|
||||||
_a = require('./helpers');
|
_a = require('./helpers');
|
||||||
helpers = _a.helpers;
|
helpers = _a.helpers;
|
||||||
|
@ -35,7 +33,7 @@
|
||||||
if (!(this.tokens[i])) {
|
if (!(this.tokens[i])) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
move = block(this.tokens[i], i);
|
move = block.call(this, this.tokens[i], i);
|
||||||
i += move;
|
i += move;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -62,7 +60,7 @@
|
||||||
return i - 1;
|
return i - 1;
|
||||||
};
|
};
|
||||||
Rewriter.prototype.adjustComments = function() {
|
Rewriter.prototype.adjustComments = function() {
|
||||||
return this.scanTokens(__bind(function(token, i) {
|
return this.scanTokens(function(token, i) {
|
||||||
var _c, _d, after, before, post, prev;
|
var _c, _d, after, before, post, prev;
|
||||||
if (!(token[0] === 'HERECOMMENT')) {
|
if (!(token[0] === 'HERECOMMENT')) {
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -91,7 +89,7 @@
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}, this));
|
});
|
||||||
};
|
};
|
||||||
Rewriter.prototype.removeLeadingNewlines = function() {
|
Rewriter.prototype.removeLeadingNewlines = function() {
|
||||||
var _c;
|
var _c;
|
||||||
|
@ -102,16 +100,16 @@
|
||||||
return _c;
|
return _c;
|
||||||
};
|
};
|
||||||
Rewriter.prototype.removeMidExpressionNewlines = function() {
|
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')) {
|
if (!(include(EXPRESSION_CLOSE, this.tag(i + 1)) && token[0] === 'TERMINATOR')) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
this.tokens.splice(i, 1);
|
this.tokens.splice(i, 1);
|
||||||
return 0;
|
return 0;
|
||||||
}, this));
|
});
|
||||||
};
|
};
|
||||||
Rewriter.prototype.closeOpenCalls = function() {
|
Rewriter.prototype.closeOpenCalls = function() {
|
||||||
return this.scanTokens(__bind(function(token, i) {
|
return this.scanTokens(function(token, i) {
|
||||||
var action, condition;
|
var action, condition;
|
||||||
if (token[0] === 'CALL_START') {
|
if (token[0] === 'CALL_START') {
|
||||||
condition = function(token, i) {
|
condition = function(token, i) {
|
||||||
|
@ -124,10 +122,10 @@
|
||||||
this.detectEnd(i + 1, condition, action);
|
this.detectEnd(i + 1, condition, action);
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}, this));
|
});
|
||||||
};
|
};
|
||||||
Rewriter.prototype.closeOpenIndexes = function() {
|
Rewriter.prototype.closeOpenIndexes = function() {
|
||||||
return this.scanTokens(__bind(function(token, i) {
|
return this.scanTokens(function(token, i) {
|
||||||
var action, condition;
|
var action, condition;
|
||||||
if (token[0] === 'INDEX_START') {
|
if (token[0] === 'INDEX_START') {
|
||||||
condition = function(token, i) {
|
condition = function(token, i) {
|
||||||
|
@ -140,12 +138,12 @@
|
||||||
this.detectEnd(i + 1, condition, action);
|
this.detectEnd(i + 1, condition, action);
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}, this));
|
});
|
||||||
};
|
};
|
||||||
Rewriter.prototype.addImplicitBraces = function() {
|
Rewriter.prototype.addImplicitBraces = function() {
|
||||||
var stack;
|
var stack;
|
||||||
stack = [];
|
stack = [];
|
||||||
return this.scanTokens(__bind(function(token, i) {
|
return this.scanTokens(function(token, i) {
|
||||||
var action, condition, idx, last;
|
var action, condition, idx, last;
|
||||||
if (include(EXPRESSION_START, token[0])) {
|
if (include(EXPRESSION_START, token[0])) {
|
||||||
stack.push((token[0] === 'INDENT' && (this.tag(i - 1) === '{')) ? '{' : token[0]);
|
stack.push((token[0] === 'INDENT' && (this.tag(i - 1) === '{')) ? '{' : token[0]);
|
||||||
|
@ -176,10 +174,10 @@
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}, this));
|
});
|
||||||
};
|
};
|
||||||
Rewriter.prototype.addImplicitParentheses = function() {
|
Rewriter.prototype.addImplicitParentheses = function() {
|
||||||
return this.scanTokens(__bind(function(token, i) {
|
return this.scanTokens(function(token, i) {
|
||||||
var _c, action, condition, prev;
|
var _c, action, condition, prev;
|
||||||
prev = this.tokens[i - 1];
|
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)))) {
|
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 2;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}, this));
|
});
|
||||||
};
|
};
|
||||||
Rewriter.prototype.addImplicitIndentation = function() {
|
Rewriter.prototype.addImplicitIndentation = function() {
|
||||||
return this.scanTokens(__bind(function(token, i) {
|
return this.scanTokens(function(token, i) {
|
||||||
var _c, action, condition, indent, outdent, starter;
|
var _c, action, condition, indent, outdent, starter;
|
||||||
if (token[0] === 'ELSE' && this.tag(i - 1) !== 'OUTDENT') {
|
if (token[0] === 'ELSE' && this.tag(i - 1) !== 'OUTDENT') {
|
||||||
this.tokens.splice.apply(this.tokens, [i, 0].concat(this.indentation(token)));
|
this.tokens.splice.apply(this.tokens, [i, 0].concat(this.indentation(token)));
|
||||||
|
@ -231,13 +229,13 @@
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}, this));
|
});
|
||||||
};
|
};
|
||||||
Rewriter.prototype.ensureBalance = function(pairs) {
|
Rewriter.prototype.ensureBalance = function(pairs) {
|
||||||
var _c, _d, key, levels, line, open, openLine, unclosed, value;
|
var _c, _d, key, levels, line, open, openLine, unclosed, value;
|
||||||
levels = {};
|
levels = {};
|
||||||
openLine = {};
|
openLine = {};
|
||||||
this.scanTokens(__bind(function(token, i) {
|
this.scanTokens(function(token, i) {
|
||||||
var _c, _d, _e, _f, close, open, pair;
|
var _c, _d, _e, _f, close, open, pair;
|
||||||
_d = pairs;
|
_d = pairs;
|
||||||
for (_c = 0, _e = _d.length; _c < _e; _c++) {
|
for (_c = 0, _e = _d.length; _c < _e; _c++) {
|
||||||
|
@ -260,7 +258,7 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}, this));
|
});
|
||||||
unclosed = (function() {
|
unclosed = (function() {
|
||||||
_c = []; _d = levels;
|
_c = []; _d = levels;
|
||||||
for (key in _d) {
|
for (key in _d) {
|
||||||
|
@ -288,7 +286,7 @@
|
||||||
val = _c[key];
|
val = _c[key];
|
||||||
(debt[key] = 0);
|
(debt[key] = 0);
|
||||||
}
|
}
|
||||||
return this.scanTokens(__bind(function(token, i) {
|
return this.scanTokens(function(token, i) {
|
||||||
var inv, match, mtag, oppos, tag;
|
var inv, match, mtag, oppos, tag;
|
||||||
tag = token[0];
|
tag = token[0];
|
||||||
inv = INVERSES[token[0]];
|
inv = INVERSES[token[0]];
|
||||||
|
@ -320,7 +318,7 @@
|
||||||
} else {
|
} else {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}, this));
|
});
|
||||||
};
|
};
|
||||||
Rewriter.prototype.indentation = function(token) {
|
Rewriter.prototype.indentation = function(token) {
|
||||||
return [['INDENT', 2, token[2]], ['OUTDENT', 2, token[2]]];
|
return [['INDENT', 2, token[2]], ['OUTDENT', 2, token[2]]];
|
||||||
|
|
|
@ -50,7 +50,7 @@ exports.Rewriter = class Rewriter
|
||||||
i = 0
|
i = 0
|
||||||
loop
|
loop
|
||||||
break unless @tokens[i]
|
break unless @tokens[i]
|
||||||
move = block @tokens[i], i
|
move = block.call this, @tokens[i], i
|
||||||
i += move
|
i += move
|
||||||
true
|
true
|
||||||
|
|
||||||
|
@ -68,7 +68,7 @@ exports.Rewriter = class Rewriter
|
||||||
# Massage newlines and indentations so that comments don't have to be
|
# Massage newlines and indentations so that comments don't have to be
|
||||||
# correctly indented, or appear on a line of their own.
|
# correctly indented, or appear on a line of their own.
|
||||||
adjustComments: ->
|
adjustComments: ->
|
||||||
@scanTokens (token, i) =>
|
@scanTokens (token, i) ->
|
||||||
return 1 unless token[0] is 'HERECOMMENT'
|
return 1 unless token[0] is 'HERECOMMENT'
|
||||||
[before, prev, post, after] = [@tokens[i - 2], @tokens[i - 1], @tokens[i + 1], @tokens[i + 2]]
|
[before, prev, post, after] = [@tokens[i - 2], @tokens[i - 1], @tokens[i + 1], @tokens[i + 2]]
|
||||||
if after and after[0] is 'INDENT'
|
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
|
# Some blocks occur in the middle of expressions -- when we're expecting
|
||||||
# this, remove their trailing newlines.
|
# this, remove their trailing newlines.
|
||||||
removeMidExpressionNewlines: ->
|
removeMidExpressionNewlines: ->
|
||||||
@scanTokens (token, i) =>
|
@scanTokens (token, i) ->
|
||||||
return 1 unless include(EXPRESSION_CLOSE, @tag(i + 1)) and token[0] is 'TERMINATOR'
|
return 1 unless include(EXPRESSION_CLOSE, @tag(i + 1)) and token[0] is 'TERMINATOR'
|
||||||
@tokens.splice i, 1
|
@tokens.splice i, 1
|
||||||
return 0
|
return 0
|
||||||
|
@ -103,7 +103,7 @@ exports.Rewriter = class Rewriter
|
||||||
# The lexer has tagged the opening parenthesis of a method call. Match it with
|
# The lexer has tagged the opening parenthesis of a method call. Match it with
|
||||||
# its paired close.
|
# its paired close.
|
||||||
closeOpenCalls: ->
|
closeOpenCalls: ->
|
||||||
@scanTokens (token, i) =>
|
@scanTokens (token, i) ->
|
||||||
if token[0] is 'CALL_START'
|
if token[0] is 'CALL_START'
|
||||||
condition = (token, i) -> token[0] in [')', 'CALL_END']
|
condition = (token, i) -> token[0] in [')', 'CALL_END']
|
||||||
action = (token, i) -> token[0] = '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.
|
# The lexer has tagged the opening parenthesis of an indexing operation call.
|
||||||
# Match it with its paired close.
|
# Match it with its paired close.
|
||||||
closeOpenIndexes: ->
|
closeOpenIndexes: ->
|
||||||
@scanTokens (token, i) =>
|
@scanTokens (token, i) ->
|
||||||
if token[0] is 'INDEX_START'
|
if token[0] is 'INDEX_START'
|
||||||
condition = (token, i) -> token[0] in [']', 'INDEX_END']
|
condition = (token, i) -> token[0] in [']', 'INDEX_END']
|
||||||
action = (token, i) -> token[0] = '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.
|
# Insert the missing braces here, so that the parser doesn't have to.
|
||||||
addImplicitBraces: ->
|
addImplicitBraces: ->
|
||||||
stack = []
|
stack = []
|
||||||
@scanTokens (token, i) =>
|
@scanTokens (token, i) ->
|
||||||
if include EXPRESSION_START, token[0]
|
if include EXPRESSION_START, token[0]
|
||||||
stack.push(if (token[0] is 'INDENT' and (@tag(i - 1) is '{')) then '{' else token[0])
|
stack.push(if (token[0] is 'INDENT' and (@tag(i - 1) is '{')) then '{' else token[0])
|
||||||
if include EXPRESSION_END, 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
|
# Insert the implicit parentheses here, so that the parser doesn't have to
|
||||||
# deal with them.
|
# deal with them.
|
||||||
addImplicitParentheses: ->
|
addImplicitParentheses: ->
|
||||||
@scanTokens (token, i) =>
|
@scanTokens (token, i) ->
|
||||||
prev = @tokens[i - 1]
|
prev = @tokens[i - 1]
|
||||||
if prev and prev.spaced and include(IMPLICIT_FUNC, prev[0]) and include(IMPLICIT_CALL, token[0]) and
|
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']))
|
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,
|
# blocks, so it doesn't need to. ')' can close a single-line block,
|
||||||
# but we need to make sure it's balanced.
|
# but we need to make sure it's balanced.
|
||||||
addImplicitIndentation: ->
|
addImplicitIndentation: ->
|
||||||
@scanTokens (token, i) =>
|
@scanTokens (token, i) ->
|
||||||
if token[0] is 'ELSE' and @tag(i - 1) isnt 'OUTDENT'
|
if token[0] is 'ELSE' and @tag(i - 1) isnt 'OUTDENT'
|
||||||
@tokens.splice i, 0, @indentation(token)...
|
@tokens.splice i, 0, @indentation(token)...
|
||||||
return 2
|
return 2
|
||||||
|
@ -201,7 +201,7 @@ exports.Rewriter = class Rewriter
|
||||||
ensureBalance: (pairs) ->
|
ensureBalance: (pairs) ->
|
||||||
levels = {}
|
levels = {}
|
||||||
openLine = {}
|
openLine = {}
|
||||||
@scanTokens (token, i) =>
|
@scanTokens (token, i) ->
|
||||||
for pair in pairs
|
for pair in pairs
|
||||||
[open, close] = pair
|
[open, close] = pair
|
||||||
levels[open] or= 0
|
levels[open] or= 0
|
||||||
|
@ -237,7 +237,7 @@ exports.Rewriter = class Rewriter
|
||||||
stack = []
|
stack = []
|
||||||
debt = {}
|
debt = {}
|
||||||
(debt[key] = 0) for key, val of INVERSES
|
(debt[key] = 0) for key, val of INVERSES
|
||||||
@scanTokens (token, i) =>
|
@scanTokens (token, i) ->
|
||||||
tag = token[0]
|
tag = token[0]
|
||||||
inv = INVERSES[token[0]]
|
inv = INVERSES[token[0]]
|
||||||
if include EXPRESSION_START, tag
|
if include EXPRESSION_START, tag
|
||||||
|
|
Loading…
Reference in New Issue