Merge pull request #1378 update coffeeScript

This commit is contained in:
nightwing 2013-04-18 18:09:10 +04:00
commit 758f500b4a
13 changed files with 1965 additions and 1403 deletions

View file

@ -1951,12 +1951,13 @@ var requirejs, require, define;
//This module may not have dependencies //This module may not have dependencies
if (!isArray(deps)) { if (!isArray(deps)) {
callback = deps; callback = deps;
deps = []; deps = null;
} }
//If no name, and callback is a function, then figure out if it a //If no name, and callback is a function, then figure out if it a
//CommonJS thing with dependencies. //CommonJS thing with dependencies.
if (!deps.length && isFunction(callback)) { if (!deps && isFunction(callback)) {
deps = [];
//Remove comments from the callback string, //Remove comments from the callback string,
//look for require calls, and pull them into the dependencies, //look for require calls, and pull them into the dependencies,
//but only if there are function args. //but only if there are function args.

View file

@ -3,7 +3,7 @@
* *
* Copyright (c) 2010, Ajax.org B.V. * Copyright (c) 2010, Ajax.org B.V.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright * * Redistributions of source code must retain the above copyright
@ -14,7 +14,7 @@
* * Neither the name of Ajax.org B.V. nor the * * Neither the name of Ajax.org B.V. nor the
* names of its contributors may be used to endorse or promote products * names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission. * derived from this software without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@ -27,17 +27,23 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
* ***** END LICENSE BLOCK ***** */ * ***** END LICENSE BLOCK ***** */
define(function(require, exports, module) { define(function(require, exports, module) {
var Lexer = require("./lexer").Lexer; var Lexer = require("./lexer").Lexer;
var parser = require("./parser"); var parser = require("./parser");
var lexer = new Lexer(); var lexer = new Lexer();
parser.lexer = { parser.lexer = {
lex: function() { lex: function() {
var tag, _ref2; var tag, token;
_ref2 = this.tokens[this.pos++] || [''], tag = _ref2[0], this.yytext = _ref2[1], this.yylineno = _ref2[2]; token = this.tokens[this.pos++];
if (token) {
tag = token[0], this.yytext = token[1], this.yylloc = token[2];
this.yylineno = this.yylloc.first_line;
} else {
tag = '';
}
return tag; return tag;
}, },
setInput: function(tokens) { setInput: function(tokens) {
@ -49,7 +55,7 @@ define(function(require, exports, module) {
} }
}; };
parser.yy = require('./nodes'); parser.yy = require('./nodes');
exports.parse = function(code) { exports.parse = function(code) {
return parser.parse(lexer.tokenize(code)); return parser.parse(lexer.tokenize(code));
}; };

View file

@ -1,5 +1,5 @@
/** /**
* Copyright (c) 2009-2012 Jeremy Ashkenas * Copyright (c) 2009-2013 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
@ -21,12 +21,13 @@
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE. * OTHER DEALINGS IN THE SOFTWARE.
*
*/ */
define(function(require, exports, module) { define(function(require, exports, module) {
// Generated by CoffeeScript 1.3.3 // Generated by CoffeeScript 1.6.2
var extend, flatten, _ref; var buildLocationData, extend, flatten, last, repeat, _ref;
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);
@ -38,6 +39,19 @@ define(function(require, exports, module) {
return literal === string.substr(string.length - len - (back || 0), len); return literal === string.substr(string.length - len - (back || 0), len);
}; };
exports.repeat = repeat = function(str, n) {
var res;
res = '';
while (n > 0) {
if (n & 1) {
res += str;
}
n >>>= 1;
str += str;
}
return res;
};
exports.compact = function(array) { exports.compact = function(array) {
var item, _i, _len, _results; var item, _i, _len, _results;
_results = []; _results = [];
@ -96,7 +110,7 @@ define(function(require, exports, module) {
return val; return val;
}; };
exports.last = function(array, back) { exports.last = last = function(array, back) {
return array[array.length - (back || 0) - 1]; return array[array.length - (back || 0) - 1];
}; };
@ -111,5 +125,127 @@ define(function(require, exports, module) {
return false; return false;
}; };
exports.invertLiterate = function(code) {
var line, lines, maybe_code;
maybe_code = true;
lines = (function() {
var _i, _len, _ref1, _results;
_ref1 = code.split('\n');
_results = [];
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
line = _ref1[_i];
if (maybe_code && /^([ ]{4}|[ ]{0,3}\t)/.test(line)) {
_results.push(line);
} else if (maybe_code = /^\s*$/.test(line)) {
_results.push(line);
} else {
_results.push('# ' + line);
}
}
return _results;
})();
return lines.join('\n');
};
buildLocationData = function(first, last) {
if (!last) {
return first;
} else {
return {
first_line: first.first_line,
first_column: first.first_column,
last_line: last.last_line,
last_column: last.last_column
};
}
};
exports.addLocationDataFn = function(first, last) {
return function(obj) {
if (((typeof obj) === 'object') && (!!obj['updateLocationDataIfMissing'])) {
obj.updateLocationDataIfMissing(buildLocationData(first, last));
}
return obj;
};
};
exports.locationDataToString = function(obj) {
var locationData;
if (("2" in obj) && ("first_line" in obj[2])) {
locationData = obj[2];
} else if ("first_line" in obj) {
locationData = obj;
}
if (locationData) {
return ("" + (locationData.first_line + 1) + ":" + (locationData.first_column + 1) + "-") + ("" + (locationData.last_line + 1) + ":" + (locationData.last_column + 1));
} else {
return "No location data";
}
};
exports.baseFileName = function(file, stripExt, useWinPathSep) {
var parts, pathSep;
if (stripExt == null) {
stripExt = false;
}
if (useWinPathSep == null) {
useWinPathSep = false;
}
pathSep = useWinPathSep ? /\\|\// : /\//;
parts = file.split(pathSep);
file = parts[parts.length - 1];
if (!stripExt) {
return file;
}
parts = file.split('.');
parts.pop();
if (parts[parts.length - 1] === 'coffee' && parts.length > 1) {
parts.pop();
}
return parts.join('.');
};
exports.isCoffee = function(file) {
return /\.((lit)?coffee|coffee\.md)$/.test(file);
};
exports.isLiterate = function(file) {
return /\.(litcoffee|coffee\.md)$/.test(file);
};
exports.throwSyntaxError = function(message, location) {
var error, _ref1, _ref2;
if ((_ref1 = location.last_line) == null) {
location.last_line = location.first_line;
}
if ((_ref2 = location.last_column) == null) {
location.last_column = location.first_column;
}
error = new SyntaxError(message);
error.location = location;
throw error;
};
exports.prettyErrorMessage = function(error, fileName, code, useColors) {
var codeLine, colorize, end, first_column, first_line, last_column, last_line, marker, message, start, _ref1;
if (!error.location) {
return error.stack || ("" + error);
}
_ref1 = error.location, first_line = _ref1.first_line, first_column = _ref1.first_column, last_line = _ref1.last_line, last_column = _ref1.last_column;
codeLine = code.split('\n')[first_line];
start = first_column;
end = first_line === last_line ? last_column + 1 : codeLine.length;
marker = repeat(' ', start) + repeat('^', end - start);
if (useColors) {
colorize = function(str) {
return "\x1B[1;31m" + str + "\x1B[0m";
};
codeLine = codeLine.slice(0, start) + colorize(codeLine.slice(start, end)) + codeLine.slice(end);
marker = colorize(marker);
}
message = "" + fileName + ":" + (first_line + 1) + ":" + (first_column + 1) + ": error: " + error.message + "\n" + codeLine + "\n" + marker;
return message;
};
}); });

View file

@ -1,5 +1,5 @@
/** /**
* Copyright (c) 2009-2012 Jeremy Ashkenas * Copyright (c) 2009-2013 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
@ -21,42 +21,42 @@
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE. * OTHER DEALINGS IN THE SOFTWARE.
*
*/ */
define(function(require, exports, module) { define(function(require, exports, module) {
// Generated by CoffeeScript 1.3.3 // Generated by CoffeeScript 1.6.2
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, var BOM, 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, invertLiterate, key, last, locationDataToString, starts, throwSyntaxError, _ref, _ref1,
__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; }; __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; };
_ref = require('./rewriter'), Rewriter = _ref.Rewriter, INVERSES = _ref.INVERSES; _ref = require('./rewriter'), Rewriter = _ref.Rewriter, INVERSES = _ref.INVERSES;
_ref1 = require('./helpers'), count = _ref1.count, starts = _ref1.starts, compact = _ref1.compact, last = _ref1.last; _ref1 = require('./helpers'), count = _ref1.count, starts = _ref1.starts, compact = _ref1.compact, last = _ref1.last, invertLiterate = _ref1.invertLiterate, locationDataToString = _ref1.locationDataToString, throwSyntaxError = _ref1.throwSyntaxError;
exports.Lexer = Lexer = (function() { exports.Lexer = Lexer = (function() {
function Lexer() {} function Lexer() {}
Lexer.prototype.tokenize = function(code, opts) { Lexer.prototype.tokenize = function(code, opts) {
var i, tag; var consumed, i, tag, _ref2;
if (opts == null) { if (opts == null) {
opts = {}; opts = {};
} }
if (WHITESPACE.test(code)) { this.literate = opts.literate;
code = "\n" + code;
}
code = code.replace(/\r/g, '').replace(TRAILING_SPACES, '');
this.code = code;
this.line = opts.line || 0;
this.indent = 0; this.indent = 0;
this.indebt = 0; this.indebt = 0;
this.outdebt = 0; this.outdebt = 0;
this.indents = []; this.indents = [];
this.ends = []; this.ends = [];
this.tokens = []; this.tokens = [];
this.chunkLine = opts.line || 0;
this.chunkColumn = opts.column || 0;
code = this.clean(code);
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(); consumed = this.identifierToken() || this.commentToken() || this.whitespaceToken() || this.lineToken() || this.heredocToken() || this.stringToken() || this.numberToken() || this.regexToken() || this.jsToken() || this.literalToken();
_ref2 = this.getLineAndColumnFromChunk(consumed), this.chunkLine = _ref2[0], this.chunkColumn = _ref2[1];
i += consumed;
} }
this.closeIndentation(); this.closeIndentation();
if (tag = this.ends.pop()) { if (tag = this.ends.pop()) {
@ -68,17 +68,34 @@ define(function(require, exports, module) {
return (new Rewriter).rewrite(this.tokens); return (new Rewriter).rewrite(this.tokens);
}; };
Lexer.prototype.clean = function(code) {
if (code.charCodeAt(0) === BOM) {
code = code.slice(1);
}
code = code.replace(/\r/g, '').replace(TRAILING_SPACES, '');
if (WHITESPACE.test(code)) {
code = "\n" + code;
this.chunkLine--;
}
if (this.literate) {
code = invertLiterate(code);
}
return code;
};
Lexer.prototype.identifierToken = function() { Lexer.prototype.identifierToken = function() {
var colon, forcedIdentifier, id, input, match, prev, tag, _ref2, _ref3; var colon, colonOffset, forcedIdentifier, id, idLength, input, match, poppedToken, prev, tag, tagToken, _ref2, _ref3, _ref4;
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];
idLength = id.length;
poppedToken = void 0;
if (id === 'own' && this.tag() === 'FOR') { if (id === 'own' && this.tag() === 'FOR') {
this.token('OWN', id); this.token('OWN', id);
return id.length; return id.length;
} }
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 === '::' || _ref2 === '?::') || !prev.spaced && prev[0] === '@');
tag = 'IDENTIFIER'; tag = 'IDENTIFIER';
if (!forcedIdentifier && (__indexOf.call(JS_KEYWORDS, id) >= 0 || __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();
@ -97,7 +114,7 @@ define(function(require, exports, module) {
} else { } else {
tag = 'RELATION'; tag = 'RELATION';
if (this.value() === '!') { if (this.value() === '!') {
this.tokens.pop(); poppedToken = this.tokens.pop();
id = '!' + id; id = '!' + id;
} }
} }
@ -137,9 +154,13 @@ define(function(require, exports, module) {
} }
})(); })();
} }
this.token(tag, id); tagToken = this.token(tag, id, 0, idLength);
if (poppedToken) {
_ref4 = [poppedToken[2].first_line, poppedToken[2].first_column], tagToken[2].first_line = _ref4[0], tagToken[2].first_column = _ref4[1];
}
if (colon) { if (colon) {
this.token(':', ':'); colonOffset = input.lastIndexOf(':');
this.token(':', ':', colonOffset, colon.length);
} }
return input.length; return input.length;
}; };
@ -166,7 +187,7 @@ define(function(require, exports, module) {
if (binaryLiteral = /^0b([01]+)/.exec(number)) { if (binaryLiteral = /^0b([01]+)/.exec(number)) {
number = '0x' + (parseInt(binaryLiteral[1], 2)).toString(16); number = '0x' + (parseInt(binaryLiteral[1], 2)).toString(16);
} }
this.token('NUMBER', number); this.token('NUMBER', number, 0, lexedLength);
return lexedLength; return lexedLength;
}; };
@ -177,16 +198,20 @@ define(function(require, exports, module) {
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')); string = match[0];
this.token('STRING', string.replace(MULTILINER, '\\\n'), 0, string.length);
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), {
strOffset: 1,
lexedLength: string.length
});
} else { } else {
this.token('STRING', this.escapeLines(string)); this.token('STRING', this.escapeLines(string, 0, string.length));
} }
break; break;
default: default:
@ -195,7 +220,6 @@ define(function(require, exports, module) {
if (octalEsc = /^(?:\\.|[^\\])*\\(?:0[0-7]|[1-7])/.test(string)) { if (octalEsc = /^(?:\\.|[^\\])*\\(?:0[0-7]|[1-7])/.test(string)) {
this.error("octal escape sequences " + string + " are not allowed"); this.error("octal escape sequences " + string + " are not allowed");
} }
this.line += count(string, '\n');
return string.length; return string.length;
}; };
@ -212,12 +236,13 @@ define(function(require, exports, module) {
}); });
if (quote === '"' && 0 <= doc.indexOf('#{')) { if (quote === '"' && 0 <= doc.indexOf('#{')) {
this.interpolateString(doc, { this.interpolateString(doc, {
heredoc: true heredoc: true,
strOffset: 3,
lexedLength: heredoc.length
}); });
} else { } else {
this.token('STRING', this.makeString(doc, quote, true)); this.token('STRING', this.makeString(doc, quote, true), 0, heredoc.length);
} }
this.line += count(heredoc, '\n');
return heredoc.length; return heredoc.length;
}; };
@ -231,9 +256,8 @@ define(function(require, exports, module) {
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(' ')
})); }), 0, comment.length);
} }
this.line += count(comment, '\n');
return comment.length; return comment.length;
}; };
@ -242,8 +266,7 @@ define(function(require, exports, module) {
if (!(this.chunk.charAt(0) === '`' && (match = JSTOKEN.exec(this.chunk)))) { if (!(this.chunk.charAt(0) === '`' && (match = JSTOKEN.exec(this.chunk)))) {
return 0; return 0;
} }
this.token('JS', (script = match[0]).slice(1, -1)); this.token('JS', (script = match[0]).slice(1, -1), 0, script.length);
this.line += count(script, '\n');
return script.length; return script.length;
}; };
@ -254,7 +277,6 @@ define(function(require, exports, module) {
} }
if (match = HEREGEX.exec(this.chunk)) { if (match = HEREGEX.exec(this.chunk)) {
length = this.heregexToken(match); length = this.heregexToken(match);
this.line += count(match[0], '\n');
return length; return length;
} }
prev = last(this.tokens); prev = last(this.tokens);
@ -271,49 +293,60 @@ define(function(require, exports, module) {
if (regex === '//') { if (regex === '//') {
regex = '/(?:)/'; regex = '/(?:)/';
} }
this.token('REGEX', "" + regex + flags); this.token('REGEX', "" + regex + flags, 0, match.length);
return match.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, flagsOffset, heregex, plusToken, prev, re, tag, token, tokens, value, _i, _len, _ref2, _ref3, _ref4;
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(/^\*/)) { if (re.match(/^\*/)) {
this.error('regular expressions cannot begin with `*`'); this.error('regular expressions cannot begin with `*`');
} }
this.token('REGEX', "/" + (re || '(?:)') + "/" + flags); this.token('REGEX', "/" + (re || '(?:)') + "/" + flags, 0, heregex.length);
return heregex.length; return heregex.length;
} }
this.token('IDENTIFIER', 'RegExp'); this.token('IDENTIFIER', 'RegExp', 0, 0);
this.tokens.push(['CALL_START', '(']); this.token('CALL_START', '(', 0, 0);
tokens = []; tokens = [];
_ref2 = this.interpolateString(body, { _ref2 = this.interpolateString(body, {
regex: true regex: true
}); });
for (_i = 0, _len = _ref2.length; _i < _len; _i++) { for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
_ref3 = _ref2[_i], tag = _ref3[0], value = _ref3[1]; token = _ref2[_i];
tag = token[0], value = token[1];
if (tag === 'TOKENS') { if (tag === 'TOKENS') {
tokens.push.apply(tokens, value); tokens.push.apply(tokens, value);
} else { } else if (tag === 'NEOSTRING') {
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)]); token[0] = 'STRING';
token[1] = this.makeString(value, '"', true);
tokens.push(token);
} else {
this.error("Unexpected " + tag);
} }
tokens.push(['+', '+']); prev = last(this.tokens);
plusToken = ['+', '+'];
plusToken[2] = prev[2];
tokens.push(plusToken);
} }
tokens.pop(); tokens.pop();
if (((_ref4 = tokens[0]) != null ? _ref4[0] : void 0) !== 'STRING') { if (((_ref3 = tokens[0]) != null ? _ref3[0] : void 0) !== 'STRING') {
this.tokens.push(['STRING', '""'], ['+', '+']); this.token('STRING', '""', 0, 0);
this.token('+', '+', 0, 0);
} }
(_ref5 = this.tokens).push.apply(_ref5, tokens); (_ref4 = this.tokens).push.apply(_ref4, tokens);
if (flags) { if (flags) {
this.tokens.push([',', ','], ['STRING', '"' + flags + '"']); flagsOffset = heregex.lastIndexOf(flags);
this.token(',', ',', flagsOffset, 0);
this.token('STRING', '"' + flags + '"', flagsOffset, flags.length);
} }
this.token(')', ')'); this.token(')', ')', heregex.length - 1, 0);
return heregex.length; return heregex.length;
}; };
@ -323,7 +356,6 @@ define(function(require, exports, module) {
return 0; return 0;
} }
indent = match[0]; indent = match[0];
this.line += count(indent, '\n');
this.seenFor = false; this.seenFor = false;
size = indent.length - 1 - indent.lastIndexOf('\n'); size = indent.length - 1 - indent.lastIndexOf('\n');
noNewlines = this.unfinished(); noNewlines = this.unfinished();
@ -331,7 +363,7 @@ define(function(require, exports, module) {
if (noNewlines) { if (noNewlines) {
this.suppressNewlines(); this.suppressNewlines();
} else { } else {
this.newlineToken(); this.newlineToken(0);
} }
return indent.length; return indent.length;
} }
@ -342,19 +374,19 @@ define(function(require, exports, module) {
return indent.length; return indent.length;
} }
diff = size - this.indent + this.outdebt; diff = size - this.indent + this.outdebt;
this.token('INDENT', diff); this.token('INDENT', diff, indent.length - size, size);
this.indents.push(diff); this.indents.push(diff);
this.ends.push('OUTDENT'); this.ends.push('OUTDENT');
this.outdebt = this.indebt = 0; this.outdebt = this.indebt = 0;
} else { } else {
this.indebt = 0; this.indebt = 0;
this.outdentToken(this.indent - size, noNewlines); this.outdentToken(this.indent - size, noNewlines, indent.length);
} }
this.indent = size; this.indent = size;
return indent.length; return indent.length;
}; };
Lexer.prototype.outdentToken = function(moveOut, noNewlines) { Lexer.prototype.outdentToken = function(moveOut, noNewlines, outdentLength) {
var dent, len; var dent, len;
while (moveOut > 0) { while (moveOut > 0) {
len = this.indents.length - 1; len = this.indents.length - 1;
@ -367,11 +399,11 @@ define(function(require, exports, module) {
this.outdebt -= this.indents[len]; this.outdebt -= this.indents[len];
moveOut -= this.indents[len]; moveOut -= this.indents[len];
} else { } else {
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.pair('OUTDENT');
this.token('OUTDENT', dent); this.token('OUTDENT', dent, 0, outdentLength);
} }
} }
if (dent) { if (dent) {
@ -381,7 +413,7 @@ define(function(require, exports, module) {
this.tokens.pop(); this.tokens.pop();
} }
if (!(this.tag() === 'TERMINATOR' || noNewlines)) { if (!(this.tag() === 'TERMINATOR' || noNewlines)) {
this.token('TERMINATOR', '\n'); this.token('TERMINATOR', '\n', outdentLength, 0);
} }
return this; return this;
}; };
@ -402,12 +434,12 @@ define(function(require, exports, module) {
} }
}; };
Lexer.prototype.newlineToken = function() { Lexer.prototype.newlineToken = function(offset) {
while (this.value() === ';') { while (this.value() === ';') {
this.tokens.pop(); this.tokens.pop();
} }
if (this.tag() !== 'TERMINATOR') { if (this.tag() !== 'TERMINATOR') {
this.token('TERMINATOR', '\n'); this.token('TERMINATOR', '\n', offset, 0);
} }
return this; return this;
}; };
@ -492,7 +524,7 @@ define(function(require, exports, module) {
if (HEREDOC_ILLEGAL.test(doc)) { if (HEREDOC_ILLEGAL.test(doc)) {
this.error("block comment cannot contain \"*/\", starting"); this.error("block comment cannot contain \"*/\", starting");
} }
if (doc.indexOf('\n') <= 0) { if (doc.indexOf('\n') < 0) {
return doc; return doc;
} }
} else { } else {
@ -581,11 +613,18 @@ define(function(require, exports, module) {
}; };
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, _i, _len, _ref2, _ref3, _ref4; var column, expr, heredoc, i, inner, interpolated, len, letter, lexedLength, line, locationToken, nested, offsetInChunk, pi, plusToken, popped, regex, rparen, strOffset, tag, token, 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, offsetInChunk = options.offsetInChunk, strOffset = options.strOffset, lexedLength = options.lexedLength;
offsetInChunk = offsetInChunk || 0;
strOffset = strOffset || 0;
lexedLength = lexedLength || str.length;
if (heredoc && str.length > 0 && str[0] === '\n') {
str = str.slice(1);
strOffset++;
}
tokens = []; tokens = [];
pi = 0; pi = 0;
i = -1; i = -1;
@ -598,22 +637,24 @@ define(function(require, exports, module) {
continue; continue;
} }
if (pi < i) { if (pi < i) {
tokens.push(['NEOSTRING', str.slice(pi, i)]); tokens.push(this.makeToken('NEOSTRING', str.slice(pi, i), strOffset + pi));
} }
inner = expr.slice(1, -1); inner = expr.slice(1, -1);
if (inner.length) { if (inner.length) {
_ref2 = this.getLineAndColumnFromChunk(strOffset + i + 1), line = _ref2[0], column = _ref2[1];
nested = new Lexer().tokenize(inner, { nested = new Lexer().tokenize(inner, {
line: this.line, line: line,
column: column,
rewrite: false rewrite: false
}); });
nested.pop(); popped = nested.pop();
if (((_ref2 = nested[0]) != null ? _ref2[0] : void 0) === 'TERMINATOR') { if (((_ref3 = nested[0]) != null ? _ref3[0] : void 0) === 'TERMINATOR') {
nested.shift(); popped = nested.shift();
} }
if (len = nested.length) { if (len = nested.length) {
if (len > 1) { if (len > 1) {
nested.unshift(['(', '(', this.line]); nested.unshift(this.makeToken('(', '(', strOffset + i + 1, 0));
nested.push([')', ')', this.line]); nested.push(this.makeToken(')', ')', strOffset + i + 1 + inner.length, 0));
} }
tokens.push(['TOKENS', nested]); tokens.push(['TOKENS', nested]);
} }
@ -622,33 +663,49 @@ define(function(require, exports, module) {
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(this.makeToken('NEOSTRING', str.slice(pi), strOffset + pi));
} }
if (regex) { if (regex) {
return tokens; return tokens;
} }
if (!tokens.length) { if (!tokens.length) {
return this.token('STRING', '""'); return this.token('STRING', '""', offsetInChunk, lexedLength);
} }
if (tokens[0][0] !== 'NEOSTRING') { if (tokens[0][0] !== 'NEOSTRING') {
tokens.unshift(['', '']); tokens.unshift(this.makeToken('NEOSTRING', '', offsetInChunk));
} }
if (interpolated = tokens.length > 1) { if (interpolated = tokens.length > 1) {
this.token('(', '('); this.token('(', '(', offsetInChunk, 0);
} }
for (i = _i = 0, _len = tokens.length; _i < _len; i = ++_i) { for (i = _i = 0, _len = tokens.length; _i < _len; i = ++_i) {
_ref3 = tokens[i], tag = _ref3[0], value = _ref3[1]; token = tokens[i];
tag = token[0], value = token[1];
if (i) { if (i) {
this.token('+', '+'); if (i) {
plusToken = this.token('+', '+');
}
locationToken = tag === 'TOKENS' ? value[0] : token;
plusToken[2] = {
first_line: locationToken[2].first_line,
first_column: locationToken[2].first_column,
last_line: locationToken[2].first_line,
last_column: locationToken[2].first_column
};
} }
if (tag === 'TOKENS') { if (tag === 'TOKENS') {
(_ref4 = this.tokens).push.apply(_ref4, value); (_ref4 = this.tokens).push.apply(_ref4, value);
} else if (tag === 'NEOSTRING') {
token[0] = 'STRING';
token[1] = this.makeString(value, '"', heredoc);
this.tokens.push(token);
} else { } else {
this.token('STRING', this.makeString(value, '"', heredoc)); this.error("Unexpected " + tag);
} }
} }
if (interpolated) { if (interpolated) {
this.token(')', ')'); rparen = this.makeToken(')', ')', offsetInChunk + lexedLength, 0);
rparen.stringEnd = true;
this.tokens.push(rparen);
} }
return tokens; return tokens;
}; };
@ -666,8 +723,48 @@ define(function(require, exports, module) {
return this.ends.pop(); return this.ends.pop();
}; };
Lexer.prototype.token = function(tag, value) { Lexer.prototype.getLineAndColumnFromChunk = function(offset) {
return this.tokens.push([tag, value, this.line]); var column, lineCount, lines, string;
if (offset === 0) {
return [this.chunkLine, this.chunkColumn];
}
if (offset >= this.chunk.length) {
string = this.chunk;
} else {
string = this.chunk.slice(0, +(offset - 1) + 1 || 9e9);
}
lineCount = count(string, '\n');
column = this.chunkColumn;
if (lineCount > 0) {
lines = string.split('\n');
column = (last(lines)).length;
} else {
column += string.length;
}
return [this.chunkLine + lineCount, column];
};
Lexer.prototype.makeToken = function(tag, value, offsetInChunk, length) {
var lastCharacter, locationData, token, _ref2, _ref3;
if (offsetInChunk == null) {
offsetInChunk = 0;
}
if (length == null) {
length = value.length;
}
locationData = {};
_ref2 = this.getLineAndColumnFromChunk(offsetInChunk), locationData.first_line = _ref2[0], locationData.first_column = _ref2[1];
lastCharacter = Math.max(0, length - 1);
_ref3 = this.getLineAndColumnFromChunk(offsetInChunk + lastCharacter), locationData.last_line = _ref3[0], locationData.last_column = _ref3[1];
token = [tag, value, locationData];
return token;
};
Lexer.prototype.token = function(tag, value, offsetInChunk, length) {
var token;
token = this.makeToken(tag, value, offsetInChunk, length);
this.tokens.push(token);
return token;
}; };
Lexer.prototype.tag = function(index, tag) { Lexer.prototype.tag = function(index, tag) {
@ -682,7 +779,7 @@ define(function(require, exports, module) {
Lexer.prototype.unfinished = function() { Lexer.prototype.unfinished = function() {
var _ref2; var _ref2;
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'); return LINE_CONTINUER.test(this.chunk) || ((_ref2 = this.tag()) === '\\' || _ref2 === '.' || _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) {
@ -705,7 +802,10 @@ define(function(require, exports, module) {
}; };
Lexer.prototype.error = function(message) { Lexer.prototype.error = function(message) {
throw SyntaxError("" + message + " on line " + (this.line + 1)); return throwSyntaxError(message, {
first_line: this.chunkLine,
first_column: this.chunkColumn
});
}; };
return Lexer; return Lexer;
@ -749,17 +849,19 @@ define(function(require, exports, module) {
exports.STRICT_PROSCRIBED = STRICT_PROSCRIBED; exports.STRICT_PROSCRIBED = STRICT_PROSCRIBED;
BOM = 65279;
IDENTIFIER = /^([$A-Za-z_\x7f-\uffff][$\w\x7f-\uffff]*)([^\n\S]*:(?!:))?/; IDENTIFIER = /^([$A-Za-z_\x7f-\uffff][$\w\x7f-\uffff]*)([^\n\S]*:(?!:))?/;
NUMBER = /^0b[01]+|^0o[0-7]+|^0x[\da-f]+|^\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 = /^[-=]>/;

File diff suppressed because it is too large Load diff

File diff suppressed because one or more lines are too long

View file

@ -38,9 +38,14 @@ define(function(require, exports, module) {
var assert = require("../../test/assertions"); var assert = require("../../test/assertions");
var coffee = require("../coffee/coffee-script"); var coffee = require("../coffee/coffee-script");
function assertLocation(e, sl, sc, el, ec) {
assert.equal(e.location.first_line, sl);
assert.equal(e.location.first_column, sc);
assert.equal(e.location.last_line, el);
assert.equal(e.location.last_column, ec);
}
module.exports = { module.exports = {
"test parse valid coffee script": function() { "test parse valid coffee script": function() {
coffee.parse("square = (x) -> x * x"); coffee.parse("square = (x) -> x * x");
}, },
@ -49,7 +54,25 @@ module.exports = {
try { try {
coffee.parse("a = 12 f"); coffee.parse("a = 12 f");
} catch (e) { } catch (e) {
assert.ok((e + "").indexOf("Parse error on line 1: Unexpected 'IDENTIFIER'") >= 0); assert.equal(e.message, "Unexpected 'IDENTIFIER'");
assertLocation(e, 0, 4, 0, 5);
}
},
"test parse missing bracket": function() {
try {
coffee.parse("a = 12 f {\n\n");
} catch (e) {
assert.equal(e.message, "missing }");
assertLocation(e, 0, 10, 0, 10);
}
},
"test unexpected indent": function() {
try {
coffee.parse("a\n a\n");
} catch (e) {
assert.equal(e.message, "Unexpected 'INDENT'");
assertLocation(e, 0, 0, 0, 0);
} }
} }
}; };

View file

@ -1,5 +1,5 @@
/** /**
* Copyright (c) 2009-2012 Jeremy Ashkenas * Copyright (c) 2009-2013 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
@ -21,17 +21,24 @@
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE. * OTHER DEALINGS IN THE SOFTWARE.
*
*/ */
define(function(require, exports, module) { define(function(require, exports, module) {
// Generated by CoffeeScript 1.3.3 // Generated by CoffeeScript 1.6.2
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 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, generate, left, rite, _i, _len, _ref,
__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; }, __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; },
__slice = [].slice; __slice = [].slice;
exports.Rewriter = (function() { generate = function(tag, value) {
var tok;
tok = [tag, value];
tok.generated = true;
return tok;
};
exports.Rewriter = (function() {
function Rewriter() {} function Rewriter() {}
Rewriter.prototype.rewrite = function(tokens) { Rewriter.prototype.rewrite = function(tokens) {
@ -42,8 +49,8 @@ define(function(require, exports, module) {
this.closeOpenIndexes(); this.closeOpenIndexes();
this.addImplicitIndentation(); this.addImplicitIndentation();
this.tagPostfixConditionals(); this.tagPostfixConditionals();
this.addImplicitBraces(); this.addImplicitBracesAndParens();
this.addImplicitParentheses(); this.addLocationDataToGeneratedTokens();
return this.tokens; return this.tokens;
}; };
@ -137,113 +144,240 @@ define(function(require, exports, module) {
}); });
}; };
Rewriter.prototype.addImplicitBraces = function() { Rewriter.prototype.matchTags = function() {
var action, condition, sameLine, stack, start, startIndent, startIndex, startsLine; var fuzz, i, j, pattern, _i, _ref, _ref1;
stack = []; i = arguments[0], pattern = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
start = null; fuzz = 0;
startsLine = null; for (j = _i = 0, _ref = pattern.length; 0 <= _ref ? _i < _ref : _i > _ref; j = 0 <= _ref ? ++_i : --_i) {
sameLine = true; while (this.tag(i + j + fuzz) === 'HERECOMMENT') {
startIndent = 0; fuzz += 2;
startIndex = 0; }
condition = function(token, i) { if (pattern[j] == null) {
var one, tag, three, two, _ref, _ref1; continue;
_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 (typeof pattern[j] === 'string') {
pattern[j] = [pattern[j]];
}
if (_ref1 = this.tag(i + j + fuzz), __indexOf.call(pattern[j], _ref1) < 0) {
return false; return false;
} }
tag = token[0]; }
if (__indexOf.call(LINEBREAKS, tag) >= 0) { return true;
sameLine = false; };
Rewriter.prototype.looksObjectish = function(j) {
return this.matchTags(j, '@', null, ':') || this.matchTags(j, null, ':');
};
Rewriter.prototype.findTagsBackwards = function(i, tags) {
var backStack, _ref, _ref1, _ref2, _ref3, _ref4, _ref5;
backStack = [];
while (i >= 0 && (backStack.length || (_ref2 = this.tag(i), __indexOf.call(tags, _ref2) < 0) && ((_ref3 = this.tag(i), __indexOf.call(EXPRESSION_START, _ref3) < 0) || this.tokens[i].generated) && (_ref4 = this.tag(i), __indexOf.call(LINEBREAKS, _ref4) < 0))) {
if (_ref = this.tag(i), __indexOf.call(EXPRESSION_END, _ref) >= 0) {
backStack.push(this.tag(i));
} }
return (((tag === 'TERMINATOR' || tag === 'OUTDENT') || (__indexOf.call(IMPLICIT_END, tag) >= 0 && sameLine && !(i - startIndex === 1))) && ((!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')); if ((_ref1 = this.tag(i), __indexOf.call(EXPRESSION_START, _ref1) >= 0) && backStack.length) {
}; backStack.pop();
action = function(token, i) { }
var tok; i -= 1;
tok = this.generate('}', '}', token[2]); }
return this.tokens.splice(i, 0, tok); return _ref5 = this.tag(i), __indexOf.call(tags, _ref5) >= 0;
}; };
Rewriter.prototype.addImplicitBracesAndParens = function() {
var stack;
stack = [];
return this.scanTokens(function(token, i, tokens) { return this.scanTokens(function(token, i, tokens) {
var ago, idx, prevTag, tag, tok, value, _ref, _ref1; var endImplicitCall, endImplicitObject, forward, inImplicit, inImplicitCall, inImplicitControl, inImplicitObject, nextTag, offset, prevTag, s, sameLine, stackIdx, stackTag, stackTop, startIdx, startImplicitCall, startImplicitObject, startsLine, tag, _ref, _ref1, _ref2, _ref3, _ref4, _ref5;
if (_ref = (tag = token[0]), __indexOf.call(EXPRESSION_START, _ref) >= 0) { tag = token[0];
stack.push([(tag === 'INDENT' && this.tag(i - 1) === '{' ? '{' : tag), i]); prevTag = (i > 0 ? tokens[i - 1] : [])[0];
return 1; nextTag = (i < tokens.length - 1 ? tokens[i + 1] : [])[0];
stackTop = function() {
return stack[stack.length - 1];
};
startIdx = i;
forward = function(n) {
return i - startIdx + n;
};
inImplicit = function() {
var _ref, _ref1;
return (_ref = stackTop()) != null ? (_ref1 = _ref[2]) != null ? _ref1.ours : void 0 : void 0;
};
inImplicitCall = function() {
var _ref;
return inImplicit() && ((_ref = stackTop()) != null ? _ref[0] : void 0) === '(';
};
inImplicitObject = function() {
var _ref;
return inImplicit() && ((_ref = stackTop()) != null ? _ref[0] : void 0) === '{';
};
inImplicitControl = function() {
var _ref;
return inImplicit && ((_ref = stackTop()) != null ? _ref[0] : void 0) === 'CONTROL';
};
startImplicitCall = function(j) {
var idx;
idx = j != null ? j : i;
stack.push([
'(', idx, {
ours: true
}
]);
tokens.splice(idx, 0, generate('CALL_START', '('));
if (j == null) {
return i += 1;
}
};
endImplicitCall = function() {
stack.pop();
tokens.splice(i, 0, generate('CALL_END', ')'));
return i += 1;
};
startImplicitObject = function(j, startsLine) {
var idx;
if (startsLine == null) {
startsLine = true;
}
idx = j != null ? j : i;
stack.push([
'{', idx, {
sameLine: true,
startsLine: startsLine,
ours: true
}
]);
tokens.splice(idx, 0, generate('{', generate(new String('{'))));
if (j == null) {
return i += 1;
}
};
endImplicitObject = function(j) {
j = j != null ? j : i;
stack.pop();
tokens.splice(j, 0, generate('}', '}'));
return i += 1;
};
if (inImplicitCall() && (tag === 'IF' || tag === 'TRY' || tag === 'FINALLY' || tag === 'CATCH' || tag === 'CLASS' || tag === 'SWITCH')) {
stack.push([
'CONTROL', i, {
ours: true
}
]);
return forward(1);
}
if (tag === 'INDENT' && inImplicit()) {
if (prevTag !== '=>' && prevTag !== '->' && prevTag !== '[' && prevTag !== '(' && prevTag !== ',' && prevTag !== '{' && prevTag !== 'TRY' && prevTag !== 'ELSE' && prevTag !== '=') {
while (inImplicitCall()) {
endImplicitCall();
}
}
if (inImplicitControl()) {
stack.pop();
}
stack.push([tag, i]);
return forward(1);
}
if (__indexOf.call(EXPRESSION_START, tag) >= 0) {
stack.push([tag, i]);
return forward(1);
} }
if (__indexOf.call(EXPRESSION_END, tag) >= 0) { if (__indexOf.call(EXPRESSION_END, tag) >= 0) {
start = stack.pop(); while (inImplicit()) {
return 1; if (inImplicitCall()) {
endImplicitCall();
} else if (inImplicitObject()) {
endImplicitObject();
} else {
stack.pop();
}
}
stack.pop();
} }
if (!(tag === ':' && ((ago = this.tag(i - 2)) === ':' || ((_ref1 = stack[stack.length - 1]) != null ? _ref1[0] : void 0) !== '{'))) { if ((__indexOf.call(IMPLICIT_FUNC, tag) >= 0 && token.spaced && !token.stringEnd || tag === '?' && i > 0 && !tokens[i - 1].spaced) && (__indexOf.call(IMPLICIT_CALL, nextTag) >= 0 || __indexOf.call(IMPLICIT_UNSPACED_CALL, nextTag) >= 0 && !((_ref = tokens[i + 1]) != null ? _ref.spaced : void 0) && !((_ref1 = tokens[i + 1]) != null ? _ref1.newLine : void 0))) {
return 1; if (tag === '?') {
tag = token[0] = 'FUNC_EXIST';
}
startImplicitCall(i + 1);
return forward(2);
} }
sameLine = true; if (this.matchTags(i, IMPLICIT_FUNC, 'INDENT', null, ':') && !this.findTagsBackwards(i, ['CLASS', 'EXTENDS', 'IF', 'CATCH', 'SWITCH', 'LEADING_WHEN', 'FOR', 'WHILE', 'UNTIL'])) {
startIndex = i + 1; startImplicitCall(i + 1);
stack.push(['{']); stack.push(['INDENT', i + 2]);
idx = ago === '@' ? i - 2 : i - 1; return forward(3);
while (this.tag(idx - 2) === 'HERECOMMENT') {
idx -= 2;
} }
prevTag = this.tag(idx - 1); if (tag === ':') {
startsLine = !prevTag || (__indexOf.call(LINEBREAKS, prevTag) >= 0); if (this.tag(i - 2) === '@') {
value = new String('{'); s = i - 2;
value.generated = true; } else {
tok = this.generate('{', value, token[2]); s = i - 1;
tokens.splice(idx, 0, tok); }
this.detectEnd(i + 2, condition, action); while (this.tag(s - 2) === 'HERECOMMENT') {
return 2; s -= 2;
}
startsLine = s === 0 || (_ref2 = this.tag(s - 1), __indexOf.call(LINEBREAKS, _ref2) >= 0) || tokens[s - 1].newLine;
if (stackTop()) {
_ref3 = stackTop(), stackTag = _ref3[0], stackIdx = _ref3[1];
if ((stackTag === '{' || stackTag === 'INDENT' && this.tag(stackIdx - 1) === '{') && (startsLine || this.tag(s - 1) === ',' || this.tag(s - 1) === '{')) {
return forward(1);
}
}
startImplicitObject(s, !!startsLine);
return forward(2);
}
if (prevTag === 'OUTDENT' && inImplicitCall() && (tag === '.' || tag === '?.' || tag === '::' || tag === '?::')) {
endImplicitCall();
return forward(1);
}
if (inImplicitObject() && __indexOf.call(LINEBREAKS, tag) >= 0) {
stackTop()[2].sameLine = false;
}
if (__indexOf.call(IMPLICIT_END, tag) >= 0) {
while (inImplicit()) {
_ref4 = stackTop(), stackTag = _ref4[0], stackIdx = _ref4[1], (_ref5 = _ref4[2], sameLine = _ref5.sameLine, startsLine = _ref5.startsLine);
if (inImplicitCall() && prevTag !== ',') {
endImplicitCall();
} else if (inImplicitObject() && sameLine && !startsLine) {
endImplicitObject();
} else if (inImplicitObject() && tag === 'TERMINATOR' && prevTag !== ',' && !(startsLine && this.looksObjectish(i + 1))) {
endImplicitObject();
} else {
break;
}
}
}
if (tag === ',' && !this.looksObjectish(i + 1) && inImplicitObject() && (nextTag !== 'TERMINATOR' || !this.looksObjectish(i + 2))) {
offset = nextTag === 'OUTDENT' ? 1 : 0;
while (inImplicitObject()) {
endImplicitObject(i + offset);
}
}
return forward(1);
}); });
}; };
Rewriter.prototype.addImplicitParentheses = function() { Rewriter.prototype.addLocationDataToGeneratedTokens = function() {
var action, condition, noCall, seenControl, seenSingle;
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) {
return this.tokens.splice(i, 0, this.generate('CALL_END', ')', token[2]));
};
return this.scanTokens(function(token, i, tokens) { return this.scanTokens(function(token, i, tokens) {
var callObject, current, next, prev, tag, _ref, _ref1, _ref2; var column, line, nextLocation, prevLocation, _ref, _ref1;
tag = token[0]; if (token[2]) {
if (tag === 'CLASS' || tag === 'IF' || tag === 'FOR' || tag === 'WHILE') {
noCall = true;
}
_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 && (_ref1 = prev[0], __indexOf.call(IMPLICIT_FUNC, _ref1) >= 0);
seenSingle = false;
seenControl = false;
if (__indexOf.call(LINEBREAKS, tag) >= 0) {
noCall = false;
}
if (prev && !prev.spaced && tag === '?') {
token.call = true;
}
if (token.fromThen) {
return 1; return 1;
} }
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))) { if (!(token.generated || token.explicit)) {
return 1; return 1;
} }
tokens.splice(i, 0, this.generate('CALL_START', '(', token[2])); if (token[0] === '{' && (nextLocation = (_ref = tokens[i + 1]) != null ? _ref[2] : void 0)) {
this.detectEnd(i + 1, condition, action); line = nextLocation.first_line, column = nextLocation.first_column;
if (prev[0] === '?') { } else if (prevLocation = (_ref1 = tokens[i - 1]) != null ? _ref1[2] : void 0) {
prev[0] = 'FUNC_EXIST'; line = prevLocation.last_line, column = prevLocation.last_column;
} else {
line = column = 0;
} }
return 2; token[2] = {
first_line: line,
first_column: column,
last_line: line,
last_column: column
};
return 1;
}); });
}; };
@ -258,23 +392,28 @@ define(function(require, exports, module) {
return this.tokens.splice((this.tag(i - 1) === ',' ? i - 1 : i), 0, outdent); 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 tag, _ref, _ref1; var j, tag, _i, _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);
return 0; return 0;
} }
if (tag === 'ELSE' && this.tag(i - 1) !== 'OUTDENT') { if (tag === 'ELSE' && this.tag(i - 1) !== 'OUTDENT') {
tokens.splice.apply(tokens, [i, 0].concat(__slice.call(this.indentation(token)))); tokens.splice.apply(tokens, [i, 0].concat(__slice.call(this.indentation())));
return 2; return 2;
} }
if (tag === 'CATCH' && ((_ref = this.tag(i + 2)) === 'OUTDENT' || _ref === 'TERMINATOR' || _ref === 'FINALLY')) { if (tag === 'CATCH') {
tokens.splice.apply(tokens, [i + 2, 0].concat(__slice.call(this.indentation(token)))); for (j = _i = 1; _i <= 2; j = ++_i) {
return 4; if (!((_ref = this.tag(i + j)) === 'OUTDENT' || _ref === 'TERMINATOR' || _ref === 'FINALLY')) {
continue;
}
tokens.splice.apply(tokens, [i + j, 0].concat(__slice.call(this.indentation())));
return 2 + j;
}
} }
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;
_ref1 = this.indentation(token, true), indent = _ref1[0], outdent = _ref1[1]; _ref1 = this.indentation(true), indent = _ref1[0], outdent = _ref1[1];
if (starter === 'THEN') { if (starter === 'THEN') {
indent.fromThen = true; indent.fromThen = true;
} }
@ -293,8 +432,10 @@ define(function(require, exports, module) {
var action, condition, original; var action, condition, original;
original = null; original = null;
condition = function(token, i) { condition = function(token, i) {
var _ref; var prevTag, tag;
return (_ref = token[0]) === 'TERMINATOR' || _ref === 'INDENT'; tag = token[0];
prevTag = this.tokens[i - 1][0];
return tag === 'TERMINATOR' || (tag === 'INDENT' && __indexOf.call(SINGLE_LINERS, prevTag) < 0);
}; };
action = function(token, i) { action = function(token, i) {
if (token[0] !== 'INDENT' || (token.generated && !token.fromThen)) { if (token[0] !== 'INDENT' || (token.generated && !token.fromThen)) {
@ -311,25 +452,23 @@ define(function(require, exports, module) {
}); });
}; };
Rewriter.prototype.indentation = function(token, implicit) { Rewriter.prototype.indentation = function(implicit) {
var indent, outdent; var indent, outdent;
if (implicit == null) { if (implicit == null) {
implicit = false; implicit = false;
} }
indent = ['INDENT', 2, token[2]]; indent = ['INDENT', 2];
outdent = ['OUTDENT', 2, token[2]]; outdent = ['OUTDENT', 2];
if (implicit) { if (implicit) {
indent.generated = outdent.generated = true; indent.generated = outdent.generated = true;
} }
if (!implicit) {
indent.explicit = outdent.explicit = true;
}
return [indent, outdent]; return [indent, outdent];
}; };
Rewriter.prototype.generate = function(tag, value, line) { Rewriter.prototype.generate = generate;
var tok;
tok = [tag, value, line];
tok.generated = true;
return tok;
};
Rewriter.prototype.tag = function(i) { Rewriter.prototype.tag = function(i) {
var _ref; var _ref;
@ -358,7 +497,7 @@ define(function(require, exports, module) {
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', 'NULL', 'UNDEFINED', 'UNARY', 'SUPER', '@', '->', '=>', '[', '(', '{', '--', '++']; IMPLICIT_CALL = ['IDENTIFIER', 'NUMBER', 'STRING', 'JS', 'REGEX', 'NEW', 'PARAM_START', 'CLASS', 'IF', 'TRY', 'SWITCH', 'THIS', 'BOOL', 'NULL', 'UNDEFINED', 'UNARY', 'SUPER', 'THROW', '@', '->', '=>', '[', '(', '{', '--', '++'];
IMPLICIT_UNSPACED_CALL = ['+', '-']; IMPLICIT_UNSPACED_CALL = ['+', '-'];

View file

@ -1,5 +1,5 @@
/** /**
* Copyright (c) 2009-2012 Jeremy Ashkenas * Copyright (c) 2009-2013 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
@ -21,17 +21,17 @@
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE. * OTHER DEALINGS IN THE SOFTWARE.
*
*/ */
define(function(require, exports, module) { define(function(require, exports, module) {
// Generated by CoffeeScript 1.3.3 // Generated by CoffeeScript 1.6.2
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.root = null; Scope.root = null;
function Scope(parent, expressions, method) { function Scope(parent, expressions, method) {
@ -65,7 +65,8 @@ define(function(require, exports, module) {
}; };
Scope.prototype.namedMethod = function() { Scope.prototype.namedMethod = function() {
if (this.method.name || !this.parent) { var _ref1;
if (((_ref1 = this.method) != null ? _ref1.name : void 0) || !this.parent) {
return this.method; return this.method;
} }
return this.parent.namedMethod(); return this.parent.namedMethod();

View file

@ -53,27 +53,16 @@ oop.inherits(Worker, Mirror);
try { try {
coffee.parse(value); coffee.parse(value);
} catch(e) { } catch(e) {
var m = e.message.match(/Parse error on line (\d+): (.*)/); var loc = e.location;
if (m) { if (loc) {
this.sender.emit("error", { this.sender.emit("error", {
row: parseInt(m[1], 10) - 1, row: loc.first_line,
column: null, column: loc.first_column,
text: m[2], endRow: loc.last_line,
endColumn: loc.last_column,
text: e.message,
type: "error" type: "error"
}); });
return;
}
if (e instanceof SyntaxError) {
var m = e.message.match(/ on line (\d+)/);
if (m) {
this.sender.emit("error", {
row: parseInt(m[1], 10) - 1,
column: null,
text: e.message.replace(m[0], ""),
type: "error"
});
}
} }
return; return;
} }

View file

@ -14,15 +14,20 @@ define(function(require, exports, module) {
}(this, 'luaparse', function (exports) { }(this, 'luaparse', function (exports) {
'use strict'; 'use strict';
exports.version = '0.0.1'; exports.version = '0.0.11';
var input, options, length; var input, options, length;
// Options can be set either globally on the parser object through
// defaultOptions, or during the parse call.
var defaultOptions = exports.defaultOptions = { var defaultOptions = exports.defaultOptions = {
// Explicitly tell the parser when the input ends. // Explicitly tell the parser when the input ends.
wait: false wait: false
// Store comments as an array in the chunk object. // Store comments as an array in the chunk object.
, comments: true , comments: true
// Track identifier scopes by adding an isLocal attribute to each
// identifier-node.
, scope: false
}; };
// The available tokens expressed as enum flags so they can be checked with // The available tokens expressed as enum flags so they can be checked with
@ -30,7 +35,7 @@ define(function(require, exports, module) {
var EOF = 1, StringLiteral = 2, Keyword = 4, Identifier = 8 var EOF = 1, StringLiteral = 2, Keyword = 4, Identifier = 8
, NumericLiteral = 16, Punctuator = 32, BooleanLiteral = 64 , NumericLiteral = 16, Punctuator = 32, BooleanLiteral = 64
, NilLiteral = 128; , NilLiteral = 128, VarargLiteral = 256;
// As this parser is a bit different from luas own, the error messages // As this parser is a bit different from luas own, the error messages
// will be different in some situations. // will be different in some situations.
@ -82,6 +87,13 @@ define(function(require, exports, module) {
, clauses: clauses , clauses: clauses
}; };
} }
, ifClause: function(condition, body) {
return {
type: 'IfClause'
, condition: condition
, body: body
};
}
, elseifClause: function(condition, body) { , elseifClause: function(condition, body) {
return { return {
type: 'ElseifClause' type: 'ElseifClause'
@ -142,12 +154,11 @@ define(function(require, exports, module) {
}; };
} }
, functionStatement: function(identifier, parameters, isVararg, isLocal, body) { , functionStatement: function(identifier, parameters, isLocal, body) {
return { return {
type: 'FunctionDeclaration' type: 'FunctionDeclaration'
, identifier: identifier , identifier: identifier
, vararg: isVararg , isLocal: isLocal
, local: isLocal
, parameters: parameters , parameters: parameters
, body: body , body: body
}; };
@ -187,18 +198,19 @@ define(function(require, exports, module) {
}; };
} }
, literal: function(value, raw) { , literal: function(type, value, raw) {
type = (type === StringLiteral) ? 'StringLiteral'
: (type === NumericLiteral) ? 'NumericLiteral'
: (type === BooleanLiteral) ? 'BooleanLiteral'
: (type === NilLiteral) ? 'NilLiteral'
: 'VarargLiteral';
return { return {
type: 'Literal' type: type
, value: value , value: value
, raw: raw , raw: raw
}; };
} }
, varargLiteral: function() {
return {
type: 'VarargLiteral'
};
}
, tableKey: function(key, value) { , tableKey: function(key, value) {
return { return {
@ -293,7 +305,14 @@ define(function(require, exports, module) {
// ------- // -------
var slice = Array.prototype.slice var slice = Array.prototype.slice
, toString = Object.prototype.toString; , toString = Object.prototype.toString
// Simple indexOf implementation which only provides what's required.
, indexOf = Array.prototype.indexOf || function indexOf(element) {
for (var i = 0, length = this.length; i < length; i++) {
if (this[i] === element) return i;
}
return -1;
};
// A sprintf implementation using %index (beginning at 1) to input // A sprintf implementation using %index (beginning at 1) to input
// arguments in the format string. // arguments in the format string.
@ -306,7 +325,6 @@ define(function(require, exports, module) {
function sprintf(format) { function sprintf(format) {
var args = slice.call(arguments, 1); var args = slice.call(arguments, 1);
format = format.replace(/%(\d)/g, function (match, index) { format = format.replace(/%(\d)/g, function (match, index) {
match = ''; // jshint
return '' + args[index - 1] || ''; return '' + args[index - 1] || '';
}); });
return format; return format;
@ -455,14 +473,14 @@ define(function(require, exports, module) {
, range: [index, index] , range: [index, index]
}; };
var char = input.charCodeAt(index) var character = input.charCodeAt(index)
, next = input.charCodeAt(index + 1); , next = input.charCodeAt(index + 1);
// Memorize the range index where the token begins. // Memorize the range index where the token begins.
tokenStart = index; tokenStart = index;
if (isIdentifierStart(char)) return scanIdentifierOrKeyword(); if (isIdentifierStart(character)) return scanIdentifierOrKeyword();
switch (char) { switch (character) {
case 39: case 34: // '" case 39: case 34: // '"
return scanStringLiteral(); return scanStringLiteral();
@ -475,7 +493,7 @@ define(function(require, exports, module) {
// If the dot is followed by a digit it's a float. // If the dot is followed by a digit it's a float.
if (isDecDigit(next)) return scanNumericLiteral(); if (isDecDigit(next)) return scanNumericLiteral();
if (46 === next) { if (46 === next) {
if (46 === input.charCodeAt(index + 2)) return scanPunctuator('...'); if (46 === input.charCodeAt(index + 2)) return scanVarargLiteral();
return scanPunctuator('..'); return scanPunctuator('..');
} }
return scanPunctuator('.'); return scanPunctuator('.');
@ -520,10 +538,10 @@ define(function(require, exports, module) {
function skipWhiteSpace() { function skipWhiteSpace() {
while (index < length) { while (index < length) {
var char = input.charCodeAt(index); var character = input.charCodeAt(index);
if (isWhiteSpace(char)) { if (isWhiteSpace(character)) {
index++; index++;
} else if (isLineTerminator(char)) { } else if (isLineTerminator(character)) {
line++; line++;
lineStart = ++index; lineStart = ++index;
} else { } else {
@ -580,26 +598,39 @@ define(function(require, exports, module) {
}; };
} }
// A vararg literal consists of three dots.
function scanVarargLiteral() {
index += 3;
return {
type: VarargLiteral
, value: '...'
, line: line
, lineStart: lineStart
, range: [tokenStart, index]
};
}
// Find the string literal by matching the delimiter marks used. // Find the string literal by matching the delimiter marks used.
function scanStringLiteral() { function scanStringLiteral() {
var delimiter = input.charCodeAt(index++) var delimiter = input.charCodeAt(index++)
, stringStart = index , stringStart = index
, string = '' , string = ''
, char; , character;
while (index < length) { while (index < length) {
char = input.charCodeAt(index++); character = input.charCodeAt(index++);
if (delimiter === char) break; if (delimiter === character) break;
if (92 === char) { // \ if (92 === character) { // \
string += input.slice(stringStart, index - 1) + readEscapeSequence(); string += input.slice(stringStart, index - 1) + readEscapeSequence();
stringStart = index; stringStart = index;
} }
// EOF or `\n` terminates a string literal. If we haven't found the // EOF or `\n` terminates a string literal. If we haven't found the
// ending delimiter by now, raise an exception. // ending delimiter by now, raise an exception.
else if (index >= length || isLineTerminator(char)) { else if (index >= length || isLineTerminator(character)) {
string += input.slice(stringStart, index - 1); string += input.slice(stringStart, index - 1);
raise({}, errors.unfinishedString, string + String.fromCharCode(char)); raise({}, errors.unfinishedString, string + String.fromCharCode(character));
} }
} }
string += input.slice(stringStart, index - 1); string += input.slice(stringStart, index - 1);
@ -638,10 +669,10 @@ define(function(require, exports, module) {
// If a hexadecimal number is encountered, it will be converted. // If a hexadecimal number is encountered, it will be converted.
function scanNumericLiteral() { function scanNumericLiteral() {
var char = input.charAt(index) var character = input.charAt(index)
, next = input.charAt(index + 1); , next = input.charAt(index + 1);
var value = ('0' === char && ~'xX'.indexOf(next || null)) ? var value = ('0' === character && 'xX'.indexOf(next || null) >= 0) ?
readHexLiteral() : readDecLiteral(); readHexLiteral() : readDecLiteral();
return { return {
@ -693,11 +724,11 @@ define(function(require, exports, module) {
} }
// Binary exponents are optional // Binary exponents are optional
if (~'pP'.indexOf(input.charAt(index) || null)) { if ('pP'.indexOf(input.charAt(index) || null) >= 0) {
index++; index++;
// Sign part is optional and defaults to 1 (positive). // Sign part is optional and defaults to 1 (positive).
if (~'+-'.indexOf(input.charAt(index) || null)) if ('+-'.indexOf(input.charAt(index) || null) >= 0)
binarySign = ('+' === input.charAt(index++)) ? 1 : -1; binarySign = ('+' === input.charAt(index++)) ? 1 : -1;
exponentStart = index; exponentStart = index;
@ -729,10 +760,10 @@ define(function(require, exports, module) {
while (isDecDigit(input.charCodeAt(index))) index++; while (isDecDigit(input.charCodeAt(index))) index++;
} }
// Exponent part is optional. // Exponent part is optional.
if (~'eE'.indexOf(input.charAt(index) || null)) { if ('eE'.indexOf(input.charAt(index) || null) >= 0) {
index++; index++;
// Sign part is optional. // Sign part is optional.
if (~'+-'.indexOf(input.charAt(index) || null)) index++; if ('+-'.indexOf(input.charAt(index) || null) >= 0) index++;
// An exponent is required to contain at least one decimal digit. // An exponent is required to contain at least one decimal digit.
if (!isDecDigit(input.charCodeAt(index))) if (!isDecDigit(input.charCodeAt(index)))
raise({}, errors.malformedNumber, input.slice(tokenStart, index)); raise({}, errors.malformedNumber, input.slice(tokenStart, index));
@ -754,7 +785,7 @@ define(function(require, exports, module) {
case 'n': index++; return '\n'; case 'n': index++; return '\n';
case 'r': index++; return '\r'; case 'r': index++; return '\r';
case 't': index++; return '\t'; case 't': index++; return '\t';
case 'v': index++; return '\v'; case 'v': index++; return '\x0B';
case 'b': index++; return '\b'; case 'b': index++; return '\b';
case 'f': index++; return '\f'; case 'f': index++; return '\f';
// Skips the following span of white-space. // Skips the following span of white-space.
@ -790,19 +821,16 @@ define(function(require, exports, module) {
tokenStart = index; tokenStart = index;
index += 2; // -- index += 2; // --
var char = input.charAt(index) var character = input.charAt(index)
, content = '' , content = ''
, isLong = false , isLong = false
, commentStart = index; , commentStart = index;
if ('[' === char) { if ('[' === character) {
content = readLongString(); content = readLongString();
// This wasn't a multiline comment after all. // This wasn't a multiline comment after all.
if (false === content) content = char; if (false === content) content = character;
else { else isLong = true;
isLong = true;
index += 2; // Trailing --
}
} }
// Scan until next line as long as it's not a multiline comment. // Scan until next line as long as it's not a multiline comment.
if (!isLong) { if (!isLong) {
@ -829,7 +857,7 @@ define(function(require, exports, module) {
var level = 0 var level = 0
, content = '' , content = ''
, terminator = false , terminator = false
, char, stringStart; , character, stringStart;
index++; // [ index++; // [
@ -848,11 +876,11 @@ define(function(require, exports, module) {
stringStart = index; stringStart = index;
while (index < length) { while (index < length) {
char = input.charAt(index++); character = input.charAt(index++);
// We have to keep track of newlines as `skipWhiteSpace()` does not get // We have to keep track of newlines as `skipWhiteSpace()` does not get
// to scan this part. // to scan this part.
if (isLineTerminator(char.charCodeAt(0))) { if (isLineTerminator(character.charCodeAt(0))) {
line++; line++;
lineStart = index; lineStart = index;
} }
@ -860,7 +888,7 @@ define(function(require, exports, module) {
// Once the delimiter is found, iterate through the depth count and see // Once the delimiter is found, iterate through the depth count and see
// if it matches. // if it matches.
if (']' === char) { if (']' === character) {
terminator = true; terminator = true;
for (var i = 0; i < level; i++) { for (var i = 0; i < level; i++) {
if ('=' !== input.charAt(index + i)) terminator = false; if ('=' !== input.charAt(index + i)) terminator = false;
@ -870,11 +898,6 @@ define(function(require, exports, module) {
// We reached the end of the multiline string. Get out now. // We reached the end of the multiline string. Get out now.
if (terminator) break; if (terminator) break;
if ('\\' === char) {
content += input.slice(stringStart, index - 1) + readEscapeSequence();
stringStart = index;
}
} }
content += input.slice(stringStart, index - 1); content += input.slice(stringStart, index - 1);
index += level + 1; index += level + 1;
@ -905,16 +928,6 @@ define(function(require, exports, module) {
return false; return false;
} }
// Check if the given expression exists and raise an exception if not.
//
// As expressions can return null due to the design of the parser, we often
// need this strict expression check as well.
function expectExpression(expression) {
if (null == expression) raiseUnexpectedToken('<expression>', token);
else return expression;
}
// Expect the next token value to match. If not, throw an exception. // Expect the next token value to match. If not, throw an exception.
function expect(value) { function expect(value) {
@ -924,31 +937,31 @@ define(function(require, exports, module) {
// ### Validation functions // ### Validation functions
function isWhiteSpace(char) { function isWhiteSpace(character) {
return 9 === char || 32 === char || 0xB === char || 0xC === char; return 9 === character || 32 === character || 0xB === character || 0xC === character;
} }
function isLineTerminator(char) { function isLineTerminator(character) {
return 10 === char || 13 === char; return 10 === character || 13 === character;
} }
function isDecDigit(char) { function isDecDigit(character) {
return char >= 48 && char <= 57; return character >= 48 && character <= 57;
} }
function isHexDigit(char) { function isHexDigit(character) {
return (char >= 48 && char <= 57) || (char >= 97 && char <= 102) || (char >= 65 && char <= 70); return (character >= 48 && character <= 57) || (character >= 97 && character <= 102) || (character >= 65 && character <= 70);
} }
// From [Lua 5.2](http://www.lua.org/manual/5.2/manual.html#8.1) onwards // From [Lua 5.2](http://www.lua.org/manual/5.2/manual.html#8.1) onwards
// identifiers cannot use locale-dependet letters. // identifiers cannot use locale-dependet letters.
function isIdentifierStart(char) { function isIdentifierStart(character) {
return (char >= 65 && char <= 90) || (char >= 97 && char <= 122) || 95 === char; return (character >= 65 && character <= 90) || (character >= 97 && character <= 122) || 95 === character;
} }
function isIdentifierPart(char) { function isIdentifierPart(character) {
return (char >= 65 && char <= 90) || (char >= 97 && char <= 122) || 95 === char || (char >= 48 && char <= 57); return (character >= 65 && character <= 90) || (character >= 97 && character <= 122) || 95 === character || (character >= 48 && character <= 57);
} }
// [3.1 Lexical Conventions](http://www.lua.org/manual/5.2/manual.html#3.1) // [3.1 Lexical Conventions](http://www.lua.org/manual/5.2/manual.html#3.1)
@ -974,7 +987,7 @@ define(function(require, exports, module) {
} }
function isUnary(token) { function isUnary(token) {
if (Punctuator === token.type) return ~'#-'.indexOf(token.value); if (Punctuator === token.type) return '#-'.indexOf(token.value) >= 0;
if (Keyword === token.type) return 'not' === token.value; if (Keyword === token.type) return 'not' === token.value;
return false; return false;
} }
@ -1004,6 +1017,61 @@ define(function(require, exports, module) {
} }
} }
// Scope
// -----
// Store each block scope as a an array of identifier names. Each scope is
// stored in an FILO-array.
var scopes
// The current scope index
, scopeDepth
// A list of all global identifier nodes.
, globals
// A list of all global identifiers names used for faster lookup.
// @TODO benchmark, with exposing the globals this entire implementation
// should probably change.
, globalNames;
// Create a new scope inheriting all declarations from the previous scope.
function createScope() {
scopes.push(Array.apply(null, scopes[scopeDepth++]));
}
// Exit and remove the current scope.
function exitScope() {
scopes.pop();
scopeDepth--;
}
// Add identifier name to the current scope if it doesnt already exist.
function scopeIdentifierName(name) {
if (-1 !== indexOf.call(scopes[scopeDepth], name)) return;
scopes[scopeDepth].push(name);
}
// Add identifier to the current scope
function scopeIdentifier(node) {
scopeIdentifierName(node.name);
attachScope(node, true);
}
// Attach scope information to node. If the node is global, store it in the
// globals array so we can return the information to the user.
function attachScope(node, isLocal) {
if (!isLocal && -1 === indexOf.call(globalNames, node.name)) {
globalNames.push(node.name);
globals.push(node);
}
node.isLocal = isLocal;
}
// Is the identifier name available in this scope.
function scopeHasName(name) {
return (-1 !== indexOf.call(scopes[scopeDepth], name));
}
// Parse functions // Parse functions
// --------------- // ---------------
@ -1027,6 +1095,9 @@ define(function(require, exports, module) {
var block = [] var block = []
, statement; , statement;
// Each block creates a new scope.
if (options.scope) createScope();
while (!isBlockFollow(token)) { while (!isBlockFollow(token)) {
// Return has to be the last statement in a block. // Return has to be the last statement in a block.
if ('return' === token.value) { if ('return' === token.value) {
@ -1038,6 +1109,8 @@ define(function(require, exports, module) {
// ignore some statements, such as EmptyStatement. // ignore some statements, such as EmptyStatement.
if (statement) block.push(statement); if (statement) block.push(statement);
} }
if (options.scope) exitScope();
// Doesn't really need an ast node // Doesn't really need an ast node
return block; return block;
} }
@ -1081,7 +1154,14 @@ define(function(require, exports, module) {
// label ::= '::' Name '::' // label ::= '::' Name '::'
function parseLabelStatement() { function parseLabelStatement() {
var label = parseIdentifier(); var name = token.value
, label = parseIdentifier();
if (options.scope) {
scopeIdentifierName('::' + name + '::');
attachScope(label, true);
}
expect('::'); expect('::');
return ast.labelStatement(label); return ast.labelStatement(label);
} }
@ -1095,7 +1175,10 @@ define(function(require, exports, module) {
// goto ::= 'goto' Name // goto ::= 'goto' Name
function parseGotoStatement() { function parseGotoStatement() {
var label = parseIdentifier(); var name = token.value
, label = parseIdentifier();
if (options.scope) label.isLabel = scopeHasName('::' + name + '::');
return ast.gotoStatement(label); return ast.gotoStatement(label);
} }
@ -1110,7 +1193,7 @@ define(function(require, exports, module) {
// while ::= 'while' exp 'do' block 'end' // while ::= 'while' exp 'do' block 'end'
function parseWhileStatement() { function parseWhileStatement() {
var condition = parseExpression(); var condition = parseExpectedExpression();
expect('do'); expect('do');
var body = parseBlock(); var body = parseBlock();
expect('end'); expect('end');
@ -1122,7 +1205,7 @@ define(function(require, exports, module) {
function parseRepeatStatement() { function parseRepeatStatement() {
var body = parseBlock(); var body = parseBlock();
expect('until'); expect('until');
var condition = expectExpression(parseExpression()); var condition = parseExpectedExpression();
return ast.repeatStatement(condition, body); return ast.repeatStatement(condition, body);
} }
@ -1135,7 +1218,7 @@ define(function(require, exports, module) {
var expression = parseExpression(); var expression = parseExpression();
if (null != expression) expressions.push(expression); if (null != expression) expressions.push(expression);
while (consume(',')) { while (consume(',')) {
expression = expectExpression(parseExpression()); expression = parseExpectedExpression();
expressions.push(expression); expressions.push(expression);
} }
consume(';'); // grammar tells us ; is optional here. consume(';'); // grammar tells us ; is optional here.
@ -1151,12 +1234,17 @@ define(function(require, exports, module) {
, condition , condition
, body; , body;
do { condition = parseExpectedExpression();
condition = parseExpression(); expect('then');
body = parseBlock();
clauses.push(ast.ifClause(condition, body));
while (consume('elseif')) {
condition = parseExpectedExpression();
expect('then'); expect('then');
body = parseBlock(); body = parseBlock();
clauses.push(ast.elseifClause(condition, body)); clauses.push(ast.elseifClause(condition, body));
} while (consume('elseif')); }
if (consume('else')) { if (consume('else')) {
body = parseBlock(); body = parseBlock();
@ -1178,16 +1266,19 @@ define(function(require, exports, module) {
var variable = parseIdentifier() var variable = parseIdentifier()
, body; , body;
// The start-identifier is local.
if (options.scope) scopeIdentifier(variable);
// If the first expression is followed by a `=` punctuator, this is a // If the first expression is followed by a `=` punctuator, this is a
// Numeric For Statement. // Numeric For Statement.
if (consume('=')) { if (consume('=')) {
// Start expression // Start expression
var start = expectExpression(parseExpression()); var start = parseExpectedExpression();
expect(','); expect(',');
// End expression // End expression
var end = expectExpression(parseExpression()); var end = parseExpectedExpression();
// Optional step expression // Optional step expression
var step = consume(',') ? expectExpression(parseExpression()) : null; var step = consume(',') ? parseExpectedExpression() : null;
expect('do'); expect('do');
body = parseBlock(); body = parseBlock();
@ -1199,13 +1290,18 @@ define(function(require, exports, module) {
} else { } else {
// The namelist can contain one or more identifiers. // The namelist can contain one or more identifiers.
var variables = [variable]; var variables = [variable];
while (consume(',')) variables.push(parseIdentifier()); while (consume(',')) {
variable = parseIdentifier();
// Each variable in the namelist is locally scoped.
if (options.scope) scopeIdentifier(variable);
variables.push(variable);
}
expect('in'); expect('in');
var iterators = []; var iterators = [];
// One or more expressions in the explist. // One or more expressions in the explist.
do { do {
var expression = expectExpression(parseExpression()); var expression = parseExpectedExpression();
iterators.push(expression); iterators.push(expression);
} while (consume(',')); } while (consume(','));
@ -1228,26 +1324,41 @@ define(function(require, exports, module) {
// | 'local' Name {',' Name} ['=' exp {',' exp} // | 'local' Name {',' Name} ['=' exp {',' exp}
function parseLocalStatement() { function parseLocalStatement() {
var name;
if (Identifier === token.type) { if (Identifier === token.type) {
var variables = []; var variables = []
var init = []; , init = [];
do { do {
variables.push(parseIdentifier()); name = parseIdentifier();
variables.push(name);
} while (consume(',')); } while (consume(','));
if (consume('=')) { if (consume('=')) {
do { do {
var expression = expectExpression(parseExpression()); var expression = parseExpectedExpression();
init.push(expression); init.push(expression);
} while (consume(',')); } while (consume(','));
} }
// Declarations doesn't exist before the statement has been evaluated.
// Therefore assignments can't use their declarator. And the identifiers
// shouldn't be added to the scope until the statement is complete.
if (options.scope) {
for (var i = 0, l = variables.length; i < l; i++) {
scopeIdentifier(variables[i]);
}
}
return ast.localStatement(variables, init); return ast.localStatement(variables, init);
} }
if (consume('function')) { if (consume('function')) {
name = parseIdentifier();
if (options.scope) scopeIdentifier(name);
// MemberExpressions are not allowed in local function statements. // MemberExpressions are not allowed in local function statements.
var name = parseIdentifier();
return parseFunctionDeclaration(name, true); return parseFunctionDeclaration(name, true);
} else { } else {
raiseUnexpectedToken('<name>', token); raiseUnexpectedToken('<name>', token);
@ -1268,18 +1379,19 @@ define(function(require, exports, module) {
, expression = parsePrefixExpression(); , expression = parsePrefixExpression();
if (null == expression) return unexpected(token); if (null == expression) return unexpected(token);
if (~',='.indexOf(token.value)) { if (',='.indexOf(token.value) >= 0) {
var variables = [expression] var variables = [expression]
, init = [] , init = []
, exp; , exp;
while (consume(',')) { while (consume(',')) {
exp = expectExpression(parsePrefixExpression()); exp = parsePrefixExpression();
if (null == exp) raiseUnexpectedToken('<expression>', token);
variables.push(exp); variables.push(exp);
} }
expect('='); expect('=');
do { do {
exp = expectExpression(parseExpression()); exp = parseExpectedExpression();
init.push(exp); init.push(exp);
} while (consume(',')); } while (consume(','));
return ast.assignmentStatement(variables, init); return ast.assignmentStatement(variables, init);
@ -1306,7 +1418,6 @@ define(function(require, exports, module) {
return ast.identifier(identifier); return ast.identifier(identifier);
} }
// Parse the functions parameters and body block. The name should already // Parse the functions parameters and body block. The name should already
// have been parsed and passed to this declaration function. By separating // have been parsed and passed to this declaration function. By separating
// this we allow for anonymous functions in expressions. // this we allow for anonymous functions in expressions.
@ -1318,28 +1429,39 @@ define(function(require, exports, module) {
// parlist ::= Name {',' Name} | [',' '...'] | '...' // parlist ::= Name {',' Name} | [',' '...'] | '...'
function parseFunctionDeclaration(name, isLocal) { function parseFunctionDeclaration(name, isLocal) {
var isVararg = false;
var parameters = []; var parameters = [];
expect('('); expect('(');
if (consume('...')) isVararg = true; // The declaration has arguments
else if (Identifier === token.type) { if (!consume(')')) {
do { // Arguments are a comma separated list of identifiers, optionally ending
if (consume('...')) { // with a vararg.
isVararg = true; while (true) {
if (Identifier === token.type) {
var parameter = parseIdentifier();
// Function parameters are local.
if (options.scope) scopeIdentifier(parameter);
parameters.push(parameter);
if (consume(',')) continue;
else if (consume(')')) break;
// No arguments are allowed after a vararg.
} else if (VarargLiteral === token.type) {
parameters.push(parsePrimaryExpression());
expect(')');
break; break;
} else {
raiseUnexpectedToken('<name> or \'...\'', token);
} }
parameters.push(parseIdentifier()); }
} while (consume(','));
} }
if (isVararg) expect(')');
else if (!consume(')')) raiseUnexpectedToken('<name> or \'...\'', token);
var body = parseBlock(); var body = parseBlock();
expect('end'); expect('end');
isLocal = isLocal || false; isLocal = isLocal || false;
return ast.functionStatement(name, parameters, isVararg, isLocal, body); return ast.functionStatement(name, parameters, isLocal, body);
} }
// Parse the function name as identifiers and member expressions. // Parse the function name as identifiers and member expressions.
@ -1347,14 +1469,20 @@ define(function(require, exports, module) {
// Name {'.' Name} [':' Name] // Name {'.' Name} [':' Name]
function parseFunctionName() { function parseFunctionName() {
var base = parseIdentifier(); var base = parseIdentifier()
, name;
if (options.scope) attachScope(base, false);
while (consume('.')) { while (consume('.')) {
base = ast.memberExpression(base, '.', parseIdentifier()); name = parseIdentifier();
if (options.scope) attachScope(name, false);
base = ast.memberExpression(base, '.', name);
} }
if (consume(':')) { if (consume(':')) {
base = ast.memberExpression(base, ':', parseIdentifier()); name = parseIdentifier();
if (options.scope) attachScope(name, false);
base = ast.memberExpression(base, ':', name);
} }
return base; return base;
@ -1372,15 +1500,15 @@ define(function(require, exports, module) {
while (true) { while (true) {
if (Punctuator === token.type && consume('[')) { if (Punctuator === token.type && consume('[')) {
key = parseExpression(); key = parseExpectedExpression();
expect(']'); expect(']');
expect('='); expect('=');
value = expectExpression(parseExpression()); value = parseExpectedExpression();
fields.push(ast.tableKey(key, value)); fields.push(ast.tableKey(key, value));
} else if (Identifier === token.type) { } else if (Identifier === token.type) {
key = parseExpression(); key = parseExpectedExpression();
if (consume('=')) { if (consume('=')) {
value = parseExpression(); value = parseExpectedExpression();
fields.push(ast.tableKeyString(key, value)); fields.push(ast.tableKeyString(key, value));
} else { } else {
fields.push(ast.tableValue(key)); fields.push(ast.tableValue(key));
@ -1389,7 +1517,7 @@ define(function(require, exports, module) {
if (null == (value = parseExpression())) break; if (null == (value = parseExpression())) break;
fields.push(ast.tableValue(value)); fields.push(ast.tableValue(value));
} }
if (~',;'.indexOf(token.value)) { if (',;'.indexOf(token.value) >= 0) {
next(); next();
continue; continue;
} }
@ -1402,7 +1530,8 @@ define(function(require, exports, module) {
// Expression parser // Expression parser
// ----------------- // -----------------
// //
// Expressions are evaluated and always return a value. // Expressions are evaluated and always return a value. If nothing is
// matched null will be returned.
// //
// exp ::= (unop exp | primary | prefixexp ) { binop exp } // exp ::= (unop exp | primary | prefixexp ) { binop exp }
// //
@ -1418,6 +1547,15 @@ define(function(require, exports, module) {
return expression; return expression;
} }
// Parse an expression expecting it to be valid.
function parseExpectedExpression() {
var expression = parseExpression();
if (null == expression) raiseUnexpectedToken('<expression>', token);
else return expression;
}
// Return the precedence priority of the operator. // Return the precedence priority of the operator.
// //
// As unary `-` can't be distinguished from binary `-`, unary precedence // As unary `-` can't be distinguished from binary `-`, unary precedence
@ -1427,23 +1565,23 @@ define(function(require, exports, module) {
// the expensive CompareICStub which took ~8% of the parse time. // the expensive CompareICStub which took ~8% of the parse time.
function binaryPrecedence(operator) { function binaryPrecedence(operator) {
var char = operator.charCodeAt(0) var character = operator.charCodeAt(0)
, length = operator.length; , length = operator.length;
if (1 === length) { if (1 === length) {
switch (char) { switch (character) {
case 94: return 10; // ^ case 94: return 10; // ^
case 42: case 47: case 37: return 7; // * / % case 42: case 47: case 37: return 7; // * / %
case 43: case 45: return 6; // + - case 43: case 45: return 6; // + -
case 60: case 62: return 3; // < > case 60: case 62: return 3; // < >
} }
} else if (2 === length) { } else if (2 === length) {
switch (char) { switch (character) {
case 46: return 5; // .. case 46: return 5; // ..
case 60: case 62: case 61: case 126: return 3; // <= >= == ~= case 60: case 62: case 61: case 126: return 3; // <= >= == ~=
case 111: return 1; // or case 111: return 1; // or
} }
} else if (97 === char && 'and' === operator) return 2; } else if (97 === character && 'and' === operator) return 2;
return 0; return 0;
} }
@ -1464,7 +1602,8 @@ define(function(require, exports, module) {
// UnaryExpression // UnaryExpression
if (isUnary(token)) { if (isUnary(token)) {
next(); next();
var argument = expectExpression(parseSubExpression(8)); var argument = parseSubExpression(8);
if (argument == null) raiseUnexpectedToken('<expression>', token);
expression = ast.unaryExpression(operator, argument); expression = ast.unaryExpression(operator, argument);
} }
if (null == expression) { if (null == expression) {
@ -1490,7 +1629,8 @@ define(function(require, exports, module) {
// Right-hand precedence operators // Right-hand precedence operators
if ('^' === operator || '..' === operator) precedence--; if ('^' === operator || '..' === operator) precedence--;
next(); next();
var right = expectExpression(parseSubExpression(precedence)); var right = parseSubExpression(precedence);
if (null == right) raiseUnexpectedToken('<expression>', token);
expression = ast.binaryExpression(operator, expression, right); expression = ast.binaryExpression(operator, expression, right);
} }
return expression; return expression;
@ -1503,14 +1643,20 @@ define(function(require, exports, module) {
// args ::= '(' [explist] ')' | tableconstructor | String // args ::= '(' [explist] ')' | tableconstructor | String
function parsePrefixExpression() { function parsePrefixExpression() {
var base; var base, name
// Keep track of the scope, if a parent is local so are the children.
, isLocal;
// The prefix // The prefix
if (Identifier === token.type) { if (Identifier === token.type) {
name = token.value;
base = parseIdentifier(); base = parseIdentifier();
// Set the parent scope.
if (options.scope) attachScope(base, isLocal = scopeHasName(name));
} else if (consume('(')) { } else if (consume('(')) {
base = parseExpression(); base = parseExpectedExpression();
expect(')'); expect(')');
if (options.scope) isLocal = base.isLocal;
} else { } else {
return null; return null;
} }
@ -1518,23 +1664,25 @@ define(function(require, exports, module) {
// The suffix // The suffix
var expression, identifier; var expression, identifier;
while (true) { while (true) {
expectExpression(base);
if (Punctuator === token.type) { if (Punctuator === token.type) {
switch (token.value) { switch (token.value) {
case '[': case '[':
next(); next();
expression = parseExpression(); expression = parseExpectedExpression();
base = ast.indexExpression(base, expression); base = ast.indexExpression(base, expression);
expect(']'); expect(']');
break; break;
case '.': case '.':
next(); next();
identifier = parseIdentifier(); identifier = parseIdentifier();
// Inherit the scope
if (options.scope) attachScope(identifier, isLocal);
base = ast.memberExpression(base, '.', identifier); base = ast.memberExpression(base, '.', identifier);
break; break;
case ':': case ':':
next(); next();
identifier = parseIdentifier(); identifier = parseIdentifier();
if (options.scope) attachScope(identifier, isLocal);
base = ast.memberExpression(base, ':', identifier); base = ast.memberExpression(base, ':', identifier);
// Once a : is found, this has to be a callexpression, otherwise // Once a : is found, this has to be a callexpression, otherwise
// throw an error. // throw an error.
@ -1569,7 +1717,7 @@ define(function(require, exports, module) {
var expression = parseExpression(); var expression = parseExpression();
if (null != expression) expressions.push(expression); if (null != expression) expressions.push(expression);
while (consume(',')) { while (consume(',')) {
expression = expectExpression(parseExpression()); expression = parseExpectedExpression();
expressions.push(expression); expressions.push(expression);
} }
@ -1583,9 +1731,7 @@ define(function(require, exports, module) {
} }
} else if (StringLiteral === token.type) { } else if (StringLiteral === token.type) {
var string = token.value; return ast.stringCallExpression(base, parsePrimaryExpression());
next();
return ast.stringCallExpression(base, string);
} }
raiseUnexpectedToken('function arguments', token); raiseUnexpectedToken('function arguments', token);
@ -1595,21 +1741,19 @@ define(function(require, exports, module) {
// | functiondef | tableconstructor | '...' // | functiondef | tableconstructor | '...'
function parsePrimaryExpression() { function parsePrimaryExpression() {
var literals = StringLiteral | NumericLiteral | BooleanLiteral | NilLiteral var literals = StringLiteral | NumericLiteral | BooleanLiteral | NilLiteral | VarargLiteral
, value = token.value; , value = token.value
, type = token.type;
if (token.type & literals) { if (type & literals) {
var raw = input.slice(token.range[0], token.range[1]); var raw = input.slice(token.range[0], token.range[1]);
next(); next();
return ast.literal(value, raw); return ast.literal(type, value, raw);
} else if (Keyword === token.type && 'function' === token.value) { } else if (Keyword === type && 'function' === value) {
next(); next();
return parseFunctionDeclaration(null); return parseFunctionDeclaration(null);
} else if (Punctuator === token.type) { } else if (consume('{'))
// Semantically dotsliteral can only exist within a vararg functions. return parseTableConstructor();
if (consume('...')) return ast.varargLiteral(value);
if (consume('{')) return parseTableConstructor();
}
} }
// Parser // Parser
@ -1618,6 +1762,8 @@ define(function(require, exports, module) {
// Export the main parser. // Export the main parser.
// //
// - `wait` Hold parsing until end() is called. Defaults to false // - `wait` Hold parsing until end() is called. Defaults to false
// - `comments` Store comments. Defaults to true.
// - `scope` Track identifier scope. Defaults to false.
// //
// Example: // Example:
// //
@ -1641,6 +1787,11 @@ define(function(require, exports, module) {
line = 1; line = 1;
lineStart = 0; lineStart = 0;
length = input.length; length = input.length;
// When tracking identifier scope, initialize with an empty scope.
scopes = [[]];
scopeDepth = 0;
globals = [];
globalNames = [];
if (options.comments) comments = []; if (options.comments) comments = [];
if (!options.wait) return end(); if (!options.wait) return end();
@ -1668,6 +1819,7 @@ define(function(require, exports, module) {
var chunk = parseChunk(); var chunk = parseChunk();
if (options.comments) chunk.comments = comments; if (options.comments) chunk.comments = comments;
if (options.scope) chunk.globals = globals;
return chunk; return chunk;
} }

View file

@ -145,15 +145,20 @@ function run() {
var src = editor1.getValue(); var src = editor1.getValue();
var path = "ace/mode/new"; var path = "ace/mode/new";
var deps = getDeps(src, path); var deps = getDeps(src, path);
src = src.replace("define(", 'define("' + path +'", ["require","exports","module",' + deps +'],'); window.require.undef(path)
src += ';require(["ace/mode/new"], function(e) {\ src = src.replace("define(", 'define("' + path +'", ["require","exports","module",' + deps +'],');
try{continueRun(e)}catch(e){log(e)}\
}, function(e){\
log(e);\
window.require.undef("ace/mode/new")\
});';
try { try {
eval(src); eval(src);
require(["ace/mode/new"], function(e) {
try{
continueRun(e)
}catch(e){
log(e)
}
}, function(e) {
log(e);
window.require.undef(path)
});
hideLog() hideLog()
} catch(e) { } catch(e) {
log(e); log(e);

View file

@ -124,6 +124,8 @@ void function(){
fs.writeFile(x.path, data, "utf-8", function(err){ fs.writeFile(x.path, data, "utf-8", function(err){
if (err) throw err if (err) throw err
console.log("File " + x.name + " saved.") console.log("File " + x.name + " saved.")
console.warn("mode/coffee/coffee-script file needs to updated manually")
console.warn("mode/coffee/parser.js: parseError function needs to be modified")
}) })
}) })
} }