mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-05-03 03:00:14 -04:00
merging the range-killer to master
This commit is contained in:
4
Cakefile
4
Cakefile
@@ -99,8 +99,8 @@ task 'loc', 'count the lines of source code in the CoffeeScript compiler', ->
|
||||
runTests = (CoffeeScript) ->
|
||||
startTime = Date.now()
|
||||
passedTests = failedTests = 0
|
||||
for all name, func of require 'assert'
|
||||
global[name] = -> ++passedTests; func arguments...
|
||||
wrap = (name, func) -> global[name] = -> ++passedTests; func arguments...
|
||||
wrap name, func for all name, func of require 'assert'
|
||||
global.eq = global.strictEqual
|
||||
global.CoffeeScript = CoffeeScript
|
||||
process.on 'exit', ->
|
||||
|
||||
@@ -25,16 +25,16 @@
|
||||
return xhr.send(null);
|
||||
};
|
||||
runScripts = function() {
|
||||
var _i, _j, _len, _ref, script;
|
||||
for (_i = 0, _len = (_ref = document.getElementsByTagName('script')).length; _i < _len; _i++) {
|
||||
(function() {
|
||||
var script = _ref[_i];
|
||||
_j = script;
|
||||
return script.type === 'text/coffeescript' ? script.src ? CoffeeScript.load(script.src) : setTimeout(function() {
|
||||
return CoffeeScript.run(script.innerHTML);
|
||||
}) : undefined;
|
||||
})();
|
||||
script = _j;
|
||||
var _i, _len, _ref, script;
|
||||
for (_i = 0, _len = (_ref = document.getElementsByTagName('script')).length; _i < _len; ++_i) {
|
||||
script = _ref[_i];
|
||||
if (script.type === 'text/coffeescript') {
|
||||
if (script.src) {
|
||||
CoffeeScript.load(script.src);
|
||||
} else {
|
||||
CoffeeScript.run(script.innerHTML);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
@@ -47,7 +47,7 @@
|
||||
}
|
||||
options = oparse.parse(args);
|
||||
_result = [];
|
||||
for (_i = 0, _len = (_ref = options.arguments).length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = (_ref = options.arguments).length; _i < _len; ++_i) {
|
||||
arg = _ref[_i];
|
||||
_result.push(invoke(arg));
|
||||
}
|
||||
|
||||
@@ -48,43 +48,39 @@
|
||||
return compileScripts();
|
||||
};
|
||||
compileScripts = function() {
|
||||
var _i, _j, _len, _ref2, _result, base, compile, source;
|
||||
var _i, _len, _ref2, _result, base, compile, source;
|
||||
_result = [];
|
||||
for (_i = 0, _len = (_ref2 = sources).length; _i < _len; _i++) {
|
||||
(function() {
|
||||
var source = _ref2[_i];
|
||||
_j = source;
|
||||
return _result.push((function() {
|
||||
base = path.join(source);
|
||||
compile = function(source, topLevel) {
|
||||
return path.exists(source, function(exists) {
|
||||
if (!exists) {
|
||||
throw new Error("File not found: " + source);
|
||||
for (_i = 0, _len = (_ref2 = sources).length; _i < _len; ++_i) {
|
||||
source = _ref2[_i];
|
||||
_result.push((function() {
|
||||
base = path.join(source);
|
||||
compile = function(source, topLevel) {
|
||||
return path.exists(source, function(exists) {
|
||||
if (!exists) {
|
||||
throw new Error("File not found: " + source);
|
||||
}
|
||||
return fs.stat(source, function(err, stats) {
|
||||
if (stats.isDirectory()) {
|
||||
return fs.readdir(source, function(err, files) {
|
||||
var _j, _len2, _result2, file;
|
||||
_result2 = [];
|
||||
for (_j = 0, _len2 = files.length; _j < _len2; ++_j) {
|
||||
file = files[_j];
|
||||
_result2.push(compile(path.join(source, file)));
|
||||
}
|
||||
return _result2;
|
||||
});
|
||||
} else if (topLevel || path.extname(source) === '.coffee') {
|
||||
fs.readFile(source, function(err, code) {
|
||||
return compileScript(source, code.toString(), base);
|
||||
});
|
||||
return opts.watch ? watch(source, base) : undefined;
|
||||
}
|
||||
return fs.stat(source, function(err, stats) {
|
||||
if (stats.isDirectory()) {
|
||||
return fs.readdir(source, function(err, files) {
|
||||
var _k, _len2, _result2, file;
|
||||
_result2 = [];
|
||||
for (_k = 0, _len2 = files.length; _k < _len2; _k++) {
|
||||
file = files[_k];
|
||||
_result2.push(compile(path.join(source, file)));
|
||||
}
|
||||
return _result2;
|
||||
});
|
||||
} else if (topLevel || path.extname(source) === '.coffee') {
|
||||
fs.readFile(source, function(err, code) {
|
||||
return compileScript(source, code.toString(), base);
|
||||
});
|
||||
return opts.watch ? watch(source, base) : undefined;
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
return compile(source, true);
|
||||
})());
|
||||
})();
|
||||
source = _j;
|
||||
});
|
||||
};
|
||||
return compile(source, true);
|
||||
})());
|
||||
}
|
||||
return _result;
|
||||
};
|
||||
@@ -93,7 +89,7 @@
|
||||
o = opts;
|
||||
options = compileOptions(file);
|
||||
if (o.require) {
|
||||
for (_i = 0, _len = (_ref2 = o.require).length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = (_ref2 = o.require).length; _i < _len; ++_i) {
|
||||
req = _ref2[_i];
|
||||
require(helpers.starts(req, '.') ? fs.realpathSync(req) : req);
|
||||
}
|
||||
@@ -190,7 +186,7 @@
|
||||
var _i, _len, _ref2, _result, strings, tag, token, value;
|
||||
strings = (function() {
|
||||
_result = [];
|
||||
for (_i = 0, _len = tokens.length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = tokens.length; _i < _len; ++_i) {
|
||||
token = tokens[_i];
|
||||
_result.push((function() {
|
||||
_ref2 = [token[0], token[1].toString().replace(/\n/, '\\n')], tag = _ref2[0], value = _ref2[1];
|
||||
|
||||
201
lib/grammar.js
201
lib/grammar.js
@@ -1,6 +1,5 @@
|
||||
(function() {
|
||||
var Parser, _i, _j, _len, _len2, _ref, _result, alt, alternatives, grammar, name, o, operators, token, tokens, unwrap;
|
||||
var __hasProp = Object.prototype.hasOwnProperty;
|
||||
Parser = require('jison').Parser;
|
||||
unwrap = /^function\s*\(\)\s*\{\s*return\s*([\s\S]*);\s*\}/;
|
||||
o = function(patternString, action, options) {
|
||||
@@ -11,7 +10,7 @@
|
||||
}
|
||||
action = (match = unwrap.exec(action)) ? match[1] : "(" + action + "())";
|
||||
action = action.replace(/\bnew /g, '$&yy.');
|
||||
action = action.replace(/\bExpressions\.wrap\b/g, 'yy.$&');
|
||||
action = action.replace(/\b(?:Expressions\.wrap|extend)\b/g, 'yy.$&');
|
||||
return [patternString, "$$ = " + action + ";", options];
|
||||
};
|
||||
grammar = {
|
||||
@@ -78,17 +77,17 @@
|
||||
})
|
||||
],
|
||||
AssignObj: [
|
||||
o("Identifier", function() {
|
||||
o('Identifier', function() {
|
||||
return new Value($1);
|
||||
}), o("AlphaNumeric"), o("ThisProperty"), o("Identifier : Expression", function() {
|
||||
}), o('AlphaNumeric'), o('ThisProperty'), o('Identifier : Expression', function() {
|
||||
return new Assign(new Value($1), $3, 'object');
|
||||
}), o("AlphaNumeric : Expression", function() {
|
||||
}), o('AlphaNumeric : Expression', function() {
|
||||
return new Assign(new Value($1), $3, 'object');
|
||||
}), o("Identifier : INDENT Expression OUTDENT", function() {
|
||||
}), o('Identifier : INDENT Expression OUTDENT', function() {
|
||||
return new Assign(new Value($1), $4, 'object');
|
||||
}), o("AlphaNumeric : INDENT Expression OUTDENT", function() {
|
||||
}), o('AlphaNumeric : INDENT Expression OUTDENT', function() {
|
||||
return new Assign(new Value($1), $4, 'object');
|
||||
}), o("Comment")
|
||||
}), o('Comment')
|
||||
],
|
||||
Return: [
|
||||
o("RETURN Expression", function() {
|
||||
@@ -168,8 +167,6 @@
|
||||
return new Value($1);
|
||||
}), o("Parenthetical", function() {
|
||||
return new Value($1);
|
||||
}), o("Range", function() {
|
||||
return new Value($1);
|
||||
}), o("This")
|
||||
],
|
||||
Accessor: [
|
||||
@@ -188,12 +185,14 @@
|
||||
Index: [
|
||||
o("INDEX_START Expression INDEX_END", function() {
|
||||
return new Index($2);
|
||||
}), o("INDEX_SOAK Index", function() {
|
||||
$2.soakNode = true;
|
||||
return $2;
|
||||
}), o("INDEX_SOAK Index", function() {
|
||||
return extend($2, {
|
||||
soakNode: true
|
||||
});
|
||||
}), o("INDEX_PROTO Index", function() {
|
||||
$2.proto = true;
|
||||
return $2;
|
||||
return extend($2, {
|
||||
proto: true
|
||||
});
|
||||
})
|
||||
],
|
||||
Object: [
|
||||
@@ -290,32 +289,11 @@
|
||||
return new Value(new Literal('this'));
|
||||
})
|
||||
],
|
||||
RangeDots: [
|
||||
o("..", function() {
|
||||
return 'inclusive';
|
||||
}), o("...", function() {
|
||||
return 'exclusive';
|
||||
})
|
||||
],
|
||||
ThisProperty: [
|
||||
o("@ Identifier", function() {
|
||||
return new Value(new Literal('this'), [new Accessor($2)], 'this');
|
||||
})
|
||||
],
|
||||
Range: [
|
||||
o("[ Expression RangeDots Expression ]", function() {
|
||||
return new Range($2, $4, $3);
|
||||
})
|
||||
],
|
||||
Slice: [
|
||||
o("INDEX_START Expression RangeDots Expression INDEX_END", function() {
|
||||
return new Range($2, $4, $3);
|
||||
}), o("INDEX_START Expression RangeDots INDEX_END", function() {
|
||||
return new Range($2, null, $3);
|
||||
}), o("INDEX_START RangeDots Expression INDEX_END", function() {
|
||||
return new Range(null, $3, $2);
|
||||
})
|
||||
],
|
||||
Array: [
|
||||
o("[ ]", function() {
|
||||
return new ArrayLiteral([]);
|
||||
@@ -405,81 +383,37 @@
|
||||
})
|
||||
],
|
||||
For: [
|
||||
o("Statement ForBody", function() {
|
||||
return new For($1, $2, $2.vars[0], $2.vars[1]);
|
||||
}), o("Expression ForBody", function() {
|
||||
return new For($1, $2, $2.vars[0], $2.vars[1]);
|
||||
}), o("ForBody Block", function() {
|
||||
return new For($2, $1, $1.vars[0], $1.vars[1]);
|
||||
})
|
||||
],
|
||||
ForBody: [
|
||||
o("FOR Range", function() {
|
||||
return {
|
||||
source: new Value($2),
|
||||
vars: []
|
||||
};
|
||||
}), o("ForStart ForSource", function() {
|
||||
$2.raw = $1.raw;
|
||||
$2.vars = $1;
|
||||
return $2;
|
||||
})
|
||||
],
|
||||
ForStart: [
|
||||
o("FOR ForVariables", function() {
|
||||
return $2;
|
||||
}), o("FOR ALL ForVariables", function() {
|
||||
$3.raw = true;
|
||||
return $3;
|
||||
o('Statement ForBody', function() {
|
||||
return new For($1, $2);
|
||||
}), o('Expression ForBody', function() {
|
||||
return new For($1, $2);
|
||||
}), o('ForBody Block', function() {
|
||||
return new For($2, $1);
|
||||
})
|
||||
],
|
||||
ForValue: [
|
||||
o("Identifier"), o("Array", function() {
|
||||
o('Identifier'), o('Array', function() {
|
||||
return new Value($1);
|
||||
}), o("Object", function() {
|
||||
}), o('Object', function() {
|
||||
return new Value($1);
|
||||
})
|
||||
],
|
||||
ForVariables: [
|
||||
o("ForValue", function() {
|
||||
return [$1];
|
||||
}), o("ForValue , ForValue", function() {
|
||||
return [$1, $3];
|
||||
})
|
||||
],
|
||||
ForSource: [
|
||||
o("FORIN Expression", function() {
|
||||
ForIn: [
|
||||
o('FORIN Expression', function() {
|
||||
return {
|
||||
source: $2
|
||||
};
|
||||
}), o("FOROF Expression", function() {
|
||||
return {
|
||||
source: $2,
|
||||
object: true
|
||||
};
|
||||
}), o("FORIN Expression WHEN Expression", function() {
|
||||
}), o('FORIN Expression WHEN Expression', function() {
|
||||
return {
|
||||
source: $2,
|
||||
guard: $4
|
||||
};
|
||||
}), o("FOROF Expression WHEN Expression", function() {
|
||||
return {
|
||||
source: $2,
|
||||
guard: $4,
|
||||
object: true
|
||||
};
|
||||
}), o("FORIN Expression BY Expression", function() {
|
||||
}), o('FORIN Expression BY Expression', function() {
|
||||
return {
|
||||
source: $2,
|
||||
step: $4
|
||||
};
|
||||
}), o("FORIN Expression WHEN Expression BY Expression", function() {
|
||||
return {
|
||||
source: $2,
|
||||
guard: $4,
|
||||
step: $6
|
||||
};
|
||||
}), o("FORIN Expression BY Expression WHEN Expression", function() {
|
||||
}), o('FORIN Expression BY Expression WHEN Expression', function() {
|
||||
return {
|
||||
source: $2,
|
||||
step: $4,
|
||||
@@ -487,6 +421,80 @@
|
||||
};
|
||||
})
|
||||
],
|
||||
ForOf: [
|
||||
o('FOROF Expression', function() {
|
||||
return {
|
||||
object: true,
|
||||
source: $2
|
||||
};
|
||||
}), o('FOROF Expression WHEN Expression', function() {
|
||||
return {
|
||||
object: true,
|
||||
source: $2,
|
||||
guard: $4
|
||||
};
|
||||
})
|
||||
],
|
||||
ForTo: [
|
||||
o('TO Expression', function() {
|
||||
return {
|
||||
to: $2
|
||||
};
|
||||
}), o('TO Expression WHEN Expression', function() {
|
||||
return {
|
||||
to: $2,
|
||||
guard: $4
|
||||
};
|
||||
}), o('TO Expression BY Expression', function() {
|
||||
return {
|
||||
to: $2,
|
||||
step: $4
|
||||
};
|
||||
}), o('TO Expression BY Expression WHEN Expression', function() {
|
||||
return {
|
||||
to: $2,
|
||||
step: $4,
|
||||
guard: $6
|
||||
};
|
||||
})
|
||||
],
|
||||
ForBody: [
|
||||
o('FOR ForValue ForIn', function() {
|
||||
return extend($3, {
|
||||
name: $2
|
||||
});
|
||||
}), o('FOR ForValue , Identifier ForIn', function() {
|
||||
return extend($5, {
|
||||
name: $2,
|
||||
index: $4
|
||||
});
|
||||
}), o('FOR Identifier ForOf', function() {
|
||||
return extend($3, {
|
||||
index: $2
|
||||
});
|
||||
}), o('FOR ForValue , ForValue ForOf', function() {
|
||||
return extend($5, {
|
||||
index: $2,
|
||||
name: $4
|
||||
});
|
||||
}), o('FOR ALL Identifier ForOf', function() {
|
||||
return extend($4, {
|
||||
raw: true,
|
||||
index: $3
|
||||
});
|
||||
}), o('FOR ALL Identifier , ForValue ForOf', function() {
|
||||
return extend($6, {
|
||||
raw: true,
|
||||
index: $3,
|
||||
name: $5
|
||||
});
|
||||
}), o('FOR Identifier FROM Expression ForTo', function() {
|
||||
return extend($5, {
|
||||
index: $2,
|
||||
from: $4
|
||||
});
|
||||
})
|
||||
],
|
||||
Switch: [
|
||||
o("SWITCH Expression INDENT Whens OUTDENT", function() {
|
||||
return new Switch($2, $4);
|
||||
@@ -588,17 +596,16 @@
|
||||
})
|
||||
]
|
||||
};
|
||||
operators = [["left", 'CALL_START', 'CALL_END'], ["nonassoc", '++', '--'], ["left", '?'], ["right", 'UNARY'], ["left", 'MATH'], ["left", '+', '-'], ["left", 'SHIFT'], ["left", 'COMPARE'], ["left", 'RELATION'], ["left", '==', '!='], ["left", 'LOGIC'], ["left", '.'], ["nonassoc", 'INDENT', 'OUTDENT'], ["right", 'WHEN', 'LEADING_WHEN', 'FORIN', 'FOROF', 'BY', 'THROW'], ["right", 'IF', 'UNLESS', 'ELSE', 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'SUPER', 'CLASS', 'EXTENDS'], ["right", '=', ':', 'COMPOUND_ASSIGN', 'RETURN'], ["right", '->', '=>', 'UNLESS', 'POST_IF', 'POST_UNLESS']];
|
||||
operators = [["left", 'CALL_START', 'CALL_END'], ["nonassoc", '++', '--'], ["left", '?'], ["right", 'UNARY'], ["left", 'MATH'], ["left", '+', '-'], ["left", 'SHIFT'], ["left", 'COMPARE'], ["left", 'RELATION'], ["left", '==', '!='], ["left", 'LOGIC'], ["left", '.'], ["nonassoc", 'INDENT', 'OUTDENT'], ["right", 'WHEN', 'LEADING_WHEN', 'FORIN', 'FOROF', 'FROM', 'TO', 'BY', 'THROW'], ["right", 'IF', 'UNLESS', 'ELSE', 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'SUPER', 'CLASS', 'EXTENDS'], ["right", '=', ':', 'COMPOUND_ASSIGN', 'RETURN'], ["right", '->', '=>', 'UNLESS', 'POST_IF', 'POST_UNLESS']];
|
||||
tokens = [];
|
||||
for (name in grammar) {
|
||||
if (!__hasProp.call(grammar, name)) continue;
|
||||
alternatives = grammar[name];
|
||||
grammar[name] = (function() {
|
||||
_result = [];
|
||||
for (_i = 0, _len = alternatives.length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = alternatives.length; _i < _len; ++_i) {
|
||||
alt = alternatives[_i];
|
||||
_result.push((function() {
|
||||
for (_j = 0, _len2 = (_ref = alt[0].split(' ')).length; _j < _len2; _j++) {
|
||||
for (_j = 0, _len2 = (_ref = alt[0].split(' ')).length; _j < _len2; ++_j) {
|
||||
token = _ref[_j];
|
||||
if (!grammar[token]) {
|
||||
tokens.push(token);
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
exports.compact = function(array) {
|
||||
var _i, _len, _result, item;
|
||||
_result = [];
|
||||
for (_i = 0, _len = array.length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = array.length; _i < _len; ++_i) {
|
||||
item = array[_i];
|
||||
if (item) {
|
||||
_result.push(item);
|
||||
@@ -41,7 +41,7 @@
|
||||
exports.flatten = flatten = function(array) {
|
||||
var _i, _len, element, flattened;
|
||||
flattened = [];
|
||||
for (_i = 0, _len = array.length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = array.length; _i < _len; ++_i) {
|
||||
element = array[_i];
|
||||
if (element instanceof Array) {
|
||||
flattened = flattened.concat(flatten(element));
|
||||
|
||||
25
lib/lexer.js
25
lib/lexer.js
@@ -23,9 +23,9 @@
|
||||
this.indent = 0;
|
||||
this.indebt = 0;
|
||||
this.outdebt = 0;
|
||||
this.seenFor = false;
|
||||
this.indents = [];
|
||||
this.tokens = [];
|
||||
this.seenFor = this.seenFrom = false;
|
||||
while (this.chunk = code.slice(this.i)) {
|
||||
this.identifierToken() || this.commentToken() || this.whitespaceToken() || this.lineToken() || this.heredocToken() || this.stringToken() || this.numberToken() || this.regexToken() || this.jsToken() || this.literalToken();
|
||||
}
|
||||
@@ -46,6 +46,17 @@
|
||||
this.token('ALL', id);
|
||||
return true;
|
||||
}
|
||||
if (id === 'from' && this.tag(1) === 'FOR') {
|
||||
this.seenFor = false;
|
||||
this.seenFrom = true;
|
||||
this.token('FROM', id);
|
||||
return true;
|
||||
}
|
||||
if (id === 'to' && this.seenFrom) {
|
||||
this.seenFrom = false;
|
||||
this.token('TO', id);
|
||||
return true;
|
||||
}
|
||||
forcedIdentifier = colon || this.tagAccessor();
|
||||
tag = 'IDENTIFIER';
|
||||
if (__indexOf.call(JS_KEYWORDS, id) >= 0 || !forcedIdentifier && __indexOf.call(COFFEE_KEYWORDS, id) >= 0) {
|
||||
@@ -217,7 +228,7 @@
|
||||
tokens = [];
|
||||
for (_i = 0, _len = (_ref2 = this.interpolateString(body, {
|
||||
regex: true
|
||||
})).length; _i < _len; _i++) {
|
||||
})).length; _i < _len; ++_i) {
|
||||
_ref3 = _ref2[_i], tag = _ref3[0], value = _ref3[1];
|
||||
if (tag === 'TOKENS') {
|
||||
tokens.push.apply(tokens, value);
|
||||
@@ -469,7 +480,7 @@
|
||||
if (levels.length && str.charAt(i) === '\\') {
|
||||
i += 1;
|
||||
} else {
|
||||
for (_i = 0, _len = delimited.length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = delimited.length; _i < _len; ++_i) {
|
||||
pair = delimited[_i];
|
||||
open = pair[0], close = pair[1];
|
||||
if (levels.length && starts(str, close, i) && last(levels) === pair) {
|
||||
@@ -495,7 +506,7 @@
|
||||
if (levels.length) {
|
||||
throw SyntaxError("Unterminated " + (levels.pop()[0]) + " starting on line " + (this.line + 1));
|
||||
}
|
||||
return !i ? false : str.slice(0, i);
|
||||
return i && str.slice(0, i);
|
||||
};
|
||||
Lexer.prototype.interpolateString = function(str, options) {
|
||||
var _len, _ref2, _ref3, _this, expr, heredoc, i, inner, interpolated, letter, nested, pi, regex, tag, tokens, value;
|
||||
@@ -545,7 +556,7 @@
|
||||
if (interpolated = tokens.length > 1) {
|
||||
this.token('(', '(');
|
||||
}
|
||||
for (i = 0, _len = tokens.length; i < _len; i++) {
|
||||
for (i = 0, _len = tokens.length; i < _len; ++i) {
|
||||
_ref3 = tokens[i], tag = _ref3[0], value = _ref3[1];
|
||||
if (i) {
|
||||
this.token('+', '+');
|
||||
@@ -611,7 +622,7 @@
|
||||
IDENTIFIER = /^([$A-Za-z_][$\w]*)([^\n\S]*:(?!:))?/;
|
||||
NUMBER = /^0x[\da-f]+|^(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?/i;
|
||||
HEREDOC = /^("""|''')([\s\S]*?)(?:\n[ \t]*)?\1/;
|
||||
OPERATOR = /^(?:-[-=>]?|\+[+=]?|\.\.\.?|[*&|\/%=<>^:!?]+)/;
|
||||
OPERATOR = /^(?:-[-=>]?|\+[+=]?|\.{3}|[*&|\/%=<>^:!?]+)/;
|
||||
WHITESPACE = /^[ \t]+/;
|
||||
COMMENT = /^###([^#][\s\S]*?)(?:###[ \t]*\n|(?:###)?$)|^(?:\s*#(?!##[^#]).*)+/;
|
||||
CODE = /^[-=]>/;
|
||||
@@ -625,7 +636,7 @@
|
||||
HEREDOC_INDENT = /\n+([ \t]*)/g;
|
||||
ASSIGNED = /^\s*@?[$A-Za-z_][$\w]*[ \t]*?[:=][^:=>]/;
|
||||
NEXT_CHARACTER = /^\s*(\S?)/;
|
||||
NEXT_ELLIPSIS = /^\s*\.\.\.?/;
|
||||
NEXT_ELLIPSIS = /^\s*\.{3}/;
|
||||
LEADING_SPACES = /^\s+/;
|
||||
TRAILING_SPACES = /\s+$/;
|
||||
NO_NEWLINE = /^(?:[-+*&|\/%=<>!.\\][<>=&|]*|and|or|is(?:nt)?|n(?:ot|ew)|delete|typeof|instanceof)$/;
|
||||
|
||||
367
lib/nodes.js
367
lib/nodes.js
@@ -1,5 +1,5 @@
|
||||
(function() {
|
||||
var Accessor, ArrayLiteral, Assign, Base, Call, Class, Closure, Code, Comment, Existence, Expressions, Extends, For, IDENTIFIER, IS_STRING, If, In, Index, Literal, NO, NUMBER, ObjectLiteral, Op, Param, Parens, Push, Range, Return, SIMPLENUM, Scope, Slice, Splat, Switch, TAB, THIS, TRAILING_WHITESPACE, Throw, Try, UTILITIES, Value, While, YES, _ref, compact, del, ends, flatten, last, merge, starts, utility;
|
||||
var Accessor, ArrayLiteral, Assign, Base, Call, Class, Closure, Code, Comment, Existence, Expressions, Extends, For, IDENTIFIER, IS_STRING, If, In, Index, Literal, NO, NUMBER, ObjectLiteral, Op, Param, Parens, Push, Return, SIMPLENUM, Scope, Splat, Switch, TAB, THIS, TRAILING_WHITESPACE, Throw, Try, UTILITIES, Value, While, YES, _ref, compact, del, ends, extend, flatten, last, merge, starts, utility;
|
||||
var __extends = function(child, parent) {
|
||||
function ctor() { this.constructor = child; }
|
||||
ctor.prototype = parent.prototype;
|
||||
@@ -11,7 +11,8 @@
|
||||
return -1;
|
||||
};
|
||||
Scope = require('./scope').Scope;
|
||||
_ref = require('./helpers'), compact = _ref.compact, flatten = _ref.flatten, merge = _ref.merge, del = _ref.del, starts = _ref.starts, ends = _ref.ends, last = _ref.last;
|
||||
_ref = require('./helpers'), compact = _ref.compact, flatten = _ref.flatten, extend = _ref.extend, merge = _ref.merge, del = _ref.del, starts = _ref.starts, ends = _ref.ends, last = _ref.last;
|
||||
exports.extend = extend;
|
||||
YES = function() {
|
||||
return true;
|
||||
};
|
||||
@@ -57,7 +58,7 @@
|
||||
}
|
||||
}).call(this);
|
||||
if ((options != null) ? options.precompile : undefined) {
|
||||
for (i = 0, _len = pair.length; i < _len; i++) {
|
||||
for (i = 0, _len = pair.length; i < _len; ++i) {
|
||||
node = pair[i];
|
||||
(pair[i] = node.compile(o));
|
||||
}
|
||||
@@ -68,6 +69,16 @@
|
||||
this.parenthetical = true;
|
||||
return this.compile(o);
|
||||
};
|
||||
Base.prototype.compileLoopReference = function(o, name) {
|
||||
var src, tmp;
|
||||
src = tmp = this.compile(o);
|
||||
if (!(NUMBER.test(src) || IDENTIFIER.test(src) && o.scope.check(src, {
|
||||
immediate: true
|
||||
}))) {
|
||||
src = "" + (tmp = o.scope.freeVariable(name)) + " = " + src;
|
||||
}
|
||||
return [src, tmp];
|
||||
};
|
||||
Base.prototype.idt = function(tabs) {
|
||||
return (this.tab || '') + Array((tabs || 0) + 1).join(TAB);
|
||||
};
|
||||
@@ -103,7 +114,7 @@
|
||||
idt || (idt = '');
|
||||
children = (function() {
|
||||
_result = [];
|
||||
for (_i = 0, _len = (_ref2 = this.collectChildren()).length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = (_ref2 = this.collectChildren()).length; _i < _len; ++_i) {
|
||||
child = _ref2[_i];
|
||||
_result.push(child.toString(idt + TAB));
|
||||
}
|
||||
@@ -117,10 +128,10 @@
|
||||
if (!this.children) {
|
||||
return;
|
||||
}
|
||||
for (_i = 0, _len = (_ref2 = this.children).length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = (_ref2 = this.children).length; _i < _len; ++_i) {
|
||||
attr = _ref2[_i];
|
||||
if (this[attr]) {
|
||||
for (_j = 0, _len2 = (_ref3 = flatten([this[attr]])).length; _j < _len2; _j++) {
|
||||
for (_j = 0, _len2 = (_ref3 = flatten([this[attr]])).length; _j < _len2; ++_j) {
|
||||
child = _ref3[_j];
|
||||
if (func(child) === false) {
|
||||
return;
|
||||
@@ -205,7 +216,7 @@
|
||||
var _i, _len, _ref2, _result, node;
|
||||
return (function() {
|
||||
_result = [];
|
||||
for (_i = 0, _len = (_ref2 = this.expressions).length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = (_ref2 = this.expressions).length; _i < _len; ++_i) {
|
||||
node = _ref2[_i];
|
||||
_result.push(this.compileExpression(node, merge(o)));
|
||||
}
|
||||
@@ -348,15 +359,12 @@
|
||||
Value.prototype.isObject = function() {
|
||||
return this.base instanceof ObjectLiteral && !this.properties.length;
|
||||
};
|
||||
Value.prototype.isSplice = function() {
|
||||
return last(this.properties) instanceof Slice;
|
||||
};
|
||||
Value.prototype.isComplex = function() {
|
||||
return this.base.isComplex() || this.hasProperties();
|
||||
};
|
||||
Value.prototype.isAtomic = function() {
|
||||
var _i, _len, _ref2, node;
|
||||
for (_i = 0, _len = (_ref2 = this.properties.concat(this.base)).length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = (_ref2 = this.properties.concat(this.base)).length; _i < _len; ++_i) {
|
||||
node = _ref2[_i];
|
||||
if (node.soakNode || node instanceof Call) {
|
||||
return false;
|
||||
@@ -417,7 +425,7 @@
|
||||
if (props[0] instanceof Accessor && this.isSimpleNumber()) {
|
||||
code = "(" + code + ")";
|
||||
}
|
||||
for (_i = 0, _len = props.length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = props.length; _i < _len; ++_i) {
|
||||
prop = props[_i];
|
||||
(code += prop.compile(o));
|
||||
}
|
||||
@@ -429,7 +437,7 @@
|
||||
Array.prototype.push.apply(ifn.body.properties, this.properties);
|
||||
return ifn;
|
||||
}
|
||||
for (i = 0, _len = (_ref2 = this.properties).length; i < _len; i++) {
|
||||
for (i = 0, _len = (_ref2 = this.properties).length; i < _len; ++i) {
|
||||
prop = _ref2[i];
|
||||
if (prop.soakNode) {
|
||||
prop.soakNode = false;
|
||||
@@ -544,7 +552,7 @@
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (_i = 0, _len = (_ref3 = list.reverse()).length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = (_ref3 = list.reverse()).length; _i < _len; ++_i) {
|
||||
call = _ref3[_i];
|
||||
if (ifn) {
|
||||
if (call.variable instanceof Call) {
|
||||
@@ -563,7 +571,7 @@
|
||||
return ifn.compile(o);
|
||||
}
|
||||
((_ref2 = this.variable) != null) ? _ref2.tags.front = this.tags.front : undefined;
|
||||
for (_i = 0, _len = (_ref3 = this.args).length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = (_ref3 = this.args).length; _i < _len; ++_i) {
|
||||
arg = _ref3[_i];
|
||||
if (arg instanceof Splat) {
|
||||
return this.compileSplat(o);
|
||||
@@ -571,7 +579,7 @@
|
||||
}
|
||||
args = (function() {
|
||||
_result = [];
|
||||
for (_j = 0, _len2 = (_ref4 = this.args).length; _j < _len2; _j++) {
|
||||
for (_j = 0, _len2 = (_ref4 = this.args).length; _j < _len2; ++_j) {
|
||||
arg = _ref4[_j];
|
||||
_result.push(arg.compileBare(o));
|
||||
}
|
||||
@@ -663,117 +671,6 @@
|
||||
};
|
||||
return Index;
|
||||
})();
|
||||
exports.Range = (function() {
|
||||
Range = (function() {
|
||||
function Range(_arg, _arg2, tag) {
|
||||
this.to = _arg2;
|
||||
this.from = _arg;
|
||||
Range.__super__.constructor.call(this);
|
||||
this.exclusive = tag === 'exclusive';
|
||||
this.equals = this.exclusive ? '' : '=';
|
||||
return this;
|
||||
};
|
||||
return Range;
|
||||
})();
|
||||
__extends(Range, Base);
|
||||
Range.prototype.children = ['from', 'to'];
|
||||
Range.prototype.compileVariables = function(o) {
|
||||
var _ref2, _ref3, _ref4, parts;
|
||||
o = merge(o, {
|
||||
top: true
|
||||
});
|
||||
_ref2 = this.from.compileReference(o, {
|
||||
precompile: true
|
||||
}), this.from = _ref2[0], this.fromVar = _ref2[1];
|
||||
_ref3 = this.to.compileReference(o, {
|
||||
precompile: true
|
||||
}), this.to = _ref3[0], this.toVar = _ref3[1];
|
||||
_ref4 = [this.fromVar.match(SIMPLENUM), this.toVar.match(SIMPLENUM)], this.fromNum = _ref4[0], this.toNum = _ref4[1];
|
||||
parts = [];
|
||||
if (this.from !== this.fromVar) {
|
||||
parts.push(this.from);
|
||||
}
|
||||
return this.to !== this.toVar ? parts.push(this.to) : undefined;
|
||||
};
|
||||
Range.prototype.compileNode = function(o) {
|
||||
var compare, idx, incr, intro, step, stepPart, vars;
|
||||
this.compileVariables(o);
|
||||
if (!o.index) {
|
||||
return this.compileArray(o);
|
||||
}
|
||||
if (this.fromNum && this.toNum) {
|
||||
return this.compileSimple(o);
|
||||
}
|
||||
idx = del(o, 'index');
|
||||
step = del(o, 'step');
|
||||
vars = ("" + idx + " = " + this.from) + (this.to !== this.toVar ? ", " + this.to : '');
|
||||
intro = "(" + this.fromVar + " <= " + this.toVar + " ? " + idx;
|
||||
compare = "" + intro + " <" + this.equals + " " + this.toVar + " : " + idx + " >" + this.equals + " " + this.toVar + ")";
|
||||
stepPart = step ? step.compile(o) : '1';
|
||||
incr = step ? "" + idx + " += " + stepPart : "" + intro + " += " + stepPart + " : " + idx + " -= " + stepPart + ")";
|
||||
return "" + vars + "; " + compare + "; " + incr;
|
||||
};
|
||||
Range.prototype.compileSimple = function(o) {
|
||||
var _ref2, from, idx, step, to;
|
||||
_ref2 = [+this.fromNum, +this.toNum], from = _ref2[0], to = _ref2[1];
|
||||
idx = del(o, 'index');
|
||||
step = del(o, 'step');
|
||||
step && (step = "" + idx + " += " + (step.compile(o)));
|
||||
return from <= to ? "" + idx + " = " + from + "; " + idx + " <" + this.equals + " " + to + "; " + (step || ("" + idx + "++")) : "" + idx + " = " + from + "; " + idx + " >" + this.equals + " " + to + "; " + (step || ("" + idx + "--"));
|
||||
};
|
||||
Range.prototype.compileArray = function(o) {
|
||||
var _i, _ref2, _ref3, _result, body, clause, i, idt, post, pre, range, result, vars;
|
||||
if (this.fromNum && this.toNum && Math.abs(this.fromNum - this.toNum) <= 20) {
|
||||
range = (function() {
|
||||
_result = [];
|
||||
for (var _i = _ref2 = +this.fromNum, _ref3 = +this.toNum; _ref2 <= _ref3 ? _i <= _ref3 : _i >= _ref3; _ref2 <= _ref3 ? _i += 1 : _i -= 1){ _result.push(_i); }
|
||||
return _result;
|
||||
}).call(this);
|
||||
if (this.exclusive) {
|
||||
range.pop();
|
||||
}
|
||||
return "[" + (range.join(', ')) + "]";
|
||||
}
|
||||
idt = this.idt(1);
|
||||
i = o.scope.freeVariable('i');
|
||||
result = o.scope.freeVariable('result');
|
||||
pre = "\n" + idt + result + " = [];";
|
||||
if (this.fromNum && this.toNum) {
|
||||
o.index = i;
|
||||
body = this.compileSimple(o);
|
||||
} else {
|
||||
vars = ("" + i + " = " + this.from) + (this.to !== this.toVar ? ", " + this.to : '');
|
||||
clause = "" + this.fromVar + " <= " + this.toVar + " ?";
|
||||
body = "var " + vars + "; " + clause + " " + i + " <" + this.equals + " " + this.toVar + " : " + i + " >" + this.equals + " " + this.toVar + "; " + clause + " " + i + " += 1 : " + i + " -= 1";
|
||||
}
|
||||
post = "{ " + result + ".push(" + i + "); }\n" + idt + "return " + result + ";\n" + o.indent;
|
||||
return "(function() {" + pre + "\n" + idt + "for (" + body + ")" + post + "}).call(this)";
|
||||
};
|
||||
return Range;
|
||||
})();
|
||||
exports.Slice = (function() {
|
||||
Slice = (function() {
|
||||
function Slice(_arg) {
|
||||
this.range = _arg;
|
||||
Slice.__super__.constructor.call(this);
|
||||
return this;
|
||||
};
|
||||
return Slice;
|
||||
})();
|
||||
__extends(Slice, Base);
|
||||
Slice.prototype.children = ['range'];
|
||||
Slice.prototype.compileNode = function(o) {
|
||||
var from, to;
|
||||
from = this.range.from ? this.range.from.compile(o) : '0';
|
||||
to = this.range.to ? this.range.to.compile(o) : '';
|
||||
to += !to || this.range.exclusive ? '' : ' + 1';
|
||||
if (to) {
|
||||
to = ', ' + to;
|
||||
}
|
||||
return ".slice(" + from + to + ")";
|
||||
};
|
||||
return Slice;
|
||||
})();
|
||||
exports.ObjectLiteral = (function() {
|
||||
ObjectLiteral = (function() {
|
||||
function ObjectLiteral(props) {
|
||||
@@ -791,7 +688,7 @@
|
||||
o.indent = this.idt(1);
|
||||
nonComments = (function() {
|
||||
_result = [];
|
||||
for (_i = 0, _len = (_ref2 = this.properties).length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = (_ref2 = this.properties).length; _i < _len; ++_i) {
|
||||
prop = _ref2[_i];
|
||||
if (!(prop instanceof Comment)) {
|
||||
_result.push(prop);
|
||||
@@ -802,7 +699,7 @@
|
||||
lastNoncom = last(nonComments);
|
||||
props = (function() {
|
||||
_result = [];
|
||||
for (i = 0, _len = (_ref2 = this.properties).length; i < _len; i++) {
|
||||
for (i = 0, _len = (_ref2 = this.properties).length; i < _len; ++i) {
|
||||
prop = _ref2[i];
|
||||
_result.push((function() {
|
||||
join = i === this.properties.length - 1 ? '' : prop === lastNoncom || prop instanceof Comment ? '\n' : ',\n';
|
||||
@@ -823,7 +720,7 @@
|
||||
};
|
||||
ObjectLiteral.prototype.assigns = function(name) {
|
||||
var _i, _len, _ref2, prop;
|
||||
for (_i = 0, _len = (_ref2 = this.properties).length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = (_ref2 = this.properties).length; _i < _len; ++_i) {
|
||||
prop = _ref2[_i];
|
||||
if (prop.assigns(name)) {
|
||||
return true;
|
||||
@@ -850,14 +747,14 @@
|
||||
ArrayLiteral.prototype.compileNode = function(o) {
|
||||
var _i, _len, _len2, _ref2, _ref3, code, i, obj, objects;
|
||||
o.indent = this.idt(1);
|
||||
for (_i = 0, _len = (_ref2 = this.objects).length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = (_ref2 = this.objects).length; _i < _len; ++_i) {
|
||||
obj = _ref2[_i];
|
||||
if (obj instanceof Splat) {
|
||||
return this.compileSplatLiteral(o);
|
||||
}
|
||||
}
|
||||
objects = [];
|
||||
for (i = 0, _len2 = (_ref3 = this.objects).length; i < _len2; i++) {
|
||||
for (i = 0, _len2 = (_ref3 = this.objects).length; i < _len2; ++i) {
|
||||
obj = _ref3[i];
|
||||
code = obj.compileBare(o);
|
||||
objects.push(obj instanceof Comment ? "\n" + code + "\n" + o.indent : i === this.objects.length - 1 ? code : code + ', ');
|
||||
@@ -867,7 +764,7 @@
|
||||
};
|
||||
ArrayLiteral.prototype.assigns = function(name) {
|
||||
var _i, _len, _ref2, obj;
|
||||
for (_i = 0, _len = (_ref2 = this.objects).length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = (_ref2 = this.objects).length; _i < _len; ++_i) {
|
||||
obj = _ref2[_i];
|
||||
if (obj.assigns(name)) {
|
||||
return true;
|
||||
@@ -914,7 +811,7 @@
|
||||
} else {
|
||||
constructor = new Code([], new Expressions([new Return(new Literal('this'))]));
|
||||
}
|
||||
for (_i = 0, _len = (_ref2 = this.properties).length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = (_ref2 = this.properties).length; _i < _len; ++_i) {
|
||||
prop = _ref2[_i];
|
||||
pvar = prop.variable, func = prop.value;
|
||||
if (pvar && pvar.base.value === 'constructor') {
|
||||
@@ -996,9 +893,6 @@
|
||||
if (this.variable.isArray() || this.variable.isObject()) {
|
||||
return this.compilePatternMatch(o);
|
||||
}
|
||||
if (this.variable.isSplice()) {
|
||||
return this.compileSplice(o);
|
||||
}
|
||||
if (ifn = If.unfoldSoak(o, this, 'variable')) {
|
||||
delete o.top;
|
||||
return ifn.compile(o);
|
||||
@@ -1058,7 +952,7 @@
|
||||
assigns.push("" + (ref = o.scope.freeVariable('ref')) + " = " + valVar);
|
||||
valVar = ref;
|
||||
}
|
||||
for (i = 0, _len = objects.length; i < _len; i++) {
|
||||
for (i = 0, _len = objects.length; i < _len; ++i) {
|
||||
obj = objects[i];
|
||||
idx = i;
|
||||
if (isObject) {
|
||||
@@ -1089,17 +983,6 @@
|
||||
code = assigns.join(', ');
|
||||
return top || this.parenthetical ? code : "(" + code + ")";
|
||||
};
|
||||
Assign.prototype.compileSplice = function(o) {
|
||||
var from, name, plus, range, ref, to, val;
|
||||
range = this.variable.properties.pop().range;
|
||||
name = this.variable.compile(o);
|
||||
plus = range.exclusive ? '' : ' + 1';
|
||||
from = range.from ? range.from.compile(o) : '0';
|
||||
to = range.to ? range.to.compile(o) + ' - ' + from + plus : "" + name + ".length";
|
||||
ref = o.scope.freeVariable('ref');
|
||||
val = this.value.compile(o);
|
||||
return "([].splice.apply(" + name + ", [" + from + ", " + to + "].concat(" + ref + " = " + val + ")), " + ref + ")";
|
||||
};
|
||||
Assign.prototype.compileConditional = function(o) {
|
||||
var _ref2, left, rite;
|
||||
_ref2 = this.variable.cacheReference(o), left = _ref2[0], rite = _ref2[1];
|
||||
@@ -1140,7 +1023,7 @@
|
||||
delete o.globals;
|
||||
splat = undefined;
|
||||
params = [];
|
||||
for (i = 0, _len = (_ref2 = this.params).length; i < _len; i++) {
|
||||
for (i = 0, _len = (_ref2 = this.params).length; i < _len; ++i) {
|
||||
param = _ref2[i];
|
||||
if (splat) {
|
||||
if (param.attach) {
|
||||
@@ -1168,7 +1051,7 @@
|
||||
o.scope.startLevel();
|
||||
params = (function() {
|
||||
_result = [];
|
||||
for (_i = 0, _len2 = params.length; _i < _len2; _i++) {
|
||||
for (_i = 0, _len2 = params.length; _i < _len2; ++_i) {
|
||||
param = params[_i];
|
||||
_result.push(param.compile(o));
|
||||
}
|
||||
@@ -1177,7 +1060,7 @@
|
||||
if (!(empty || this.noReturn)) {
|
||||
this.body.makeReturn();
|
||||
}
|
||||
for (_i = 0, _len2 = params.length; _i < _len2; _i++) {
|
||||
for (_i = 0, _len2 = params.length; _i < _len2; ++_i) {
|
||||
param = params[_i];
|
||||
o.scope.parameter(param);
|
||||
}
|
||||
@@ -1258,7 +1141,7 @@
|
||||
variadic = o.scope.freeVariable('result');
|
||||
o.scope.assign(variadic, len + ' >= ' + this.arglength);
|
||||
end = this.trailings.length ? ", " + len + " - " + this.trailings.length : undefined;
|
||||
for (idx = 0, _len = (_ref2 = this.trailings).length; idx < _len; idx++) {
|
||||
for (idx = 0, _len = (_ref2 = this.trailings).length; idx < _len; ++idx) {
|
||||
trailing = _ref2[idx];
|
||||
if (trailing.attach) {
|
||||
assign = trailing.assign;
|
||||
@@ -1280,7 +1163,7 @@
|
||||
var _len, arg, args, code, end, i, prev;
|
||||
args = [];
|
||||
end = -1;
|
||||
for (i = 0, _len = list.length; i < _len; i++) {
|
||||
for (i = 0, _len = list.length; i < _len; ++i) {
|
||||
arg = list[i];
|
||||
code = arg.compile(o);
|
||||
prev = args[end];
|
||||
@@ -1480,7 +1363,7 @@
|
||||
_ref3 = this.negated ? [' !== ', ' && '] : [' === ', ' || '], cmp = _ref3[0], cnj = _ref3[1];
|
||||
tests = (function() {
|
||||
_result = [];
|
||||
for (i = 0, _len = (_ref4 = this.array.base.objects).length; i < _len; i++) {
|
||||
for (i = 0, _len = (_ref4 = this.array.base.objects).length; i < _len; ++i) {
|
||||
item = _ref4[i];
|
||||
_result.push((i ? ref : sub) + cmp + item.compile(o));
|
||||
}
|
||||
@@ -1616,22 +1499,16 @@
|
||||
})();
|
||||
exports.For = (function() {
|
||||
For = (function() {
|
||||
function For(_arg, source, _arg2, _arg3) {
|
||||
var _ref2;
|
||||
this.index = _arg3;
|
||||
this.name = _arg2;
|
||||
function For(_arg, head) {
|
||||
this.body = _arg;
|
||||
For.__super__.constructor.call(this);
|
||||
this.source = source.source, this.guard = source.guard, this.step = source.step;
|
||||
this.raw = !!source.raw;
|
||||
this.object = !!source.object;
|
||||
if (this.object) {
|
||||
_ref2 = [this.index, this.name], this.name = _ref2[0], this.index = _ref2[1];
|
||||
}
|
||||
if (this.index instanceof Value) {
|
||||
if (head.index instanceof Value) {
|
||||
throw SyntaxError('index cannot be a pattern matching expression');
|
||||
}
|
||||
this.range = this.source instanceof Value && this.source.base instanceof Range && !this.source.properties.length;
|
||||
For.__super__.constructor.call(this);
|
||||
extend(this, head);
|
||||
if (!this.object) {
|
||||
this.step || (this.step = new Literal(1));
|
||||
}
|
||||
this.pattern = this.name instanceof Value;
|
||||
if (this.range && this.pattern) {
|
||||
throw SyntaxError('cannot pattern match a range loop');
|
||||
@@ -1642,7 +1519,7 @@
|
||||
return For;
|
||||
})();
|
||||
__extends(For, Base);
|
||||
For.prototype.children = ['body', 'source', 'guard'];
|
||||
For.prototype.children = ['body', 'source', 'guard', 'step', 'from', 'to'];
|
||||
For.prototype.topSensitive = YES;
|
||||
For.prototype.isStatement = YES;
|
||||
For.prototype.makeReturn = function() {
|
||||
@@ -1659,16 +1536,16 @@
|
||||
return '';
|
||||
};
|
||||
For.prototype.compileNode = function(o) {
|
||||
var body, codeInBody, forPart, guardPart, idt1, index, ivar, lastLine, lvar, name, namePart, nvar, ref, resultPart, returnResult, rvar, scope, source, sourcePart, stepPart, svar, topLevel, unstepPart, varPart, vars;
|
||||
topLevel = del(o, 'top') && !this.returns;
|
||||
source = this.range ? this.source.base : this.source;
|
||||
codeInBody = !this.body.containsPureStatement() && this.body.contains(function(node) {
|
||||
return node instanceof Code;
|
||||
});
|
||||
var _ref2, _ref3, _ref4, _ref5, _ref6, body, cond, forPart, guardPart, idt, index, ivar, lvar, name, namePart, pvar, resultDef, resultRet, rvar, scope, sourcePart, step, svar, tail, top, tvar, varPart, vars;
|
||||
scope = o.scope;
|
||||
name = this.name && this.name.compile(o);
|
||||
index = this.index && this.index.compile(o);
|
||||
if (name && !this.pattern && (this.range || !codeInBody)) {
|
||||
top = del(o, 'top') && !this.returns;
|
||||
name = !this.pattern && (((_ref2 = this.name) != null) ? _ref2.compile(o) : undefined);
|
||||
index = ((_ref3 = this.index) != null) ? _ref3.compile(o) : undefined;
|
||||
ivar = !index ? scope.freeVariable('i') : index;
|
||||
varPart = '';
|
||||
body = Expressions.wrap([this.body]);
|
||||
idt = this.idt(1);
|
||||
if (name) {
|
||||
scope.find(name, {
|
||||
immediate: true
|
||||
});
|
||||
@@ -1678,101 +1555,73 @@
|
||||
immediate: true
|
||||
});
|
||||
}
|
||||
if (!topLevel) {
|
||||
rvar = scope.freeVariable('result');
|
||||
if (this.step) {
|
||||
_ref4 = this.step.compileLoopReference(o, 'step'), step = _ref4[0], pvar = _ref4[1];
|
||||
}
|
||||
ivar = this.range ? name : index;
|
||||
if (!ivar || codeInBody) {
|
||||
ivar = scope.freeVariable('i');
|
||||
}
|
||||
if (name && !this.range && codeInBody) {
|
||||
nvar = scope.freeVariable('i');
|
||||
}
|
||||
varPart = '';
|
||||
guardPart = '';
|
||||
unstepPart = '';
|
||||
body = Expressions.wrap([this.body]);
|
||||
idt1 = this.idt(1);
|
||||
if (this.range) {
|
||||
forPart = source.compile(merge(o, {
|
||||
index: ivar,
|
||||
step: this.step
|
||||
}));
|
||||
if (this.from) {
|
||||
_ref5 = this.to.compileLoopReference(o, 'to'), tail = _ref5[0], tvar = _ref5[1];
|
||||
vars = "" + ivar + " = " + (this.from.compile(o));
|
||||
if (tail !== tvar) {
|
||||
vars += ", " + tail;
|
||||
}
|
||||
cond = +pvar ? "" + ivar + " " + (pvar < 0 ? '>' : '<') + "= " + tvar : "" + pvar + " < 0 ? " + ivar + " >= " + tvar + " : " + ivar + " <= " + tvar;
|
||||
} else {
|
||||
svar = sourcePart = this.source.compile(o);
|
||||
if ((name || !this.raw) && !(IDENTIFIER.test(svar) && scope.check(svar, {
|
||||
immediate: true
|
||||
}))) {
|
||||
sourcePart = "" + (ref = scope.freeVariable('ref')) + " = " + svar;
|
||||
if (!this.object) {
|
||||
if (name || !this.raw) {
|
||||
_ref6 = this.source.compileLoopReference(o, 'ref'), sourcePart = _ref6[0], svar = _ref6[1];
|
||||
if (!(sourcePart === svar || this.object)) {
|
||||
sourcePart = "(" + sourcePart + ")";
|
||||
}
|
||||
svar = ref;
|
||||
} else {
|
||||
sourcePart = svar = this.source.compile(o);
|
||||
}
|
||||
namePart = this.pattern ? new Assign(this.name, new Literal("" + svar + "[" + ivar + "]")).compile(merge(o, {
|
||||
top: true
|
||||
})) : name ? "" + name + " = " + svar + "[" + ivar + "]" : undefined;
|
||||
if (!this.object) {
|
||||
lvar = scope.freeVariable('len');
|
||||
stepPart = this.step ? "" + ivar + " += " + (this.step.compile(o)) : "" + ivar + "++";
|
||||
forPart = "" + ivar + " = 0, " + lvar + " = " + sourcePart + ".length; " + ivar + " < " + lvar + "; " + stepPart;
|
||||
if (0 > pvar && (pvar | 0) === +pvar) {
|
||||
vars = "" + ivar + " = " + sourcePart + ".length - 1";
|
||||
cond = "" + ivar + " >= 0";
|
||||
} else {
|
||||
lvar = scope.freeVariable('len');
|
||||
vars = "" + ivar + " = 0, " + lvar + " = " + sourcePart + ".length";
|
||||
cond = "" + ivar + " < " + lvar;
|
||||
}
|
||||
}
|
||||
}
|
||||
resultPart = rvar ? "" + this.tab + rvar + " = [];\n" : '';
|
||||
returnResult = this.compileReturnValue(rvar, o);
|
||||
if (!topLevel) {
|
||||
if (this.object) {
|
||||
forPart = "" + ivar + " in " + sourcePart;
|
||||
guardPart = !this.raw && ("" + idt + "if (!" + (utility('hasProp')) + ".call(" + svar + ", " + ivar + ")) continue;\n");
|
||||
} else {
|
||||
if (step !== pvar) {
|
||||
vars += ", " + step;
|
||||
}
|
||||
forPart = ("" + vars + "; " + cond + "; ") + (function() {
|
||||
switch (+pvar) {
|
||||
case 1:
|
||||
return '++' + ivar;
|
||||
case -1:
|
||||
return '--' + ivar;
|
||||
default:
|
||||
return ivar + (pvar < 0 ? ' -= ' + pvar.slice(1) : ' += ' + pvar);
|
||||
}
|
||||
})();
|
||||
}
|
||||
if (!top) {
|
||||
rvar = scope.freeVariable('result');
|
||||
resultDef = "" + this.tab + rvar + " = [];\n";
|
||||
resultRet = this.compileReturnValue(rvar, o);
|
||||
body = Push.wrap(rvar, body);
|
||||
}
|
||||
if (this.guard) {
|
||||
body = Expressions.wrap([new If(this.guard, body)]);
|
||||
}
|
||||
if (codeInBody) {
|
||||
if (this.range) {
|
||||
body.unshift(new Literal("var " + name + " = " + ivar));
|
||||
}
|
||||
if (namePart) {
|
||||
body.unshift(new Literal("var " + namePart));
|
||||
}
|
||||
if (index) {
|
||||
body.unshift(new Literal("var " + index + " = " + ivar));
|
||||
}
|
||||
lastLine = body.expressions.pop();
|
||||
if (index) {
|
||||
body.push(new Assign(new Literal(ivar), new Literal(index)));
|
||||
}
|
||||
if (nvar) {
|
||||
body.push(new Assign(new Literal(nvar), new Literal(name)));
|
||||
}
|
||||
body.push(lastLine);
|
||||
o.indent = this.idt(1);
|
||||
body = Expressions.wrap([new Literal(body.compile(o))]);
|
||||
if (index) {
|
||||
body.push(new Assign(this.index, new Literal(ivar)));
|
||||
}
|
||||
if (name) {
|
||||
body.push(new Assign(this.name, new Literal(nvar || ivar)));
|
||||
}
|
||||
} else {
|
||||
if (namePart) {
|
||||
varPart = "" + idt1 + namePart + ";\n";
|
||||
}
|
||||
if (forPart && name === ivar) {
|
||||
unstepPart = this.step ? "" + name + " -= " + (this.step.compile(o)) + ";" : "" + name + "--;";
|
||||
unstepPart = ("\n" + this.tab) + unstepPart;
|
||||
}
|
||||
if (namePart) {
|
||||
varPart = "" + idt + namePart + ";\n";
|
||||
}
|
||||
if (this.object) {
|
||||
forPart = "" + ivar + " in " + sourcePart;
|
||||
if (!this.raw) {
|
||||
guardPart = "\n" + idt1 + "if (!" + (utility('hasProp')) + ".call(" + svar + ", " + ivar + ")) continue;";
|
||||
}
|
||||
}
|
||||
body = body.compile(merge(o, {
|
||||
indent: idt1,
|
||||
return "" + (resultDef || '') + this.tab + "for (" + forPart + ") {\n" + (guardPart || '') + varPart + (body.compile(merge(o, {
|
||||
indent: idt,
|
||||
top: true
|
||||
}));
|
||||
vars = this.range ? name : "" + name + ", " + ivar;
|
||||
return "" + resultPart + this.tab + "for (" + forPart + ") {" + guardPart + "\n" + varPart + body + "\n" + this.tab + "}" + unstepPart + returnResult;
|
||||
}))) + "\n" + this.tab + "}" + (resultRet || '');
|
||||
};
|
||||
return For;
|
||||
})();
|
||||
@@ -1792,7 +1641,7 @@
|
||||
Switch.prototype.isStatement = YES;
|
||||
Switch.prototype.makeReturn = function() {
|
||||
var _i, _len, _ref2, pair;
|
||||
for (_i = 0, _len = (_ref2 = this.cases).length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = (_ref2 = this.cases).length; _i < _len; ++_i) {
|
||||
pair = _ref2[_i];
|
||||
pair[1].makeReturn();
|
||||
}
|
||||
@@ -1807,9 +1656,9 @@
|
||||
idt2 = o.indent = this.idt(2);
|
||||
o.top = true;
|
||||
code = "" + this.tab + "switch (" + ((((_ref2 = this.subject) != null) ? _ref2.compile(o) : undefined) || true) + ") {";
|
||||
for (_i = 0, _len = (_ref3 = this.cases).length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = (_ref3 = this.cases).length; _i < _len; ++_i) {
|
||||
_ref4 = _ref3[_i], conditions = _ref4[0], block = _ref4[1];
|
||||
for (_j = 0, _len2 = (_ref5 = flatten([conditions])).length; _j < _len2; _j++) {
|
||||
for (_j = 0, _len2 = (_ref5 = flatten([conditions])).length; _j < _len2; ++_j) {
|
||||
condition = _ref5[_j];
|
||||
if (!this.subject) {
|
||||
condition = condition.invert().invert();
|
||||
@@ -1968,7 +1817,7 @@
|
||||
TAB = ' ';
|
||||
TRAILING_WHITESPACE = /[ \t]+$/gm;
|
||||
IDENTIFIER = /^[$A-Za-z_][$\w]*$/;
|
||||
NUMBER = /^0x[\da-f]+$|^(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?$/i;
|
||||
NUMBER = /^-?(?:0x[\da-f]+|(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?)$/i;
|
||||
SIMPLENUM = /^[+-]?\d+$/;
|
||||
IS_STRING = /^['"]/;
|
||||
utility = function(name) {
|
||||
|
||||
@@ -15,11 +15,11 @@
|
||||
arguments: []
|
||||
};
|
||||
args = normalizeArguments(args);
|
||||
for (i = 0, _len = args.length; i < _len; i++) {
|
||||
for (i = 0, _len = args.length; i < _len; ++i) {
|
||||
arg = args[i];
|
||||
isOption = !!(arg.match(LONG_FLAG) || arg.match(SHORT_FLAG));
|
||||
matchedRule = false;
|
||||
for (_i = 0, _len2 = (_ref = this.rules).length; _i < _len2; _i++) {
|
||||
for (_i = 0, _len2 = (_ref = this.rules).length; _i < _len2; ++_i) {
|
||||
rule = _ref[_i];
|
||||
if (rule.shortFlag === arg || rule.longFlag === arg) {
|
||||
value = rule.hasArgument ? args[i += 1] : true;
|
||||
@@ -44,7 +44,7 @@
|
||||
if (this.banner) {
|
||||
lines.unshift("" + this.banner + "\n");
|
||||
}
|
||||
for (_i = 0, _len = (_ref = this.rules).length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = (_ref = this.rules).length; _i < _len; ++_i) {
|
||||
rule = _ref[_i];
|
||||
spaces = 15 - rule.longFlag.length;
|
||||
spaces = spaces > 0 ? Array(spaces + 1).join(' ') : '';
|
||||
@@ -62,7 +62,7 @@
|
||||
buildRules = function(rules) {
|
||||
var _i, _len, _result, tuple;
|
||||
_result = [];
|
||||
for (_i = 0, _len = rules.length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = rules.length; _i < _len; ++_i) {
|
||||
tuple = rules[_i];
|
||||
_result.push((function() {
|
||||
if (tuple.length < 3) {
|
||||
@@ -91,10 +91,10 @@
|
||||
var _i, _j, _len, _len2, _ref, arg, l, match, result;
|
||||
args = args.slice(0);
|
||||
result = [];
|
||||
for (_i = 0, _len = args.length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = args.length; _i < _len; ++_i) {
|
||||
arg = args[_i];
|
||||
if (match = arg.match(MULTI_FLAG)) {
|
||||
for (_j = 0, _len2 = (_ref = match[1].split('')).length; _j < _len2; _j++) {
|
||||
for (_j = 0, _len2 = (_ref = match[1].split('')).length; _j < _len2; ++_j) {
|
||||
l = _ref[_j];
|
||||
result.push('-' + l);
|
||||
}
|
||||
|
||||
386
lib/parser.js
386
lib/parser.js
File diff suppressed because one or more lines are too long
@@ -87,7 +87,7 @@
|
||||
};
|
||||
exports.Rewriter.prototype.removeLeadingNewlines = function() {
|
||||
var _len, _ref, i, tag;
|
||||
for (i = 0, _len = (_ref = this.tokens).length; i < _len; i++) {
|
||||
for (i = 0, _len = (_ref = this.tokens).length; i < _len; ++i) {
|
||||
tag = _ref[i][0];
|
||||
if (tag !== 'TERMINATOR') {
|
||||
break;
|
||||
@@ -286,7 +286,7 @@
|
||||
this.scanTokens(function(token, i) {
|
||||
var _i, _len, _ref, _ref2, close, open, tag;
|
||||
tag = token[0];
|
||||
for (_i = 0, _len = (_ref = pairs).length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = (_ref = pairs).length; _i < _len; ++_i) {
|
||||
_ref2 = _ref[_i], open = _ref2[0], close = _ref2[1];
|
||||
levels[open] |= 0;
|
||||
if (tag === open) {
|
||||
@@ -366,7 +366,7 @@
|
||||
INVERSES = {};
|
||||
EXPRESSION_START = [];
|
||||
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];
|
||||
EXPRESSION_START.push(INVERSES[rite] = left);
|
||||
EXPRESSION_END.push(INVERSES[left] = rite);
|
||||
|
||||
10
lib/scope.js
10
lib/scope.js
@@ -42,7 +42,7 @@
|
||||
};
|
||||
Scope.prototype.endLevel = function() {
|
||||
var _i, _len, _ref2, name;
|
||||
for (_i = 0, _len = (_ref2 = this.garbage.pop()).length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = (_ref2 = this.garbage.pop()).length; _i < _len; ++_i) {
|
||||
name = _ref2[_i];
|
||||
if (this.type(name) === 'var') {
|
||||
this.add(name, 'reuse');
|
||||
@@ -59,7 +59,7 @@
|
||||
};
|
||||
Scope.prototype.any = function(fn) {
|
||||
var _i, _len, _ref2, v;
|
||||
for (_i = 0, _len = (_ref2 = this.variables).length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = (_ref2 = this.variables).length; _i < _len; ++_i) {
|
||||
v = _ref2[_i];
|
||||
if (fn(v)) {
|
||||
return true;
|
||||
@@ -83,7 +83,7 @@
|
||||
};
|
||||
Scope.prototype.type = function(name) {
|
||||
var _i, _len, _ref2, v;
|
||||
for (_i = 0, _len = (_ref2 = this.variables).length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = (_ref2 = this.variables).length; _i < _len; ++_i) {
|
||||
v = _ref2[_i];
|
||||
if (v.name === name) {
|
||||
return v.type;
|
||||
@@ -122,7 +122,7 @@
|
||||
var _i, _len, _ref2, _ref3, _result, v;
|
||||
return (function() {
|
||||
_result = [];
|
||||
for (_i = 0, _len = (_ref2 = this.variables).length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = (_ref2 = this.variables).length; _i < _len; ++_i) {
|
||||
v = _ref2[_i];
|
||||
if ((_ref3 = v.type) === 'var' || _ref3 === 'reuse') {
|
||||
_result.push(v.name);
|
||||
@@ -134,7 +134,7 @@
|
||||
Scope.prototype.assignedVariables = function() {
|
||||
var _i, _len, _ref2, _result, v;
|
||||
_result = [];
|
||||
for (_i = 0, _len = (_ref2 = this.variables).length; _i < _len; _i++) {
|
||||
for (_i = 0, _len = (_ref2 = this.variables).length; _i < _len; ++_i) {
|
||||
v = _ref2[_i];
|
||||
if (v.type.assigned) {
|
||||
_result.push("" + v.name + " = " + v.type.value);
|
||||
|
||||
@@ -33,7 +33,7 @@ runScripts = ->
|
||||
if script.src
|
||||
CoffeeScript.load script.src
|
||||
else
|
||||
setTimeout -> CoffeeScript.run script.innerHTML
|
||||
CoffeeScript.run script.innerHTML
|
||||
null
|
||||
if window.addEventListener
|
||||
addEventListener 'DOMContentLoaded', runScripts, no
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
# from our rules and saves it into `lib/parser.js`.
|
||||
|
||||
# The only dependency is on the **Jison.Parser**.
|
||||
Parser = require('jison').Parser
|
||||
{Parser} = require 'jison'
|
||||
|
||||
# Jison DSL
|
||||
# ---------
|
||||
@@ -35,7 +35,7 @@ o = (patternString, action, options) ->
|
||||
return [patternString, '$$ = $1;', options] unless action
|
||||
action = if match = unwrap.exec action then match[1] else "(#{action}())"
|
||||
action = action.replace /\bnew /g, '$&yy.'
|
||||
action = action.replace /\bExpressions\.wrap\b/g, 'yy.$&'
|
||||
action = action.replace /\b(?:Expressions\.wrap|extend)\b/g, 'yy.$&'
|
||||
[patternString, "$$ = #{action};", options]
|
||||
|
||||
# Grammatical Rules
|
||||
@@ -144,14 +144,14 @@ grammar =
|
||||
# Assignment when it happens within an object literal. The difference from
|
||||
# the ordinary **Assign** is that these allow numbers and strings as keys.
|
||||
AssignObj: [
|
||||
o "Identifier", -> new Value $1
|
||||
o "AlphaNumeric"
|
||||
o "ThisProperty"
|
||||
o "Identifier : Expression", -> new Assign new Value($1), $3, 'object'
|
||||
o "AlphaNumeric : Expression", -> new Assign new Value($1), $3, 'object'
|
||||
o "Identifier : INDENT Expression OUTDENT", -> new Assign new Value($1), $4, 'object'
|
||||
o "AlphaNumeric : INDENT Expression OUTDENT", -> new Assign new Value($1), $4, 'object'
|
||||
o "Comment"
|
||||
o 'Identifier', -> new Value $1
|
||||
o 'AlphaNumeric'
|
||||
o 'ThisProperty'
|
||||
o 'Identifier : Expression', -> new Assign new Value($1), $3, 'object'
|
||||
o 'AlphaNumeric : Expression', -> new Assign new Value($1), $3, 'object'
|
||||
o 'Identifier : INDENT Expression OUTDENT', -> new Assign new Value($1), $4, 'object'
|
||||
o 'AlphaNumeric : INDENT Expression OUTDENT', -> new Assign new Value($1), $4, 'object'
|
||||
o 'Comment'
|
||||
]
|
||||
|
||||
# A return statement from a function body.
|
||||
@@ -233,7 +233,6 @@ grammar =
|
||||
o "Assignable"
|
||||
o "Literal", -> new Value $1
|
||||
o "Parenthetical", -> new Value $1
|
||||
o "Range", -> new Value $1
|
||||
o "This"
|
||||
]
|
||||
|
||||
@@ -251,8 +250,8 @@ grammar =
|
||||
# Indexing into an object or array using bracket notation.
|
||||
Index: [
|
||||
o "INDEX_START Expression INDEX_END", -> new Index $2
|
||||
o "INDEX_SOAK Index", -> $2.soakNode = yes; $2
|
||||
o "INDEX_PROTO Index", -> $2.proto = yes; $2
|
||||
o "INDEX_SOAK Index", -> extend $2, soakNode: yes
|
||||
o "INDEX_PROTO Index", -> extend $2, proto: yes
|
||||
]
|
||||
|
||||
# In CoffeeScript, an object literal is simply a list of assignments.
|
||||
@@ -330,28 +329,11 @@ grammar =
|
||||
o "@", -> new Value new Literal 'this'
|
||||
]
|
||||
|
||||
RangeDots: [
|
||||
o "..", -> 'inclusive'
|
||||
o "...", -> 'exclusive'
|
||||
]
|
||||
|
||||
# A reference to a property on *this*.
|
||||
ThisProperty: [
|
||||
o "@ Identifier", -> new Value new Literal('this'), [new Accessor($2)], 'this'
|
||||
]
|
||||
|
||||
# The CoffeeScript range literal.
|
||||
Range: [
|
||||
o "[ Expression RangeDots Expression ]", -> new Range $2, $4, $3
|
||||
]
|
||||
|
||||
# The slice literal.
|
||||
Slice: [
|
||||
o "INDEX_START Expression RangeDots Expression INDEX_END", -> new Range $2, $4, $3
|
||||
o "INDEX_START Expression RangeDots INDEX_END", -> new Range $2, null, $3
|
||||
o "INDEX_START RangeDots Expression INDEX_END", -> new Range null, $3, $2
|
||||
]
|
||||
|
||||
# The array literal.
|
||||
Array: [
|
||||
o "[ ]", -> new ArrayLiteral []
|
||||
@@ -435,48 +417,49 @@ grammar =
|
||||
# Comprehensions can either be normal, with a block of expressions to execute,
|
||||
# or postfix, with a single expression.
|
||||
For: [
|
||||
o "Statement ForBody", -> new For $1, $2, $2.vars[0], $2.vars[1]
|
||||
o "Expression ForBody", -> new For $1, $2, $2.vars[0], $2.vars[1]
|
||||
o "ForBody Block", -> new For $2, $1, $1.vars[0], $1.vars[1]
|
||||
]
|
||||
|
||||
ForBody: [
|
||||
o "FOR Range", -> source: new Value($2), vars: []
|
||||
o "ForStart ForSource", -> $2.raw = $1.raw; $2.vars = $1; $2
|
||||
]
|
||||
|
||||
ForStart: [
|
||||
o "FOR ForVariables", -> $2
|
||||
o "FOR ALL ForVariables", -> $3.raw = true; $3
|
||||
o 'Statement ForBody', -> new For $1, $2
|
||||
o 'Expression ForBody', -> new For $1, $2
|
||||
o 'ForBody Block', -> new For $2, $1
|
||||
]
|
||||
|
||||
# An array of all accepted values for a variable inside the loop. This
|
||||
# enables support for pattern matching.
|
||||
ForValue: [
|
||||
o "Identifier"
|
||||
o "Array", -> new Value $1
|
||||
o "Object", -> new Value $1
|
||||
o 'Identifier'
|
||||
o 'Array', -> new Value $1
|
||||
o 'Object', -> new Value $1
|
||||
]
|
||||
|
||||
# An array or range comprehension has variables for the current element and
|
||||
# (optional) reference to the current index. Or, *key, value*, in the case
|
||||
# of object comprehensions.
|
||||
ForVariables: [
|
||||
o "ForValue", -> [$1]
|
||||
o "ForValue , ForValue", -> [$1, $3]
|
||||
ForIn: [
|
||||
o 'FORIN Expression', -> source: $2
|
||||
o 'FORIN Expression WHEN Expression', -> source: $2, guard: $4
|
||||
o 'FORIN Expression BY Expression', -> source: $2, step: $4
|
||||
o 'FORIN Expression BY Expression WHEN Expression', -> source: $2, step: $4, guard: $6
|
||||
]
|
||||
|
||||
ForOf: [
|
||||
o 'FOROF Expression', -> object: on, source: $2
|
||||
o 'FOROF Expression WHEN Expression', -> object: on, source: $2, guard: $4
|
||||
]
|
||||
|
||||
ForTo: [
|
||||
o 'TO Expression', -> to: $2
|
||||
o 'TO Expression WHEN Expression', -> to: $2, guard: $4
|
||||
o 'TO Expression BY Expression', -> to: $2, step: $4
|
||||
o 'TO Expression BY Expression WHEN Expression', -> to: $2, step: $4, guard: $6
|
||||
]
|
||||
|
||||
# The source of a comprehension is an array or object with an optional guard
|
||||
# clause. If it's an array comprehension, you can also choose to step through
|
||||
# in fixed-size increments.
|
||||
ForSource: [
|
||||
o "FORIN Expression", -> source: $2
|
||||
o "FOROF Expression", -> source: $2, object: true
|
||||
o "FORIN Expression WHEN Expression", -> source: $2, guard: $4
|
||||
o "FOROF Expression WHEN Expression", -> source: $2, guard: $4, object: true
|
||||
o "FORIN Expression BY Expression", -> source: $2, step: $4
|
||||
o "FORIN Expression WHEN Expression BY Expression", -> source: $2, guard: $4, step: $6
|
||||
o "FORIN Expression BY Expression WHEN Expression", -> source: $2, step: $4, guard: $6
|
||||
ForBody: [
|
||||
o 'FOR ForValue ForIn', -> extend $3, name: $2
|
||||
o 'FOR ForValue , Identifier ForIn', -> extend $5, name: $2, index: $4
|
||||
o 'FOR Identifier ForOf', -> extend $3, index: $2
|
||||
o 'FOR ForValue , ForValue ForOf', -> extend $5, index: $2, name: $4
|
||||
o 'FOR ALL Identifier ForOf', -> extend $4, raw: on, index: $3
|
||||
o 'FOR ALL Identifier , ForValue ForOf', -> extend $6, raw: on, index: $3, name: $5
|
||||
o 'FOR Identifier FROM Expression ForTo', -> extend $5, index: $2, from: $4
|
||||
]
|
||||
|
||||
Switch: [
|
||||
@@ -578,7 +561,7 @@ operators = [
|
||||
["left", 'LOGIC']
|
||||
["left", '.']
|
||||
["nonassoc", 'INDENT', 'OUTDENT']
|
||||
["right", 'WHEN', 'LEADING_WHEN', 'FORIN', 'FOROF', 'BY', 'THROW']
|
||||
["right", 'WHEN', 'LEADING_WHEN', 'FORIN', 'FOROF', 'FROM', 'TO', 'BY', 'THROW']
|
||||
["right", 'IF', 'UNLESS', 'ELSE', 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'SUPER', 'CLASS', 'EXTENDS']
|
||||
["right", '=', ':', 'COMPOUND_ASSIGN', 'RETURN']
|
||||
["right", '->', '=>', 'UNLESS', 'POST_IF', 'POST_UNLESS']
|
||||
@@ -592,7 +575,7 @@ operators = [
|
||||
# terminals (every symbol which does not appear as the name of a rule above)
|
||||
# as "tokens".
|
||||
tokens = []
|
||||
for name, alternatives of grammar
|
||||
for all name, alternatives of grammar
|
||||
grammar[name] = for alt in alternatives
|
||||
for token in alt[0].split ' '
|
||||
tokens.push token unless grammar[token]
|
||||
|
||||
@@ -41,9 +41,10 @@ exports.Lexer = class Lexer
|
||||
@indent = 0 # The current indentation level.
|
||||
@indebt = 0 # The over-indentation at the current level.
|
||||
@outdebt = 0 # The under-outdentation at the current level.
|
||||
@seenFor = no # The flag for distinguishing FORIN/FOROF from IN/OF.
|
||||
@indents = [] # The stack of all current indentation levels.
|
||||
@tokens = [] # Stream of parsed tokens in the form ['TYPE', value, line]
|
||||
# Flags for distinguishing FORIN/FOROF/FROM/TO.
|
||||
@seenFor = @seenFrom = no
|
||||
# At every position, run through this list of attempted matches,
|
||||
# short-circuiting if any of them succeed. Their order determines precedence:
|
||||
# `@literalToken` is the fallback catch-all.
|
||||
@@ -78,6 +79,15 @@ exports.Lexer = class Lexer
|
||||
if id is 'all' and @tag() is 'FOR'
|
||||
@token 'ALL', id
|
||||
return true
|
||||
if id is 'from' and @tag(1) is 'FOR'
|
||||
@seenFor = no
|
||||
@seenFrom = yes
|
||||
@token 'FROM', id
|
||||
return true
|
||||
if id is 'to' and @seenFrom
|
||||
@seenFrom = no
|
||||
@token 'TO', id
|
||||
return true
|
||||
forcedIdentifier = colon or @tagAccessor()
|
||||
tag = 'IDENTIFIER'
|
||||
if id in JS_KEYWORDS or
|
||||
@@ -426,7 +436,7 @@ exports.Lexer = class Lexer
|
||||
i += 1
|
||||
if levels.length
|
||||
throw SyntaxError "Unterminated #{levels.pop()[0]} starting on line #{@line + 1}"
|
||||
if not i then false else str[0...i]
|
||||
i and str.slice 0, i
|
||||
|
||||
# Expand variables and expressions inside double-quoted strings using
|
||||
# Ruby-like notation for substitution of arbitrary expressions.
|
||||
@@ -549,7 +559,7 @@ IDENTIFIER = /// ^
|
||||
///
|
||||
NUMBER = /^0x[\da-f]+|^(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?/i
|
||||
HEREDOC = /^("""|''')([\s\S]*?)(?:\n[ \t]*)?\1/
|
||||
OPERATOR = /// ^ (?: -[-=>]? | \+[+=]? | \.\.\.? | [*&|/%=<>^:!?]+ ) ///
|
||||
OPERATOR = /// ^ (?: -[-=>]? | \+[+=]? | \.{3} | [*&|/%=<>^:!?]+ ) ///
|
||||
WHITESPACE = /^[ \t]+/
|
||||
COMMENT = /^###([^#][\s\S]*?)(?:###[ \t]*\n|(?:###)?$)|^(?:\s*#(?!##[^#]).*)+/
|
||||
CODE = /^[-=]>/
|
||||
@@ -579,7 +589,7 @@ MULTILINER = /\n/g
|
||||
HEREDOC_INDENT = /\n+([ \t]*)/g
|
||||
ASSIGNED = /^\s*@?[$A-Za-z_][$\w]*[ \t]*?[:=][^:=>]/
|
||||
NEXT_CHARACTER = /^\s*(\S?)/
|
||||
NEXT_ELLIPSIS = /^\s*\.\.\.?/
|
||||
NEXT_ELLIPSIS = /^\s*\.{3}/
|
||||
LEADING_SPACES = /^\s+/
|
||||
TRAILING_SPACES = /\s+$/
|
||||
NO_NEWLINE = /// ^
|
||||
|
||||
239
src/nodes.coffee
239
src/nodes.coffee
@@ -6,7 +6,9 @@
|
||||
{Scope} = require './scope'
|
||||
|
||||
# Import the helpers we plan to use.
|
||||
{compact, flatten, merge, del, starts, ends, last} = require './helpers'
|
||||
{compact, flatten, extend, merge, del, starts, ends, last} = require './helpers'
|
||||
|
||||
exports.extend = extend # for parser
|
||||
|
||||
# Constant functions for nodes that don't need customization.
|
||||
YES = -> yes
|
||||
@@ -73,6 +75,14 @@ exports.Base = class Base
|
||||
@parenthetical = on
|
||||
@compile o
|
||||
|
||||
# Compile to a source/variable pair suitable for looping.
|
||||
compileLoopReference: (o, name) ->
|
||||
src = tmp = @compile o
|
||||
unless NUMBER.test(src) or
|
||||
IDENTIFIER.test(src) and o.scope.check(src, immediate: on)
|
||||
src = "#{ tmp = o.scope.freeVariable name } = #{src}"
|
||||
[src, tmp]
|
||||
|
||||
# Convenience method to grab the current indentation level, plus tabbing in.
|
||||
idt: (tabs) ->
|
||||
(@tab or '') + Array((tabs or 0) + 1).join TAB
|
||||
@@ -329,9 +339,6 @@ exports.Value = class Value extends Base
|
||||
isObject: ->
|
||||
@base instanceof ObjectLiteral and not @properties.length
|
||||
|
||||
isSplice: ->
|
||||
last(@properties) instanceof Slice
|
||||
|
||||
isComplex: ->
|
||||
@base.isComplex() or @hasProperties()
|
||||
|
||||
@@ -590,96 +597,6 @@ exports.Index = class Index extends Base
|
||||
|
||||
isComplex: -> @index.isComplex()
|
||||
|
||||
#### Range
|
||||
|
||||
# A range literal. Ranges can be used to extract portions (slices) of arrays,
|
||||
# to specify a range for comprehensions, or as a value, to be expanded into the
|
||||
# corresponding array of integers at runtime.
|
||||
exports.Range = class Range extends Base
|
||||
|
||||
children: ['from', 'to']
|
||||
|
||||
constructor: (@from, @to, tag) ->
|
||||
super()
|
||||
@exclusive = tag is 'exclusive'
|
||||
@equals = if @exclusive then '' else '='
|
||||
|
||||
# Compiles the range's source variables -- where it starts and where it ends.
|
||||
# But only if they need to be cached to avoid double evaluation.
|
||||
compileVariables: (o) ->
|
||||
o = merge(o, top: true)
|
||||
[@from, @fromVar] = @from.compileReference o, precompile: yes
|
||||
[@to, @toVar] = @to.compileReference o, precompile: yes
|
||||
[@fromNum, @toNum] = [@fromVar.match(SIMPLENUM), @toVar.match(SIMPLENUM)]
|
||||
parts = []
|
||||
parts.push @from if @from isnt @fromVar
|
||||
parts.push @to if @to isnt @toVar
|
||||
|
||||
# When compiled normally, the range returns the contents of the *for loop*
|
||||
# needed to iterate over the values in the range. Used by comprehensions.
|
||||
compileNode: (o) ->
|
||||
@compileVariables o
|
||||
return @compileArray(o) unless o.index
|
||||
return @compileSimple(o) if @fromNum and @toNum
|
||||
idx = del o, 'index'
|
||||
step = del o, 'step'
|
||||
vars = "#{idx} = #{@from}" + if @to isnt @toVar then ", #{@to}" else ''
|
||||
intro = "(#{@fromVar} <= #{@toVar} ? #{idx}"
|
||||
compare = "#{intro} <#{@equals} #{@toVar} : #{idx} >#{@equals} #{@toVar})"
|
||||
stepPart = if step then step.compile(o) else '1'
|
||||
incr = if step then "#{idx} += #{stepPart}" else "#{intro} += #{stepPart} : #{idx} -= #{stepPart})"
|
||||
"#{vars}; #{compare}; #{incr}"
|
||||
|
||||
# Compile a simple range comprehension, with integers.
|
||||
compileSimple: (o) ->
|
||||
[from, to] = [+@fromNum, +@toNum]
|
||||
idx = del o, 'index'
|
||||
step = del o, 'step'
|
||||
step and= "#{idx} += #{step.compile(o)}"
|
||||
if from <= to
|
||||
"#{idx} = #{from}; #{idx} <#{@equals} #{to}; #{step or "#{idx}++"}"
|
||||
else
|
||||
"#{idx} = #{from}; #{idx} >#{@equals} #{to}; #{step or "#{idx}--"}"
|
||||
|
||||
# When used as a value, expand the range into the equivalent array.
|
||||
compileArray: (o) ->
|
||||
if @fromNum and @toNum and Math.abs(@fromNum - @toNum) <= 20
|
||||
range = [+@fromNum..+@toNum]
|
||||
range.pop() if @exclusive
|
||||
return "[#{ range.join(', ') }]"
|
||||
idt = @idt 1
|
||||
i = o.scope.freeVariable 'i'
|
||||
result = o.scope.freeVariable 'result'
|
||||
pre = "\n#{idt}#{result} = [];"
|
||||
if @fromNum and @toNum
|
||||
o.index = i
|
||||
body = @compileSimple o
|
||||
else
|
||||
vars = "#{i} = #{@from}" + if @to isnt @toVar then ", #{@to}" else ''
|
||||
clause = "#{@fromVar} <= #{@toVar} ?"
|
||||
body = "var #{vars}; #{clause} #{i} <#{@equals} #{@toVar} : #{i} >#{@equals} #{@toVar}; #{clause} #{i} += 1 : #{i} -= 1"
|
||||
post = "{ #{result}.push(#{i}); }\n#{idt}return #{result};\n#{o.indent}"
|
||||
"(function() {#{pre}\n#{idt}for (#{body})#{post}}).call(this)"
|
||||
|
||||
#### Slice
|
||||
|
||||
# An array slice literal. Unlike JavaScript's `Array#slice`, the second parameter
|
||||
# specifies the index of the end of the slice, just as the first parameter
|
||||
# is the index of the beginning.
|
||||
exports.Slice = class Slice extends Base
|
||||
|
||||
children: ['range']
|
||||
|
||||
constructor: (@range) ->
|
||||
super()
|
||||
|
||||
compileNode: (o) ->
|
||||
from = if @range.from then @range.from.compile(o) else '0'
|
||||
to = if @range.to then @range.to.compile(o) else ''
|
||||
to += if not to or @range.exclusive then '' else ' + 1'
|
||||
to = ', ' + to if to
|
||||
".slice(#{from}#{to})"
|
||||
|
||||
#### ObjectLiteral
|
||||
|
||||
# An object literal, nothing fancy.
|
||||
@@ -863,7 +780,6 @@ exports.Assign = class Assign extends Base
|
||||
compileNode: (o) ->
|
||||
if isValue = @variable instanceof Value
|
||||
return @compilePatternMatch(o) if @variable.isArray() or @variable.isObject()
|
||||
return @compileSplice(o) if @variable.isSplice()
|
||||
if ifn = If.unfoldSoak o, this, 'variable'
|
||||
delete o.top
|
||||
return ifn.compile o
|
||||
@@ -933,18 +849,6 @@ exports.Assign = class Assign extends Base
|
||||
code = assigns.join ', '
|
||||
if top or @parenthetical then code else "(#{code})"
|
||||
|
||||
# Compile the assignment from an array splice literal, using JavaScript's
|
||||
# `Array#splice` method.
|
||||
compileSplice: (o) ->
|
||||
{range} = @variable.properties.pop()
|
||||
name = @variable.compile o
|
||||
plus = if range.exclusive then '' else ' + 1'
|
||||
from = if range.from then range.from.compile(o) else '0'
|
||||
to = if range.to then range.to.compile(o) + ' - ' + from + plus else "#{name}.length"
|
||||
ref = o.scope.freeVariable 'ref'
|
||||
val = @value.compile(o)
|
||||
"([].splice.apply(#{name}, [#{from}, #{to}].concat(#{ref} = #{val})), #{ref})"
|
||||
|
||||
# When compiling a conditional assignment, take care to ensure that the
|
||||
# operands are only evaluated once, even though we have to reference them
|
||||
# more than once.
|
||||
@@ -1399,19 +1303,17 @@ exports.Parens = class Parens extends Base
|
||||
# you can map and filter in a single pass.
|
||||
exports.For = class For extends Base
|
||||
|
||||
children: ['body', 'source', 'guard']
|
||||
children: ['body', 'source', 'guard', 'step', 'from', 'to']
|
||||
|
||||
topSensitive: YES
|
||||
isStatement : YES
|
||||
|
||||
constructor: (@body, source, @name, @index) ->
|
||||
constructor: (@body, head) ->
|
||||
if head.index instanceof Value
|
||||
throw SyntaxError 'index cannot be a pattern matching expression'
|
||||
super()
|
||||
{@source, @guard, @step} = source
|
||||
@raw = !!source.raw
|
||||
@object = !!source.object
|
||||
[@name, @index] = [@index, @name] if @object
|
||||
throw SyntaxError 'index cannot be a pattern matching expression' if @index instanceof Value
|
||||
@range = @source instanceof Value and @source.base instanceof Range and not @source.properties.length
|
||||
extend this, head
|
||||
@step or= new Literal 1 unless @object
|
||||
@pattern = @name instanceof Value
|
||||
throw SyntaxError 'cannot pattern match a range loop' if @range and @pattern
|
||||
@returns = false
|
||||
@@ -1430,71 +1332,64 @@ exports.For = class For extends Base
|
||||
# comprehensions. Some of the generated code can be shared in common, and
|
||||
# some cannot.
|
||||
compileNode: (o) ->
|
||||
topLevel = del(o, 'top') and not @returns
|
||||
source = if @range then @source.base else @source
|
||||
codeInBody = not @body.containsPureStatement() and @body.contains (node) -> node instanceof Code
|
||||
scope = o.scope
|
||||
name = @name and @name.compile o
|
||||
index = @index and @index.compile o
|
||||
scope.find(name, immediate: yes) if name and not @pattern and (@range or not codeInBody)
|
||||
{scope} = o
|
||||
top = del(o, 'top') and not @returns
|
||||
name = not @pattern and @name?.compile o
|
||||
index = @index?.compile o
|
||||
ivar = if not index then scope.freeVariable 'i' else index
|
||||
varPart = ''
|
||||
body = Expressions.wrap [@body]
|
||||
idt = @idt 1
|
||||
scope.find(name, immediate: yes) if name
|
||||
scope.find(index, immediate: yes) if index
|
||||
rvar = scope.freeVariable 'result' unless topLevel
|
||||
ivar = if @range then name else index
|
||||
ivar = scope.freeVariable 'i' if not ivar or codeInBody
|
||||
nvar = scope.freeVariable 'i' if name and not @range and codeInBody
|
||||
varPart = ''
|
||||
guardPart = ''
|
||||
unstepPart = ''
|
||||
body = Expressions.wrap [@body]
|
||||
idt1 = @idt 1
|
||||
if @range
|
||||
forPart = source.compile merge o, {index: ivar, @step}
|
||||
[step, pvar] = @step.compileLoopReference o, 'step' if @step
|
||||
if @from
|
||||
[tail, tvar] = @to.compileLoopReference o, 'to'
|
||||
vars = "#{ivar} = #{ @from.compile o }"
|
||||
vars += ", #{tail}" if tail isnt tvar
|
||||
cond = if +pvar
|
||||
"#{ivar} #{ if pvar < 0 then '>' else '<' }= #{tvar}"
|
||||
else
|
||||
"#{pvar} < 0 ? #{ivar} >= #{tvar} : #{ivar} <= #{tvar}"
|
||||
else
|
||||
svar = sourcePart = @source.compile o
|
||||
if (name or not @raw) and
|
||||
not (IDENTIFIER.test(svar) and scope.check svar, immediate: on)
|
||||
sourcePart = "#{ref = scope.freeVariable 'ref'} = #{svar}"
|
||||
sourcePart = "(#{sourcePart})" unless @object
|
||||
svar = ref
|
||||
if name or not @raw
|
||||
[sourcePart, svar] = @source.compileLoopReference o, 'ref'
|
||||
sourcePart = "(#{sourcePart})" unless sourcePart is svar or @object
|
||||
else
|
||||
sourcePart = svar = @source.compile o
|
||||
namePart = if @pattern
|
||||
new Assign(@name, new Literal "#{svar}[#{ivar}]").compile merge o, top: on
|
||||
else if name
|
||||
"#{name} = #{svar}[#{ivar}]"
|
||||
unless @object
|
||||
lvar = scope.freeVariable 'len'
|
||||
stepPart = if @step then "#{ivar} += #{ @step.compile(o) }" else "#{ivar}++"
|
||||
forPart = "#{ivar} = 0, #{lvar} = #{sourcePart}.length; #{ivar} < #{lvar}; #{stepPart}"
|
||||
resultPart = if rvar then "#{@tab}#{rvar} = [];\n" else ''
|
||||
returnResult = @compileReturnValue rvar, o
|
||||
body = Push.wrap rvar, body unless topLevel
|
||||
if @guard
|
||||
body = Expressions.wrap [new If @guard, body]
|
||||
if codeInBody
|
||||
body.unshift new Literal "var #{name} = #{ivar}" if @range
|
||||
body.unshift new Literal "var #{namePart}" if namePart
|
||||
body.unshift new Literal "var #{index} = #{ivar}" if index
|
||||
lastLine = body.expressions.pop()
|
||||
body.push new Assign new Literal(ivar), new Literal index if index
|
||||
body.push new Assign new Literal(nvar), new Literal name if nvar
|
||||
body.push lastLine
|
||||
o.indent = @idt 1
|
||||
body = Expressions.wrap [new Literal body.compile o]
|
||||
body.push new Assign @index, new Literal ivar if index
|
||||
body.push new Assign @name, new Literal nvar or ivar if name
|
||||
else
|
||||
varPart = "#{idt1}#{namePart};\n" if namePart
|
||||
if forPart and name is ivar
|
||||
unstepPart = if @step then "#{name} -= #{ @step.compile(o) };" else "#{name}--;"
|
||||
unstepPart = "\n#{@tab}" + unstepPart
|
||||
if 0 > pvar and (pvar | 0) is +pvar # negative int
|
||||
vars = "#{ivar} = #{sourcePart}.length - 1"
|
||||
cond = "#{ivar} >= 0"
|
||||
else
|
||||
lvar = scope.freeVariable 'len'
|
||||
vars = "#{ivar} = 0, #{lvar} = #{sourcePart}.length"
|
||||
cond = "#{ivar} < #{lvar}"
|
||||
if @object
|
||||
forPart = "#{ivar} in #{sourcePart}"
|
||||
guardPart = "\n#{idt1}if (!#{utility('hasProp')}.call(#{svar}, #{ivar})) continue;" unless @raw
|
||||
body = body.compile merge o, indent: idt1, top: true
|
||||
vars = if @range then name else "#{name}, #{ivar}"
|
||||
forPart = "#{ivar} in #{sourcePart}"
|
||||
guardPart = not @raw and
|
||||
"#{idt}if (!#{ utility 'hasProp' }.call(#{svar}, #{ivar})) continue;\n"
|
||||
else
|
||||
vars += ", #{step}" if step isnt pvar
|
||||
forPart = "#{vars}; #{cond}; " + switch +pvar
|
||||
when 1 then '++' + ivar
|
||||
when -1 then '--' + ivar
|
||||
else ivar + if pvar < 0 then ' -= ' + pvar.slice 1 else ' += ' + pvar
|
||||
unless top
|
||||
rvar = scope.freeVariable 'result'
|
||||
resultDef = "#{@tab}#{rvar} = [];\n"
|
||||
resultRet = @compileReturnValue rvar, o
|
||||
body = Push.wrap rvar, body
|
||||
body = Expressions.wrap [new If @guard, body] if @guard
|
||||
varPart = "#{idt}#{namePart};\n" if namePart
|
||||
"""
|
||||
#{resultPart}#{@tab}for (#{forPart}) {#{guardPart}
|
||||
#{varPart}#{body}
|
||||
#{@tab}}#{unstepPart}#{returnResult}
|
||||
#{ resultDef or '' }#{@tab}for (#{forPart}) {
|
||||
#{ guardPart or '' }#{varPart}#{ body.compile merge o, indent: idt, top: on }
|
||||
#{@tab}}#{ resultRet or '' }
|
||||
"""
|
||||
|
||||
#### Switch
|
||||
@@ -1703,7 +1598,7 @@ TAB = ' '
|
||||
TRAILING_WHITESPACE = /[ \t]+$/gm
|
||||
|
||||
IDENTIFIER = /^[$A-Za-z_][$\w]*$/
|
||||
NUMBER = /^0x[\da-f]+$|^(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?$/i
|
||||
NUMBER = /// ^ -? (?: 0x[\da-f]+ | (?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)? ) $ ///i
|
||||
SIMPLENUM = /^[+-]?\d+$/
|
||||
|
||||
# Is a literal value a string?
|
||||
|
||||
@@ -177,7 +177,7 @@ ok obj.func().name is 'Dog'
|
||||
class Mini
|
||||
num: 10
|
||||
generate: =>
|
||||
for i in [1..3]
|
||||
for i from 1 to 3
|
||||
=>
|
||||
@num
|
||||
|
||||
|
||||
@@ -15,37 +15,15 @@ ok odds.join(' ') is "one! three!"
|
||||
|
||||
|
||||
# Basic range comprehensions.
|
||||
nums = i * 3 for i in [1..3]
|
||||
|
||||
negs = x for x in [-20..-5*2]
|
||||
negs = negs[0..2]
|
||||
|
||||
result = nums.concat(negs).join(', ')
|
||||
|
||||
ok result is '3, 6, 9, -20, -19, -18'
|
||||
ok i is 3
|
||||
ok x is -10
|
||||
nums = i * 3 for i from 1 to 3
|
||||
negs = x for x from -20 to -5*2
|
||||
eq nums.concat(negs.slice 0, 3).join(' '), '3 6 9 -20 -19 -18'
|
||||
|
||||
|
||||
# With range comprehensions, you can loop in steps.
|
||||
results = x for x in [0...15] by 5
|
||||
ok results.join(' ') is '0 5 10'
|
||||
|
||||
results = x for x in [0..100] by 10
|
||||
ok results.join(' ') is '0 10 20 30 40 50 60 70 80 90 100'
|
||||
|
||||
|
||||
# And can loop downwards, with a negative step.
|
||||
results = x for x in [5..1]
|
||||
|
||||
ok results.join(' ') is '5 4 3 2 1'
|
||||
ok results.join(' ') is [(10-5)..(-2+3)].join(' ')
|
||||
|
||||
results = x for x in [10..1]
|
||||
ok results.join(' ') is [10..1].join(' ')
|
||||
|
||||
results = x for x in [10...0] by -2
|
||||
ok results.join(' ') is [10, 8, 6, 4, 2].join(' ')
|
||||
eq "#{ x for x from 0 to 9 by 3 }", '0,3,6,9'
|
||||
eq "#{ x for x from 9 to 0 by -3 }", '9,6,3,0'
|
||||
eq "#{ x for x from 3*3 to 0*0 by 0-3 }", '9,6,3,0'
|
||||
|
||||
|
||||
# Multiline array comprehension with filter.
|
||||
@@ -53,77 +31,29 @@ evens = for num in [1, 2, 3, 4, 5, 6] when num % 2 is 0
|
||||
num *= -1
|
||||
num -= 2
|
||||
num * -1
|
||||
eq evens + '', '4,6,8'
|
||||
|
||||
ok evens.join(', ') is '4, 6, 8'
|
||||
|
||||
# Backward traversing.
|
||||
odds = num for num in [0, 1, 2, 3, 4, 5] by -2
|
||||
eq odds + '', '5,3,1'
|
||||
|
||||
|
||||
# The in operator still works, standalone.
|
||||
ok 2 of evens
|
||||
|
||||
|
||||
# Ensure that the closure wrapper preserves local variables.
|
||||
obj = {}
|
||||
|
||||
for method in ['one', 'two', 'three']
|
||||
obj[method] = ->
|
||||
"I'm " + method
|
||||
|
||||
ok obj.one() is "I'm one"
|
||||
ok obj.two() is "I'm two"
|
||||
ok obj.three() is "I'm three"
|
||||
|
||||
i = 0
|
||||
for i in [1..3]
|
||||
-> 'func'
|
||||
break if false
|
||||
ok i is 3
|
||||
|
||||
|
||||
# Ensure that local variables are closed over for range comprehensions.
|
||||
funcs = for i in [1..3]
|
||||
-> -i
|
||||
|
||||
ok (func() for func in funcs).join(' ') is '-1 -2 -3'
|
||||
ok i is 3
|
||||
|
||||
|
||||
# Ensure that closing over local variables doesn't break scoping laws.
|
||||
for i in [0]
|
||||
count = 0
|
||||
i = 50
|
||||
->
|
||||
ok count is 0
|
||||
ok i is 50
|
||||
|
||||
for [a, b] in [[0, 1]] then ->
|
||||
ok a is 0
|
||||
ok b is 1
|
||||
|
||||
|
||||
# Even when referenced in the filter.
|
||||
list = ['one', 'two', 'three']
|
||||
|
||||
methods = for num, i in list when num isnt 'two' and i isnt 1
|
||||
-> num + ' ' + i
|
||||
|
||||
ok methods.length is 2
|
||||
ok methods[0]() is 'one 0'
|
||||
ok methods[1]() is 'three 2'
|
||||
|
||||
|
||||
# Naked ranges are expanded into arrays.
|
||||
array = [0..10]
|
||||
ok(num % 2 is 0 for num in array by 2)
|
||||
# all/from/to aren't reserved.
|
||||
all = from = to = 1
|
||||
|
||||
|
||||
# Nested comprehensions.
|
||||
multiLiner =
|
||||
for x in [3..5]
|
||||
for y in [3..5]
|
||||
for x from 3 to 5
|
||||
for y from 3 to 5
|
||||
[x, y]
|
||||
|
||||
singleLiner =
|
||||
[x, y] for y in [3..5] for x in [3..5]
|
||||
[x, y] for y from 3 to 5 for x from 3 to 5
|
||||
|
||||
ok multiLiner.length is singleLiner.length
|
||||
ok 5 is multiLiner[2][2][1]
|
||||
@@ -159,11 +89,6 @@ ok own.join(' ') is 'Whiskers'
|
||||
ok all.sort().join(' ') is 'Whiskers cream tabby'
|
||||
|
||||
|
||||
# Optimized range comprehensions.
|
||||
exxes = 'x' for [0...10]
|
||||
ok exxes.join(' ') is 'x x x x x x x x x x'
|
||||
|
||||
|
||||
# Comprehensions safely redeclare parameters if they're not present in closest
|
||||
# scope.
|
||||
rule = (x) -> x
|
||||
@@ -173,4 +98,4 @@ learn = ->
|
||||
|
||||
ok learn().join(' ') is '1 2 3'
|
||||
|
||||
ok rule(101) is 101
|
||||
ok rule(101) is 101
|
||||
|
||||
@@ -62,11 +62,6 @@ ok Math.AnonymousAdd(10, 10) is 20
|
||||
ok Math.FastAdd(20, 20) is 40
|
||||
|
||||
|
||||
# Parens are optional on simple function calls.
|
||||
ok 100 > 1 if 1 > 0
|
||||
ok true unless false
|
||||
ok true for i in [1..3]
|
||||
|
||||
okFunc = (f) -> ok(f())
|
||||
okFunc -> true
|
||||
|
||||
@@ -122,7 +117,7 @@ mult = (x, mids..., y) ->
|
||||
|
||||
ok mult(1, 2,) is 2
|
||||
ok mult(1, 2, 3,) is 6
|
||||
ok mult(10,[1..6]...,) is 7200
|
||||
ok mult(10, (i for i from 1 to 6)...) is 7200
|
||||
|
||||
|
||||
# Test for inline functions with parentheses and implicit calls.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{starts, ends, compact, count, merge, extend, flatten, del, last} = CoffeeScript.helpers
|
||||
|
||||
array = [0..4]
|
||||
array = [0, 1, 2, 3, 4]
|
||||
string = array.join ''
|
||||
object = {}
|
||||
|
||||
|
||||
@@ -14,10 +14,6 @@ ok value is 1
|
||||
value = 0.0 + -.25 - -.75 + 0.0
|
||||
ok value is 0.5
|
||||
|
||||
# Decimals don't interfere with ranges.
|
||||
ok [0..10].join(' ') is '0 1 2 3 4 5 6 7 8 9 10'
|
||||
ok [0...10].join(' ') is '0 1 2 3 4 5 6 7 8 9'
|
||||
|
||||
|
||||
# Can call methods directly on numbers.
|
||||
4.valueOf() is 4
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
# Slice.
|
||||
array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
|
||||
a = array[7..9]
|
||||
b = array[2...4]
|
||||
|
||||
result = a.concat(b).join(' ')
|
||||
|
||||
ok result is "7 8 9 2 3"
|
||||
|
||||
a = [0, 1, 2, 3, 4, 5, 6, 7]
|
||||
eq a[2...6].join(' '), '2 3 4 5'
|
||||
|
||||
|
||||
# Ranges.
|
||||
countdown = [10..1].join(' ')
|
||||
ok countdown is "10 9 8 7 6 5 4 3 2 1"
|
||||
|
||||
a = 1
|
||||
b = 5
|
||||
nums = [a...b]
|
||||
ok nums.join(' ') is '1 2 3 4'
|
||||
|
||||
b = -5
|
||||
nums = [a..b]
|
||||
ok nums.join(' ') is '1 0 -1 -2 -3 -4 -5'
|
||||
|
||||
|
||||
# Expression-based range.
|
||||
array = [(1+5)..1+9]
|
||||
ok array.join(' ') is "6 7 8 9 10"
|
||||
|
||||
array = [5..1]
|
||||
ok array.join(' ') is '5 4 3 2 1'
|
||||
|
||||
array = [30...0]
|
||||
ok (len = array.length) is 30
|
||||
ok array[len - 1] is 1
|
||||
|
||||
|
||||
|
||||
# String slicing (at least on Node).
|
||||
hello = "Hello World"
|
||||
|
||||
ok hello[1...1] is ""
|
||||
ok hello[1..1] is "e"
|
||||
ok hello[1...5] is "ello"
|
||||
ok hello[0..4] is "Hello"
|
||||
|
||||
|
||||
# Splice literals.
|
||||
array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
|
||||
array[5..10] = [0, 0, 0]
|
||||
|
||||
ok array.join(' ') is '0 1 2 3 4 0 0 0'
|
||||
|
||||
|
||||
# Slices and splices that omit their beginning or end.
|
||||
array = [0..10]
|
||||
|
||||
ok array[7..].join(' ') is '7 8 9 10'
|
||||
ok array[-2..].join(' ') is '9 10'
|
||||
|
||||
ok array[...3].join(' ') is '0 1 2'
|
||||
ok array[..-5].join(' ') is '0 1 2 3 4 5 6'
|
||||
|
||||
array[3..] = [9, 8, 7]
|
||||
|
||||
ok array.join(' ') is '0 1 2 9 8 7'
|
||||
|
||||
array[...3] = [7, 8, 9]
|
||||
|
||||
ok array.join(' ') is '7 8 9 9 8 7'
|
||||
|
||||
@@ -37,7 +37,7 @@ ok last is "Usain Bolt"
|
||||
ok theField.length is 8
|
||||
|
||||
contenders.reverse()
|
||||
medalists contenders[0...2]..., "Mighty Mouse", contenders[2...contenders.length]...
|
||||
medalists contenders.slice(0, 2)..., "Mighty Mouse", contenders.slice(2)...
|
||||
|
||||
ok gold is "Usain Bolt"
|
||||
ok silver is "Asafa Powell"
|
||||
@@ -69,7 +69,7 @@ crowd = [
|
||||
|
||||
bests = [
|
||||
"Mighty Mouse"
|
||||
contenders[0..3]...
|
||||
contenders.slice(0, 4)...
|
||||
]
|
||||
|
||||
ok crowd[0] is contenders[0]
|
||||
|
||||
Reference in New Issue
Block a user