update dependencies

This commit is contained in:
nightwing 2012-03-20 23:06:28 +04:00
commit 9244fd0440
9 changed files with 9278 additions and 5518 deletions

View file

@ -1,6 +1,6 @@
/** /**
* Copyright (c) 2011 Jeremy Ashkenas * Copyright (c) 2011 Jeremy Ashkenas
* *
* Permission is hereby granted, free of charge, to any person * Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation * obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without * files (the "Software"), to deal in the Software without
@ -24,41 +24,44 @@
*/ */
define(function(require, exports, module) { define(function(require, exports, module) {
// Generated by CoffeeScript 1.2.1-pre
var extend, flatten; var extend, flatten;
exports.starts = function(string, literal, start) { exports.starts = function(string, literal, start) {
return literal === string.substr(start, literal.length); return literal === string.substr(start, literal.length);
}; };
exports.ends = function(string, literal, back) { exports.ends = function(string, literal, back) {
var len; var len;
len = literal.length; len = literal.length;
return literal === string.substr(string.length - len - (back || 0), len); return literal === string.substr(string.length - len - (back || 0), len);
}; };
exports.compact = function(array) { exports.compact = function(array) {
var item, _i, _len, _results; var item, _i, _len, _results;
_results = []; _results = [];
for (_i = 0, _len = array.length; _i < _len; _i++) { for (_i = 0, _len = array.length; _i < _len; _i++) {
item = array[_i]; item = array[_i];
if (item) { if (item) _results.push(item);
_results.push(item);
}
} }
return _results; return _results;
}; };
exports.count = function(string, substr) { exports.count = function(string, substr) {
var num, pos; var num, pos;
num = pos = 0; num = pos = 0;
if (!substr.length) { if (!substr.length) return 1 / 0;
return 1 / 0;
}
while (pos = 1 + string.indexOf(substr, pos)) { while (pos = 1 + string.indexOf(substr, pos)) {
num++; num++;
} }
return num; return num;
}; };
exports.merge = function(options, overrides) { exports.merge = function(options, overrides) {
return extend(extend({}, options), overrides); return extend(extend({}, options), overrides);
}; };
extend = exports.extend = function(object, properties) { extend = exports.extend = function(object, properties) {
var key, val; var key, val;
for (key in properties) { for (key in properties) {
@ -67,6 +70,7 @@ define(function(require, exports, module) {
} }
return object; return object;
}; };
exports.flatten = flatten = function(array) { exports.flatten = flatten = function(array) {
var element, flattened, _i, _len; var element, flattened, _i, _len;
flattened = []; flattened = [];
@ -80,13 +84,17 @@ define(function(require, exports, module) {
} }
return flattened; return flattened;
}; };
exports.del = function(obj, key) { exports.del = function(obj, key) {
var val; var val;
val = obj[key]; val = obj[key];
delete obj[key]; delete obj[key];
return val; return val;
}; };
exports.last = function(array, back) { exports.last = function(array, back) {
return array[array.length - (back || 0) - 1]; return array[array.length - (back || 0) - 1];
}; };
});
});

View file

@ -1,6 +1,6 @@
/** /**
* Copyright (c) 2011 Jeremy Ashkenas * Copyright (c) 2011 Jeremy Ashkenas
* *
* Permission is hereby granted, free of charge, to any person * Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation * obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without * files (the "Software"), to deal in the Software without
@ -24,26 +24,25 @@
*/ */
define(function(require, exports, module) { define(function(require, exports, module) {
// Generated by CoffeeScript 1.2.1-pre
var ASSIGNED, BOOL, CALLABLE, CODE, COFFEE_ALIASES, COFFEE_ALIAS_MAP, COFFEE_KEYWORDS, COMMENT, COMPARE, COMPOUND_ASSIGN, HEREDOC, HEREDOC_ILLEGAL, HEREDOC_INDENT, HEREGEX, HEREGEX_OMIT, IDENTIFIER, INDEXABLE, JSTOKEN, JS_FORBIDDEN, JS_KEYWORDS, LINE_BREAK, LINE_CONTINUER, LOGIC, Lexer, MATH, MULTILINER, MULTI_DENT, NOT_REGEX, NOT_SPACED_REGEX, NO_NEWLINE, NUMBER, OPERATOR, REGEX, RELATION, RESERVED, Rewriter, SHIFT, SIMPLESTR, TRAILING_SPACES, UNARY, WHITESPACE, compact, count, key, last, starts, _ref;
var __indexOf = Array.prototype.indexOf || function(item) { var BOOL, CALLABLE, CODE, COFFEE_ALIASES, COFFEE_ALIAS_MAP, COFFEE_KEYWORDS, COMMENT, COMPARE, COMPOUND_ASSIGN, HEREDOC, HEREDOC_ILLEGAL, HEREDOC_INDENT, HEREGEX, HEREGEX_OMIT, IDENTIFIER, INDEXABLE, INVERSES, JSTOKEN, JS_FORBIDDEN, JS_KEYWORDS, LINE_BREAK, LINE_CONTINUER, LOGIC, Lexer, MATH, MULTILINER, MULTI_DENT, NOT_REGEX, NOT_SPACED_REGEX, NUMBER, OPERATOR, REGEX, RELATION, RESERVED, Rewriter, SHIFT, SIMPLESTR, STRICT_PROSCRIBED, TRAILING_SPACES, UNARY, WHITESPACE, compact, count, key, last, starts, _ref, _ref1,
for (var i = 0, l = this.length; i < l; i++) { __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
if (this[i] === item) return i;
} _ref = require('./rewriter'), Rewriter = _ref.Rewriter, INVERSES = _ref.INVERSES;
return -1;
}; _ref1 = require('./helpers'), count = _ref1.count, starts = _ref1.starts, compact = _ref1.compact, last = _ref1.last;
Rewriter = require('./rewriter').Rewriter;
_ref = require('./helpers'), count = _ref.count, starts = _ref.starts, compact = _ref.compact, last = _ref.last;
exports.Lexer = Lexer = (function() { exports.Lexer = Lexer = (function() {
Lexer.name = 'Lexer';
function Lexer() {} function Lexer() {}
Lexer.prototype.tokenize = function(code, opts) { Lexer.prototype.tokenize = function(code, opts) {
var i; var i, tag;
if (opts == null) { if (opts == null) opts = {};
opts = {}; if (WHITESPACE.test(code)) code = "\n" + code;
}
if (WHITESPACE.test(code)) {
code = "\n" + code;
}
code = code.replace(/\r/g, '').replace(TRAILING_SPACES, ''); code = code.replace(/\r/g, '').replace(TRAILING_SPACES, '');
this.code = code; this.code = code;
this.line = opts.line || 0; this.line = opts.line || 0;
@ -51,22 +50,21 @@ define(function(require, exports, module) {
this.indebt = 0; this.indebt = 0;
this.outdebt = 0; this.outdebt = 0;
this.indents = []; this.indents = [];
this.ends = [];
this.tokens = []; this.tokens = [];
i = 0; i = 0;
while (this.chunk = code.slice(i)) { while (this.chunk = code.slice(i)) {
i += this.identifierToken() || this.commentToken() || this.whitespaceToken() || this.lineToken() || this.heredocToken() || this.stringToken() || this.numberToken() || this.regexToken() || this.jsToken() || this.literalToken(); i += this.identifierToken() || this.commentToken() || this.whitespaceToken() || this.lineToken() || this.heredocToken() || this.stringToken() || this.numberToken() || this.regexToken() || this.jsToken() || this.literalToken();
} }
this.closeIndentation(); this.closeIndentation();
if (opts.rewrite === false) { if (tag = this.ends.pop()) this.error("missing " + tag);
return this.tokens; if (opts.rewrite === false) return this.tokens;
}
return (new Rewriter).rewrite(this.tokens); return (new Rewriter).rewrite(this.tokens);
}; };
Lexer.prototype.identifierToken = function() { Lexer.prototype.identifierToken = function() {
var colon, forcedIdentifier, id, input, match, prev, tag, _ref2, _ref3; var colon, forcedIdentifier, id, input, match, prev, tag, _ref2, _ref3;
if (!(match = IDENTIFIER.exec(this.chunk))) { if (!(match = IDENTIFIER.exec(this.chunk))) return 0;
return 0;
}
input = match[0], id = match[1], colon = match[2]; input = match[0], id = match[1], colon = match[2];
if (id === 'own' && this.tag() === 'FOR') { if (id === 'own' && this.tag() === 'FOR') {
this.token('OWN', id); this.token('OWN', id);
@ -74,7 +72,7 @@ define(function(require, exports, module) {
} }
forcedIdentifier = colon || (prev = last(this.tokens)) && (((_ref2 = prev[0]) === '.' || _ref2 === '?.' || _ref2 === '::') || !prev.spaced && prev[0] === '@'); forcedIdentifier = colon || (prev = last(this.tokens)) && (((_ref2 = prev[0]) === '.' || _ref2 === '?.' || _ref2 === '::') || !prev.spaced && prev[0] === '@');
tag = 'IDENTIFIER'; tag = 'IDENTIFIER';
if (__indexOf.call(JS_KEYWORDS, id) >= 0 || !forcedIdentifier && __indexOf.call(COFFEE_KEYWORDS, id) >= 0) { if (!forcedIdentifier && (__indexOf.call(JS_KEYWORDS, id) >= 0 || __indexOf.call(COFFEE_KEYWORDS, id) >= 0)) {
tag = id.toUpperCase(); tag = id.toUpperCase();
if (tag === 'WHEN' && (_ref3 = this.tag(), __indexOf.call(LINE_BREAK, _ref3) >= 0)) { if (tag === 'WHEN' && (_ref3 = this.tag(), __indexOf.call(LINE_BREAK, _ref3) >= 0)) {
tag = 'LEADING_WHEN'; tag = 'LEADING_WHEN';
@ -103,13 +101,11 @@ define(function(require, exports, module) {
id = new String(id); id = new String(id);
id.reserved = true; id.reserved = true;
} else if (__indexOf.call(RESERVED, id) >= 0) { } else if (__indexOf.call(RESERVED, id) >= 0) {
this.identifierError(id); this.error("reserved word \"" + id + "\"");
} }
} }
if (!forcedIdentifier) { if (!forcedIdentifier) {
if (__indexOf.call(COFFEE_ALIASES, id) >= 0) { if (__indexOf.call(COFFEE_ALIASES, id) >= 0) id = COFFEE_ALIAS_MAP[id];
id = COFFEE_ALIAS_MAP[id];
}
tag = (function() { tag = (function() {
switch (id) { switch (id) {
case '!': case '!':
@ -127,7 +123,6 @@ define(function(require, exports, module) {
return 'BOOL'; return 'BOOL';
case 'break': case 'break':
case 'continue': case 'continue':
case 'debugger':
return 'STATEMENT'; return 'STATEMENT';
default: default:
return tag; return tag;
@ -135,33 +130,43 @@ define(function(require, exports, module) {
})(); })();
} }
this.token(tag, id); this.token(tag, id);
if (colon) { if (colon) this.token(':', ':');
this.token(':', ':');
}
return input.length; return input.length;
}; };
Lexer.prototype.numberToken = function() { Lexer.prototype.numberToken = function() {
var match, number; var binaryLiteral, lexedLength, match, number, octalLiteral;
if (!(match = NUMBER.exec(this.chunk))) { if (!(match = NUMBER.exec(this.chunk))) return 0;
return 0;
}
number = match[0]; number = match[0];
if (/E/.test(number)) {
this.error("exponential notation '" + number + "' must be indicated with a lowercase 'e'");
} else if (/[BOX]/.test(number)) {
this.error("radix prefix '" + number + "' must be lowercase");
} else if (/^0[89]/.test(number)) {
this.error("decimal literal '" + number + "' must not be prefixed with '0'");
} else if (/^0[0-7]/.test(number)) {
this.error("octal literal '" + number + "' must be prefixed with '0o'");
}
lexedLength = number.length;
if (octalLiteral = /0o([0-7]+)/.exec(number)) {
number = (parseInt(octalLiteral[1], 8)).toString();
}
if (binaryLiteral = /0b([01]+)/.exec(number)) {
number = (parseInt(binaryLiteral[1], 2)).toString();
}
this.token('NUMBER', number); this.token('NUMBER', number);
return number.length; return lexedLength;
}; };
Lexer.prototype.stringToken = function() { Lexer.prototype.stringToken = function() {
var match, string; var match, octalEsc, string;
switch (this.chunk.charAt(0)) { switch (this.chunk.charAt(0)) {
case "'": case "'":
if (!(match = SIMPLESTR.exec(this.chunk))) { if (!(match = SIMPLESTR.exec(this.chunk))) return 0;
return 0;
}
this.token('STRING', (string = match[0]).replace(MULTILINER, '\\\n')); this.token('STRING', (string = match[0]).replace(MULTILINER, '\\\n'));
break; break;
case '"': case '"':
if (!(string = this.balancedString(this.chunk, '"'))) { if (!(string = this.balancedString(this.chunk, '"'))) return 0;
return 0;
}
if (0 < string.indexOf('#{', 1)) { if (0 < string.indexOf('#{', 1)) {
this.interpolateString(string.slice(1, -1)); this.interpolateString(string.slice(1, -1));
} else { } else {
@ -171,14 +176,16 @@ define(function(require, exports, module) {
default: default:
return 0; return 0;
} }
if (octalEsc = /^(?:\\.|[^\\])*\\[0-7]/.test(string)) {
this.error("octal escape sequences " + string + " are not allowed");
}
this.line += count(string, '\n'); this.line += count(string, '\n');
return string.length; return string.length;
}; };
Lexer.prototype.heredocToken = function() { Lexer.prototype.heredocToken = function() {
var doc, heredoc, match, quote; var doc, heredoc, match, quote;
if (!(match = HEREDOC.exec(this.chunk))) { if (!(match = HEREDOC.exec(this.chunk))) return 0;
return 0;
}
heredoc = match[0]; heredoc = match[0];
quote = heredoc.charAt(0); quote = heredoc.charAt(0);
doc = this.sanitizeHeredoc(match[2], { doc = this.sanitizeHeredoc(match[2], {
@ -195,22 +202,21 @@ define(function(require, exports, module) {
this.line += count(heredoc, '\n'); this.line += count(heredoc, '\n');
return heredoc.length; return heredoc.length;
}; };
Lexer.prototype.commentToken = function() { Lexer.prototype.commentToken = function() {
var comment, here, match; var comment, here, match;
if (!(match = this.chunk.match(COMMENT))) { if (!(match = this.chunk.match(COMMENT))) return 0;
return 0;
}
comment = match[0], here = match[1]; comment = match[0], here = match[1];
if (here) { if (here) {
this.token('HERECOMMENT', this.sanitizeHeredoc(here, { this.token('HERECOMMENT', this.sanitizeHeredoc(here, {
herecomment: true, herecomment: true,
indent: Array(this.indent + 1).join(' ') indent: Array(this.indent + 1).join(' ')
})); }));
this.token('TERMINATOR', '\n');
} }
this.line += count(comment, '\n'); this.line += count(comment, '\n');
return comment.length; return comment.length;
}; };
Lexer.prototype.jsToken = function() { Lexer.prototype.jsToken = function() {
var match, script; var match, script;
if (!(this.chunk.charAt(0) === '`' && (match = JSTOKEN.exec(this.chunk)))) { if (!(this.chunk.charAt(0) === '`' && (match = JSTOKEN.exec(this.chunk)))) {
@ -219,31 +225,37 @@ define(function(require, exports, module) {
this.token('JS', (script = match[0]).slice(1, -1)); this.token('JS', (script = match[0]).slice(1, -1));
return script.length; return script.length;
}; };
Lexer.prototype.regexToken = function() { Lexer.prototype.regexToken = function() {
var match, prev, regex, _ref2; var flags, length, match, prev, regex, _ref2, _ref3;
if (this.chunk.charAt(0) !== '/') { if (this.chunk.charAt(0) !== '/') return 0;
return 0;
}
if (match = HEREGEX.exec(this.chunk)) { if (match = HEREGEX.exec(this.chunk)) {
this.line += count(match[0], '\n'); length = this.heregexToken(match);
return this.heregexToken(match); this.line += count(match[0], '\n');
return length;
} }
prev = last(this.tokens); prev = last(this.tokens);
if (prev && (_ref2 = prev[0], __indexOf.call((prev.spaced ? NOT_REGEX : NOT_SPACED_REGEX), _ref2) >= 0)) { if (prev && (_ref2 = prev[0], __indexOf.call((prev.spaced ? NOT_REGEX : NOT_SPACED_REGEX), _ref2) >= 0)) {
return 0; return 0;
} }
if (!(match = REGEX.exec(this.chunk))) { if (!(match = REGEX.exec(this.chunk))) return 0;
return 0; _ref3 = match, match = _ref3[0], regex = _ref3[1], flags = _ref3[2];
if (regex.slice(0, 2) === '/*') {
this.error('regular expressions cannot begin with `*`');
} }
regex = match[0]; if (regex === '//') regex = '/(?:)/';
this.token('REGEX', regex === '//' ? '/(?:)/' : regex); this.token('REGEX', "" + regex + flags);
return regex.length; return match.length;
}; };
Lexer.prototype.heregexToken = function(match) { Lexer.prototype.heregexToken = function(match) {
var body, flags, heregex, re, tag, tokens, value, _i, _len, _ref2, _ref3, _ref4, _ref5; var body, flags, heregex, re, tag, tokens, value, _i, _len, _ref2, _ref3, _ref4, _ref5;
heregex = match[0], body = match[1], flags = match[2]; heregex = match[0], body = match[1], flags = match[2];
if (0 > body.indexOf('#{')) { if (0 > body.indexOf('#{')) {
re = body.replace(HEREGEX_OMIT, '').replace(/\//g, '\\/'); re = body.replace(HEREGEX_OMIT, '').replace(/\//g, '\\/');
if (re.match(/^\*/)) {
this.error('regular expressions cannot begin with `*`');
}
this.token('REGEX', "/" + (re || '(?:)') + "/" + flags); this.token('REGEX', "/" + (re || '(?:)') + "/" + flags);
return heregex.length; return heregex.length;
} }
@ -258,9 +270,7 @@ define(function(require, exports, module) {
if (tag === 'TOKENS') { if (tag === 'TOKENS') {
tokens.push.apply(tokens, value); tokens.push.apply(tokens, value);
} else { } else {
if (!(value = value.replace(HEREGEX_OMIT, ''))) { if (!(value = value.replace(HEREGEX_OMIT, ''))) continue;
continue;
}
value = value.replace(/\\/g, '\\\\'); value = value.replace(/\\/g, '\\\\');
tokens.push(['STRING', this.makeString(value, '"', true)]); tokens.push(['STRING', this.makeString(value, '"', true)]);
} }
@ -271,19 +281,17 @@ define(function(require, exports, module) {
this.tokens.push(['STRING', '""'], ['+', '+']); this.tokens.push(['STRING', '""'], ['+', '+']);
} }
(_ref5 = this.tokens).push.apply(_ref5, tokens); (_ref5 = this.tokens).push.apply(_ref5, tokens);
if (flags) { if (flags) this.tokens.push([',', ','], ['STRING', '"' + flags + '"']);
this.tokens.push([',', ','], ['STRING', '"' + flags + '"']);
}
this.token(')', ')'); this.token(')', ')');
return heregex.length; return heregex.length;
}; };
Lexer.prototype.lineToken = function() { Lexer.prototype.lineToken = function() {
var diff, indent, match, noNewlines, prev, size; var diff, indent, match, noNewlines, prev, size;
if (!(match = MULTI_DENT.exec(this.chunk))) { if (!(match = MULTI_DENT.exec(this.chunk))) return 0;
return 0;
}
indent = match[0]; indent = match[0];
this.line += count(indent, '\n'); this.line += count(indent, '\n');
this.seenFor = false;
prev = last(this.tokens, 1); prev = last(this.tokens, 1);
size = indent.length - 1 - indent.lastIndexOf('\n'); size = indent.length - 1 - indent.lastIndexOf('\n');
noNewlines = this.unfinished(); noNewlines = this.unfinished();
@ -304,6 +312,7 @@ define(function(require, exports, module) {
diff = size - this.indent + this.outdebt; diff = size - this.indent + this.outdebt;
this.token('INDENT', diff); this.token('INDENT', diff);
this.indents.push(diff); this.indents.push(diff);
this.ends.push('OUTDENT');
this.outdebt = this.indebt = 0; this.outdebt = this.indebt = 0;
} else { } else {
this.indebt = 0; this.indebt = 0;
@ -312,7 +321,8 @@ define(function(require, exports, module) {
this.indent = size; this.indent = size;
return indent.length; return indent.length;
}; };
Lexer.prototype.outdentToken = function(moveOut, noNewlines, close) {
Lexer.prototype.outdentToken = function(moveOut, noNewlines) {
var dent, len; var dent, len;
while (moveOut > 0) { while (moveOut > 0) {
len = this.indents.length - 1; len = this.indents.length - 1;
@ -328,51 +338,52 @@ define(function(require, exports, module) {
dent = this.indents.pop() - this.outdebt; dent = this.indents.pop() - this.outdebt;
moveOut -= dent; moveOut -= dent;
this.outdebt = 0; this.outdebt = 0;
this.pair('OUTDENT');
this.token('OUTDENT', dent); this.token('OUTDENT', dent);
} }
} }
if (dent) { if (dent) this.outdebt -= moveOut;
this.outdebt -= moveOut; while (this.value() === ';') {
this.tokens.pop();
} }
if (!(this.tag() === 'TERMINATOR' || noNewlines)) { if (!(this.tag() === 'TERMINATOR' || noNewlines)) {
this.token('TERMINATOR', '\n'); this.token('TERMINATOR', '\n');
} }
return this; return this;
}; };
Lexer.prototype.whitespaceToken = function() { Lexer.prototype.whitespaceToken = function() {
var match, nline, prev; var match, nline, prev;
if (!((match = WHITESPACE.exec(this.chunk)) || (nline = this.chunk.charAt(0) === '\n'))) { if (!((match = WHITESPACE.exec(this.chunk)) || (nline = this.chunk.charAt(0) === '\n'))) {
return 0; return 0;
} }
prev = last(this.tokens); prev = last(this.tokens);
if (prev) { if (prev) prev[match ? 'spaced' : 'newLine'] = true;
prev[match ? 'spaced' : 'newLine'] = true;
}
if (match) { if (match) {
return match[0].length; return match[0].length;
} else { } else {
return 0; return 0;
} }
}; };
Lexer.prototype.newlineToken = function() { Lexer.prototype.newlineToken = function() {
if (this.tag() !== 'TERMINATOR') { while (this.value() === ';') {
this.token('TERMINATOR', '\n');
}
return this;
};
Lexer.prototype.suppressNewlines = function() {
if (this.value() === '\\') {
this.tokens.pop(); this.tokens.pop();
} }
if (this.tag() !== 'TERMINATOR') this.token('TERMINATOR', '\n');
return this; return this;
}; };
Lexer.prototype.suppressNewlines = function() {
if (this.value() === '\\') this.tokens.pop();
return this;
};
Lexer.prototype.literalToken = function() { Lexer.prototype.literalToken = function() {
var match, prev, tag, value, _ref2, _ref3, _ref4, _ref5; var match, prev, tag, value, _ref2, _ref3, _ref4, _ref5;
if (match = OPERATOR.exec(this.chunk)) { if (match = OPERATOR.exec(this.chunk)) {
value = match[0]; value = match[0];
if (CODE.test(value)) { if (CODE.test(value)) this.tagParameters();
this.tagParameters();
}
} else { } else {
value = this.chunk.charAt(0); value = this.chunk.charAt(0);
} }
@ -380,7 +391,7 @@ define(function(require, exports, module) {
prev = last(this.tokens); prev = last(this.tokens);
if (value === '=' && prev) { if (value === '=' && prev) {
if (!prev[1].reserved && (_ref2 = prev[1], __indexOf.call(JS_FORBIDDEN, _ref2) >= 0)) { if (!prev[1].reserved && (_ref2 = prev[1], __indexOf.call(JS_FORBIDDEN, _ref2) >= 0)) {
this.assignmentError(); this.error("reserved word \"" + (this.value()) + "\" can't be assigned");
} }
if ((_ref3 = prev[1]) === '||' || _ref3 === '&&') { if ((_ref3 = prev[1]) === '||' || _ref3 === '&&') {
prev[0] = 'COMPOUND_ASSIGN'; prev[0] = 'COMPOUND_ASSIGN';
@ -389,6 +400,7 @@ define(function(require, exports, module) {
} }
} }
if (value === ';') { if (value === ';') {
this.seenFor = false;
tag = 'TERMINATOR'; tag = 'TERMINATOR';
} else if (__indexOf.call(MATH, value) >= 0) { } else if (__indexOf.call(MATH, value) >= 0) {
tag = 'MATH'; tag = 'MATH';
@ -404,34 +416,39 @@ define(function(require, exports, module) {
tag = 'LOGIC'; tag = 'LOGIC';
} else if (prev && !prev.spaced) { } else if (prev && !prev.spaced) {
if (value === '(' && (_ref4 = prev[0], __indexOf.call(CALLABLE, _ref4) >= 0)) { if (value === '(' && (_ref4 = prev[0], __indexOf.call(CALLABLE, _ref4) >= 0)) {
if (prev[0] === '?') { if (prev[0] === '?') prev[0] = 'FUNC_EXIST';
prev[0] = 'FUNC_EXIST';
}
tag = 'CALL_START'; tag = 'CALL_START';
} else if (value === '[' && (_ref5 = prev[0], __indexOf.call(INDEXABLE, _ref5) >= 0)) { } else if (value === '[' && (_ref5 = prev[0], __indexOf.call(INDEXABLE, _ref5) >= 0)) {
tag = 'INDEX_START'; tag = 'INDEX_START';
switch (prev[0]) { switch (prev[0]) {
case '?': case '?':
prev[0] = 'INDEX_SOAK'; prev[0] = 'INDEX_SOAK';
break;
case '::':
prev[0] = 'INDEX_PROTO';
} }
} }
} }
switch (value) {
case '(':
case '{':
case '[':
this.ends.push(INVERSES[value]);
break;
case ')':
case '}':
case ']':
this.pair(value);
}
this.token(tag, value); this.token(tag, value);
return value.length; return value.length;
}; };
Lexer.prototype.sanitizeHeredoc = function(doc, options) { Lexer.prototype.sanitizeHeredoc = function(doc, options) {
var attempt, herecomment, indent, match, _ref2; var attempt, herecomment, indent, match, _ref2;
indent = options.indent, herecomment = options.herecomment; indent = options.indent, herecomment = options.herecomment;
if (herecomment) { if (herecomment) {
if (HEREDOC_ILLEGAL.test(doc)) { if (HEREDOC_ILLEGAL.test(doc)) {
throw new Error("block comment cannot contain \"*/\", starting on line " + (this.line + 1)); this.error("block comment cannot contain \"*/\", starting");
}
if (doc.indexOf('\n') <= 0) {
return doc;
} }
if (doc.indexOf('\n') <= 0) return doc;
} else { } else {
while (match = HEREDOC_INDENT.exec(doc)) { while (match = HEREDOC_INDENT.exec(doc)) {
attempt = match[1]; attempt = match[1];
@ -440,19 +457,14 @@ define(function(require, exports, module) {
} }
} }
} }
if (indent) { if (indent) doc = doc.replace(RegExp("\\n" + indent, "g"), '\n');
doc = doc.replace(RegExp("\\n" + indent, "g"), '\n'); if (!herecomment) doc = doc.replace(/^\n/, '');
}
if (!herecomment) {
doc = doc.replace(/^\n/, '');
}
return doc; return doc;
}; };
Lexer.prototype.tagParameters = function() { Lexer.prototype.tagParameters = function() {
var i, stack, tok, tokens; var i, stack, tok, tokens;
if (this.tag() !== ')') { if (this.tag() !== ')') return this;
return this;
}
stack = []; stack = [];
tokens = this.tokens; tokens = this.tokens;
i = tokens.length; i = tokens.length;
@ -469,38 +481,41 @@ define(function(require, exports, module) {
} else if (tok[0] === '(') { } else if (tok[0] === '(') {
tok[0] = 'PARAM_START'; tok[0] = 'PARAM_START';
return this; return this;
} else {
return this;
} }
} }
} }
return this; return this;
}; };
Lexer.prototype.closeIndentation = function() { Lexer.prototype.closeIndentation = function() {
return this.outdentToken(this.indent); return this.outdentToken(this.indent);
}; };
Lexer.prototype.identifierError = function(word) {
throw SyntaxError("Reserved word \"" + word + "\" on line " + (this.line + 1));
};
Lexer.prototype.assignmentError = function() {
throw SyntaxError("Reserved word \"" + (this.value()) + "\" on line " + (this.line + 1) + " can't be assigned");
};
Lexer.prototype.balancedString = function(str, end) { Lexer.prototype.balancedString = function(str, end) {
var i, letter, prev, stack, _ref2; var continueCount, i, letter, match, prev, stack, _i, _ref2;
continueCount = 0;
stack = [end]; stack = [end];
for (i = 1, _ref2 = str.length; 1 <= _ref2 ? i < _ref2 : i > _ref2; 1 <= _ref2 ? i++ : i--) { for (i = _i = 1, _ref2 = str.length; 1 <= _ref2 ? _i < _ref2 : _i > _ref2; i = 1 <= _ref2 ? ++_i : --_i) {
if (continueCount) {
--continueCount;
continue;
}
switch (letter = str.charAt(i)) { switch (letter = str.charAt(i)) {
case '\\': case '\\':
i++; ++continueCount;
continue; continue;
case end: case end:
stack.pop(); stack.pop();
if (!stack.length) { if (!stack.length) return str.slice(0, i + 1 || 9e9);
return str.slice(0, i + 1);
}
end = stack[stack.length - 1]; end = stack[stack.length - 1];
continue; continue;
} }
if (end === '}' && (letter === '"' || letter === "'")) { if (end === '}' && (letter === '"' || letter === "'")) {
stack.push(end = letter); stack.push(end = letter);
} else if (end === '}' && letter === '/' && (match = HEREGEX.exec(str.slice(i)) || REGEX.exec(str.slice(i)))) {
continueCount += match[0].length - 1;
} else if (end === '}' && letter === '{') { } else if (end === '}' && letter === '{') {
stack.push(end = '}'); stack.push(end = '}');
} else if (end === '"' && prev === '#' && letter === '{') { } else if (end === '"' && prev === '#' && letter === '{') {
@ -508,13 +523,12 @@ define(function(require, exports, module) {
} }
prev = letter; prev = letter;
} }
throw new Error("missing " + (stack.pop()) + ", starting on line " + (this.line + 1)); return this.error("missing " + (stack.pop()) + ", starting");
}; };
Lexer.prototype.interpolateString = function(str, options) { Lexer.prototype.interpolateString = function(str, options) {
var expr, heredoc, i, inner, interpolated, len, letter, nested, pi, regex, tag, tokens, value, _len, _ref2, _ref3, _ref4; var expr, heredoc, i, inner, interpolated, len, letter, nested, pi, regex, tag, tokens, value, _i, _len, _ref2, _ref3, _ref4;
if (options == null) { if (options == null) options = {};
options = {};
}
heredoc = options.heredoc, regex = options.regex; heredoc = options.heredoc, regex = options.regex;
tokens = []; tokens = [];
pi = 0; pi = 0;
@ -527,9 +541,7 @@ define(function(require, exports, module) {
if (!(letter === '#' && str.charAt(i + 1) === '{' && (expr = this.balancedString(str.slice(i + 1), '}')))) { if (!(letter === '#' && str.charAt(i + 1) === '{' && (expr = this.balancedString(str.slice(i + 1), '}')))) {
continue; continue;
} }
if (pi < i) { if (pi < i) tokens.push(['NEOSTRING', str.slice(pi, i)]);
tokens.push(['NEOSTRING', str.slice(pi, i)]);
}
inner = expr.slice(1, -1); inner = expr.slice(1, -1);
if (inner.length) { if (inner.length) {
nested = new Lexer().tokenize(inner, { nested = new Lexer().tokenize(inner, {
@ -542,8 +554,8 @@ define(function(require, exports, module) {
} }
if (len = nested.length) { if (len = nested.length) {
if (len > 1) { if (len > 1) {
nested.unshift(['(', '(']); nested.unshift(['(', '(', this.line]);
nested.push([')', ')']); nested.push([')', ')', this.line]);
} }
tokens.push(['TOKENS', nested]); tokens.push(['TOKENS', nested]);
} }
@ -551,59 +563,60 @@ define(function(require, exports, module) {
i += expr.length; i += expr.length;
pi = i + 1; pi = i + 1;
} }
if ((i > pi && pi < str.length)) { if ((i > pi && pi < str.length)) tokens.push(['NEOSTRING', str.slice(pi)]);
tokens.push(['NEOSTRING', str.slice(pi)]); if (regex) return tokens;
} if (!tokens.length) return this.token('STRING', '""');
if (regex) { if (tokens[0][0] !== 'NEOSTRING') tokens.unshift(['', '']);
return tokens; if (interpolated = tokens.length > 1) this.token('(', '(');
} for (i = _i = 0, _len = tokens.length; _i < _len; i = ++_i) {
if (!tokens.length) {
return this.token('STRING', '""');
}
if (tokens[0][0] !== 'NEOSTRING') {
tokens.unshift(['', '']);
}
if (interpolated = tokens.length > 1) {
this.token('(', '(');
}
for (i = 0, _len = tokens.length; i < _len; i++) {
_ref3 = tokens[i], tag = _ref3[0], value = _ref3[1]; _ref3 = tokens[i], tag = _ref3[0], value = _ref3[1];
if (i) { if (i) this.token('+', '+');
this.token('+', '+');
}
if (tag === 'TOKENS') { if (tag === 'TOKENS') {
(_ref4 = this.tokens).push.apply(_ref4, value); (_ref4 = this.tokens).push.apply(_ref4, value);
} else { } else {
this.token('STRING', this.makeString(value, '"', heredoc)); this.token('STRING', this.makeString(value, '"', heredoc));
} }
} }
if (interpolated) { if (interpolated) this.token(')', ')');
this.token(')', ')');
}
return tokens; return tokens;
}; };
Lexer.prototype.pair = function(tag) {
var size, wanted;
if (tag !== (wanted = last(this.ends))) {
if ('OUTDENT' !== wanted) this.error("unmatched " + tag);
this.indent -= size = last(this.indents);
this.outdentToken(size, true);
return this.pair(tag);
}
return this.ends.pop();
};
Lexer.prototype.token = function(tag, value) { Lexer.prototype.token = function(tag, value) {
return this.tokens.push([tag, value, this.line]); return this.tokens.push([tag, value, this.line]);
}; };
Lexer.prototype.tag = function(index, tag) { Lexer.prototype.tag = function(index, tag) {
var tok; var tok;
return (tok = last(this.tokens, index)) && (tag ? tok[0] = tag : tok[0]); return (tok = last(this.tokens, index)) && (tag ? tok[0] = tag : tok[0]);
}; };
Lexer.prototype.value = function(index, val) { Lexer.prototype.value = function(index, val) {
var tok; var tok;
return (tok = last(this.tokens, index)) && (val ? tok[1] = val : tok[1]); return (tok = last(this.tokens, index)) && (val ? tok[1] = val : tok[1]);
}; };
Lexer.prototype.unfinished = function() { Lexer.prototype.unfinished = function() {
var prev, value; var _ref2;
return LINE_CONTINUER.test(this.chunk) || (prev = last(this.tokens, 1)) && prev[0] !== '.' && (value = this.value()) && !value.reserved && NO_NEWLINE.test(value) && !CODE.test(value) && !ASSIGNED.test(this.chunk); return LINE_CONTINUER.test(this.chunk) || ((_ref2 = this.tag()) === '\\' || _ref2 === '.' || _ref2 === '?.' || _ref2 === 'UNARY' || _ref2 === 'MATH' || _ref2 === '+' || _ref2 === '-' || _ref2 === 'SHIFT' || _ref2 === 'RELATION' || _ref2 === 'COMPARE' || _ref2 === 'LOGIC' || _ref2 === 'THROW' || _ref2 === 'EXTENDS');
}; };
Lexer.prototype.escapeLines = function(str, heredoc) { Lexer.prototype.escapeLines = function(str, heredoc) {
return str.replace(MULTILINER, heredoc ? '\\n' : ''); return str.replace(MULTILINER, heredoc ? '\\n' : '');
}; };
Lexer.prototype.makeString = function(body, quote, heredoc) { Lexer.prototype.makeString = function(body, quote, heredoc) {
if (!body) { if (!body) return quote + quote;
return quote + quote;
}
body = body.replace(/\\([\s\S])/g, function(match, contents) { body = body.replace(/\\([\s\S])/g, function(match, contents) {
if (contents === '\n' || contents === quote) { if (contents === '\n' || contents === quote) {
return contents; return contents;
@ -614,10 +627,19 @@ define(function(require, exports, module) {
body = body.replace(RegExp("" + quote, "g"), '\\$&'); body = body.replace(RegExp("" + quote, "g"), '\\$&');
return quote + this.escapeLines(body, heredoc) + quote; return quote + this.escapeLines(body, heredoc) + quote;
}; };
Lexer.prototype.error = function(message) {
throw SyntaxError("" + message + " on line " + (this.line + 1));
};
return Lexer; return Lexer;
})(); })();
JS_KEYWORDS = ['true', 'false', 'null', 'this', 'new', 'delete', 'typeof', 'in', 'instanceof', 'return', 'throw', 'break', 'continue', 'debugger', 'if', 'else', 'switch', 'for', 'while', 'do', 'try', 'catch', 'finally', 'class', 'extends', 'super']; JS_KEYWORDS = ['true', 'false', 'null', 'this', 'new', 'delete', 'typeof', 'in', 'instanceof', 'return', 'throw', 'break', 'continue', 'debugger', 'if', 'else', 'switch', 'for', 'while', 'do', 'try', 'catch', 'finally', 'class', 'extends', 'super'];
COFFEE_KEYWORDS = ['undefined', 'then', 'unless', 'until', 'loop', 'of', 'by', 'when']; COFFEE_KEYWORDS = ['undefined', 'then', 'unless', 'until', 'loop', 'of', 'by', 'when'];
COFFEE_ALIAS_MAP = { COFFEE_ALIAS_MAP = {
and: '&&', and: '&&',
or: '||', or: '||',
@ -629,6 +651,7 @@ define(function(require, exports, module) {
on: 'true', on: 'true',
off: 'false' off: 'false'
}; };
COFFEE_ALIASES = (function() { COFFEE_ALIASES = (function() {
var _results; var _results;
_results = []; _results = [];
@ -637,41 +660,80 @@ define(function(require, exports, module) {
} }
return _results; return _results;
})(); })();
COFFEE_KEYWORDS = COFFEE_KEYWORDS.concat(COFFEE_ALIASES); COFFEE_KEYWORDS = COFFEE_KEYWORDS.concat(COFFEE_ALIASES);
RESERVED = ['case', 'default', 'function', 'var', 'void', 'with', 'const', 'let', 'enum', 'export', 'import', 'native', '__hasProp', '__extends', '__slice', '__bind', '__indexOf'];
JS_FORBIDDEN = JS_KEYWORDS.concat(RESERVED); RESERVED = ['case', 'default', 'function', 'var', 'void', 'with', 'const', 'let', 'enum', 'export', 'import', 'native', '__hasProp', '__extends', '__slice', '__bind', '__indexOf', 'implements', 'interface', 'let', 'package', 'private', 'protected', 'public', 'static', 'yield'];
exports.RESERVED = RESERVED.concat(JS_KEYWORDS).concat(COFFEE_KEYWORDS);
STRICT_PROSCRIBED = ['arguments', 'eval'];
JS_FORBIDDEN = JS_KEYWORDS.concat(RESERVED).concat(STRICT_PROSCRIBED);
exports.RESERVED = RESERVED.concat(JS_KEYWORDS).concat(COFFEE_KEYWORDS).concat(STRICT_PROSCRIBED);
exports.STRICT_PROSCRIBED = STRICT_PROSCRIBED;
IDENTIFIER = /^([$A-Za-z_\x7f-\uffff][$\w\x7f-\uffff]*)([^\n\S]*:(?!:))?/; IDENTIFIER = /^([$A-Za-z_\x7f-\uffff][$\w\x7f-\uffff]*)([^\n\S]*:(?!:))?/;
NUMBER = /^0x[\da-f]+|^(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?/i;
NUMBER = /^0b[01]+|^0o[0-7]+|^0x[\da-f]+|^\d*\.?\d+(?:e[+-]?\d+)?/i;
HEREDOC = /^("""|''')([\s\S]*?)(?:\n[^\n\S]*)?\1/; HEREDOC = /^("""|''')([\s\S]*?)(?:\n[^\n\S]*)?\1/;
OPERATOR = /^(?:[-=]>|[-+*\/%<>&|^!?=]=|>>>=?|([-+:])\1|([&|<>])\2=?|\?\.|\.{2,3})/; OPERATOR = /^(?:[-=]>|[-+*\/%<>&|^!?=]=|>>>=?|([-+:])\1|([&|<>])\2=?|\?\.|\.{2,3})/;
WHITESPACE = /^[^\n\S]+/; WHITESPACE = /^[^\n\S]+/;
COMMENT = /^###([^#][\s\S]*?)(?:###[^\n\S]*|(?:###)?$)|^(?:\s*#(?!##[^#]).*)+/; COMMENT = /^###([^#][\s\S]*?)(?:###[^\n\S]*|(?:###)?$)|^(?:\s*#(?!##[^#]).*)+/;
CODE = /^[-=]>/; CODE = /^[-=]>/;
MULTI_DENT = /^(?:\n[^\n\S]*)+/; MULTI_DENT = /^(?:\n[^\n\S]*)+/;
SIMPLESTR = /^'[^\\']*(?:\\.[^\\']*)*'/; SIMPLESTR = /^'[^\\']*(?:\\.[^\\']*)*'/;
JSTOKEN = /^`[^\\`]*(?:\\.[^\\`]*)*`/; JSTOKEN = /^`[^\\`]*(?:\\.[^\\`]*)*`/;
REGEX = /^\/(?![\s=])[^[\/\n\\]*(?:(?:\\[\s\S]|\[[^\]\n\\]*(?:\\[\s\S][^\]\n\\]*)*])[^[\/\n\\]*)*\/[imgy]{0,4}(?!\w)/;
REGEX = /^(\/(?![\s=])[^[\/\n\\]*(?:(?:\\[\s\S]|\[[^\]\n\\]*(?:\\[\s\S][^\]\n\\]*)*])[^[\/\n\\]*)*\/)([imgy]{0,4})(?!\w)/;
HEREGEX = /^\/{3}([\s\S]+?)\/{3}([imgy]{0,4})(?!\w)/; HEREGEX = /^\/{3}([\s\S]+?)\/{3}([imgy]{0,4})(?!\w)/;
HEREGEX_OMIT = /\s+(?:#.*)?/g; HEREGEX_OMIT = /\s+(?:#.*)?/g;
MULTILINER = /\n/g; MULTILINER = /\n/g;
HEREDOC_INDENT = /\n+([^\n\S]*)/g; HEREDOC_INDENT = /\n+([^\n\S]*)/g;
HEREDOC_ILLEGAL = /\*\//; HEREDOC_ILLEGAL = /\*\//;
ASSIGNED = /^\s*@?([$A-Za-z_][$\w\x7f-\uffff]*|['"].*['"])[^\n\S]*?[:=][^:=>]/;
LINE_CONTINUER = /^\s*(?:,|\??\.(?![.\d])|::)/; LINE_CONTINUER = /^\s*(?:,|\??\.(?![.\d])|::)/;
TRAILING_SPACES = /\s+$/; TRAILING_SPACES = /\s+$/;
NO_NEWLINE = /^(?:[-+*&|\/%=<>!.\\][<>=&|]*|and|or|is(?:nt)?|n(?:ot|ew)|delete|typeof|instanceof)$/;
COMPOUND_ASSIGN = ['-=', '+=', '/=', '*=', '%=', '||=', '&&=', '?=', '<<=', '>>=', '>>>=', '&=', '^=', '|=']; COMPOUND_ASSIGN = ['-=', '+=', '/=', '*=', '%=', '||=', '&&=', '?=', '<<=', '>>=', '>>>=', '&=', '^=', '|='];
UNARY = ['!', '~', 'NEW', 'TYPEOF', 'DELETE', 'DO']; UNARY = ['!', '~', 'NEW', 'TYPEOF', 'DELETE', 'DO'];
LOGIC = ['&&', '||', '&', '|', '^']; LOGIC = ['&&', '||', '&', '|', '^'];
SHIFT = ['<<', '>>', '>>>']; SHIFT = ['<<', '>>', '>>>'];
COMPARE = ['==', '!=', '<', '>', '<=', '>=']; COMPARE = ['==', '!=', '<', '>', '<=', '>='];
MATH = ['*', '/', '%']; MATH = ['*', '/', '%'];
RELATION = ['IN', 'OF', 'INSTANCEOF']; RELATION = ['IN', 'OF', 'INSTANCEOF'];
BOOL = ['TRUE', 'FALSE', 'NULL', 'UNDEFINED']; BOOL = ['TRUE', 'FALSE', 'NULL', 'UNDEFINED'];
NOT_REGEX = ['NUMBER', 'REGEX', 'BOOL', '++', '--', ']']; NOT_REGEX = ['NUMBER', 'REGEX', 'BOOL', '++', '--', ']'];
NOT_SPACED_REGEX = NOT_REGEX.concat(')', '}', 'THIS', 'IDENTIFIER', 'STRING'); NOT_SPACED_REGEX = NOT_REGEX.concat(')', '}', 'THIS', 'IDENTIFIER', 'STRING');
CALLABLE = ['IDENTIFIER', 'STRING', 'REGEX', ')', ']', '}', '?', '::', '@', 'THIS', 'SUPER']; CALLABLE = ['IDENTIFIER', 'STRING', 'REGEX', ')', ']', '}', '?', '::', '@', 'THIS', 'SUPER'];
INDEXABLE = CALLABLE.concat('NUMBER', 'BOOL'); INDEXABLE = CALLABLE.concat('NUMBER', 'BOOL');
LINE_BREAK = ['INDENT', 'OUTDENT', 'TERMINATOR']; LINE_BREAK = ['INDENT', 'OUTDENT', 'TERMINATOR'];
});
});

File diff suppressed because it is too large Load diff

File diff suppressed because one or more lines are too long

View file

@ -1,6 +1,6 @@
/** /**
* Copyright (c) 2011 Jeremy Ashkenas * Copyright (c) 2011 Jeremy Ashkenas
* *
* Permission is hereby granted, free of charge, to any person * Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation * obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without * files (the "Software"), to deal in the Software without
@ -24,16 +24,18 @@
*/ */
define(function(require, exports, module) { define(function(require, exports, module) {
// Generated by CoffeeScript 1.2.1-pre
var BALANCED_PAIRS, EXPRESSION_CLOSE, EXPRESSION_END, EXPRESSION_START, IMPLICIT_BLOCK, IMPLICIT_CALL, IMPLICIT_END, IMPLICIT_FUNC, IMPLICIT_UNSPACED_CALL, INVERSES, LINEBREAKS, SINGLE_CLOSERS, SINGLE_LINERS, left, rite, _i, _len, _ref;
var __indexOf = Array.prototype.indexOf || function(item) { var BALANCED_PAIRS, EXPRESSION_CLOSE, EXPRESSION_END, EXPRESSION_START, IMPLICIT_BLOCK, IMPLICIT_CALL, IMPLICIT_END, IMPLICIT_FUNC, IMPLICIT_UNSPACED_CALL, INVERSES, LINEBREAKS, SINGLE_CLOSERS, SINGLE_LINERS, left, rite, _i, _len, _ref,
for (var i = 0, l = this.length; i < l; i++) { __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; },
if (this[i] === item) return i; __slice = [].slice;
}
return -1;
}, __slice = Array.prototype.slice;
exports.Rewriter = (function() { exports.Rewriter = (function() {
Rewriter.name = 'Rewriter';
function Rewriter() {} function Rewriter() {}
Rewriter.prototype.rewrite = function(tokens) { Rewriter.prototype.rewrite = function(tokens) {
this.tokens = tokens; this.tokens = tokens;
this.removeLeadingNewlines(); this.removeLeadingNewlines();
@ -44,10 +46,9 @@ define(function(require, exports, module) {
this.tagPostfixConditionals(); this.tagPostfixConditionals();
this.addImplicitBraces(); this.addImplicitBraces();
this.addImplicitParentheses(); this.addImplicitParentheses();
this.ensureBalance(BALANCED_PAIRS);
this.rewriteClosingParens();
return this.tokens; return this.tokens;
}; };
Rewriter.prototype.scanTokens = function(block) { Rewriter.prototype.scanTokens = function(block) {
var i, token, tokens; var i, token, tokens;
tokens = this.tokens; tokens = this.tokens;
@ -57,39 +58,36 @@ define(function(require, exports, module) {
} }
return true; return true;
}; };
Rewriter.prototype.detectEnd = function(i, condition, action) { Rewriter.prototype.detectEnd = function(i, condition, action) {
var levels, token, tokens, _ref, _ref2; var levels, token, tokens, _ref, _ref1;
tokens = this.tokens; tokens = this.tokens;
levels = 0; levels = 0;
while (token = tokens[i]) { while (token = tokens[i]) {
if (levels === 0 && condition.call(this, token, i)) { if (levels === 0 && condition.call(this, token, i)) {
return action.call(this, token, i); return action.call(this, token, i);
} }
if (!token || levels < 0) { if (!token || levels < 0) return action.call(this, token, i - 1);
return action.call(this, token, i - 1);
}
if (_ref = token[0], __indexOf.call(EXPRESSION_START, _ref) >= 0) { if (_ref = token[0], __indexOf.call(EXPRESSION_START, _ref) >= 0) {
levels += 1; levels += 1;
} else if (_ref2 = token[0], __indexOf.call(EXPRESSION_END, _ref2) >= 0) { } else if (_ref1 = token[0], __indexOf.call(EXPRESSION_END, _ref1) >= 0) {
levels -= 1; levels -= 1;
} }
i += 1; i += 1;
} }
return i - 1; return i - 1;
}; };
Rewriter.prototype.removeLeadingNewlines = function() { Rewriter.prototype.removeLeadingNewlines = function() {
var i, tag, _len, _ref; var i, tag, _i, _len, _ref;
_ref = this.tokens; _ref = this.tokens;
for (i = 0, _len = _ref.length; i < _len; i++) { for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
tag = _ref[i][0]; tag = _ref[i][0];
if (tag !== 'TERMINATOR') { if (tag !== 'TERMINATOR') break;
break;
}
}
if (i) {
return this.tokens.splice(0, i);
} }
if (i) return this.tokens.splice(0, i);
}; };
Rewriter.prototype.removeMidExpressionNewlines = function() { Rewriter.prototype.removeMidExpressionNewlines = function() {
return this.scanTokens(function(token, i, tokens) { return this.scanTokens(function(token, i, tokens) {
var _ref; var _ref;
@ -100,6 +98,7 @@ define(function(require, exports, module) {
return 0; return 0;
}); });
}; };
Rewriter.prototype.closeOpenCalls = function() { Rewriter.prototype.closeOpenCalls = function() {
var action, condition; var action, condition;
condition = function(token, i) { condition = function(token, i) {
@ -110,12 +109,11 @@ define(function(require, exports, module) {
return this.tokens[token[0] === 'OUTDENT' ? i - 1 : i][0] = 'CALL_END'; return this.tokens[token[0] === 'OUTDENT' ? i - 1 : i][0] = 'CALL_END';
}; };
return this.scanTokens(function(token, i) { return this.scanTokens(function(token, i) {
if (token[0] === 'CALL_START') { if (token[0] === 'CALL_START') this.detectEnd(i + 1, condition, action);
this.detectEnd(i + 1, condition, action);
}
return 1; return 1;
}); });
}; };
Rewriter.prototype.closeOpenIndexes = function() { Rewriter.prototype.closeOpenIndexes = function() {
var action, condition; var action, condition;
condition = function(token, i) { condition = function(token, i) {
@ -126,34 +124,33 @@ define(function(require, exports, module) {
return token[0] = 'INDEX_END'; return token[0] = 'INDEX_END';
}; };
return this.scanTokens(function(token, i) { return this.scanTokens(function(token, i) {
if (token[0] === 'INDEX_START') { if (token[0] === 'INDEX_START') this.detectEnd(i + 1, condition, action);
this.detectEnd(i + 1, condition, action);
}
return 1; return 1;
}); });
}; };
Rewriter.prototype.addImplicitBraces = function() { Rewriter.prototype.addImplicitBraces = function() {
var action, condition, stack, start, startIndent; var action, condition, sameLine, stack, start, startIndent, startsLine;
stack = []; stack = [];
start = null; start = null;
startsLine = null;
sameLine = true;
startIndent = 0; startIndent = 0;
condition = function(token, i) { condition = function(token, i) {
var one, tag, three, two, _ref, _ref2; var one, tag, three, two, _ref, _ref1;
_ref = this.tokens.slice(i + 1, (i + 3 + 1) || 9e9), one = _ref[0], two = _ref[1], three = _ref[2]; _ref = this.tokens.slice(i + 1, (i + 3) + 1 || 9e9), one = _ref[0], two = _ref[1], three = _ref[2];
if ('HERECOMMENT' === (one != null ? one[0] : void 0)) { if ('HERECOMMENT' === (one != null ? one[0] : void 0)) return false;
return false;
}
tag = token[0]; tag = token[0];
return ((tag === 'TERMINATOR' || tag === 'OUTDENT') && !((two != null ? two[0] : void 0) === ':' || (one != null ? one[0] : void 0) === '@' && (three != null ? three[0] : void 0) === ':')) || (tag === ',' && one && ((_ref2 = one[0]) !== 'IDENTIFIER' && _ref2 !== 'NUMBER' && _ref2 !== 'STRING' && _ref2 !== '@' && _ref2 !== 'TERMINATOR' && _ref2 !== 'OUTDENT')); if (__indexOf.call(LINEBREAKS, tag) >= 0) sameLine = false;
return (((tag === 'TERMINATOR' || tag === 'OUTDENT') || (__indexOf.call(IMPLICIT_END, tag) >= 0 && sameLine)) && ((!startsLine && this.tag(i - 1) !== ',') || !((two != null ? two[0] : void 0) === ':' || (one != null ? one[0] : void 0) === '@' && (three != null ? three[0] : void 0) === ':'))) || (tag === ',' && one && ((_ref1 = one[0]) !== 'IDENTIFIER' && _ref1 !== 'NUMBER' && _ref1 !== 'STRING' && _ref1 !== '@' && _ref1 !== 'TERMINATOR' && _ref1 !== 'OUTDENT'));
}; };
action = function(token, i) { action = function(token, i) {
var tok; var tok;
tok = ['}', '}', token[2]]; tok = this.generate('}', '}', token[2]);
tok.generated = true;
return this.tokens.splice(i, 0, tok); return this.tokens.splice(i, 0, tok);
}; };
return this.scanTokens(function(token, i, tokens) { return this.scanTokens(function(token, i, tokens) {
var ago, idx, tag, tok, value, _ref, _ref2; var ago, idx, prevTag, tag, tok, value, _ref, _ref1;
if (_ref = (tag = token[0]), __indexOf.call(EXPRESSION_START, _ref) >= 0) { if (_ref = (tag = token[0]), __indexOf.call(EXPRESSION_START, _ref) >= 0) {
stack.push([(tag === 'INDENT' && this.tag(i - 1) === '{' ? '{' : tag), i]); stack.push([(tag === 'INDENT' && this.tag(i - 1) === '{' ? '{' : tag), i]);
return 1; return 1;
@ -162,80 +159,82 @@ define(function(require, exports, module) {
start = stack.pop(); start = stack.pop();
return 1; return 1;
} }
if (!(tag === ':' && ((ago = this.tag(i - 2)) === ':' || ((_ref2 = stack[stack.length - 1]) != null ? _ref2[0] : void 0) !== '{'))) { if (!(tag === ':' && ((ago = this.tag(i - 2)) === ':' || ((_ref1 = stack[stack.length - 1]) != null ? _ref1[0] : void 0) !== '{'))) {
return 1; return 1;
} }
sameLine = true;
stack.push(['{']); stack.push(['{']);
idx = ago === '@' ? i - 2 : i - 1; idx = ago === '@' ? i - 2 : i - 1;
while (this.tag(idx - 2) === 'HERECOMMENT') { while (this.tag(idx - 2) === 'HERECOMMENT') {
idx -= 2; idx -= 2;
} }
prevTag = this.tag(idx - 1);
startsLine = !prevTag || (__indexOf.call(LINEBREAKS, prevTag) >= 0);
value = new String('{'); value = new String('{');
value.generated = true; value.generated = true;
tok = ['{', value, token[2]]; tok = this.generate('{', value, token[2]);
tok.generated = true;
tokens.splice(idx, 0, tok); tokens.splice(idx, 0, tok);
this.detectEnd(i + 2, condition, action); this.detectEnd(i + 2, condition, action);
return 2; return 2;
}); });
}; };
Rewriter.prototype.addImplicitParentheses = function() { Rewriter.prototype.addImplicitParentheses = function() {
var action, noCall; var action, condition, noCall, seenControl, seenSingle;
noCall = false; noCall = seenSingle = seenControl = false;
condition = function(token, i) {
var post, tag, _ref, _ref1;
tag = token[0];
if (!seenSingle && token.fromThen) return true;
if (tag === 'IF' || tag === 'ELSE' || tag === 'CATCH' || tag === '->' || tag === '=>' || tag === 'CLASS') {
seenSingle = true;
}
if (tag === 'IF' || tag === 'ELSE' || tag === 'SWITCH' || tag === 'TRY' || tag === '=') {
seenControl = true;
}
if ((tag === '.' || tag === '?.' || tag === '::') && this.tag(i - 1) === 'OUTDENT') {
return true;
}
return !token.generated && this.tag(i - 1) !== ',' && (__indexOf.call(IMPLICIT_END, tag) >= 0 || (tag === 'INDENT' && !seenControl)) && (tag !== 'INDENT' || (((_ref = this.tag(i - 2)) !== 'CLASS' && _ref !== 'EXTENDS') && (_ref1 = this.tag(i - 1), __indexOf.call(IMPLICIT_BLOCK, _ref1) < 0) && !((post = this.tokens[i + 1]) && post.generated && post[0] === '{')));
};
action = function(token, i) { action = function(token, i) {
var idx; return this.tokens.splice(i, 0, this.generate('CALL_END', ')', token[2]));
idx = token[0] === 'OUTDENT' ? i + 1 : i;
return this.tokens.splice(idx, 0, ['CALL_END', ')', token[2]]);
}; };
return this.scanTokens(function(token, i, tokens) { return this.scanTokens(function(token, i, tokens) {
var callObject, current, next, prev, seenControl, seenSingle, tag, _ref, _ref2, _ref3; var callObject, current, next, prev, tag, _ref, _ref1, _ref2;
tag = token[0]; tag = token[0];
if (tag === 'CLASS' || tag === 'IF') { if (tag === 'CLASS' || tag === 'IF' || tag === 'FOR' || tag === 'WHILE') {
noCall = true; noCall = true;
} }
_ref = tokens.slice(i - 1, (i + 1 + 1) || 9e9), prev = _ref[0], current = _ref[1], next = _ref[2]; _ref = tokens.slice(i - 1, (i + 1) + 1 || 9e9), prev = _ref[0], current = _ref[1], next = _ref[2];
callObject = !noCall && tag === 'INDENT' && next && next.generated && next[0] === '{' && prev && (_ref2 = prev[0], __indexOf.call(IMPLICIT_FUNC, _ref2) >= 0); callObject = !noCall && tag === 'INDENT' && next && next.generated && next[0] === '{' && prev && (_ref1 = prev[0], __indexOf.call(IMPLICIT_FUNC, _ref1) >= 0);
seenSingle = false; seenSingle = false;
seenControl = false; seenControl = false;
if (__indexOf.call(LINEBREAKS, tag) >= 0) { if (__indexOf.call(LINEBREAKS, tag) >= 0) noCall = false;
noCall = false; if (prev && !prev.spaced && tag === '?') token.call = true;
} if (token.fromThen) return 1;
if (prev && !prev.spaced && tag === '?') { if (!(callObject || (prev != null ? prev.spaced : void 0) && (prev.call || (_ref2 = prev[0], __indexOf.call(IMPLICIT_FUNC, _ref2) >= 0)) && (__indexOf.call(IMPLICIT_CALL, tag) >= 0 || !(token.spaced || token.newLine) && __indexOf.call(IMPLICIT_UNSPACED_CALL, tag) >= 0))) {
token.call = true;
}
if (token.fromThen) {
return 1; return 1;
} }
if (!(callObject || (prev != null ? prev.spaced : void 0) && (prev.call || (_ref3 = prev[0], __indexOf.call(IMPLICIT_FUNC, _ref3) >= 0)) && (__indexOf.call(IMPLICIT_CALL, tag) >= 0 || !(token.spaced || token.newLine) && __indexOf.call(IMPLICIT_UNSPACED_CALL, tag) >= 0))) { tokens.splice(i, 0, this.generate('CALL_START', '(', token[2]));
return 1; this.detectEnd(i + 1, condition, action);
} if (prev[0] === '?') prev[0] = 'FUNC_EXIST';
tokens.splice(i, 0, ['CALL_START', '(', token[2]]);
this.detectEnd(i + 1, function(token, i) {
var post, _ref4;
tag = token[0];
if (!seenSingle && token.fromThen) {
return true;
}
if (tag === 'IF' || tag === 'ELSE' || tag === 'CATCH' || tag === '->' || tag === '=>') {
seenSingle = true;
}
if (tag === 'IF' || tag === 'ELSE' || tag === 'SWITCH' || tag === 'TRY') {
seenControl = true;
}
if ((tag === '.' || tag === '?.' || tag === '::') && this.tag(i - 1) === 'OUTDENT') {
return true;
}
return !token.generated && this.tag(i - 1) !== ',' && (__indexOf.call(IMPLICIT_END, tag) >= 0 || (tag === 'INDENT' && !seenControl)) && (tag !== 'INDENT' || (this.tag(i - 2) !== 'CLASS' && (_ref4 = this.tag(i - 1), __indexOf.call(IMPLICIT_BLOCK, _ref4) < 0) && !((post = this.tokens[i + 1]) && post.generated && post[0] === '{')));
}, action);
if (prev[0] === '?') {
prev[0] = 'FUNC_EXIST';
}
return 2; return 2;
}); });
}; };
Rewriter.prototype.addImplicitIndentation = function() { Rewriter.prototype.addImplicitIndentation = function() {
var action, condition, indent, outdent, starter;
starter = indent = outdent = null;
condition = function(token, i) {
var _ref;
return token[1] !== ';' && (_ref = token[0], __indexOf.call(SINGLE_CLOSERS, _ref) >= 0) && !(token[0] === 'ELSE' && (starter !== 'IF' && starter !== 'THEN'));
};
action = function(token, i) {
return this.tokens.splice((this.tag(i - 1) === ',' ? i - 1 : i), 0, outdent);
};
return this.scanTokens(function(token, i, tokens) { return this.scanTokens(function(token, i, tokens) {
var action, condition, indent, outdent, starter, tag, _ref, _ref2; var tag, _ref, _ref1;
tag = token[0]; tag = token[0];
if (tag === 'TERMINATOR' && this.tag(i + 1) === 'THEN') { if (tag === 'TERMINATOR' && this.tag(i + 1) === 'THEN') {
tokens.splice(i, 1); tokens.splice(i, 1);
@ -251,139 +250,93 @@ define(function(require, exports, module) {
} }
if (__indexOf.call(SINGLE_LINERS, tag) >= 0 && this.tag(i + 1) !== 'INDENT' && !(tag === 'ELSE' && this.tag(i + 1) === 'IF')) { if (__indexOf.call(SINGLE_LINERS, tag) >= 0 && this.tag(i + 1) !== 'INDENT' && !(tag === 'ELSE' && this.tag(i + 1) === 'IF')) {
starter = tag; starter = tag;
_ref2 = this.indentation(token), indent = _ref2[0], outdent = _ref2[1]; _ref1 = this.indentation(token, true), indent = _ref1[0], outdent = _ref1[1];
if (starter === 'THEN') { if (starter === 'THEN') indent.fromThen = true;
indent.fromThen = true;
}
indent.generated = outdent.generated = true;
tokens.splice(i + 1, 0, indent); tokens.splice(i + 1, 0, indent);
condition = function(token, i) {
var _ref3;
return token[1] !== ';' && (_ref3 = token[0], __indexOf.call(SINGLE_CLOSERS, _ref3) >= 0) && !(token[0] === 'ELSE' && (starter !== 'IF' && starter !== 'THEN'));
};
action = function(token, i) {
return this.tokens.splice((this.tag(i - 1) === ',' ? i - 1 : i), 0, outdent);
};
this.detectEnd(i + 2, condition, action); this.detectEnd(i + 2, condition, action);
if (tag === 'THEN') { if (tag === 'THEN') tokens.splice(i, 1);
tokens.splice(i, 1);
}
return 1; return 1;
} }
return 1; return 1;
}); });
}; };
Rewriter.prototype.tagPostfixConditionals = function() { Rewriter.prototype.tagPostfixConditionals = function() {
var condition; var action, condition, original;
original = null;
condition = function(token, i) { condition = function(token, i) {
var _ref; var _ref;
return (_ref = token[0]) === 'TERMINATOR' || _ref === 'INDENT'; return (_ref = token[0]) === 'TERMINATOR' || _ref === 'INDENT';
}; };
action = function(token, i) {
if (token[0] !== 'INDENT' || (token.generated && !token.fromThen)) {
return original[0] = 'POST_' + original[0];
}
};
return this.scanTokens(function(token, i) { return this.scanTokens(function(token, i) {
var original; if (token[0] !== 'IF') return 1;
if (token[0] !== 'IF') {
return 1;
}
original = token; original = token;
this.detectEnd(i + 1, condition, function(token, i) { this.detectEnd(i + 1, condition, action);
if (token[0] !== 'INDENT') {
return original[0] = 'POST_' + original[0];
}
});
return 1; return 1;
}); });
}; };
Rewriter.prototype.ensureBalance = function(pairs) {
var close, level, levels, open, openLine, tag, token, _i, _j, _len, _len2, _ref, _ref2; Rewriter.prototype.indentation = function(token, implicit) {
levels = {}; var indent, outdent;
openLine = {}; if (implicit == null) implicit = false;
_ref = this.tokens; indent = ['INDENT', 2, token[2]];
for (_i = 0, _len = _ref.length; _i < _len; _i++) { outdent = ['OUTDENT', 2, token[2]];
token = _ref[_i]; if (implicit) indent.generated = outdent.generated = true;
tag = token[0]; return [indent, outdent];
for (_j = 0, _len2 = pairs.length; _j < _len2; _j++) {
_ref2 = pairs[_j], open = _ref2[0], close = _ref2[1];
levels[open] |= 0;
if (tag === open) {
if (levels[open]++ === 0) {
openLine[open] = token[2];
}
} else if (tag === close && --levels[open] < 0) {
throw Error("too many " + token[1] + " on line " + (token[2] + 1));
}
}
}
for (open in levels) {
level = levels[open];
if (level > 0) {
throw Error("unclosed " + open + " on line " + (openLine[open] + 1));
}
}
return this;
}; };
Rewriter.prototype.rewriteClosingParens = function() {
var debt, key, stack; Rewriter.prototype.generate = function(tag, value, line) {
stack = []; var tok;
debt = {}; tok = [tag, value, line];
for (key in INVERSES) { tok.generated = true;
debt[key] = 0; return tok;
}
return this.scanTokens(function(token, i, tokens) {
var inv, match, mtag, oppos, tag, val, _ref;
if (_ref = (tag = token[0]), __indexOf.call(EXPRESSION_START, _ref) >= 0) {
stack.push(token);
return 1;
}
if (__indexOf.call(EXPRESSION_END, tag) < 0) {
return 1;
}
if (debt[inv = INVERSES[tag]] > 0) {
debt[inv] -= 1;
tokens.splice(i, 1);
return 0;
}
match = stack.pop();
mtag = match[0];
oppos = INVERSES[mtag];
if (tag === oppos) {
return 1;
}
debt[mtag] += 1;
val = [oppos, mtag === 'INDENT' ? match[1] : oppos];
if (this.tag(i + 2) === mtag) {
tokens.splice(i + 3, 0, val);
stack.push(match);
} else {
tokens.splice(i, 0, val);
}
return 1;
});
};
Rewriter.prototype.indentation = function(token) {
return [['INDENT', 2, token[2]], ['OUTDENT', 2, token[2]]];
}; };
Rewriter.prototype.tag = function(i) { Rewriter.prototype.tag = function(i) {
var _ref; var _ref;
return (_ref = this.tokens[i]) != null ? _ref[0] : void 0; return (_ref = this.tokens[i]) != null ? _ref[0] : void 0;
}; };
return Rewriter; return Rewriter;
})(); })();
BALANCED_PAIRS = [['(', ')'], ['[', ']'], ['{', '}'], ['INDENT', 'OUTDENT'], ['CALL_START', 'CALL_END'], ['PARAM_START', 'PARAM_END'], ['INDEX_START', 'INDEX_END']]; BALANCED_PAIRS = [['(', ')'], ['[', ']'], ['{', '}'], ['INDENT', 'OUTDENT'], ['CALL_START', 'CALL_END'], ['PARAM_START', 'PARAM_END'], ['INDEX_START', 'INDEX_END']];
INVERSES = {};
exports.INVERSES = INVERSES = {};
EXPRESSION_START = []; EXPRESSION_START = [];
EXPRESSION_END = []; EXPRESSION_END = [];
for (_i = 0, _len = BALANCED_PAIRS.length; _i < _len; _i++) { for (_i = 0, _len = BALANCED_PAIRS.length; _i < _len; _i++) {
_ref = BALANCED_PAIRS[_i], left = _ref[0], rite = _ref[1]; _ref = BALANCED_PAIRS[_i], left = _ref[0], rite = _ref[1];
EXPRESSION_START.push(INVERSES[rite] = left); EXPRESSION_START.push(INVERSES[rite] = left);
EXPRESSION_END.push(INVERSES[left] = rite); EXPRESSION_END.push(INVERSES[left] = rite);
} }
EXPRESSION_CLOSE = ['CATCH', 'WHEN', 'ELSE', 'FINALLY'].concat(EXPRESSION_END); EXPRESSION_CLOSE = ['CATCH', 'WHEN', 'ELSE', 'FINALLY'].concat(EXPRESSION_END);
IMPLICIT_FUNC = ['IDENTIFIER', 'SUPER', ')', 'CALL_END', ']', 'INDEX_END', '@', 'THIS']; IMPLICIT_FUNC = ['IDENTIFIER', 'SUPER', ')', 'CALL_END', ']', 'INDEX_END', '@', 'THIS'];
IMPLICIT_CALL = ['IDENTIFIER', 'NUMBER', 'STRING', 'JS', 'REGEX', 'NEW', 'PARAM_START', 'CLASS', 'IF', 'TRY', 'SWITCH', 'THIS', 'BOOL', 'UNARY', 'SUPER', '@', '->', '=>', '[', '(', '{', '--', '++']; IMPLICIT_CALL = ['IDENTIFIER', 'NUMBER', 'STRING', 'JS', 'REGEX', 'NEW', 'PARAM_START', 'CLASS', 'IF', 'TRY', 'SWITCH', 'THIS', 'BOOL', 'UNARY', 'SUPER', '@', '->', '=>', '[', '(', '{', '--', '++'];
IMPLICIT_UNSPACED_CALL = ['+', '-']; IMPLICIT_UNSPACED_CALL = ['+', '-'];
IMPLICIT_BLOCK = ['->', '=>', '{', '[', ',']; IMPLICIT_BLOCK = ['->', '=>', '{', '[', ','];
IMPLICIT_END = ['POST_IF', 'FOR', 'WHILE', 'UNTIL', 'WHEN', 'BY', 'LOOP', 'TERMINATOR']; IMPLICIT_END = ['POST_IF', 'FOR', 'WHILE', 'UNTIL', 'WHEN', 'BY', 'LOOP', 'TERMINATOR'];
SINGLE_LINERS = ['ELSE', '->', '=>', 'TRY', 'FINALLY', 'THEN']; SINGLE_LINERS = ['ELSE', '->', '=>', 'TRY', 'FINALLY', 'THEN'];
SINGLE_CLOSERS = ['TERMINATOR', 'CATCH', 'FINALLY', 'ELSE', 'OUTDENT', 'LEADING_WHEN']; SINGLE_CLOSERS = ['TERMINATOR', 'CATCH', 'FINALLY', 'ELSE', 'OUTDENT', 'LEADING_WHEN'];
LINEBREAKS = ['TERMINATOR', 'INDENT', 'OUTDENT']; LINEBREAKS = ['TERMINATOR', 'INDENT', 'OUTDENT'];
});
});

View file

@ -1,6 +1,6 @@
/** /**
* Copyright (c) 2011 Jeremy Ashkenas * Copyright (c) 2011 Jeremy Ashkenas
* *
* Permission is hereby granted, free of charge, to any person * Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation * obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without * files (the "Software"), to deal in the Software without
@ -24,10 +24,18 @@
*/ */
define(function(require, exports, module) { define(function(require, exports, module) {
// Generated by CoffeeScript 1.2.1-pre
var Scope, extend, last, _ref; var Scope, extend, last, _ref;
_ref = require('./helpers'), extend = _ref.extend, last = _ref.last; _ref = require('./helpers'), extend = _ref.extend, last = _ref.last;
exports.Scope = Scope = (function() { exports.Scope = Scope = (function() {
Scope.name = 'Scope';
Scope.root = null; Scope.root = null;
function Scope(parent, expressions, method) { function Scope(parent, expressions, method) {
this.parent = parent; this.parent = parent;
this.expressions = expressions; this.expressions = expressions;
@ -39,17 +47,13 @@ define(function(require, exports, module) {
} }
]; ];
this.positions = {}; this.positions = {};
if (!this.parent) { if (!this.parent) Scope.root = this;
Scope.root = this;
}
} }
Scope.prototype.add = function(name, type, immediate) { Scope.prototype.add = function(name, type, immediate) {
var pos; if (this.shared && !immediate) return this.parent.add(name, type, immediate);
if (this.shared && !immediate) { if (Object.prototype.hasOwnProperty.call(this.positions, name)) {
return this.parent.add(name, type, immediate); return this.variables[this.positions[name]].type = type;
}
if (typeof (pos = this.positions[name]) === 'number') {
return this.variables[pos].type = type;
} else { } else {
return this.positions[name] = this.variables.push({ return this.positions[name] = this.variables.push({
name: name, name: name,
@ -57,89 +61,94 @@ define(function(require, exports, module) {
}) - 1; }) - 1;
} }
}; };
Scope.prototype.find = function(name, options) { Scope.prototype.find = function(name, options) {
if (this.check(name, options)) { if (this.check(name, options)) return true;
return true;
}
this.add(name, 'var'); this.add(name, 'var');
return false; return false;
}; };
Scope.prototype.parameter = function(name) { Scope.prototype.parameter = function(name) {
if (this.shared && this.parent.check(name, true)) { if (this.shared && this.parent.check(name, true)) return;
return;
}
return this.add(name, 'param'); return this.add(name, 'param');
}; };
Scope.prototype.check = function(name, immediate) { Scope.prototype.check = function(name, immediate) {
var found, _ref2; var found, _ref1;
found = !!this.type(name); found = !!this.type(name);
if (found || immediate) { if (found || immediate) return found;
return found; return !!((_ref1 = this.parent) != null ? _ref1.check(name) : void 0);
}
return !!((_ref2 = this.parent) != null ? _ref2.check(name) : void 0);
}; };
Scope.prototype.temporary = function(name, index) { Scope.prototype.temporary = function(name, index) {
if (name.length > 1) { if (name.length > 1) {
return '_' + name + (index > 1 ? index : ''); return '_' + name + (index > 1 ? index - 1 : '');
} else { } else {
return '_' + (index + parseInt(name, 36)).toString(36).replace(/\d/g, 'a'); return '_' + (index + parseInt(name, 36)).toString(36).replace(/\d/g, 'a');
} }
}; };
Scope.prototype.type = function(name) { Scope.prototype.type = function(name) {
var v, _i, _len, _ref2; var v, _i, _len, _ref1;
_ref2 = this.variables; _ref1 = this.variables;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) { for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
v = _ref2[_i]; v = _ref1[_i];
if (v.name === name) { if (v.name === name) return v.type;
return v.type;
}
} }
return null; return null;
}; };
Scope.prototype.freeVariable = function(type) {
Scope.prototype.freeVariable = function(name, reserve) {
var index, temp; var index, temp;
if (reserve == null) reserve = true;
index = 0; index = 0;
while (this.check((temp = this.temporary(type, index)))) { while (this.check((temp = this.temporary(name, index)))) {
index++; index++;
} }
this.add(temp, 'var', true); if (reserve) this.add(temp, 'var', true);
return temp; return temp;
}; };
Scope.prototype.assign = function(name, value) { Scope.prototype.assign = function(name, value) {
this.add(name, { this.add(name, {
value: value, value: value,
assigned: true assigned: true
}); }, true);
return this.hasAssignments = true; return this.hasAssignments = true;
}; };
Scope.prototype.hasDeclarations = function() { Scope.prototype.hasDeclarations = function() {
return !!this.declaredVariables().length; return !!this.declaredVariables().length;
}; };
Scope.prototype.declaredVariables = function() { Scope.prototype.declaredVariables = function() {
var realVars, tempVars, v, _i, _len, _ref2; var realVars, tempVars, v, _i, _len, _ref1;
realVars = []; realVars = [];
tempVars = []; tempVars = [];
_ref2 = this.variables; _ref1 = this.variables;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) { for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
v = _ref2[_i]; v = _ref1[_i];
if (v.type === 'var') { if (v.type === 'var') {
(v.name.charAt(0) === '_' ? tempVars : realVars).push(v.name); (v.name.charAt(0) === '_' ? tempVars : realVars).push(v.name);
} }
} }
return realVars.sort().concat(tempVars.sort()); return realVars.sort().concat(tempVars.sort());
}; };
Scope.prototype.assignedVariables = function() { Scope.prototype.assignedVariables = function() {
var v, _i, _len, _ref2, _results; var v, _i, _len, _ref1, _results;
_ref2 = this.variables; _ref1 = this.variables;
_results = []; _results = [];
for (_i = 0, _len = _ref2.length; _i < _len; _i++) { for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
v = _ref2[_i]; v = _ref1[_i];
if (v.type.assigned) { if (v.type.assigned) _results.push("" + v.name + " = " + v.type.value);
_results.push("" + v.name + " = " + v.type.value);
}
} }
return _results; return _results;
}; };
return Scope; return Scope;
})(); })();
});
});

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff