Merge pull request #1378 update coffeeScript
This commit is contained in:
commit
758f500b4a
13 changed files with 1965 additions and 1403 deletions
|
|
@ -1951,12 +1951,13 @@ var requirejs, require, define;
|
|||
//This module may not have dependencies
|
||||
if (!isArray(deps)) {
|
||||
callback = deps;
|
||||
deps = [];
|
||||
deps = null;
|
||||
}
|
||||
|
||||
//If no name, and callback is a function, then figure out if it a
|
||||
//CommonJS thing with dependencies.
|
||||
if (!deps.length && isFunction(callback)) {
|
||||
if (!deps && isFunction(callback)) {
|
||||
deps = [];
|
||||
//Remove comments from the callback string,
|
||||
//look for require calls, and pull them into the dependencies,
|
||||
//but only if there are function args.
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
*
|
||||
* Copyright (c) 2010, Ajax.org B.V.
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
|
|
@ -14,7 +14,7 @@
|
|||
* * Neither the name of Ajax.org B.V. nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
*
|
||||
* 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
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
|
|
@ -27,17 +27,23 @@
|
|||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
|
||||
define(function(require, exports, module) {
|
||||
|
||||
|
||||
var Lexer = require("./lexer").Lexer;
|
||||
var parser = require("./parser");
|
||||
|
||||
var lexer = new Lexer();
|
||||
parser.lexer = {
|
||||
lex: function() {
|
||||
var tag, _ref2;
|
||||
_ref2 = this.tokens[this.pos++] || [''], tag = _ref2[0], this.yytext = _ref2[1], this.yylineno = _ref2[2];
|
||||
var tag, token;
|
||||
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;
|
||||
},
|
||||
setInput: function(tokens) {
|
||||
|
|
@ -49,7 +55,7 @@ define(function(require, exports, module) {
|
|||
}
|
||||
};
|
||||
parser.yy = require('./nodes');
|
||||
|
||||
|
||||
exports.parse = function(code) {
|
||||
return parser.parse(lexer.tokenize(code));
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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
|
||||
* obtaining a copy of this software and associated documentation
|
||||
|
|
@ -21,12 +21,13 @@
|
|||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
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) {
|
||||
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);
|
||||
};
|
||||
|
||||
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) {
|
||||
var item, _i, _len, _results;
|
||||
_results = [];
|
||||
|
|
@ -96,7 +110,7 @@ define(function(require, exports, module) {
|
|||
return val;
|
||||
};
|
||||
|
||||
exports.last = function(array, back) {
|
||||
exports.last = last = function(array, back) {
|
||||
return array[array.length - (back || 0) - 1];
|
||||
};
|
||||
|
||||
|
|
@ -111,5 +125,127 @@ define(function(require, exports, module) {
|
|||
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;
|
||||
};
|
||||
|
||||
|
||||
});
|
||||
|
|
@ -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
|
||||
* obtaining a copy of this software and associated documentation
|
||||
|
|
@ -21,42 +21,42 @@
|
|||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
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; };
|
||||
|
||||
_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() {
|
||||
|
||||
function Lexer() {}
|
||||
|
||||
Lexer.prototype.tokenize = function(code, opts) {
|
||||
var i, tag;
|
||||
var consumed, i, tag, _ref2;
|
||||
if (opts == null) {
|
||||
opts = {};
|
||||
}
|
||||
if (WHITESPACE.test(code)) {
|
||||
code = "\n" + code;
|
||||
}
|
||||
code = code.replace(/\r/g, '').replace(TRAILING_SPACES, '');
|
||||
this.code = code;
|
||||
this.line = opts.line || 0;
|
||||
this.literate = opts.literate;
|
||||
this.indent = 0;
|
||||
this.indebt = 0;
|
||||
this.outdebt = 0;
|
||||
this.indents = [];
|
||||
this.ends = [];
|
||||
this.tokens = [];
|
||||
this.chunkLine = opts.line || 0;
|
||||
this.chunkColumn = opts.column || 0;
|
||||
code = this.clean(code);
|
||||
i = 0;
|
||||
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();
|
||||
if (tag = this.ends.pop()) {
|
||||
|
|
@ -68,17 +68,34 @@ define(function(require, exports, module) {
|
|||
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() {
|
||||
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))) {
|
||||
return 0;
|
||||
}
|
||||
input = match[0], id = match[1], colon = match[2];
|
||||
idLength = id.length;
|
||||
poppedToken = void 0;
|
||||
if (id === 'own' && this.tag() === 'FOR') {
|
||||
this.token('OWN', id);
|
||||
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';
|
||||
if (!forcedIdentifier && (__indexOf.call(JS_KEYWORDS, id) >= 0 || __indexOf.call(COFFEE_KEYWORDS, id) >= 0)) {
|
||||
tag = id.toUpperCase();
|
||||
|
|
@ -97,7 +114,7 @@ define(function(require, exports, module) {
|
|||
} else {
|
||||
tag = 'RELATION';
|
||||
if (this.value() === '!') {
|
||||
this.tokens.pop();
|
||||
poppedToken = this.tokens.pop();
|
||||
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) {
|
||||
this.token(':', ':');
|
||||
colonOffset = input.lastIndexOf(':');
|
||||
this.token(':', ':', colonOffset, colon.length);
|
||||
}
|
||||
return input.length;
|
||||
};
|
||||
|
|
@ -166,7 +187,7 @@ define(function(require, exports, module) {
|
|||
if (binaryLiteral = /^0b([01]+)/.exec(number)) {
|
||||
number = '0x' + (parseInt(binaryLiteral[1], 2)).toString(16);
|
||||
}
|
||||
this.token('NUMBER', number);
|
||||
this.token('NUMBER', number, 0, lexedLength);
|
||||
return lexedLength;
|
||||
};
|
||||
|
||||
|
|
@ -177,16 +198,20 @@ define(function(require, exports, module) {
|
|||
if (!(match = SIMPLESTR.exec(this.chunk))) {
|
||||
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;
|
||||
case '"':
|
||||
if (!(string = this.balancedString(this.chunk, '"'))) {
|
||||
return 0;
|
||||
}
|
||||
if (0 < string.indexOf('#{', 1)) {
|
||||
this.interpolateString(string.slice(1, -1));
|
||||
this.interpolateString(string.slice(1, -1), {
|
||||
strOffset: 1,
|
||||
lexedLength: string.length
|
||||
});
|
||||
} else {
|
||||
this.token('STRING', this.escapeLines(string));
|
||||
this.token('STRING', this.escapeLines(string, 0, string.length));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
|
|
@ -195,7 +220,6 @@ define(function(require, exports, module) {
|
|||
if (octalEsc = /^(?:\\.|[^\\])*\\(?:0[0-7]|[1-7])/.test(string)) {
|
||||
this.error("octal escape sequences " + string + " are not allowed");
|
||||
}
|
||||
this.line += count(string, '\n');
|
||||
return string.length;
|
||||
};
|
||||
|
||||
|
|
@ -212,12 +236,13 @@ define(function(require, exports, module) {
|
|||
});
|
||||
if (quote === '"' && 0 <= doc.indexOf('#{')) {
|
||||
this.interpolateString(doc, {
|
||||
heredoc: true
|
||||
heredoc: true,
|
||||
strOffset: 3,
|
||||
lexedLength: heredoc.length
|
||||
});
|
||||
} 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;
|
||||
};
|
||||
|
||||
|
|
@ -231,9 +256,8 @@ define(function(require, exports, module) {
|
|||
this.token('HERECOMMENT', this.sanitizeHeredoc(here, {
|
||||
herecomment: true,
|
||||
indent: Array(this.indent + 1).join(' ')
|
||||
}));
|
||||
}), 0, comment.length);
|
||||
}
|
||||
this.line += count(comment, '\n');
|
||||
return comment.length;
|
||||
};
|
||||
|
||||
|
|
@ -242,8 +266,7 @@ define(function(require, exports, module) {
|
|||
if (!(this.chunk.charAt(0) === '`' && (match = JSTOKEN.exec(this.chunk)))) {
|
||||
return 0;
|
||||
}
|
||||
this.token('JS', (script = match[0]).slice(1, -1));
|
||||
this.line += count(script, '\n');
|
||||
this.token('JS', (script = match[0]).slice(1, -1), 0, script.length);
|
||||
return script.length;
|
||||
};
|
||||
|
||||
|
|
@ -254,7 +277,6 @@ define(function(require, exports, module) {
|
|||
}
|
||||
if (match = HEREGEX.exec(this.chunk)) {
|
||||
length = this.heregexToken(match);
|
||||
this.line += count(match[0], '\n');
|
||||
return length;
|
||||
}
|
||||
prev = last(this.tokens);
|
||||
|
|
@ -271,49 +293,60 @@ define(function(require, exports, module) {
|
|||
if (regex === '//') {
|
||||
regex = '/(?:)/';
|
||||
}
|
||||
this.token('REGEX', "" + regex + flags);
|
||||
this.token('REGEX', "" + regex + flags, 0, match.length);
|
||||
return match.length;
|
||||
};
|
||||
|
||||
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];
|
||||
if (0 > body.indexOf('#{')) {
|
||||
re = body.replace(HEREGEX_OMIT, '').replace(/\//g, '\\/');
|
||||
if (re.match(/^\*/)) {
|
||||
this.error('regular expressions cannot begin with `*`');
|
||||
}
|
||||
this.token('REGEX', "/" + (re || '(?:)') + "/" + flags);
|
||||
this.token('REGEX', "/" + (re || '(?:)') + "/" + flags, 0, heregex.length);
|
||||
return heregex.length;
|
||||
}
|
||||
this.token('IDENTIFIER', 'RegExp');
|
||||
this.tokens.push(['CALL_START', '(']);
|
||||
this.token('IDENTIFIER', 'RegExp', 0, 0);
|
||||
this.token('CALL_START', '(', 0, 0);
|
||||
tokens = [];
|
||||
_ref2 = this.interpolateString(body, {
|
||||
regex: true
|
||||
});
|
||||
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') {
|
||||
tokens.push.apply(tokens, value);
|
||||
} else {
|
||||
} else if (tag === 'NEOSTRING') {
|
||||
if (!(value = value.replace(HEREGEX_OMIT, ''))) {
|
||||
continue;
|
||||
}
|
||||
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();
|
||||
if (((_ref4 = tokens[0]) != null ? _ref4[0] : void 0) !== 'STRING') {
|
||||
this.tokens.push(['STRING', '""'], ['+', '+']);
|
||||
if (((_ref3 = tokens[0]) != null ? _ref3[0] : void 0) !== '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) {
|
||||
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;
|
||||
};
|
||||
|
||||
|
|
@ -323,7 +356,6 @@ define(function(require, exports, module) {
|
|||
return 0;
|
||||
}
|
||||
indent = match[0];
|
||||
this.line += count(indent, '\n');
|
||||
this.seenFor = false;
|
||||
size = indent.length - 1 - indent.lastIndexOf('\n');
|
||||
noNewlines = this.unfinished();
|
||||
|
|
@ -331,7 +363,7 @@ define(function(require, exports, module) {
|
|||
if (noNewlines) {
|
||||
this.suppressNewlines();
|
||||
} else {
|
||||
this.newlineToken();
|
||||
this.newlineToken(0);
|
||||
}
|
||||
return indent.length;
|
||||
}
|
||||
|
|
@ -342,19 +374,19 @@ define(function(require, exports, module) {
|
|||
return indent.length;
|
||||
}
|
||||
diff = size - this.indent + this.outdebt;
|
||||
this.token('INDENT', diff);
|
||||
this.token('INDENT', diff, indent.length - size, size);
|
||||
this.indents.push(diff);
|
||||
this.ends.push('OUTDENT');
|
||||
this.outdebt = this.indebt = 0;
|
||||
} else {
|
||||
this.indebt = 0;
|
||||
this.outdentToken(this.indent - size, noNewlines);
|
||||
this.outdentToken(this.indent - size, noNewlines, indent.length);
|
||||
}
|
||||
this.indent = size;
|
||||
return indent.length;
|
||||
};
|
||||
|
||||
Lexer.prototype.outdentToken = function(moveOut, noNewlines) {
|
||||
Lexer.prototype.outdentToken = function(moveOut, noNewlines, outdentLength) {
|
||||
var dent, len;
|
||||
while (moveOut > 0) {
|
||||
len = this.indents.length - 1;
|
||||
|
|
@ -367,11 +399,11 @@ define(function(require, exports, module) {
|
|||
this.outdebt -= this.indents[len];
|
||||
moveOut -= this.indents[len];
|
||||
} else {
|
||||
dent = this.indents.pop() - this.outdebt;
|
||||
dent = this.indents.pop() + this.outdebt;
|
||||
moveOut -= dent;
|
||||
this.outdebt = 0;
|
||||
this.pair('OUTDENT');
|
||||
this.token('OUTDENT', dent);
|
||||
this.token('OUTDENT', dent, 0, outdentLength);
|
||||
}
|
||||
}
|
||||
if (dent) {
|
||||
|
|
@ -381,7 +413,7 @@ define(function(require, exports, module) {
|
|||
this.tokens.pop();
|
||||
}
|
||||
if (!(this.tag() === 'TERMINATOR' || noNewlines)) {
|
||||
this.token('TERMINATOR', '\n');
|
||||
this.token('TERMINATOR', '\n', outdentLength, 0);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
|
@ -402,12 +434,12 @@ define(function(require, exports, module) {
|
|||
}
|
||||
};
|
||||
|
||||
Lexer.prototype.newlineToken = function() {
|
||||
Lexer.prototype.newlineToken = function(offset) {
|
||||
while (this.value() === ';') {
|
||||
this.tokens.pop();
|
||||
}
|
||||
if (this.tag() !== 'TERMINATOR') {
|
||||
this.token('TERMINATOR', '\n');
|
||||
this.token('TERMINATOR', '\n', offset, 0);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
|
@ -492,7 +524,7 @@ define(function(require, exports, module) {
|
|||
if (HEREDOC_ILLEGAL.test(doc)) {
|
||||
this.error("block comment cannot contain \"*/\", starting");
|
||||
}
|
||||
if (doc.indexOf('\n') <= 0) {
|
||||
if (doc.indexOf('\n') < 0) {
|
||||
return doc;
|
||||
}
|
||||
} else {
|
||||
|
|
@ -581,11 +613,18 @@ define(function(require, exports, module) {
|
|||
};
|
||||
|
||||
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) {
|
||||
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 = [];
|
||||
pi = 0;
|
||||
i = -1;
|
||||
|
|
@ -598,22 +637,24 @@ define(function(require, exports, module) {
|
|||
continue;
|
||||
}
|
||||
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);
|
||||
if (inner.length) {
|
||||
_ref2 = this.getLineAndColumnFromChunk(strOffset + i + 1), line = _ref2[0], column = _ref2[1];
|
||||
nested = new Lexer().tokenize(inner, {
|
||||
line: this.line,
|
||||
line: line,
|
||||
column: column,
|
||||
rewrite: false
|
||||
});
|
||||
nested.pop();
|
||||
if (((_ref2 = nested[0]) != null ? _ref2[0] : void 0) === 'TERMINATOR') {
|
||||
nested.shift();
|
||||
popped = nested.pop();
|
||||
if (((_ref3 = nested[0]) != null ? _ref3[0] : void 0) === 'TERMINATOR') {
|
||||
popped = nested.shift();
|
||||
}
|
||||
if (len = nested.length) {
|
||||
if (len > 1) {
|
||||
nested.unshift(['(', '(', this.line]);
|
||||
nested.push([')', ')', this.line]);
|
||||
nested.unshift(this.makeToken('(', '(', strOffset + i + 1, 0));
|
||||
nested.push(this.makeToken(')', ')', strOffset + i + 1 + inner.length, 0));
|
||||
}
|
||||
tokens.push(['TOKENS', nested]);
|
||||
}
|
||||
|
|
@ -622,33 +663,49 @@ define(function(require, exports, module) {
|
|||
pi = i + 1;
|
||||
}
|
||||
if ((i > pi && pi < str.length)) {
|
||||
tokens.push(['NEOSTRING', str.slice(pi)]);
|
||||
tokens.push(this.makeToken('NEOSTRING', str.slice(pi), strOffset + pi));
|
||||
}
|
||||
if (regex) {
|
||||
return tokens;
|
||||
}
|
||||
if (!tokens.length) {
|
||||
return this.token('STRING', '""');
|
||||
return this.token('STRING', '""', offsetInChunk, lexedLength);
|
||||
}
|
||||
if (tokens[0][0] !== 'NEOSTRING') {
|
||||
tokens.unshift(['', '']);
|
||||
tokens.unshift(this.makeToken('NEOSTRING', '', offsetInChunk));
|
||||
}
|
||||
if (interpolated = tokens.length > 1) {
|
||||
this.token('(', '(');
|
||||
this.token('(', '(', offsetInChunk, 0);
|
||||
}
|
||||
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) {
|
||||
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') {
|
||||
(_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 {
|
||||
this.token('STRING', this.makeString(value, '"', heredoc));
|
||||
this.error("Unexpected " + tag);
|
||||
}
|
||||
}
|
||||
if (interpolated) {
|
||||
this.token(')', ')');
|
||||
rparen = this.makeToken(')', ')', offsetInChunk + lexedLength, 0);
|
||||
rparen.stringEnd = true;
|
||||
this.tokens.push(rparen);
|
||||
}
|
||||
return tokens;
|
||||
};
|
||||
|
|
@ -666,8 +723,48 @@ define(function(require, exports, module) {
|
|||
return this.ends.pop();
|
||||
};
|
||||
|
||||
Lexer.prototype.token = function(tag, value) {
|
||||
return this.tokens.push([tag, value, this.line]);
|
||||
Lexer.prototype.getLineAndColumnFromChunk = function(offset) {
|
||||
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) {
|
||||
|
|
@ -682,7 +779,7 @@ define(function(require, exports, module) {
|
|||
|
||||
Lexer.prototype.unfinished = function() {
|
||||
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) {
|
||||
|
|
@ -705,7 +802,10 @@ define(function(require, exports, module) {
|
|||
};
|
||||
|
||||
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;
|
||||
|
|
@ -749,17 +849,19 @@ define(function(require, exports, module) {
|
|||
|
||||
exports.STRICT_PROSCRIBED = STRICT_PROSCRIBED;
|
||||
|
||||
BOM = 65279;
|
||||
|
||||
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;
|
||||
|
||||
HEREDOC = /^("""|''')([\s\S]*?)(?:\n[^\n\S]*)?\1/;
|
||||
|
||||
OPERATOR = /^(?:[-=]>|[-+*\/%<>&|^!?=]=|>>>=?|([-+:])\1|([&|<>])\2=?|\?\.|\.{2,3})/;
|
||||
OPERATOR = /^(?:[-=]>|[-+*\/%<>&|^!?=]=|>>>=?|([-+:])\1|([&|<>])\2=?|\?(\.|::)|\.{2,3})/;
|
||||
|
||||
WHITESPACE = /^[^\n\S]+/;
|
||||
|
||||
COMMENT = /^###([^#][\s\S]*?)(?:###[^\n\S]*|(?:###)?$)|^(?:\s*#(?!##[^#]).*)+/;
|
||||
COMMENT = /^###([^#][\s\S]*?)(?:###[^\n\S]*|(?:###)$)|^(?:\s*#(?!##[^#]).*)+/;
|
||||
|
||||
CODE = /^[-=]>/;
|
||||
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
File diff suppressed because one or more lines are too long
|
|
@ -38,9 +38,14 @@ define(function(require, exports, module) {
|
|||
var assert = require("../../test/assertions");
|
||||
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 = {
|
||||
|
||||
"test parse valid coffee script": function() {
|
||||
coffee.parse("square = (x) -> x * x");
|
||||
},
|
||||
|
|
@ -49,7 +54,25 @@ module.exports = {
|
|||
try {
|
||||
coffee.parse("a = 12 f");
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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
|
||||
* obtaining a copy of this software and associated documentation
|
||||
|
|
@ -21,17 +21,24 @@
|
|||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
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; },
|
||||
__slice = [].slice;
|
||||
|
||||
exports.Rewriter = (function() {
|
||||
generate = function(tag, value) {
|
||||
var tok;
|
||||
tok = [tag, value];
|
||||
tok.generated = true;
|
||||
return tok;
|
||||
};
|
||||
|
||||
exports.Rewriter = (function() {
|
||||
function Rewriter() {}
|
||||
|
||||
Rewriter.prototype.rewrite = function(tokens) {
|
||||
|
|
@ -42,8 +49,8 @@ define(function(require, exports, module) {
|
|||
this.closeOpenIndexes();
|
||||
this.addImplicitIndentation();
|
||||
this.tagPostfixConditionals();
|
||||
this.addImplicitBraces();
|
||||
this.addImplicitParentheses();
|
||||
this.addImplicitBracesAndParens();
|
||||
this.addLocationDataToGeneratedTokens();
|
||||
return this.tokens;
|
||||
};
|
||||
|
||||
|
|
@ -137,113 +144,240 @@ define(function(require, exports, module) {
|
|||
});
|
||||
};
|
||||
|
||||
Rewriter.prototype.addImplicitBraces = function() {
|
||||
var action, condition, sameLine, stack, start, startIndent, startIndex, startsLine;
|
||||
stack = [];
|
||||
start = null;
|
||||
startsLine = null;
|
||||
sameLine = true;
|
||||
startIndent = 0;
|
||||
startIndex = 0;
|
||||
condition = function(token, i) {
|
||||
var one, tag, three, two, _ref, _ref1;
|
||||
_ref = this.tokens.slice(i + 1, +(i + 3) + 1 || 9e9), one = _ref[0], two = _ref[1], three = _ref[2];
|
||||
if ('HERECOMMENT' === (one != null ? one[0] : void 0)) {
|
||||
Rewriter.prototype.matchTags = function() {
|
||||
var fuzz, i, j, pattern, _i, _ref, _ref1;
|
||||
i = arguments[0], pattern = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
|
||||
fuzz = 0;
|
||||
for (j = _i = 0, _ref = pattern.length; 0 <= _ref ? _i < _ref : _i > _ref; j = 0 <= _ref ? ++_i : --_i) {
|
||||
while (this.tag(i + j + fuzz) === 'HERECOMMENT') {
|
||||
fuzz += 2;
|
||||
}
|
||||
if (pattern[j] == null) {
|
||||
continue;
|
||||
}
|
||||
if (typeof pattern[j] === 'string') {
|
||||
pattern[j] = [pattern[j]];
|
||||
}
|
||||
if (_ref1 = this.tag(i + j + fuzz), __indexOf.call(pattern[j], _ref1) < 0) {
|
||||
return false;
|
||||
}
|
||||
tag = token[0];
|
||||
if (__indexOf.call(LINEBREAKS, tag) >= 0) {
|
||||
sameLine = false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
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'));
|
||||
};
|
||||
action = function(token, i) {
|
||||
var tok;
|
||||
tok = this.generate('}', '}', token[2]);
|
||||
return this.tokens.splice(i, 0, tok);
|
||||
};
|
||||
if ((_ref1 = this.tag(i), __indexOf.call(EXPRESSION_START, _ref1) >= 0) && backStack.length) {
|
||||
backStack.pop();
|
||||
}
|
||||
i -= 1;
|
||||
}
|
||||
return _ref5 = this.tag(i), __indexOf.call(tags, _ref5) >= 0;
|
||||
};
|
||||
|
||||
Rewriter.prototype.addImplicitBracesAndParens = function() {
|
||||
var stack;
|
||||
stack = [];
|
||||
return this.scanTokens(function(token, i, tokens) {
|
||||
var ago, idx, prevTag, tag, tok, value, _ref, _ref1;
|
||||
if (_ref = (tag = token[0]), __indexOf.call(EXPRESSION_START, _ref) >= 0) {
|
||||
stack.push([(tag === 'INDENT' && this.tag(i - 1) === '{' ? '{' : tag), i]);
|
||||
return 1;
|
||||
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;
|
||||
tag = token[0];
|
||||
prevTag = (i > 0 ? tokens[i - 1] : [])[0];
|
||||
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) {
|
||||
start = stack.pop();
|
||||
return 1;
|
||||
while (inImplicit()) {
|
||||
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) !== '{'))) {
|
||||
return 1;
|
||||
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))) {
|
||||
if (tag === '?') {
|
||||
tag = token[0] = 'FUNC_EXIST';
|
||||
}
|
||||
startImplicitCall(i + 1);
|
||||
return forward(2);
|
||||
}
|
||||
sameLine = true;
|
||||
startIndex = i + 1;
|
||||
stack.push(['{']);
|
||||
idx = ago === '@' ? i - 2 : i - 1;
|
||||
while (this.tag(idx - 2) === 'HERECOMMENT') {
|
||||
idx -= 2;
|
||||
if (this.matchTags(i, IMPLICIT_FUNC, 'INDENT', null, ':') && !this.findTagsBackwards(i, ['CLASS', 'EXTENDS', 'IF', 'CATCH', 'SWITCH', 'LEADING_WHEN', 'FOR', 'WHILE', 'UNTIL'])) {
|
||||
startImplicitCall(i + 1);
|
||||
stack.push(['INDENT', i + 2]);
|
||||
return forward(3);
|
||||
}
|
||||
prevTag = this.tag(idx - 1);
|
||||
startsLine = !prevTag || (__indexOf.call(LINEBREAKS, prevTag) >= 0);
|
||||
value = new String('{');
|
||||
value.generated = true;
|
||||
tok = this.generate('{', value, token[2]);
|
||||
tokens.splice(idx, 0, tok);
|
||||
this.detectEnd(i + 2, condition, action);
|
||||
return 2;
|
||||
if (tag === ':') {
|
||||
if (this.tag(i - 2) === '@') {
|
||||
s = i - 2;
|
||||
} else {
|
||||
s = i - 1;
|
||||
}
|
||||
while (this.tag(s - 2) === 'HERECOMMENT') {
|
||||
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() {
|
||||
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]));
|
||||
};
|
||||
Rewriter.prototype.addLocationDataToGeneratedTokens = function() {
|
||||
return this.scanTokens(function(token, i, tokens) {
|
||||
var callObject, current, next, prev, tag, _ref, _ref1, _ref2;
|
||||
tag = token[0];
|
||||
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) {
|
||||
var column, line, nextLocation, prevLocation, _ref, _ref1;
|
||||
if (token[2]) {
|
||||
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;
|
||||
}
|
||||
tokens.splice(i, 0, this.generate('CALL_START', '(', token[2]));
|
||||
this.detectEnd(i + 1, condition, action);
|
||||
if (prev[0] === '?') {
|
||||
prev[0] = 'FUNC_EXIST';
|
||||
if (token[0] === '{' && (nextLocation = (_ref = tokens[i + 1]) != null ? _ref[2] : void 0)) {
|
||||
line = nextLocation.first_line, column = nextLocation.first_column;
|
||||
} else if (prevLocation = (_ref1 = tokens[i - 1]) != null ? _ref1[2] : void 0) {
|
||||
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.scanTokens(function(token, i, tokens) {
|
||||
var tag, _ref, _ref1;
|
||||
var j, tag, _i, _ref, _ref1;
|
||||
tag = token[0];
|
||||
if (tag === 'TERMINATOR' && this.tag(i + 1) === 'THEN') {
|
||||
tokens.splice(i, 1);
|
||||
return 0;
|
||||
}
|
||||
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;
|
||||
}
|
||||
if (tag === 'CATCH' && ((_ref = this.tag(i + 2)) === 'OUTDENT' || _ref === 'TERMINATOR' || _ref === 'FINALLY')) {
|
||||
tokens.splice.apply(tokens, [i + 2, 0].concat(__slice.call(this.indentation(token))));
|
||||
return 4;
|
||||
if (tag === 'CATCH') {
|
||||
for (j = _i = 1; _i <= 2; j = ++_i) {
|
||||
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')) {
|
||||
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') {
|
||||
indent.fromThen = true;
|
||||
}
|
||||
|
|
@ -293,8 +432,10 @@ define(function(require, exports, module) {
|
|||
var action, condition, original;
|
||||
original = null;
|
||||
condition = function(token, i) {
|
||||
var _ref;
|
||||
return (_ref = token[0]) === 'TERMINATOR' || _ref === 'INDENT';
|
||||
var prevTag, tag;
|
||||
tag = token[0];
|
||||
prevTag = this.tokens[i - 1][0];
|
||||
return tag === 'TERMINATOR' || (tag === 'INDENT' && __indexOf.call(SINGLE_LINERS, prevTag) < 0);
|
||||
};
|
||||
action = function(token, i) {
|
||||
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;
|
||||
if (implicit == null) {
|
||||
implicit = false;
|
||||
}
|
||||
indent = ['INDENT', 2, token[2]];
|
||||
outdent = ['OUTDENT', 2, token[2]];
|
||||
indent = ['INDENT', 2];
|
||||
outdent = ['OUTDENT', 2];
|
||||
if (implicit) {
|
||||
indent.generated = outdent.generated = true;
|
||||
}
|
||||
if (!implicit) {
|
||||
indent.explicit = outdent.explicit = true;
|
||||
}
|
||||
return [indent, outdent];
|
||||
};
|
||||
|
||||
Rewriter.prototype.generate = function(tag, value, line) {
|
||||
var tok;
|
||||
tok = [tag, value, line];
|
||||
tok.generated = true;
|
||||
return tok;
|
||||
};
|
||||
Rewriter.prototype.generate = generate;
|
||||
|
||||
Rewriter.prototype.tag = function(i) {
|
||||
var _ref;
|
||||
|
|
@ -358,7 +497,7 @@ define(function(require, exports, module) {
|
|||
|
||||
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 = ['+', '-'];
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
* obtaining a copy of this software and associated documentation
|
||||
|
|
@ -21,17 +21,17 @@
|
|||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
define(function(require, exports, module) {
|
||||
// Generated by CoffeeScript 1.3.3
|
||||
// Generated by CoffeeScript 1.6.2
|
||||
|
||||
var Scope, extend, last, _ref;
|
||||
|
||||
_ref = require('./helpers'), extend = _ref.extend, last = _ref.last;
|
||||
|
||||
exports.Scope = Scope = (function() {
|
||||
|
||||
Scope.root = null;
|
||||
|
||||
function Scope(parent, expressions, method) {
|
||||
|
|
@ -65,7 +65,8 @@ define(function(require, exports, module) {
|
|||
};
|
||||
|
||||
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.parent.namedMethod();
|
||||
|
|
|
|||
|
|
@ -53,27 +53,16 @@ oop.inherits(Worker, Mirror);
|
|||
try {
|
||||
coffee.parse(value);
|
||||
} catch(e) {
|
||||
var m = e.message.match(/Parse error on line (\d+): (.*)/);
|
||||
if (m) {
|
||||
var loc = e.location;
|
||||
if (loc) {
|
||||
this.sender.emit("error", {
|
||||
row: parseInt(m[1], 10) - 1,
|
||||
column: null,
|
||||
text: m[2],
|
||||
row: loc.first_line,
|
||||
column: loc.first_column,
|
||||
endRow: loc.last_line,
|
||||
endColumn: loc.last_column,
|
||||
text: e.message,
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,15 +14,20 @@ define(function(require, exports, module) {
|
|||
}(this, 'luaparse', function (exports) {
|
||||
'use strict';
|
||||
|
||||
exports.version = '0.0.1';
|
||||
exports.version = '0.0.11';
|
||||
|
||||
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 = {
|
||||
// Explicitly tell the parser when the input ends.
|
||||
wait: false
|
||||
// Store comments as an array in the chunk object.
|
||||
, 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
|
||||
|
|
@ -30,7 +35,7 @@ define(function(require, exports, module) {
|
|||
|
||||
var EOF = 1, StringLiteral = 2, Keyword = 4, Identifier = 8
|
||||
, 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
|
||||
// will be different in some situations.
|
||||
|
|
@ -82,6 +87,13 @@ define(function(require, exports, module) {
|
|||
, clauses: clauses
|
||||
};
|
||||
}
|
||||
, ifClause: function(condition, body) {
|
||||
return {
|
||||
type: 'IfClause'
|
||||
, condition: condition
|
||||
, body: body
|
||||
};
|
||||
}
|
||||
, elseifClause: function(condition, body) {
|
||||
return {
|
||||
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 {
|
||||
type: 'FunctionDeclaration'
|
||||
, identifier: identifier
|
||||
, vararg: isVararg
|
||||
, local: isLocal
|
||||
, isLocal: isLocal
|
||||
, parameters: parameters
|
||||
, 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 {
|
||||
type: 'Literal'
|
||||
type: type
|
||||
, value: value
|
||||
, raw: raw
|
||||
};
|
||||
}
|
||||
, varargLiteral: function() {
|
||||
return {
|
||||
type: 'VarargLiteral'
|
||||
};
|
||||
}
|
||||
|
||||
, tableKey: function(key, value) {
|
||||
return {
|
||||
|
|
@ -293,7 +305,14 @@ define(function(require, exports, module) {
|
|||
// -------
|
||||
|
||||
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
|
||||
// arguments in the format string.
|
||||
|
|
@ -306,7 +325,6 @@ define(function(require, exports, module) {
|
|||
function sprintf(format) {
|
||||
var args = slice.call(arguments, 1);
|
||||
format = format.replace(/%(\d)/g, function (match, index) {
|
||||
match = ''; // jshint
|
||||
return '' + args[index - 1] || '';
|
||||
});
|
||||
return format;
|
||||
|
|
@ -455,14 +473,14 @@ define(function(require, exports, module) {
|
|||
, range: [index, index]
|
||||
};
|
||||
|
||||
var char = input.charCodeAt(index)
|
||||
var character = input.charCodeAt(index)
|
||||
, next = input.charCodeAt(index + 1);
|
||||
|
||||
// Memorize the range index where the token begins.
|
||||
tokenStart = index;
|
||||
if (isIdentifierStart(char)) return scanIdentifierOrKeyword();
|
||||
if (isIdentifierStart(character)) return scanIdentifierOrKeyword();
|
||||
|
||||
switch (char) {
|
||||
switch (character) {
|
||||
case 39: case 34: // '"
|
||||
return scanStringLiteral();
|
||||
|
||||
|
|
@ -475,7 +493,7 @@ define(function(require, exports, module) {
|
|||
// If the dot is followed by a digit it's a float.
|
||||
if (isDecDigit(next)) return scanNumericLiteral();
|
||||
if (46 === next) {
|
||||
if (46 === input.charCodeAt(index + 2)) return scanPunctuator('...');
|
||||
if (46 === input.charCodeAt(index + 2)) return scanVarargLiteral();
|
||||
return scanPunctuator('..');
|
||||
}
|
||||
return scanPunctuator('.');
|
||||
|
|
@ -520,10 +538,10 @@ define(function(require, exports, module) {
|
|||
|
||||
function skipWhiteSpace() {
|
||||
while (index < length) {
|
||||
var char = input.charCodeAt(index);
|
||||
if (isWhiteSpace(char)) {
|
||||
var character = input.charCodeAt(index);
|
||||
if (isWhiteSpace(character)) {
|
||||
index++;
|
||||
} else if (isLineTerminator(char)) {
|
||||
} else if (isLineTerminator(character)) {
|
||||
line++;
|
||||
lineStart = ++index;
|
||||
} 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.
|
||||
|
||||
function scanStringLiteral() {
|
||||
var delimiter = input.charCodeAt(index++)
|
||||
, stringStart = index
|
||||
, string = ''
|
||||
, char;
|
||||
, character;
|
||||
|
||||
while (index < length) {
|
||||
char = input.charCodeAt(index++);
|
||||
if (delimiter === char) break;
|
||||
if (92 === char) { // \
|
||||
character = input.charCodeAt(index++);
|
||||
if (delimiter === character) break;
|
||||
if (92 === character) { // \
|
||||
string += input.slice(stringStart, index - 1) + readEscapeSequence();
|
||||
stringStart = index;
|
||||
}
|
||||
// EOF or `\n` terminates a string literal. If we haven't found the
|
||||
// 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);
|
||||
raise({}, errors.unfinishedString, string + String.fromCharCode(char));
|
||||
raise({}, errors.unfinishedString, string + String.fromCharCode(character));
|
||||
}
|
||||
}
|
||||
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.
|
||||
|
||||
function scanNumericLiteral() {
|
||||
var char = input.charAt(index)
|
||||
var character = input.charAt(index)
|
||||
, next = input.charAt(index + 1);
|
||||
|
||||
var value = ('0' === char && ~'xX'.indexOf(next || null)) ?
|
||||
var value = ('0' === character && 'xX'.indexOf(next || null) >= 0) ?
|
||||
readHexLiteral() : readDecLiteral();
|
||||
|
||||
return {
|
||||
|
|
@ -693,11 +724,11 @@ define(function(require, exports, module) {
|
|||
}
|
||||
|
||||
// Binary exponents are optional
|
||||
if (~'pP'.indexOf(input.charAt(index) || null)) {
|
||||
if ('pP'.indexOf(input.charAt(index) || null) >= 0) {
|
||||
index++;
|
||||
|
||||
// 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;
|
||||
|
||||
exponentStart = index;
|
||||
|
|
@ -729,10 +760,10 @@ define(function(require, exports, module) {
|
|||
while (isDecDigit(input.charCodeAt(index))) index++;
|
||||
}
|
||||
// Exponent part is optional.
|
||||
if (~'eE'.indexOf(input.charAt(index) || null)) {
|
||||
if ('eE'.indexOf(input.charAt(index) || null) >= 0) {
|
||||
index++;
|
||||
// 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.
|
||||
if (!isDecDigit(input.charCodeAt(index)))
|
||||
raise({}, errors.malformedNumber, input.slice(tokenStart, index));
|
||||
|
|
@ -754,7 +785,7 @@ define(function(require, exports, module) {
|
|||
case 'n': index++; return '\n';
|
||||
case 'r': index++; return '\r';
|
||||
case 't': index++; return '\t';
|
||||
case 'v': index++; return '\v';
|
||||
case 'v': index++; return '\x0B';
|
||||
case 'b': index++; return '\b';
|
||||
case 'f': index++; return '\f';
|
||||
// Skips the following span of white-space.
|
||||
|
|
@ -790,19 +821,16 @@ define(function(require, exports, module) {
|
|||
tokenStart = index;
|
||||
index += 2; // --
|
||||
|
||||
var char = input.charAt(index)
|
||||
var character = input.charAt(index)
|
||||
, content = ''
|
||||
, isLong = false
|
||||
, commentStart = index;
|
||||
|
||||
if ('[' === char) {
|
||||
if ('[' === character) {
|
||||
content = readLongString();
|
||||
// This wasn't a multiline comment after all.
|
||||
if (false === content) content = char;
|
||||
else {
|
||||
isLong = true;
|
||||
index += 2; // Trailing --
|
||||
}
|
||||
if (false === content) content = character;
|
||||
else isLong = true;
|
||||
}
|
||||
// Scan until next line as long as it's not a multiline comment.
|
||||
if (!isLong) {
|
||||
|
|
@ -829,7 +857,7 @@ define(function(require, exports, module) {
|
|||
var level = 0
|
||||
, content = ''
|
||||
, terminator = false
|
||||
, char, stringStart;
|
||||
, character, stringStart;
|
||||
|
||||
index++; // [
|
||||
|
||||
|
|
@ -848,11 +876,11 @@ define(function(require, exports, module) {
|
|||
|
||||
stringStart = index;
|
||||
while (index < length) {
|
||||
char = input.charAt(index++);
|
||||
character = input.charAt(index++);
|
||||
|
||||
// We have to keep track of newlines as `skipWhiteSpace()` does not get
|
||||
// to scan this part.
|
||||
if (isLineTerminator(char.charCodeAt(0))) {
|
||||
if (isLineTerminator(character.charCodeAt(0))) {
|
||||
line++;
|
||||
lineStart = index;
|
||||
}
|
||||
|
|
@ -860,7 +888,7 @@ define(function(require, exports, module) {
|
|||
// Once the delimiter is found, iterate through the depth count and see
|
||||
// if it matches.
|
||||
|
||||
if (']' === char) {
|
||||
if (']' === character) {
|
||||
terminator = true;
|
||||
for (var i = 0; i < level; i++) {
|
||||
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.
|
||||
if (terminator) break;
|
||||
|
||||
if ('\\' === char) {
|
||||
content += input.slice(stringStart, index - 1) + readEscapeSequence();
|
||||
stringStart = index;
|
||||
}
|
||||
}
|
||||
content += input.slice(stringStart, index - 1);
|
||||
index += level + 1;
|
||||
|
|
@ -905,16 +928,6 @@ define(function(require, exports, module) {
|
|||
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.
|
||||
|
||||
function expect(value) {
|
||||
|
|
@ -924,31 +937,31 @@ define(function(require, exports, module) {
|
|||
|
||||
// ### Validation functions
|
||||
|
||||
function isWhiteSpace(char) {
|
||||
return 9 === char || 32 === char || 0xB === char || 0xC === char;
|
||||
function isWhiteSpace(character) {
|
||||
return 9 === character || 32 === character || 0xB === character || 0xC === character;
|
||||
}
|
||||
|
||||
function isLineTerminator(char) {
|
||||
return 10 === char || 13 === char;
|
||||
function isLineTerminator(character) {
|
||||
return 10 === character || 13 === character;
|
||||
}
|
||||
|
||||
function isDecDigit(char) {
|
||||
return char >= 48 && char <= 57;
|
||||
function isDecDigit(character) {
|
||||
return character >= 48 && character <= 57;
|
||||
}
|
||||
|
||||
function isHexDigit(char) {
|
||||
return (char >= 48 && char <= 57) || (char >= 97 && char <= 102) || (char >= 65 && char <= 70);
|
||||
function isHexDigit(character) {
|
||||
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
|
||||
// identifiers cannot use locale-dependet letters.
|
||||
|
||||
function isIdentifierStart(char) {
|
||||
return (char >= 65 && char <= 90) || (char >= 97 && char <= 122) || 95 === char;
|
||||
function isIdentifierStart(character) {
|
||||
return (character >= 65 && character <= 90) || (character >= 97 && character <= 122) || 95 === character;
|
||||
}
|
||||
|
||||
function isIdentifierPart(char) {
|
||||
return (char >= 65 && char <= 90) || (char >= 97 && char <= 122) || 95 === char || (char >= 48 && char <= 57);
|
||||
function isIdentifierPart(character) {
|
||||
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)
|
||||
|
|
@ -974,7 +987,7 @@ define(function(require, exports, module) {
|
|||
}
|
||||
|
||||
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;
|
||||
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
|
||||
// ---------------
|
||||
|
||||
|
|
@ -1027,6 +1095,9 @@ define(function(require, exports, module) {
|
|||
var block = []
|
||||
, statement;
|
||||
|
||||
// Each block creates a new scope.
|
||||
if (options.scope) createScope();
|
||||
|
||||
while (!isBlockFollow(token)) {
|
||||
// Return has to be the last statement in a block.
|
||||
if ('return' === token.value) {
|
||||
|
|
@ -1038,6 +1109,8 @@ define(function(require, exports, module) {
|
|||
// ignore some statements, such as EmptyStatement.
|
||||
if (statement) block.push(statement);
|
||||
}
|
||||
|
||||
if (options.scope) exitScope();
|
||||
// Doesn't really need an ast node
|
||||
return block;
|
||||
}
|
||||
|
|
@ -1081,7 +1154,14 @@ define(function(require, exports, module) {
|
|||
// label ::= '::' Name '::'
|
||||
|
||||
function parseLabelStatement() {
|
||||
var label = parseIdentifier();
|
||||
var name = token.value
|
||||
, label = parseIdentifier();
|
||||
|
||||
if (options.scope) {
|
||||
scopeIdentifierName('::' + name + '::');
|
||||
attachScope(label, true);
|
||||
}
|
||||
|
||||
expect('::');
|
||||
return ast.labelStatement(label);
|
||||
}
|
||||
|
|
@ -1095,7 +1175,10 @@ define(function(require, exports, module) {
|
|||
// goto ::= 'goto' Name
|
||||
|
||||
function parseGotoStatement() {
|
||||
var label = parseIdentifier();
|
||||
var name = token.value
|
||||
, label = parseIdentifier();
|
||||
|
||||
if (options.scope) label.isLabel = scopeHasName('::' + name + '::');
|
||||
return ast.gotoStatement(label);
|
||||
}
|
||||
|
||||
|
|
@ -1110,7 +1193,7 @@ define(function(require, exports, module) {
|
|||
// while ::= 'while' exp 'do' block 'end'
|
||||
|
||||
function parseWhileStatement() {
|
||||
var condition = parseExpression();
|
||||
var condition = parseExpectedExpression();
|
||||
expect('do');
|
||||
var body = parseBlock();
|
||||
expect('end');
|
||||
|
|
@ -1122,7 +1205,7 @@ define(function(require, exports, module) {
|
|||
function parseRepeatStatement() {
|
||||
var body = parseBlock();
|
||||
expect('until');
|
||||
var condition = expectExpression(parseExpression());
|
||||
var condition = parseExpectedExpression();
|
||||
return ast.repeatStatement(condition, body);
|
||||
}
|
||||
|
||||
|
|
@ -1135,7 +1218,7 @@ define(function(require, exports, module) {
|
|||
var expression = parseExpression();
|
||||
if (null != expression) expressions.push(expression);
|
||||
while (consume(',')) {
|
||||
expression = expectExpression(parseExpression());
|
||||
expression = parseExpectedExpression();
|
||||
expressions.push(expression);
|
||||
}
|
||||
consume(';'); // grammar tells us ; is optional here.
|
||||
|
|
@ -1151,12 +1234,17 @@ define(function(require, exports, module) {
|
|||
, condition
|
||||
, body;
|
||||
|
||||
do {
|
||||
condition = parseExpression();
|
||||
condition = parseExpectedExpression();
|
||||
expect('then');
|
||||
body = parseBlock();
|
||||
clauses.push(ast.ifClause(condition, body));
|
||||
|
||||
while (consume('elseif')) {
|
||||
condition = parseExpectedExpression();
|
||||
expect('then');
|
||||
body = parseBlock();
|
||||
clauses.push(ast.elseifClause(condition, body));
|
||||
} while (consume('elseif'));
|
||||
}
|
||||
|
||||
if (consume('else')) {
|
||||
body = parseBlock();
|
||||
|
|
@ -1178,16 +1266,19 @@ define(function(require, exports, module) {
|
|||
var variable = parseIdentifier()
|
||||
, body;
|
||||
|
||||
// The start-identifier is local.
|
||||
if (options.scope) scopeIdentifier(variable);
|
||||
|
||||
// If the first expression is followed by a `=` punctuator, this is a
|
||||
// Numeric For Statement.
|
||||
if (consume('=')) {
|
||||
// Start expression
|
||||
var start = expectExpression(parseExpression());
|
||||
var start = parseExpectedExpression();
|
||||
expect(',');
|
||||
// End expression
|
||||
var end = expectExpression(parseExpression());
|
||||
var end = parseExpectedExpression();
|
||||
// Optional step expression
|
||||
var step = consume(',') ? expectExpression(parseExpression()) : null;
|
||||
var step = consume(',') ? parseExpectedExpression() : null;
|
||||
|
||||
expect('do');
|
||||
body = parseBlock();
|
||||
|
|
@ -1199,13 +1290,18 @@ define(function(require, exports, module) {
|
|||
} else {
|
||||
// The namelist can contain one or more identifiers.
|
||||
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');
|
||||
var iterators = [];
|
||||
|
||||
// One or more expressions in the explist.
|
||||
do {
|
||||
var expression = expectExpression(parseExpression());
|
||||
var expression = parseExpectedExpression();
|
||||
iterators.push(expression);
|
||||
} while (consume(','));
|
||||
|
||||
|
|
@ -1228,26 +1324,41 @@ define(function(require, exports, module) {
|
|||
// | 'local' Name {',' Name} ['=' exp {',' exp}
|
||||
|
||||
function parseLocalStatement() {
|
||||
var name;
|
||||
|
||||
if (Identifier === token.type) {
|
||||
var variables = [];
|
||||
var init = [];
|
||||
var variables = []
|
||||
, init = [];
|
||||
|
||||
do {
|
||||
variables.push(parseIdentifier());
|
||||
name = parseIdentifier();
|
||||
|
||||
variables.push(name);
|
||||
} while (consume(','));
|
||||
|
||||
if (consume('=')) {
|
||||
do {
|
||||
var expression = expectExpression(parseExpression());
|
||||
var expression = parseExpectedExpression();
|
||||
init.push(expression);
|
||||
} 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);
|
||||
}
|
||||
if (consume('function')) {
|
||||
name = parseIdentifier();
|
||||
if (options.scope) scopeIdentifier(name);
|
||||
|
||||
// MemberExpressions are not allowed in local function statements.
|
||||
var name = parseIdentifier();
|
||||
return parseFunctionDeclaration(name, true);
|
||||
} else {
|
||||
raiseUnexpectedToken('<name>', token);
|
||||
|
|
@ -1268,18 +1379,19 @@ define(function(require, exports, module) {
|
|||
, expression = parsePrefixExpression();
|
||||
|
||||
if (null == expression) return unexpected(token);
|
||||
if (~',='.indexOf(token.value)) {
|
||||
if (',='.indexOf(token.value) >= 0) {
|
||||
var variables = [expression]
|
||||
, init = []
|
||||
, exp;
|
||||
|
||||
while (consume(',')) {
|
||||
exp = expectExpression(parsePrefixExpression());
|
||||
exp = parsePrefixExpression();
|
||||
if (null == exp) raiseUnexpectedToken('<expression>', token);
|
||||
variables.push(exp);
|
||||
}
|
||||
expect('=');
|
||||
do {
|
||||
exp = expectExpression(parseExpression());
|
||||
exp = parseExpectedExpression();
|
||||
init.push(exp);
|
||||
} while (consume(','));
|
||||
return ast.assignmentStatement(variables, init);
|
||||
|
|
@ -1306,7 +1418,6 @@ define(function(require, exports, module) {
|
|||
return ast.identifier(identifier);
|
||||
}
|
||||
|
||||
|
||||
// Parse the functions parameters and body block. The name should already
|
||||
// have been parsed and passed to this declaration function. By separating
|
||||
// this we allow for anonymous functions in expressions.
|
||||
|
|
@ -1318,28 +1429,39 @@ define(function(require, exports, module) {
|
|||
// parlist ::= Name {',' Name} | [',' '...'] | '...'
|
||||
|
||||
function parseFunctionDeclaration(name, isLocal) {
|
||||
var isVararg = false;
|
||||
var parameters = [];
|
||||
expect('(');
|
||||
|
||||
if (consume('...')) isVararg = true;
|
||||
else if (Identifier === token.type) {
|
||||
do {
|
||||
if (consume('...')) {
|
||||
isVararg = true;
|
||||
// The declaration has arguments
|
||||
if (!consume(')')) {
|
||||
// Arguments are a comma separated list of identifiers, optionally ending
|
||||
// with a vararg.
|
||||
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;
|
||||
} else {
|
||||
raiseUnexpectedToken('<name> or \'...\'', token);
|
||||
}
|
||||
parameters.push(parseIdentifier());
|
||||
} while (consume(','));
|
||||
}
|
||||
}
|
||||
if (isVararg) expect(')');
|
||||
else if (!consume(')')) raiseUnexpectedToken('<name> or \'...\'', token);
|
||||
|
||||
var body = parseBlock();
|
||||
expect('end');
|
||||
|
||||
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.
|
||||
|
|
@ -1347,14 +1469,20 @@ define(function(require, exports, module) {
|
|||
// Name {'.' Name} [':' Name]
|
||||
|
||||
function parseFunctionName() {
|
||||
var base = parseIdentifier();
|
||||
var base = parseIdentifier()
|
||||
, name;
|
||||
if (options.scope) attachScope(base, false);
|
||||
|
||||
while (consume('.')) {
|
||||
base = ast.memberExpression(base, '.', parseIdentifier());
|
||||
name = parseIdentifier();
|
||||
if (options.scope) attachScope(name, false);
|
||||
base = ast.memberExpression(base, '.', name);
|
||||
}
|
||||
|
||||
if (consume(':')) {
|
||||
base = ast.memberExpression(base, ':', parseIdentifier());
|
||||
name = parseIdentifier();
|
||||
if (options.scope) attachScope(name, false);
|
||||
base = ast.memberExpression(base, ':', name);
|
||||
}
|
||||
|
||||
return base;
|
||||
|
|
@ -1372,15 +1500,15 @@ define(function(require, exports, module) {
|
|||
|
||||
while (true) {
|
||||
if (Punctuator === token.type && consume('[')) {
|
||||
key = parseExpression();
|
||||
key = parseExpectedExpression();
|
||||
expect(']');
|
||||
expect('=');
|
||||
value = expectExpression(parseExpression());
|
||||
value = parseExpectedExpression();
|
||||
fields.push(ast.tableKey(key, value));
|
||||
} else if (Identifier === token.type) {
|
||||
key = parseExpression();
|
||||
key = parseExpectedExpression();
|
||||
if (consume('=')) {
|
||||
value = parseExpression();
|
||||
value = parseExpectedExpression();
|
||||
fields.push(ast.tableKeyString(key, value));
|
||||
} else {
|
||||
fields.push(ast.tableValue(key));
|
||||
|
|
@ -1389,7 +1517,7 @@ define(function(require, exports, module) {
|
|||
if (null == (value = parseExpression())) break;
|
||||
fields.push(ast.tableValue(value));
|
||||
}
|
||||
if (~',;'.indexOf(token.value)) {
|
||||
if (',;'.indexOf(token.value) >= 0) {
|
||||
next();
|
||||
continue;
|
||||
}
|
||||
|
|
@ -1402,7 +1530,8 @@ define(function(require, exports, module) {
|
|||
// 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 }
|
||||
//
|
||||
|
|
@ -1418,6 +1547,15 @@ define(function(require, exports, module) {
|
|||
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.
|
||||
//
|
||||
// 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.
|
||||
|
||||
function binaryPrecedence(operator) {
|
||||
var char = operator.charCodeAt(0)
|
||||
var character = operator.charCodeAt(0)
|
||||
, length = operator.length;
|
||||
|
||||
if (1 === length) {
|
||||
switch (char) {
|
||||
switch (character) {
|
||||
case 94: return 10; // ^
|
||||
case 42: case 47: case 37: return 7; // * / %
|
||||
case 43: case 45: return 6; // + -
|
||||
case 60: case 62: return 3; // < >
|
||||
}
|
||||
} else if (2 === length) {
|
||||
switch (char) {
|
||||
switch (character) {
|
||||
case 46: return 5; // ..
|
||||
case 60: case 62: case 61: case 126: return 3; // <= >= == ~=
|
||||
case 111: return 1; // or
|
||||
}
|
||||
} else if (97 === char && 'and' === operator) return 2;
|
||||
} else if (97 === character && 'and' === operator) return 2;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -1464,7 +1602,8 @@ define(function(require, exports, module) {
|
|||
// UnaryExpression
|
||||
if (isUnary(token)) {
|
||||
next();
|
||||
var argument = expectExpression(parseSubExpression(8));
|
||||
var argument = parseSubExpression(8);
|
||||
if (argument == null) raiseUnexpectedToken('<expression>', token);
|
||||
expression = ast.unaryExpression(operator, argument);
|
||||
}
|
||||
if (null == expression) {
|
||||
|
|
@ -1490,7 +1629,8 @@ define(function(require, exports, module) {
|
|||
// Right-hand precedence operators
|
||||
if ('^' === operator || '..' === operator) precedence--;
|
||||
next();
|
||||
var right = expectExpression(parseSubExpression(precedence));
|
||||
var right = parseSubExpression(precedence);
|
||||
if (null == right) raiseUnexpectedToken('<expression>', token);
|
||||
expression = ast.binaryExpression(operator, expression, right);
|
||||
}
|
||||
return expression;
|
||||
|
|
@ -1503,14 +1643,20 @@ define(function(require, exports, module) {
|
|||
// args ::= '(' [explist] ')' | tableconstructor | String
|
||||
|
||||
function parsePrefixExpression() {
|
||||
var base;
|
||||
var base, name
|
||||
// Keep track of the scope, if a parent is local so are the children.
|
||||
, isLocal;
|
||||
|
||||
// The prefix
|
||||
if (Identifier === token.type) {
|
||||
name = token.value;
|
||||
base = parseIdentifier();
|
||||
// Set the parent scope.
|
||||
if (options.scope) attachScope(base, isLocal = scopeHasName(name));
|
||||
} else if (consume('(')) {
|
||||
base = parseExpression();
|
||||
base = parseExpectedExpression();
|
||||
expect(')');
|
||||
if (options.scope) isLocal = base.isLocal;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
|
@ -1518,23 +1664,25 @@ define(function(require, exports, module) {
|
|||
// The suffix
|
||||
var expression, identifier;
|
||||
while (true) {
|
||||
expectExpression(base);
|
||||
if (Punctuator === token.type) {
|
||||
switch (token.value) {
|
||||
case '[':
|
||||
next();
|
||||
expression = parseExpression();
|
||||
expression = parseExpectedExpression();
|
||||
base = ast.indexExpression(base, expression);
|
||||
expect(']');
|
||||
break;
|
||||
case '.':
|
||||
next();
|
||||
identifier = parseIdentifier();
|
||||
// Inherit the scope
|
||||
if (options.scope) attachScope(identifier, isLocal);
|
||||
base = ast.memberExpression(base, '.', identifier);
|
||||
break;
|
||||
case ':':
|
||||
next();
|
||||
identifier = parseIdentifier();
|
||||
if (options.scope) attachScope(identifier, isLocal);
|
||||
base = ast.memberExpression(base, ':', identifier);
|
||||
// Once a : is found, this has to be a callexpression, otherwise
|
||||
// throw an error.
|
||||
|
|
@ -1569,7 +1717,7 @@ define(function(require, exports, module) {
|
|||
var expression = parseExpression();
|
||||
if (null != expression) expressions.push(expression);
|
||||
while (consume(',')) {
|
||||
expression = expectExpression(parseExpression());
|
||||
expression = parseExpectedExpression();
|
||||
expressions.push(expression);
|
||||
}
|
||||
|
||||
|
|
@ -1583,9 +1731,7 @@ define(function(require, exports, module) {
|
|||
}
|
||||
|
||||
} else if (StringLiteral === token.type) {
|
||||
var string = token.value;
|
||||
next();
|
||||
return ast.stringCallExpression(base, string);
|
||||
return ast.stringCallExpression(base, parsePrimaryExpression());
|
||||
}
|
||||
|
||||
raiseUnexpectedToken('function arguments', token);
|
||||
|
|
@ -1595,21 +1741,19 @@ define(function(require, exports, module) {
|
|||
// | functiondef | tableconstructor | '...'
|
||||
|
||||
function parsePrimaryExpression() {
|
||||
var literals = StringLiteral | NumericLiteral | BooleanLiteral | NilLiteral
|
||||
, value = token.value;
|
||||
var literals = StringLiteral | NumericLiteral | BooleanLiteral | NilLiteral | VarargLiteral
|
||||
, value = token.value
|
||||
, type = token.type;
|
||||
|
||||
if (token.type & literals) {
|
||||
if (type & literals) {
|
||||
var raw = input.slice(token.range[0], token.range[1]);
|
||||
next();
|
||||
return ast.literal(value, raw);
|
||||
} else if (Keyword === token.type && 'function' === token.value) {
|
||||
return ast.literal(type, value, raw);
|
||||
} else if (Keyword === type && 'function' === value) {
|
||||
next();
|
||||
return parseFunctionDeclaration(null);
|
||||
} else if (Punctuator === token.type) {
|
||||
// Semantically dotsliteral can only exist within a vararg functions.
|
||||
if (consume('...')) return ast.varargLiteral(value);
|
||||
if (consume('{')) return parseTableConstructor();
|
||||
}
|
||||
} else if (consume('{'))
|
||||
return parseTableConstructor();
|
||||
}
|
||||
|
||||
// Parser
|
||||
|
|
@ -1618,6 +1762,8 @@ define(function(require, exports, module) {
|
|||
// Export the main parser.
|
||||
//
|
||||
// - `wait` Hold parsing until end() is called. Defaults to false
|
||||
// - `comments` Store comments. Defaults to true.
|
||||
// - `scope` Track identifier scope. Defaults to false.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
|
|
@ -1641,6 +1787,11 @@ define(function(require, exports, module) {
|
|||
line = 1;
|
||||
lineStart = 0;
|
||||
length = input.length;
|
||||
// When tracking identifier scope, initialize with an empty scope.
|
||||
scopes = [[]];
|
||||
scopeDepth = 0;
|
||||
globals = [];
|
||||
globalNames = [];
|
||||
|
||||
if (options.comments) comments = [];
|
||||
if (!options.wait) return end();
|
||||
|
|
@ -1668,6 +1819,7 @@ define(function(require, exports, module) {
|
|||
|
||||
var chunk = parseChunk();
|
||||
if (options.comments) chunk.comments = comments;
|
||||
if (options.scope) chunk.globals = globals;
|
||||
return chunk;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -145,15 +145,20 @@ function run() {
|
|||
var src = editor1.getValue();
|
||||
var path = "ace/mode/new";
|
||||
var deps = getDeps(src, path);
|
||||
src = src.replace("define(", 'define("' + path +'", ["require","exports","module",' + deps +'],');
|
||||
src += ';require(["ace/mode/new"], function(e) {\
|
||||
try{continueRun(e)}catch(e){log(e)}\
|
||||
}, function(e){\
|
||||
log(e);\
|
||||
window.require.undef("ace/mode/new")\
|
||||
});';
|
||||
window.require.undef(path)
|
||||
src = src.replace("define(", 'define("' + path +'", ["require","exports","module",' + deps +'],');
|
||||
try {
|
||||
eval(src);
|
||||
require(["ace/mode/new"], function(e) {
|
||||
try{
|
||||
continueRun(e)
|
||||
}catch(e){
|
||||
log(e)
|
||||
}
|
||||
}, function(e) {
|
||||
log(e);
|
||||
window.require.undef(path)
|
||||
});
|
||||
hideLog()
|
||||
} catch(e) {
|
||||
log(e);
|
||||
|
|
|
|||
|
|
@ -124,6 +124,8 @@ void function(){
|
|||
fs.writeFile(x.path, data, "utf-8", function(err){
|
||||
if (err) throw err
|
||||
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")
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue