update narcissus. fix #365
This commit is contained in:
parent
69d8e19314
commit
1b02f2895e
4 changed files with 2817 additions and 1948 deletions
|
|
@ -87,7 +87,7 @@ var Document = function(text) {
|
||||||
|
|
||||||
|
|
||||||
this.$detectNewLine = function(text) {
|
this.$detectNewLine = function(text) {
|
||||||
var match = text.match(/^.*?(\r?\n)/m);
|
var match = text.match(/^.*?(\r\n|\r|\n)/m);
|
||||||
if (match) {
|
if (match) {
|
||||||
this.$autoNewLine = match[1];
|
this.$autoNewLine = match[1];
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -111,7 +111,8 @@ var Document = function(text) {
|
||||||
this.$autoNewLine = "\n";
|
this.$autoNewLine = "\n";
|
||||||
this.$newLineMode = "auto";
|
this.$newLineMode = "auto";
|
||||||
this.setNewLineMode = function(newLineMode) {
|
this.setNewLineMode = function(newLineMode) {
|
||||||
if (this.$newLineMode === newLineMode) return;
|
if (this.$newLineMode === newLineMode)
|
||||||
|
return;
|
||||||
|
|
||||||
this.$newLineMode = newLineMode;
|
this.$newLineMode = newLineMode;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -52,13 +52,24 @@
|
||||||
|
|
||||||
define(function(require, exports, module) {
|
define(function(require, exports, module) {
|
||||||
|
|
||||||
exports.options = {
|
var narcissus = {
|
||||||
|
options: {
|
||||||
version: 185,
|
version: 185,
|
||||||
|
// Global variables to hide from the interpreter
|
||||||
|
hiddenHostGlobals: { Narcissus: true },
|
||||||
|
// Desugar SpiderMonkey language extensions?
|
||||||
|
desugarExtensions: false
|
||||||
|
},
|
||||||
|
hostSupportsEvalConst: (function() {
|
||||||
|
try {
|
||||||
|
return eval("(function(s) { eval(s); return x })('const x = true;')");
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
})(),
|
||||||
|
hostGlobal: this
|
||||||
};
|
};
|
||||||
|
Narcissus = narcissus;
|
||||||
(function() {
|
|
||||||
exports.hostGlobal = this
|
|
||||||
})();
|
|
||||||
|
|
||||||
var tokens = [
|
var tokens = [
|
||||||
// End of source.
|
// End of source.
|
||||||
|
|
@ -99,10 +110,10 @@ var tokens = [
|
||||||
"break",
|
"break",
|
||||||
"case", "catch", "const", "continue",
|
"case", "catch", "const", "continue",
|
||||||
"debugger", "default", "delete", "do",
|
"debugger", "default", "delete", "do",
|
||||||
"else",
|
"else", "export",
|
||||||
"false", "finally", "for", "function",
|
"false", "finally", "for", "function",
|
||||||
"if", "in", "instanceof",
|
"if", "import", "in", "instanceof",
|
||||||
"let",
|
"let", "module",
|
||||||
"new", "null",
|
"new", "null",
|
||||||
"return",
|
"return",
|
||||||
"switch",
|
"switch",
|
||||||
|
|
@ -126,6 +137,23 @@ var statementStartTokens = [
|
||||||
"while", "with",
|
"while", "with",
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// Whitespace characters (see ECMA-262 7.2)
|
||||||
|
var whitespaceChars = [
|
||||||
|
// normal whitespace:
|
||||||
|
"\u0009", "\u000B", "\u000C", "\u0020", "\u00A0", "\uFEFF",
|
||||||
|
|
||||||
|
// high-Unicode whitespace:
|
||||||
|
"\u1680", "\u180E",
|
||||||
|
"\u2000", "\u2001", "\u2002", "\u2003", "\u2004", "\u2005", "\u2006",
|
||||||
|
"\u2007", "\u2008", "\u2009", "\u200A",
|
||||||
|
"\u202F", "\u205F", "\u3000"
|
||||||
|
];
|
||||||
|
|
||||||
|
var whitespace = {};
|
||||||
|
for (var i = 0; i < whitespaceChars.length; i++) {
|
||||||
|
whitespace[whitespaceChars[i]] = true;
|
||||||
|
}
|
||||||
|
|
||||||
// Operator and punctuator mapping from token to tree node type name.
|
// Operator and punctuator mapping from token to tree node type name.
|
||||||
// NB: because the lexer doesn't backtrack, all token prefixes must themselves
|
// NB: because the lexer doesn't backtrack, all token prefixes must themselves
|
||||||
// be valid tokens (e.g. !== is acceptable because its prefixes are the valid
|
// be valid tokens (e.g. !== is acceptable because its prefixes are the valid
|
||||||
|
|
@ -179,7 +207,7 @@ var keywords = {__proto__: null};
|
||||||
var tokenIds = {};
|
var tokenIds = {};
|
||||||
|
|
||||||
// Building up a string to be eval'd in different contexts.
|
// Building up a string to be eval'd in different contexts.
|
||||||
var consts = "const ";
|
var consts = Narcissus.hostSupportsEvalConst ? "const " : "var ";
|
||||||
for (var i = 0, j = tokens.length; i < j; i++) {
|
for (var i = 0, j = tokens.length; i < j; i++) {
|
||||||
if (i > 0)
|
if (i > 0)
|
||||||
consts += ", ";
|
consts += ", ";
|
||||||
|
|
@ -214,6 +242,27 @@ function defineGetter(obj, prop, fn, dontDelete, dontEnum) {
|
||||||
{ get: fn, configurable: !dontDelete, enumerable: !dontEnum });
|
{ get: fn, configurable: !dontDelete, enumerable: !dontEnum });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function defineGetterSetter(obj, prop, getter, setter, dontDelete, dontEnum) {
|
||||||
|
Object.defineProperty(obj, prop, {
|
||||||
|
get: getter,
|
||||||
|
set: setter,
|
||||||
|
configurable: !dontDelete,
|
||||||
|
enumerable: !dontEnum
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function defineMemoGetter(obj, prop, fn, dontDelete, dontEnum) {
|
||||||
|
Object.defineProperty(obj, prop, {
|
||||||
|
get: function() {
|
||||||
|
var val = fn();
|
||||||
|
defineProperty(obj, prop, val, dontDelete, true, dontEnum);
|
||||||
|
return val;
|
||||||
|
},
|
||||||
|
configurable: true,
|
||||||
|
enumerable: !dontEnum
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function defineProperty(obj, prop, val, dontDelete, readOnly, dontEnum) {
|
function defineProperty(obj, prop, val, dontDelete, readOnly, dontEnum) {
|
||||||
Object.defineProperty(obj, prop,
|
Object.defineProperty(obj, prop,
|
||||||
{ value: val, writable: !readOnly, configurable: !dontDelete,
|
{ value: val, writable: !readOnly, configurable: !dontDelete,
|
||||||
|
|
@ -234,6 +283,17 @@ function getPropertyDescriptor(obj, name) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getPropertyNames(obj) {
|
||||||
|
var table = Object.create(null, {});
|
||||||
|
while (obj) {
|
||||||
|
var names = Object.getOwnPropertyNames(obj);
|
||||||
|
for (var i = 0, n = names.length; i < n; i++)
|
||||||
|
table[names[i]] = true;
|
||||||
|
obj = Object.getPrototypeOf(obj);
|
||||||
|
}
|
||||||
|
return Object.keys(table);
|
||||||
|
}
|
||||||
|
|
||||||
function getOwnProperties(obj) {
|
function getOwnProperties(obj) {
|
||||||
var map = {};
|
var map = {};
|
||||||
for (var name in Object.getOwnPropertyNames(obj))
|
for (var name in Object.getOwnPropertyNames(obj))
|
||||||
|
|
@ -241,6 +301,125 @@ function getOwnProperties(obj) {
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function blacklistHandler(target, blacklist) {
|
||||||
|
var mask = Object.create(null, {});
|
||||||
|
var redirect = StringMap.create(blacklist).mapObject(function(name) { return mask; });
|
||||||
|
return mixinHandler(redirect, target);
|
||||||
|
}
|
||||||
|
|
||||||
|
function whitelistHandler(target, whitelist) {
|
||||||
|
var catchall = Object.create(null, {});
|
||||||
|
var redirect = StringMap.create(whitelist).mapObject(function(name) { return target; });
|
||||||
|
return mixinHandler(redirect, catchall);
|
||||||
|
}
|
||||||
|
|
||||||
|
function mirrorHandler(target, writable) {
|
||||||
|
var handler = makePassthruHandler(target);
|
||||||
|
|
||||||
|
var defineProperty = handler.defineProperty;
|
||||||
|
handler.defineProperty = function(name, desc) {
|
||||||
|
if (!desc.enumerable)
|
||||||
|
throw new Error("mirror property must be enumerable");
|
||||||
|
if (!desc.configurable)
|
||||||
|
throw new Error("mirror property must be configurable");
|
||||||
|
if (desc.writable !== writable)
|
||||||
|
throw new Error("mirror property must " + (writable ? "" : "not ") + "be writable");
|
||||||
|
defineProperty(name, desc);
|
||||||
|
};
|
||||||
|
|
||||||
|
handler.fix = function() { };
|
||||||
|
handler.getOwnPropertyDescriptor = handler.getPropertyDescriptor;
|
||||||
|
handler.getOwnPropertyNames = getPropertyNames.bind(handler, target);
|
||||||
|
handler.keys = handler.enumerate;
|
||||||
|
handler["delete"] = function() { return false; };
|
||||||
|
handler.hasOwn = handler.has;
|
||||||
|
return handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Mixin proxies break the single-inheritance model of prototypes, so
|
||||||
|
* the handler treats all properties as own-properties:
|
||||||
|
*
|
||||||
|
* X
|
||||||
|
* |
|
||||||
|
* +------------+------------+
|
||||||
|
* | O |
|
||||||
|
* | | |
|
||||||
|
* | O O O |
|
||||||
|
* | | | | |
|
||||||
|
* | O O O O |
|
||||||
|
* | | | | | |
|
||||||
|
* | O O O O O |
|
||||||
|
* | | | | | | |
|
||||||
|
* +-(*)--(w)--(x)--(y)--(z)-+
|
||||||
|
*/
|
||||||
|
|
||||||
|
function mixinHandler(redirect, catchall) {
|
||||||
|
function targetFor(name) {
|
||||||
|
return hasOwn(redirect, name) ? redirect[name] : catchall;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getMuxPropertyDescriptor(name) {
|
||||||
|
var desc = getPropertyDescriptor(targetFor(name), name);
|
||||||
|
if (desc)
|
||||||
|
desc.configurable = true;
|
||||||
|
return desc;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getMuxPropertyNames() {
|
||||||
|
var names1 = Object.getOwnPropertyNames(redirect).filter(function(name) {
|
||||||
|
return name in redirect[name];
|
||||||
|
});
|
||||||
|
var names2 = getPropertyNames(catchall).filter(function(name) {
|
||||||
|
return !hasOwn(redirect, name);
|
||||||
|
});
|
||||||
|
return names1.concat(names2);
|
||||||
|
}
|
||||||
|
|
||||||
|
function enumerateMux() {
|
||||||
|
var result = Object.getOwnPropertyNames(redirect).filter(function(name) {
|
||||||
|
return name in redirect[name];
|
||||||
|
});
|
||||||
|
for (name in catchall) {
|
||||||
|
if (!hasOwn(redirect, name))
|
||||||
|
result.push(name);
|
||||||
|
};
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function hasMux(name) {
|
||||||
|
return name in targetFor(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
getOwnPropertyDescriptor: getMuxPropertyDescriptor,
|
||||||
|
getPropertyDescriptor: getMuxPropertyDescriptor,
|
||||||
|
getOwnPropertyNames: getMuxPropertyNames,
|
||||||
|
defineProperty: function(name, desc) {
|
||||||
|
Object.defineProperty(targetFor(name), name, desc);
|
||||||
|
},
|
||||||
|
"delete": function(name) {
|
||||||
|
var target = targetFor(name);
|
||||||
|
return delete target[name];
|
||||||
|
},
|
||||||
|
// FIXME: ha ha ha
|
||||||
|
fix: function() { },
|
||||||
|
has: hasMux,
|
||||||
|
hasOwn: hasMux,
|
||||||
|
get: function(receiver, name) {
|
||||||
|
var target = targetFor(name);
|
||||||
|
return target[name];
|
||||||
|
},
|
||||||
|
set: function(receiver, name, val) {
|
||||||
|
var target = targetFor(name);
|
||||||
|
target[name] = val;
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
enumerate: enumerateMux,
|
||||||
|
keys: enumerateMux
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function makePassthruHandler(obj) {
|
function makePassthruHandler(obj) {
|
||||||
// Handler copied from
|
// Handler copied from
|
||||||
// http://wiki.ecmascript.org/doku.php?id=harmony:proxies&s=proxy%20object#examplea_no-op_forwarding_proxy
|
// http://wiki.ecmascript.org/doku.php?id=harmony:proxies&s=proxy%20object#examplea_no-op_forwarding_proxy
|
||||||
|
|
@ -290,16 +469,29 @@ function makePassthruHandler(obj) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// default function used when looking for a property in the global object
|
|
||||||
function noPropFound() { return undefined; }
|
|
||||||
|
|
||||||
var hasOwnProperty = ({}).hasOwnProperty;
|
var hasOwnProperty = ({}).hasOwnProperty;
|
||||||
|
|
||||||
function StringMap() {
|
function hasOwn(obj, name) {
|
||||||
this.table = Object.create(null, {});
|
return hasOwnProperty.call(obj, name);
|
||||||
this.size = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function StringMap(table, size) {
|
||||||
|
this.table = table || Object.create(null, {});
|
||||||
|
this.size = size || 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringMap.create = function(table) {
|
||||||
|
var init = Object.create(null, {});
|
||||||
|
var size = 0;
|
||||||
|
var names = Object.getOwnPropertyNames(table);
|
||||||
|
for (var i = 0, n = names.length; i < n; i++) {
|
||||||
|
var name = names[i];
|
||||||
|
init[name] = table[name];
|
||||||
|
size++;
|
||||||
|
}
|
||||||
|
return new StringMap(init, size);
|
||||||
|
};
|
||||||
|
|
||||||
StringMap.prototype = {
|
StringMap.prototype = {
|
||||||
has: function(x) { return hasOwnProperty.call(this.table, x); },
|
has: function(x) { return hasOwnProperty.call(this.table, x); },
|
||||||
set: function(x, v) {
|
set: function(x, v) {
|
||||||
|
|
@ -320,9 +512,113 @@ StringMap.prototype = {
|
||||||
for (var key in table)
|
for (var key in table)
|
||||||
f.call(this, key, table[key]);
|
f.call(this, key, table[key]);
|
||||||
},
|
},
|
||||||
|
map: function(f) {
|
||||||
|
var table1 = this.table;
|
||||||
|
var table2 = Object.create(null, {});
|
||||||
|
this.forEach(function(key, val) {
|
||||||
|
table2[key] = f.call(this, val, key);
|
||||||
|
});
|
||||||
|
return new StringMap(table2, this.size);
|
||||||
|
},
|
||||||
|
mapObject: function(f) {
|
||||||
|
var table1 = this.table;
|
||||||
|
var table2 = Object.create(null, {});
|
||||||
|
this.forEach(function(key, val) {
|
||||||
|
table2[key] = f.call(this, val, key);
|
||||||
|
});
|
||||||
|
return table2;
|
||||||
|
},
|
||||||
|
toObject: function() {
|
||||||
|
return this.mapObject(function(val) { return val; });
|
||||||
|
},
|
||||||
|
choose: function() {
|
||||||
|
return Object.getOwnPropertyNames(this.table)[0];
|
||||||
|
},
|
||||||
|
remove: function(x) {
|
||||||
|
if (hasOwnProperty.call(this.table, x)) {
|
||||||
|
this.size--;
|
||||||
|
delete this.table[x];
|
||||||
|
}
|
||||||
|
},
|
||||||
|
copy: function() {
|
||||||
|
var table = Object.create(null, {});
|
||||||
|
for (var key in this.table)
|
||||||
|
table[key] = this.table[key];
|
||||||
|
return new StringMap(table, this.size);
|
||||||
|
},
|
||||||
toString: function() { return "[object StringMap]" }
|
toString: function() { return "[object StringMap]" }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// an object-key table with poor asymptotics (replace with WeakMap when possible)
|
||||||
|
function ObjectMap(array) {
|
||||||
|
this.array = array || [];
|
||||||
|
}
|
||||||
|
|
||||||
|
function searchMap(map, key, found, notFound) {
|
||||||
|
var a = map.array;
|
||||||
|
for (var i = 0, n = a.length; i < n; i++) {
|
||||||
|
var pair = a[i];
|
||||||
|
if (pair.key === key)
|
||||||
|
return found(pair, i);
|
||||||
|
}
|
||||||
|
return notFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
ObjectMap.prototype = {
|
||||||
|
has: function(x) {
|
||||||
|
return searchMap(this, x, function() { return true }, function() { return false });
|
||||||
|
},
|
||||||
|
set: function(x, v) {
|
||||||
|
var a = this.array;
|
||||||
|
searchMap(this, x,
|
||||||
|
function(pair) { pair.value = v },
|
||||||
|
function() { a.push({ key: x, value: v }) });
|
||||||
|
},
|
||||||
|
get: function(x) {
|
||||||
|
return searchMap(this, x,
|
||||||
|
function(pair) { return pair.value },
|
||||||
|
function() { return null });
|
||||||
|
},
|
||||||
|
getDef: function(x, thunk) {
|
||||||
|
var a = this.array;
|
||||||
|
return searchMap(this, x,
|
||||||
|
function(pair) { return pair.value },
|
||||||
|
function() {
|
||||||
|
var v = thunk();
|
||||||
|
a.push({ key: x, value: v });
|
||||||
|
return v;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
forEach: function(f) {
|
||||||
|
var a = this.array;
|
||||||
|
for (var i = 0, n = a.length; i < n; i++) {
|
||||||
|
var pair = a[i];
|
||||||
|
f.call(this, pair.key, pair.value);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
choose: function() {
|
||||||
|
return this.array[0].key;
|
||||||
|
},
|
||||||
|
get size() {
|
||||||
|
return this.array.length;
|
||||||
|
},
|
||||||
|
remove: function(x) {
|
||||||
|
var a = this.array;
|
||||||
|
searchMap(this, x,
|
||||||
|
function(pair, i) { a.splice(i, 1) },
|
||||||
|
function() { });
|
||||||
|
},
|
||||||
|
copy: function() {
|
||||||
|
return new ObjectMap(this.array.map(function(pair) {
|
||||||
|
return { key: pair.key, value: pair.value }
|
||||||
|
}));
|
||||||
|
},
|
||||||
|
clear: function() {
|
||||||
|
this.array = [];
|
||||||
|
},
|
||||||
|
toString: function() { return "[object ObjectMap]" }
|
||||||
|
};
|
||||||
|
|
||||||
// non-destructive stack
|
// non-destructive stack
|
||||||
function Stack(elts) {
|
function Stack(elts) {
|
||||||
this.elts = elts || null;
|
this.elts = elts || null;
|
||||||
|
|
@ -357,19 +653,27 @@ Stack.prototype = {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.tokens = tokens;
|
module.exports = {
|
||||||
exports.opTypeNames = opTypeNames;
|
tokens: tokens,
|
||||||
exports.keywords = keywords;
|
whitespace: whitespace,
|
||||||
exports.isStatementStartCode = isStatementStartCode;
|
opTypeNames: opTypeNames,
|
||||||
exports.tokenIds = tokenIds;
|
keywords: keywords,
|
||||||
exports.consts = consts;
|
isStatementStartCode: isStatementStartCode,
|
||||||
exports.assignOps = assignOps;
|
tokenIds: tokenIds,
|
||||||
exports.defineGetter = defineGetter;
|
consts: consts,
|
||||||
exports.defineProperty = defineProperty;
|
assignOps: assignOps,
|
||||||
exports.isNativeCode = isNativeCode;
|
defineGetter: defineGetter,
|
||||||
exports.makePassthruHandler = makePassthruHandler;
|
defineGetterSetter: defineGetterSetter,
|
||||||
exports.noPropFound = noPropFound;
|
defineMemoGetter: defineMemoGetter,
|
||||||
exports.StringMap = StringMap;
|
defineProperty: defineProperty,
|
||||||
exports.Stack = Stack;
|
isNativeCode: isNativeCode,
|
||||||
|
mirrorHandler: mirrorHandler,
|
||||||
|
mixinHandler: mixinHandler,
|
||||||
|
whitelistHandler: whitelistHandler,
|
||||||
|
blacklistHandler: blacklistHandler,
|
||||||
|
makePassthruHandler: makePassthruHandler,
|
||||||
|
StringMap: StringMap,
|
||||||
|
ObjectMap: ObjectMap,
|
||||||
|
Stack: Stack
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
@ -54,6 +54,13 @@ var definitions = require("ace/narcissus/jsdefs");
|
||||||
// Set constants in the local scope.
|
// Set constants in the local scope.
|
||||||
eval(definitions.consts);
|
eval(definitions.consts);
|
||||||
|
|
||||||
|
// Banned keywords by language version
|
||||||
|
const blackLists = { 160: {}, 185: {}, harmony: {} };
|
||||||
|
blackLists[160][LET] = true;
|
||||||
|
blackLists[160][MODULE] = true;
|
||||||
|
blackLists[160][YIELD] = true;
|
||||||
|
blackLists[185][MODULE] = true;
|
||||||
|
|
||||||
// Build up a trie of operator tokens.
|
// Build up a trie of operator tokens.
|
||||||
var opTokens = {};
|
var opTokens = {};
|
||||||
for (var op in definitions.opTypeNames) {
|
for (var op in definitions.opTypeNames) {
|
||||||
|
|
@ -83,6 +90,8 @@ function Tokenizer(s, f, l) {
|
||||||
this.unexpectedEOF = false;
|
this.unexpectedEOF = false;
|
||||||
this.filename = f || "";
|
this.filename = f || "";
|
||||||
this.lineno = l || 1;
|
this.lineno = l || 1;
|
||||||
|
this.blackList = blackLists[Narcissus.options.version];
|
||||||
|
this.blockComments = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Tokenizer.prototype = {
|
Tokenizer.prototype = {
|
||||||
|
|
@ -129,16 +138,31 @@ Tokenizer.prototype = {
|
||||||
return tt;
|
return tt;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
lastBlockComment: function() {
|
||||||
|
var length = this.blockComments.length;
|
||||||
|
return length ? this.blockComments[length - 1] : null;
|
||||||
|
},
|
||||||
|
|
||||||
// Eat comments and whitespace.
|
// Eat comments and whitespace.
|
||||||
skip: function () {
|
skip: function () {
|
||||||
var input = this.source;
|
var input = this.source;
|
||||||
|
this.blockComments = [];
|
||||||
for (;;) {
|
for (;;) {
|
||||||
var ch = input[this.cursor++];
|
var ch = input[this.cursor++];
|
||||||
var next = input[this.cursor];
|
var next = input[this.cursor];
|
||||||
|
// handle \r, \r\n and (always preferable) \n
|
||||||
|
if (ch === '\r') {
|
||||||
|
// if the next character is \n, we don't care about this at all
|
||||||
|
if (next === '\n') continue;
|
||||||
|
|
||||||
|
// otherwise, we want to consider this as a newline
|
||||||
|
ch = '\n';
|
||||||
|
}
|
||||||
|
|
||||||
if (ch === '\n' && !this.scanNewlines) {
|
if (ch === '\n' && !this.scanNewlines) {
|
||||||
this.lineno++;
|
this.lineno++;
|
||||||
} else if (ch === '/' && next === '*') {
|
} else if (ch === '/' && next === '*') {
|
||||||
this.cursor++;
|
var commentStart = ++this.cursor;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
ch = input[this.cursor++];
|
ch = input[this.cursor++];
|
||||||
if (ch === undefined)
|
if (ch === undefined)
|
||||||
|
|
@ -147,6 +171,7 @@ Tokenizer.prototype = {
|
||||||
if (ch === '*') {
|
if (ch === '*') {
|
||||||
next = input[this.cursor];
|
next = input[this.cursor];
|
||||||
if (next === '/') {
|
if (next === '/') {
|
||||||
|
var commentEnd = this.cursor - 1;
|
||||||
this.cursor++;
|
this.cursor++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -154,19 +179,30 @@ Tokenizer.prototype = {
|
||||||
this.lineno++;
|
this.lineno++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.blockComments.push(input.substring(commentStart, commentEnd));
|
||||||
} else if (ch === '/' && next === '/') {
|
} else if (ch === '/' && next === '/') {
|
||||||
this.cursor++;
|
this.cursor++;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
ch = input[this.cursor++];
|
ch = input[this.cursor++];
|
||||||
|
next = input[this.cursor];
|
||||||
if (ch === undefined)
|
if (ch === undefined)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
if (ch === '\r') {
|
||||||
|
// check for \r\n
|
||||||
|
if (next !== '\n') ch = '\n';
|
||||||
|
}
|
||||||
|
|
||||||
if (ch === '\n') {
|
if (ch === '\n') {
|
||||||
|
if (this.scanNewlines) {
|
||||||
|
this.cursor--;
|
||||||
|
} else {
|
||||||
this.lineno++;
|
this.lineno++;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (ch !== ' ' && ch !== '\t') {
|
} else if (!(ch in definitions.whitespace)) {
|
||||||
this.cursor--;
|
this.cursor--;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -363,19 +399,19 @@ Tokenizer.prototype = {
|
||||||
},
|
},
|
||||||
|
|
||||||
// FIXME: Unicode escape sequences
|
// FIXME: Unicode escape sequences
|
||||||
// FIXME: Unicode identifiers
|
|
||||||
lexIdent: function (ch) {
|
lexIdent: function (ch) {
|
||||||
var token = this.token, input = this.source;
|
var token = this.token;
|
||||||
|
var id = ch;
|
||||||
|
|
||||||
do {
|
while ((ch = this.getValidIdentifierChar(false)) !== null) {
|
||||||
ch = input[this.cursor++];
|
id += ch;
|
||||||
} while ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
|
}
|
||||||
(ch >= '0' && ch <= '9') || ch === '$' || ch === '_');
|
|
||||||
|
|
||||||
this.cursor--; // Put the non-word character back.
|
|
||||||
|
|
||||||
var id = input.substring(token.start, this.cursor);
|
|
||||||
token.type = definitions.keywords[id] || IDENTIFIER;
|
token.type = definitions.keywords[id] || IDENTIFIER;
|
||||||
|
if (token.type in this.blackList) {
|
||||||
|
// banned keyword, this is an identifier
|
||||||
|
token.type = IDENTIFIER;
|
||||||
|
}
|
||||||
token.value = id;
|
token.value = id;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -403,15 +439,16 @@ Tokenizer.prototype = {
|
||||||
this.tokens[this.tokenIndex] = token = {};
|
this.tokens[this.tokenIndex] = token = {};
|
||||||
|
|
||||||
var input = this.source;
|
var input = this.source;
|
||||||
if (this.cursor === input.length)
|
if (this.cursor >= input.length)
|
||||||
return token.type = END;
|
return token.type = END;
|
||||||
|
|
||||||
token.start = this.cursor;
|
token.start = this.cursor;
|
||||||
token.lineno = this.lineno;
|
token.lineno = this.lineno;
|
||||||
|
|
||||||
var ch = input[this.cursor++];
|
var ich = this.getValidIdentifierChar(true);
|
||||||
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch === '$' || ch === '_') {
|
var ch = (ich === null) ? input[this.cursor++] : null;
|
||||||
this.lexIdent(ch);
|
if (ich !== null) {
|
||||||
|
this.lexIdent(ich);
|
||||||
} else if (scanOperand && ch === '/') {
|
} else if (scanOperand && ch === '/') {
|
||||||
this.lexRegExp(ch);
|
this.lexRegExp(ch);
|
||||||
} else if (ch in opTokens) {
|
} else if (ch in opTokens) {
|
||||||
|
|
@ -424,7 +461,9 @@ Tokenizer.prototype = {
|
||||||
this.lexZeroNumber(ch);
|
this.lexZeroNumber(ch);
|
||||||
} else if (ch === '"' || ch === "'") {
|
} else if (ch === '"' || ch === "'") {
|
||||||
this.lexString(ch);
|
this.lexString(ch);
|
||||||
} else if (this.scanNewlines && ch === '\n') {
|
} else if (this.scanNewlines && (ch === '\n' || ch === '\r')) {
|
||||||
|
// if this was a \r, look for \r\n
|
||||||
|
if (ch === '\r' && input[this.cursor] === '\n') this.cursor++;
|
||||||
token.type = NEWLINE;
|
token.type = NEWLINE;
|
||||||
token.value = '\n';
|
token.value = '\n';
|
||||||
this.lineno++;
|
this.lineno++;
|
||||||
|
|
@ -447,16 +486,64 @@ Tokenizer.prototype = {
|
||||||
},
|
},
|
||||||
|
|
||||||
newSyntaxError: function (m) {
|
newSyntaxError: function (m) {
|
||||||
|
m = (this.filename ? this.filename + ":" : "") + this.lineno + ": " + m;
|
||||||
var e = new SyntaxError(m, this.filename, this.lineno);
|
var e = new SyntaxError(m, this.filename, this.lineno);
|
||||||
e.source = this.source;
|
e.source = this.source;
|
||||||
e.lineno = this.lineno;
|
|
||||||
e.cursor = this.lookahead
|
e.cursor = this.lookahead
|
||||||
? this.tokens[(this.tokenIndex + this.lookahead) & 3].start
|
? this.tokens[(this.tokenIndex + this.lookahead) & 3].start
|
||||||
: this.cursor;
|
: this.cursor;
|
||||||
return e;
|
return e;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/* Gets a single valid identifier char from the input stream, or null
|
||||||
|
* if there is none.
|
||||||
|
* Since JavaScript provides no convenient way to determine if a
|
||||||
|
* character is in a particular Unicode category, we use
|
||||||
|
* metacircularity to accomplish this (oh yeaaaah!) */
|
||||||
|
getValidIdentifierChar: function(first) {
|
||||||
|
var input = this.source;
|
||||||
|
if (this.cursor >= input.length) return null;
|
||||||
|
var ch = input[this.cursor];
|
||||||
|
|
||||||
|
// first check for \u escapes
|
||||||
|
if (ch === '\\' && input[this.cursor+1] === 'u') {
|
||||||
|
// get the character value
|
||||||
|
try {
|
||||||
|
ch = String.fromCharCode(parseInt(
|
||||||
|
input.substring(this.cursor + 2, this.cursor + 6),
|
||||||
|
16));
|
||||||
|
} catch (ex) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
this.cursor += 5;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check directly for ASCII
|
||||||
|
if (ch <= "\u007F") {
|
||||||
|
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch === '$' || ch === '_' ||
|
||||||
|
(!first && (ch >= '0' && ch <= '9'))) {
|
||||||
|
this.cursor++;
|
||||||
|
return ch;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// create an object to test this in
|
||||||
|
var x = {};
|
||||||
|
x["x"+ch] = true;
|
||||||
|
x[ch] = true;
|
||||||
|
|
||||||
|
// then use eval to determine if it's a valid character
|
||||||
|
var valid = false;
|
||||||
|
try {
|
||||||
|
valid = (Function("x", "return (x." + (first?"":"x") + ch + ");")(x) === true);
|
||||||
|
} catch (ex) {}
|
||||||
|
if (valid) this.cursor++;
|
||||||
|
return (valid ? ch : null);
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.Tokenizer = Tokenizer;
|
|
||||||
|
return { Tokenizer: Tokenizer };
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
@ -60,6 +60,18 @@ const Stack = definitions.Stack;
|
||||||
// Set constants in the local scope.
|
// Set constants in the local scope.
|
||||||
eval(definitions.consts);
|
eval(definitions.consts);
|
||||||
|
|
||||||
|
// Banned statement types by language version.
|
||||||
|
const blackLists = { 160: {}, 185: {}, harmony: {} };
|
||||||
|
blackLists[160][IMPORT] = true;
|
||||||
|
blackLists[160][EXPORT] = true;
|
||||||
|
blackLists[160][LET] = true;
|
||||||
|
blackLists[160][MODULE] = true;
|
||||||
|
blackLists[160][YIELD] = true;
|
||||||
|
blackLists[185][IMPORT] = true;
|
||||||
|
blackLists[185][EXPORT] = true;
|
||||||
|
blackLists[185][MODULE] = true;
|
||||||
|
blackLists.harmony[WITH] = true;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* pushDestructuringVarDecls :: (node, hoisting node) -> void
|
* pushDestructuringVarDecls :: (node, hoisting node) -> void
|
||||||
*
|
*
|
||||||
|
|
@ -76,23 +88,21 @@ function pushDestructuringVarDecls(n, s) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NESTING_TOP: top-level
|
function StaticContext(parentScript, parentBlock, inModule, inFunction) {
|
||||||
// NESTING_SHALLOW: nested within static forms such as { ... } or labeled statement
|
|
||||||
// NESTING_DEEP: nested within dynamic forms such as if, loops, etc.
|
|
||||||
const NESTING_TOP = 0, NESTING_SHALLOW = 1, NESTING_DEEP = 2;
|
|
||||||
|
|
||||||
function StaticContext(parentScript, parentBlock, inFunction, inForLoopInit, nesting) {
|
|
||||||
this.parentScript = parentScript;
|
this.parentScript = parentScript;
|
||||||
this.parentBlock = parentBlock;
|
this.parentBlock = parentBlock || parentScript;
|
||||||
this.inFunction = inFunction;
|
this.inModule = inModule || false;
|
||||||
this.inForLoopInit = inForLoopInit;
|
this.inFunction = inFunction || false;
|
||||||
this.nesting = nesting;
|
this.inForLoopInit = false;
|
||||||
|
this.topLevel = true;
|
||||||
this.allLabels = new Stack();
|
this.allLabels = new Stack();
|
||||||
this.currentLabels = new Stack();
|
this.currentLabels = new Stack();
|
||||||
this.labeledTargets = new Stack();
|
this.labeledTargets = new Stack();
|
||||||
|
this.defaultLoopTarget = null;
|
||||||
this.defaultTarget = null;
|
this.defaultTarget = null;
|
||||||
definitions.options.ecma3OnlyMode && (this.ecma3OnlyMode = true);
|
this.blackList = blackLists[Narcissus.options.version];
|
||||||
definitions.options.parenFreeMode && (this.parenFreeMode = true);
|
Narcissus.options.ecma3OnlyMode && (this.ecma3OnlyMode = true);
|
||||||
|
Narcissus.options.parenFreeMode && (this.parenFreeMode = true);
|
||||||
}
|
}
|
||||||
|
|
||||||
StaticContext.prototype = {
|
StaticContext.prototype = {
|
||||||
|
|
@ -116,12 +126,13 @@ StaticContext.prototype = {
|
||||||
allLabels: this.allLabels.push(label) });
|
allLabels: this.allLabels.push(label) });
|
||||||
},
|
},
|
||||||
pushTarget: function(target) {
|
pushTarget: function(target) {
|
||||||
var isDefaultTarget = target.isLoop || target.type === SWITCH;
|
var isDefaultLoopTarget = target.isLoop;
|
||||||
|
var isDefaultTarget = isDefaultLoopTarget || target.type === SWITCH;
|
||||||
|
|
||||||
if (this.currentLabels.isEmpty()) {
|
if (this.currentLabels.isEmpty()) {
|
||||||
return isDefaultTarget
|
if (isDefaultLoopTarget) this.update({ defaultLoopTarget: target });
|
||||||
? this.update({ defaultTarget: target })
|
if (isDefaultTarget) this.update({ defaultTarget: target });
|
||||||
: this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
target.labels = new StringMap();
|
target.labels = new StringMap();
|
||||||
|
|
@ -130,27 +141,43 @@ StaticContext.prototype = {
|
||||||
});
|
});
|
||||||
return this.update({ currentLabels: new Stack(),
|
return this.update({ currentLabels: new Stack(),
|
||||||
labeledTargets: this.labeledTargets.push(target),
|
labeledTargets: this.labeledTargets.push(target),
|
||||||
|
defaultLoopTarget: isDefaultLoopTarget
|
||||||
|
? target
|
||||||
|
: this.defaultLoopTarget,
|
||||||
defaultTarget: isDefaultTarget
|
defaultTarget: isDefaultTarget
|
||||||
? target
|
? target
|
||||||
: this.defaultTarget });
|
: this.defaultTarget });
|
||||||
},
|
},
|
||||||
nest: function(atLeast) {
|
nest: function() {
|
||||||
var nesting = Math.max(this.nesting, atLeast);
|
return this.topLevel ? this.update({ topLevel: false }) : this;
|
||||||
return (nesting !== this.nesting)
|
},
|
||||||
? this.update({ nesting: nesting })
|
allow: function(type) {
|
||||||
: this;
|
switch (type) {
|
||||||
|
case EXPORT:
|
||||||
|
if (!this.inModule || this.inFunction || !this.topLevel)
|
||||||
|
return false;
|
||||||
|
// FALL THROUGH
|
||||||
|
|
||||||
|
case IMPORT:
|
||||||
|
return !this.inFunction && this.topLevel;
|
||||||
|
|
||||||
|
case MODULE:
|
||||||
|
return !this.inFunction && this.topLevel;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Script :: (tokenizer, boolean) -> node
|
* Script :: (tokenizer, boolean, boolean) -> node
|
||||||
*
|
*
|
||||||
* Parses the toplevel and function bodies.
|
* Parses the toplevel and module/function bodies.
|
||||||
*/
|
*/
|
||||||
function Script(t, inFunction) {
|
function Script(t, inModule, inFunction) {
|
||||||
var n = new Node(t, scriptInit());
|
var n = new Node(t, scriptInit());
|
||||||
var x = new StaticContext(n, n, inFunction, false, NESTING_TOP);
|
Statements(t, new StaticContext(n, n, inModule, inFunction), n);
|
||||||
Statements(t, x, n);
|
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -186,9 +213,63 @@ function Node(t, init) {
|
||||||
this[prop] = init[prop];
|
this[prop] = init[prop];
|
||||||
}
|
}
|
||||||
|
|
||||||
var Np = Node.prototype = {};
|
/*
|
||||||
|
* SyntheticNode :: (tokenizer, optional init object) -> node
|
||||||
|
*/
|
||||||
|
function SyntheticNode(t, init) {
|
||||||
|
// print("SYNTHETIC NODE");
|
||||||
|
// if (init.type === COMMA) {
|
||||||
|
// print("SYNTHETIC COMMA");
|
||||||
|
// print(init);
|
||||||
|
// }
|
||||||
|
this.tokenizer = t;
|
||||||
|
this.children = [];
|
||||||
|
for (var prop in init)
|
||||||
|
this[prop] = init[prop];
|
||||||
|
this.synthetic = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
var Np = Node.prototype = SyntheticNode.prototype = {};
|
||||||
Np.constructor = Node;
|
Np.constructor = Node;
|
||||||
Np.toSource = Object.prototype.toSource;
|
|
||||||
|
const TO_SOURCE_SKIP = {
|
||||||
|
type: true,
|
||||||
|
value: true,
|
||||||
|
lineno: true,
|
||||||
|
start: true,
|
||||||
|
end: true,
|
||||||
|
tokenizer: true,
|
||||||
|
assignOp: true
|
||||||
|
};
|
||||||
|
function unevalableConst(code) {
|
||||||
|
var token = definitions.tokens[code];
|
||||||
|
var constName = definitions.opTypeNames.hasOwnProperty(token)
|
||||||
|
? definitions.opTypeNames[token]
|
||||||
|
: token in definitions.keywords
|
||||||
|
? token.toUpperCase()
|
||||||
|
: token;
|
||||||
|
return { toSource: function() { return constName } };
|
||||||
|
}
|
||||||
|
Np.toSource = function toSource() {
|
||||||
|
var mock = {};
|
||||||
|
var self = this;
|
||||||
|
mock.type = unevalableConst(this.type);
|
||||||
|
if ("value" in this)
|
||||||
|
mock.value = this.value;
|
||||||
|
if ("lineno" in this)
|
||||||
|
mock.lineno = this.lineno;
|
||||||
|
if ("start" in this)
|
||||||
|
mock.start = this.start;
|
||||||
|
if ("end" in this)
|
||||||
|
mock.end = this.end;
|
||||||
|
if (this.assignOp)
|
||||||
|
mock.assignOp = unevalableConst(this.assignOp);
|
||||||
|
for (var key in this) {
|
||||||
|
if (this.hasOwnProperty(key) && !(key in TO_SOURCE_SKIP))
|
||||||
|
mock[key] = this[key];
|
||||||
|
}
|
||||||
|
return mock.toSource();
|
||||||
|
};
|
||||||
|
|
||||||
// Always use push to add operands to an expression, to update start and end.
|
// Always use push to add operands to an expression, to update start and end.
|
||||||
Np.push = function (kid) {
|
Np.push = function (kid) {
|
||||||
|
|
@ -244,10 +325,13 @@ function scriptInit() {
|
||||||
return { type: SCRIPT,
|
return { type: SCRIPT,
|
||||||
funDecls: [],
|
funDecls: [],
|
||||||
varDecls: [],
|
varDecls: [],
|
||||||
modDecls: [],
|
modDefns: new StringMap(),
|
||||||
|
modAssns: new StringMap(),
|
||||||
|
modDecls: new StringMap(),
|
||||||
|
modLoads: new StringMap(),
|
||||||
impDecls: [],
|
impDecls: [],
|
||||||
expDecls: [],
|
expDecls: [],
|
||||||
loadDeps: [],
|
exports: new StringMap(),
|
||||||
hasEmptyReturn: false,
|
hasEmptyReturn: false,
|
||||||
hasReturnWithValue: false,
|
hasReturnWithValue: false,
|
||||||
isGenerator: false };
|
isGenerator: false };
|
||||||
|
|
@ -258,6 +342,12 @@ definitions.defineGetter(Np, "filename",
|
||||||
return this.tokenizer.filename;
|
return this.tokenizer.filename;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
definitions.defineGetter(Np, "length",
|
||||||
|
function() {
|
||||||
|
throw new Error("Node.prototype.length is gone; " +
|
||||||
|
"use n.children.length instead");
|
||||||
|
});
|
||||||
|
|
||||||
definitions.defineProperty(String.prototype, "repeat",
|
definitions.defineProperty(String.prototype, "repeat",
|
||||||
function(n) {
|
function(n) {
|
||||||
var s = "", t = this + s;
|
var s = "", t = this + s;
|
||||||
|
|
@ -303,6 +393,102 @@ function Block(t, x) {
|
||||||
|
|
||||||
const DECLARED_FORM = 0, EXPRESSED_FORM = 1, STATEMENT_FORM = 2;
|
const DECLARED_FORM = 0, EXPRESSED_FORM = 1, STATEMENT_FORM = 2;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Export :: (binding node, boolean) -> Export
|
||||||
|
*
|
||||||
|
* Static semantic representation of a module export.
|
||||||
|
*/
|
||||||
|
function Export(node, isDefinition) {
|
||||||
|
this.node = node; // the AST node declaring this individual export
|
||||||
|
this.isDefinition = isDefinition; // is the node an 'export'-annotated definition?
|
||||||
|
this.resolved = null; // resolved pointer to the target of this export
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* registerExport :: (StringMap, EXPORT node) -> void
|
||||||
|
*/
|
||||||
|
function registerExport(exports, decl) {
|
||||||
|
function register(name, exp) {
|
||||||
|
if (exports.has(name))
|
||||||
|
throw new SyntaxError("multiple exports of " + name);
|
||||||
|
exports.set(name, exp);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (decl.type) {
|
||||||
|
case MODULE:
|
||||||
|
case FUNCTION:
|
||||||
|
register(decl.name, new Export(decl, true));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case VAR:
|
||||||
|
for (var i = 0; i < decl.children.length; i++)
|
||||||
|
register(decl.children[i].name, new Export(decl.children[i], true));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case LET:
|
||||||
|
case CONST:
|
||||||
|
throw new Error("NYI: " + definitions.tokens[decl.type]);
|
||||||
|
|
||||||
|
case EXPORT:
|
||||||
|
for (var i = 0; i < decl.pathList.length; i++) {
|
||||||
|
var path = decl.pathList[i];
|
||||||
|
switch (path.type) {
|
||||||
|
case OBJECT_INIT:
|
||||||
|
for (var j = 0; j < path.children.length; j++) {
|
||||||
|
// init :: IDENTIFIER | PROPERTY_INIT
|
||||||
|
var init = path.children[j];
|
||||||
|
if (init.type === IDENTIFIER)
|
||||||
|
register(init.value, new Export(init, false));
|
||||||
|
else
|
||||||
|
register(init.children[0].value, new Export(init.children[1], false));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case DOT:
|
||||||
|
register(path.children[1].value, new Export(path, false));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case IDENTIFIER:
|
||||||
|
register(path.value, new Export(path, false));
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new Error("unexpected export path: " + definitions.tokens[path.type]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new Error("unexpected export decl: " + definitions.tokens[exp.type]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Module :: (node) -> Module
|
||||||
|
*
|
||||||
|
* Static semantic representation of a module.
|
||||||
|
*/
|
||||||
|
function Module(node) {
|
||||||
|
var exports = node.body.exports;
|
||||||
|
var modDefns = node.body.modDefns;
|
||||||
|
|
||||||
|
var exportedModules = new StringMap();
|
||||||
|
|
||||||
|
exports.forEach(function(name, exp) {
|
||||||
|
var node = exp.node;
|
||||||
|
if (node.type === MODULE) {
|
||||||
|
exportedModules.set(name, node);
|
||||||
|
} else if (!exp.isDefinition && node.type === IDENTIFIER && modDefns.has(node.value)) {
|
||||||
|
var mod = modDefns.get(node.value);
|
||||||
|
exportedModules.set(name, mod);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
this.node = node;
|
||||||
|
this.exports = exports;
|
||||||
|
this.exportedModules = exportedModules;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Statement :: (tokenizer, compiler context) -> node
|
* Statement :: (tokenizer, compiler context) -> node
|
||||||
*
|
*
|
||||||
|
|
@ -311,35 +497,87 @@ const DECLARED_FORM = 0, EXPRESSED_FORM = 1, STATEMENT_FORM = 2;
|
||||||
function Statement(t, x) {
|
function Statement(t, x) {
|
||||||
var i, label, n, n2, p, c, ss, tt = t.get(true), tt2, x2, x3;
|
var i, label, n, n2, p, c, ss, tt = t.get(true), tt2, x2, x3;
|
||||||
|
|
||||||
|
var comments = t.blockComments;
|
||||||
|
|
||||||
|
if (x.blackList[tt])
|
||||||
|
throw t.newSyntaxError(definitions.tokens[tt] + " statements only allowed in Harmony");
|
||||||
|
if (!x.allow(tt))
|
||||||
|
throw t.newSyntaxError(definitions.tokens[tt] + " statement in illegal context");
|
||||||
|
|
||||||
// Cases for statements ending in a right curly return early, avoiding the
|
// Cases for statements ending in a right curly return early, avoiding the
|
||||||
// common semicolon insertion magic after this switch.
|
// common semicolon insertion magic after this switch.
|
||||||
switch (tt) {
|
switch (tt) {
|
||||||
|
case IMPORT:
|
||||||
|
n = new Node(t);
|
||||||
|
n.pathList = ImportPathList(t, x);
|
||||||
|
x.parentScript.impDecls.push(n);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case EXPORT:
|
||||||
|
switch (t.peek()) {
|
||||||
|
case MODULE:
|
||||||
|
case FUNCTION:
|
||||||
|
case LET:
|
||||||
|
case VAR:
|
||||||
|
case CONST:
|
||||||
|
n = Statement(t, x);
|
||||||
|
n.blockComments = comments;
|
||||||
|
n.exported = true;
|
||||||
|
x.parentScript.expDecls.push(n);
|
||||||
|
registerExport(x.parentScript.exports, n);
|
||||||
|
return n;
|
||||||
|
|
||||||
|
default:
|
||||||
|
n = new Node(t);
|
||||||
|
n.pathList = ExportPathList(t, x);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
x.parentScript.expDecls.push(n);
|
||||||
|
registerExport(x.parentScript.exports, n);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MODULE:
|
||||||
|
n = new Node(t);
|
||||||
|
n.blockComments = comments;
|
||||||
|
t.mustMatch(IDENTIFIER);
|
||||||
|
label = t.token.value;
|
||||||
|
|
||||||
|
if (t.match(LEFT_CURLY)) {
|
||||||
|
n.name = label;
|
||||||
|
n.body = Script(t, true, false);
|
||||||
|
n.module = new Module(n);
|
||||||
|
t.mustMatch(RIGHT_CURLY);
|
||||||
|
x.parentScript.modDefns.set(n.name, n);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
t.unget();
|
||||||
|
ModuleVariables(t, x, n);
|
||||||
|
return n;
|
||||||
|
|
||||||
case FUNCTION:
|
case FUNCTION:
|
||||||
// DECLARED_FORM extends funDecls of x, STATEMENT_FORM doesn't.
|
// DECLARED_FORM extends funDecls of x, STATEMENT_FORM doesn't.
|
||||||
return FunctionDefinition(t, x, true,
|
return FunctionDefinition(t, x, true, x.topLevel ? DECLARED_FORM : STATEMENT_FORM, comments);
|
||||||
(x.nesting !== NESTING_TOP)
|
|
||||||
? STATEMENT_FORM
|
|
||||||
: DECLARED_FORM);
|
|
||||||
|
|
||||||
case LEFT_CURLY:
|
case LEFT_CURLY:
|
||||||
n = new Node(t, blockInit());
|
n = new Node(t, blockInit());
|
||||||
Statements(t, x.update({ parentBlock: n }).pushTarget(n).nest(NESTING_SHALLOW), n);
|
Statements(t, x.update({ parentBlock: n }).pushTarget(n).nest(), n);
|
||||||
t.mustMatch(RIGHT_CURLY);
|
t.mustMatch(RIGHT_CURLY);
|
||||||
return n;
|
return n;
|
||||||
|
|
||||||
case IF:
|
case IF:
|
||||||
n = new Node(t);
|
n = new Node(t);
|
||||||
n.condition = HeadExpression(t, x);
|
n.condition = HeadExpression(t, x);
|
||||||
x2 = x.pushTarget(n).nest(NESTING_DEEP);
|
x2 = x.pushTarget(n).nest();
|
||||||
n.thenPart = Statement(t, x2);
|
n.thenPart = Statement(t, x2);
|
||||||
n.elsePart = t.match(ELSE) ? Statement(t, x2) : null;
|
n.elsePart = t.match(ELSE, true) ? Statement(t, x2) : null;
|
||||||
return n;
|
return n;
|
||||||
|
|
||||||
case SWITCH:
|
case SWITCH:
|
||||||
// This allows CASEs after a DEFAULT, which is in the standard.
|
// This allows CASEs after a DEFAULT, which is in the standard.
|
||||||
n = new Node(t, { cases: [], defaultIndex: -1 });
|
n = new Node(t, { cases: [], defaultIndex: -1 });
|
||||||
n.discriminant = HeadExpression(t, x);
|
n.discriminant = HeadExpression(t, x);
|
||||||
x2 = x.pushTarget(n).nest(NESTING_DEEP);
|
x2 = x.pushTarget(n).nest();
|
||||||
t.mustMatch(LEFT_CURLY);
|
t.mustMatch(LEFT_CURLY);
|
||||||
while ((tt = t.get()) !== RIGHT_CURLY) {
|
while ((tt = t.get()) !== RIGHT_CURLY) {
|
||||||
switch (tt) {
|
switch (tt) {
|
||||||
|
|
@ -369,6 +607,7 @@ function Statement(t, x) {
|
||||||
|
|
||||||
case FOR:
|
case FOR:
|
||||||
n = new Node(t, LOOP_INIT);
|
n = new Node(t, LOOP_INIT);
|
||||||
|
n.blockComments = comments;
|
||||||
if (t.match(IDENTIFIER)) {
|
if (t.match(IDENTIFIER)) {
|
||||||
if (t.token.value === "each")
|
if (t.token.value === "each")
|
||||||
n.isEach = true;
|
n.isEach = true;
|
||||||
|
|
@ -377,9 +616,10 @@ function Statement(t, x) {
|
||||||
}
|
}
|
||||||
if (!x.parenFreeMode)
|
if (!x.parenFreeMode)
|
||||||
t.mustMatch(LEFT_PAREN);
|
t.mustMatch(LEFT_PAREN);
|
||||||
x2 = x.pushTarget(n).nest(NESTING_DEEP);
|
x2 = x.pushTarget(n).nest();
|
||||||
x3 = x.update({ inForLoopInit: true });
|
x3 = x.update({ inForLoopInit: true });
|
||||||
if ((tt = t.peek()) !== SEMICOLON) {
|
n2 = null;
|
||||||
|
if ((tt = t.peek(true)) !== SEMICOLON) {
|
||||||
if (tt === VAR || tt === CONST) {
|
if (tt === VAR || tt === CONST) {
|
||||||
t.get();
|
t.get();
|
||||||
n2 = Variables(t, x3);
|
n2 = Variables(t, x3);
|
||||||
|
|
@ -424,15 +664,16 @@ function Statement(t, x) {
|
||||||
n.iterator = n2;
|
n.iterator = n2;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
x3.inForLoopInit = false;
|
||||||
n.setup = n2;
|
n.setup = n2;
|
||||||
t.mustMatch(SEMICOLON);
|
t.mustMatch(SEMICOLON);
|
||||||
if (n.isEach)
|
if (n.isEach)
|
||||||
throw t.newSyntaxError("Invalid for each..in loop");
|
throw t.newSyntaxError("Invalid for each..in loop");
|
||||||
n.condition = (t.peek() === SEMICOLON)
|
n.condition = (t.peek(true) === SEMICOLON)
|
||||||
? null
|
? null
|
||||||
: Expression(t, x3);
|
: Expression(t, x3);
|
||||||
t.mustMatch(SEMICOLON);
|
t.mustMatch(SEMICOLON);
|
||||||
tt2 = t.peek();
|
tt2 = t.peek(true);
|
||||||
n.update = (x.parenFreeMode
|
n.update = (x.parenFreeMode
|
||||||
? tt2 === LEFT_CURLY || definitions.isStatementStartCode[tt2]
|
? tt2 === LEFT_CURLY || definitions.isStatementStartCode[tt2]
|
||||||
: tt2 === RIGHT_PAREN)
|
: tt2 === RIGHT_PAREN)
|
||||||
|
|
@ -446,13 +687,15 @@ function Statement(t, x) {
|
||||||
|
|
||||||
case WHILE:
|
case WHILE:
|
||||||
n = new Node(t, { isLoop: true });
|
n = new Node(t, { isLoop: true });
|
||||||
|
n.blockComments = comments;
|
||||||
n.condition = HeadExpression(t, x);
|
n.condition = HeadExpression(t, x);
|
||||||
n.body = Statement(t, x.pushTarget(n).nest(NESTING_DEEP));
|
n.body = Statement(t, x.pushTarget(n).nest());
|
||||||
return n;
|
return n;
|
||||||
|
|
||||||
case DO:
|
case DO:
|
||||||
n = new Node(t, { isLoop: true });
|
n = new Node(t, { isLoop: true });
|
||||||
n.body = Statement(t, x.pushTarget(n).nest(NESTING_DEEP));
|
n.blockComments = comments;
|
||||||
|
n.body = Statement(t, x.pushTarget(n).nest());
|
||||||
t.mustMatch(WHILE);
|
t.mustMatch(WHILE);
|
||||||
n.condition = HeadExpression(t, x);
|
n.condition = HeadExpression(t, x);
|
||||||
if (!x.ecmaStrictMode) {
|
if (!x.ecmaStrictMode) {
|
||||||
|
|
@ -467,6 +710,7 @@ function Statement(t, x) {
|
||||||
case BREAK:
|
case BREAK:
|
||||||
case CONTINUE:
|
case CONTINUE:
|
||||||
n = new Node(t);
|
n = new Node(t);
|
||||||
|
n.blockComments = comments;
|
||||||
|
|
||||||
// handle the |foo: break foo;| corner case
|
// handle the |foo: break foo;| corner case
|
||||||
x2 = x.pushTarget(n);
|
x2 = x.pushTarget(n);
|
||||||
|
|
@ -476,9 +720,13 @@ function Statement(t, x) {
|
||||||
n.label = t.token.value;
|
n.label = t.token.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
n.target = n.label
|
if (n.label) {
|
||||||
? x2.labeledTargets.find(function(target) { return target.labels.has(n.label) })
|
n.target = x2.labeledTargets.find(function(target) { return target.labels.has(n.label) });
|
||||||
: x2.defaultTarget;
|
} else if (tt === CONTINUE) {
|
||||||
|
n.target = x2.defaultLoopTarget;
|
||||||
|
} else {
|
||||||
|
n.target = x2.defaultTarget;
|
||||||
|
}
|
||||||
|
|
||||||
if (!n.target)
|
if (!n.target)
|
||||||
throw t.newSyntaxError("Invalid " + ((tt === BREAK) ? "break" : "continue"));
|
throw t.newSyntaxError("Invalid " + ((tt === BREAK) ? "break" : "continue"));
|
||||||
|
|
@ -489,6 +737,7 @@ function Statement(t, x) {
|
||||||
|
|
||||||
case TRY:
|
case TRY:
|
||||||
n = new Node(t, { catchClauses: [] });
|
n = new Node(t, { catchClauses: [] });
|
||||||
|
n.blockComments = comments;
|
||||||
n.tryBlock = Block(t, x);
|
n.tryBlock = Block(t, x);
|
||||||
while (t.match(CATCH)) {
|
while (t.match(CATCH)) {
|
||||||
n2 = new Node(t);
|
n2 = new Node(t);
|
||||||
|
|
@ -539,8 +788,9 @@ function Statement(t, x) {
|
||||||
|
|
||||||
case WITH:
|
case WITH:
|
||||||
n = new Node(t);
|
n = new Node(t);
|
||||||
|
n.blockComments = comments;
|
||||||
n.object = HeadExpression(t, x);
|
n.object = HeadExpression(t, x);
|
||||||
n.body = Statement(t, x.pushTarget(n).nest(NESTING_DEEP));
|
n.body = Statement(t, x.pushTarget(n).nest());
|
||||||
return n;
|
return n;
|
||||||
|
|
||||||
case VAR:
|
case VAR:
|
||||||
|
|
@ -562,6 +812,7 @@ function Statement(t, x) {
|
||||||
case NEWLINE:
|
case NEWLINE:
|
||||||
case SEMICOLON:
|
case SEMICOLON:
|
||||||
n = new Node(t, { type: SEMICOLON });
|
n = new Node(t, { type: SEMICOLON });
|
||||||
|
n.blockComments = comments;
|
||||||
n.expression = null;
|
n.expression = null;
|
||||||
return n;
|
return n;
|
||||||
|
|
||||||
|
|
@ -575,7 +826,8 @@ function Statement(t, x) {
|
||||||
throw t.newSyntaxError("Duplicate label");
|
throw t.newSyntaxError("Duplicate label");
|
||||||
t.get();
|
t.get();
|
||||||
n = new Node(t, { type: LABEL, label: label });
|
n = new Node(t, { type: LABEL, label: label });
|
||||||
n.statement = Statement(t, x.pushLabel(label).nest(NESTING_SHALLOW));
|
n.blockComments = comments;
|
||||||
|
n.statement = Statement(t, x.pushLabel(label).nest());
|
||||||
n.target = (n.statement.type === LABEL) ? n.statement.target : n.statement;
|
n.target = (n.statement.type === LABEL) ? n.statement.target : n.statement;
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
@ -585,15 +837,20 @@ function Statement(t, x) {
|
||||||
// We unget the current token to parse the expression as a whole.
|
// We unget the current token to parse the expression as a whole.
|
||||||
n = new Node(t, { type: SEMICOLON });
|
n = new Node(t, { type: SEMICOLON });
|
||||||
t.unget();
|
t.unget();
|
||||||
|
n.blockComments = comments;
|
||||||
n.expression = Expression(t, x);
|
n.expression = Expression(t, x);
|
||||||
n.end = n.expression.end;
|
n.end = n.expression.end;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
n.blockComments = comments;
|
||||||
MagicalSemicolon(t);
|
MagicalSemicolon(t);
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* MagicalSemicolon :: (tokenizer) -> void
|
||||||
|
*/
|
||||||
function MagicalSemicolon(t) {
|
function MagicalSemicolon(t) {
|
||||||
var tt;
|
var tt;
|
||||||
if (t.lineno === t.token.lineno) {
|
if (t.lineno === t.token.lineno) {
|
||||||
|
|
@ -604,6 +861,9 @@ function MagicalSemicolon(t) {
|
||||||
t.match(SEMICOLON);
|
t.match(SEMICOLON);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ReturnOrYield :: (tokenizer, compiler context) -> (RETURN | YIELD) node
|
||||||
|
*/
|
||||||
function ReturnOrYield(t, x) {
|
function ReturnOrYield(t, x) {
|
||||||
var n, b, tt = t.token.type, tt2;
|
var n, b, tt = t.token.type, tt2;
|
||||||
|
|
||||||
|
|
@ -619,7 +879,7 @@ function ReturnOrYield(t, x) {
|
||||||
}
|
}
|
||||||
n = new Node(t, { value: undefined });
|
n = new Node(t, { value: undefined });
|
||||||
|
|
||||||
tt2 = t.peek(true);
|
tt2 = (tt === RETURN) ? t.peekOnSameLine(true) : t.peek(true);
|
||||||
if (tt2 !== END && tt2 !== NEWLINE &&
|
if (tt2 !== END && tt2 !== NEWLINE &&
|
||||||
tt2 !== SEMICOLON && tt2 !== RIGHT_CURLY
|
tt2 !== SEMICOLON && tt2 !== RIGHT_CURLY
|
||||||
&& (tt !== YIELD ||
|
&& (tt !== YIELD ||
|
||||||
|
|
@ -642,14 +902,153 @@ function ReturnOrYield(t, x) {
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ModuleExpression :: (tokenizer, compiler context) -> (STRING | IDENTIFIER | DOT) node
|
||||||
|
*/
|
||||||
|
function ModuleExpression(t, x) {
|
||||||
|
return t.match(STRING) ? new Node(t) : QualifiedPath(t, x);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ImportPathList :: (tokenizer, compiler context) -> Array[DOT node]
|
||||||
|
*/
|
||||||
|
function ImportPathList(t, x) {
|
||||||
|
var a = [];
|
||||||
|
do {
|
||||||
|
a.push(ImportPath(t, x));
|
||||||
|
} while (t.match(COMMA));
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ImportPath :: (tokenizer, compiler context) -> DOT node
|
||||||
|
*/
|
||||||
|
function ImportPath(t, x) {
|
||||||
|
var n = QualifiedPath(t, x);
|
||||||
|
if (!t.match(DOT)) {
|
||||||
|
if (n.type === IDENTIFIER)
|
||||||
|
throw t.newSyntaxError("cannot import local variable");
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
var n2 = new Node(t);
|
||||||
|
n2.push(n);
|
||||||
|
n2.push(ImportSpecifierSet(t, x));
|
||||||
|
return n2;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ExplicitSpecifierSet :: (tokenizer, compiler context, (tokenizer, compiler context) -> node)
|
||||||
|
* -> OBJECT_INIT node
|
||||||
|
*/
|
||||||
|
function ExplicitSpecifierSet(t, x, SpecifierRHS) {
|
||||||
|
var n, n2, id, tt;
|
||||||
|
|
||||||
|
n = new Node(t, { type: OBJECT_INIT });
|
||||||
|
t.mustMatch(LEFT_CURLY);
|
||||||
|
|
||||||
|
if (!t.match(RIGHT_CURLY)) {
|
||||||
|
do {
|
||||||
|
id = Identifier(t, x);
|
||||||
|
if (t.match(COLON)) {
|
||||||
|
n2 = new Node(t, { type: PROPERTY_INIT });
|
||||||
|
n2.push(id);
|
||||||
|
n2.push(SpecifierRHS(t, x));
|
||||||
|
n.push(n2);
|
||||||
|
} else {
|
||||||
|
n.push(id);
|
||||||
|
}
|
||||||
|
} while (!t.match(RIGHT_CURLY) && t.mustMatch(COMMA));
|
||||||
|
}
|
||||||
|
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ImportSpecifierSet :: (tokenizer, compiler context) -> (IDENTIFIER | OBJECT_INIT) node
|
||||||
|
*/
|
||||||
|
function ImportSpecifierSet(t, x) {
|
||||||
|
return t.match(MUL)
|
||||||
|
? new Node(t, { type: IDENTIFIER, name: "*" })
|
||||||
|
: ExplicitSpecifierSet(t, x, Identifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Identifier :: (tokenizer, compiler context) -> IDENTIFIER node
|
||||||
|
*/
|
||||||
|
function Identifier(t, x) {
|
||||||
|
t.mustMatch(IDENTIFIER);
|
||||||
|
return new Node(t, { type: IDENTIFIER });
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* IdentifierName :: (tokenizer) -> IDENTIFIER node
|
||||||
|
*/
|
||||||
|
function IdentifierName(t) {
|
||||||
|
if (t.match(IDENTIFIER))
|
||||||
|
return new Node(t, { type: IDENTIFIER });
|
||||||
|
t.get();
|
||||||
|
if (t.token.value in definitions.keywords)
|
||||||
|
return new Node(t, { type: IDENTIFIER });
|
||||||
|
throw t.newSyntaxError("missing IdentifierName");
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* QualifiedPath :: (tokenizer, compiler context) -> (IDENTIFIER | DOT) node
|
||||||
|
*/
|
||||||
|
function QualifiedPath(t, x) {
|
||||||
|
var n, n2;
|
||||||
|
|
||||||
|
n = Identifier(t, x);
|
||||||
|
|
||||||
|
while (t.match(DOT)) {
|
||||||
|
if (t.peek() !== IDENTIFIER) {
|
||||||
|
// Unget the '.' token, which isn't part of the QualifiedPath.
|
||||||
|
t.unget();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
n2 = new Node(t);
|
||||||
|
n2.push(n);
|
||||||
|
n2.push(Identifier(t, x));
|
||||||
|
n = n2;
|
||||||
|
}
|
||||||
|
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ExportPath :: (tokenizer, compiler context) -> (IDENTIFIER | DOT | OBJECT_INIT) node
|
||||||
|
*/
|
||||||
|
function ExportPath(t, x) {
|
||||||
|
if (t.peek() === LEFT_CURLY)
|
||||||
|
return ExplicitSpecifierSet(t, x, QualifiedPath);
|
||||||
|
return QualifiedPath(t, x);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ExportPathList :: (tokenizer, compiler context)
|
||||||
|
* -> Array[(IDENTIFIER | DOT | OBJECT_INIT) node]
|
||||||
|
*/
|
||||||
|
function ExportPathList(t, x) {
|
||||||
|
var a = [];
|
||||||
|
do {
|
||||||
|
a.push(ExportPath(t, x));
|
||||||
|
} while (t.match(COMMA));
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* FunctionDefinition :: (tokenizer, compiler context, boolean,
|
* FunctionDefinition :: (tokenizer, compiler context, boolean,
|
||||||
* DECLARED_FORM or EXPRESSED_FORM or STATEMENT_FORM)
|
* DECLARED_FORM or EXPRESSED_FORM or STATEMENT_FORM,
|
||||||
|
* [string] or null or undefined)
|
||||||
* -> node
|
* -> node
|
||||||
*/
|
*/
|
||||||
function FunctionDefinition(t, x, requireName, functionForm) {
|
function FunctionDefinition(t, x, requireName, functionForm, comments) {
|
||||||
var tt;
|
var tt;
|
||||||
var f = new Node(t, { params: [] });
|
var f = new Node(t, { params: [], paramComments: [] });
|
||||||
|
if (typeof comment === "undefined")
|
||||||
|
comment = null;
|
||||||
|
f.blockComments = comments;
|
||||||
if (f.type !== FUNCTION)
|
if (f.type !== FUNCTION)
|
||||||
f.type = (f.value === "get") ? GETTER : SETTER;
|
f.type = (f.value === "get") ? GETTER : SETTER;
|
||||||
if (t.match(IDENTIFIER))
|
if (t.match(IDENTIFIER))
|
||||||
|
|
@ -657,12 +1056,15 @@ function FunctionDefinition(t, x, requireName, functionForm) {
|
||||||
else if (requireName)
|
else if (requireName)
|
||||||
throw t.newSyntaxError("missing function identifier");
|
throw t.newSyntaxError("missing function identifier");
|
||||||
|
|
||||||
var x2 = new StaticContext(null, null, true, false, NESTING_TOP);
|
var inModule = x ? x.inModule : false;
|
||||||
|
var x2 = new StaticContext(null, null, inModule, true);
|
||||||
|
|
||||||
t.mustMatch(LEFT_PAREN);
|
t.mustMatch(LEFT_PAREN);
|
||||||
if (!t.match(RIGHT_PAREN)) {
|
if (!t.match(RIGHT_PAREN)) {
|
||||||
do {
|
do {
|
||||||
switch (t.get()) {
|
tt = t.get();
|
||||||
|
f.paramComments.push(t.lastBlockComment());
|
||||||
|
switch (tt) {
|
||||||
case LEFT_BRACKET:
|
case LEFT_BRACKET:
|
||||||
case LEFT_CURLY:
|
case LEFT_CURLY:
|
||||||
// Destructured formal parameters.
|
// Destructured formal parameters.
|
||||||
|
|
@ -690,7 +1092,7 @@ function FunctionDefinition(t, x, requireName, functionForm) {
|
||||||
if (f.body.isGenerator)
|
if (f.body.isGenerator)
|
||||||
throw t.newSyntaxError("Generator returns a value");
|
throw t.newSyntaxError("Generator returns a value");
|
||||||
} else {
|
} else {
|
||||||
f.body = Script(t, true);
|
f.body = Script(t, inModule, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tt === LEFT_CURLY)
|
if (tt === LEFT_CURLY)
|
||||||
|
|
@ -703,6 +1105,28 @@ function FunctionDefinition(t, x, requireName, functionForm) {
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ModuleVariables :: (tokenizer, compiler context, MODULE node) -> void
|
||||||
|
*
|
||||||
|
* Parses a comma-separated list of module declarations (and maybe
|
||||||
|
* initializations).
|
||||||
|
*/
|
||||||
|
function ModuleVariables(t, x, n) {
|
||||||
|
var n1, n2;
|
||||||
|
do {
|
||||||
|
n1 = Identifier(t, x);
|
||||||
|
if (t.match(ASSIGN)) {
|
||||||
|
n2 = ModuleExpression(t, x);
|
||||||
|
n1.initializer = n2;
|
||||||
|
if (n2.type === STRING)
|
||||||
|
x.parentScript.modLoads.set(n1.value, n2.value);
|
||||||
|
else
|
||||||
|
x.parentScript.modAssns.set(n1.value, n1);
|
||||||
|
}
|
||||||
|
n.push(n1);
|
||||||
|
} while (t.match(COMMA));
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Variables :: (tokenizer, compiler context) -> node
|
* Variables :: (tokenizer, compiler context) -> node
|
||||||
*
|
*
|
||||||
|
|
@ -752,6 +1176,7 @@ function Variables(t, x, letBlock) {
|
||||||
if (t.token.assignOp)
|
if (t.token.assignOp)
|
||||||
throw t.newSyntaxError("Invalid variable initialization");
|
throw t.newSyntaxError("Invalid variable initialization");
|
||||||
|
|
||||||
|
n2.blockComment = t.lastBlockComment();
|
||||||
n2.initializer = AssignExpression(t, x);
|
n2.initializer = AssignExpression(t, x);
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -767,11 +1192,15 @@ function Variables(t, x, letBlock) {
|
||||||
s.varDecls.push(n2);
|
s.varDecls.push(n2);
|
||||||
|
|
||||||
if (t.match(ASSIGN)) {
|
if (t.match(ASSIGN)) {
|
||||||
|
var comment = t.lastBlockComment();
|
||||||
if (t.token.assignOp)
|
if (t.token.assignOp)
|
||||||
throw t.newSyntaxError("Invalid variable initialization");
|
throw t.newSyntaxError("Invalid variable initialization");
|
||||||
|
|
||||||
n2.initializer = AssignExpression(t, x);
|
n2.initializer = AssignExpression(t, x);
|
||||||
|
} else {
|
||||||
|
var comment = t.lastBlockComment();
|
||||||
}
|
}
|
||||||
|
n2.blockComment = comment;
|
||||||
} while (t.match(COMMA));
|
} while (t.match(COMMA));
|
||||||
|
|
||||||
return n;
|
return n;
|
||||||
|
|
@ -983,6 +1412,8 @@ function AssignExpression(t, x) {
|
||||||
return lhs;
|
return lhs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
n.blockComment = t.lastBlockComment();
|
||||||
|
|
||||||
switch (lhs.type) {
|
switch (lhs.type) {
|
||||||
case OBJECT_INIT:
|
case OBJECT_INIT:
|
||||||
case ARRAY_INIT:
|
case ARRAY_INIT:
|
||||||
|
|
@ -995,7 +1426,7 @@ function AssignExpression(t, x) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
n.assignOp = t.token.assignOp;
|
n.assignOp = lhs.assignOp = t.token.assignOp;
|
||||||
n.push(lhs);
|
n.push(lhs);
|
||||||
n.push(AssignExpression(t, x));
|
n.push(AssignExpression(t, x));
|
||||||
|
|
||||||
|
|
@ -1232,8 +1663,7 @@ function MemberExpression(t, x, allowCallSyntax) {
|
||||||
case DOT:
|
case DOT:
|
||||||
n2 = new Node(t);
|
n2 = new Node(t);
|
||||||
n2.push(n);
|
n2.push(n);
|
||||||
t.mustMatch(IDENTIFIER);
|
n2.push(IdentifierName(t));
|
||||||
n2.push(new Node(t));
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case LEFT_BRACKET:
|
case LEFT_BRACKET:
|
||||||
|
|
@ -1331,6 +1761,7 @@ function PrimaryExpression(t, x) {
|
||||||
throw t.newSyntaxError("Illegal property accessor");
|
throw t.newSyntaxError("Illegal property accessor");
|
||||||
n.push(FunctionDefinition(t, x, true, EXPRESSED_FORM));
|
n.push(FunctionDefinition(t, x, true, EXPRESSED_FORM));
|
||||||
} else {
|
} else {
|
||||||
|
var comments = t.blockComments;
|
||||||
switch (tt) {
|
switch (tt) {
|
||||||
case IDENTIFIER: case NUMBER: case STRING:
|
case IDENTIFIER: case NUMBER: case STRING:
|
||||||
id = new Node(t, { type: IDENTIFIER });
|
id = new Node(t, { type: IDENTIFIER });
|
||||||
|
|
@ -1350,6 +1781,7 @@ function PrimaryExpression(t, x) {
|
||||||
n2 = new Node(t, { type: PROPERTY_INIT });
|
n2 = new Node(t, { type: PROPERTY_INIT });
|
||||||
n2.push(id);
|
n2.push(id);
|
||||||
n2.push(AssignExpression(t, x));
|
n2.push(AssignExpression(t, x));
|
||||||
|
n2.blockComments = comments;
|
||||||
n.push(n2);
|
n.push(n2);
|
||||||
} else {
|
} else {
|
||||||
// Support, e.g., |var {x, y} = o| as destructuring shorthand
|
// Support, e.g., |var {x, y} = o| as destructuring shorthand
|
||||||
|
|
@ -1392,7 +1824,7 @@ function PrimaryExpression(t, x) {
|
||||||
*/
|
*/
|
||||||
function parse(s, f, l) {
|
function parse(s, f, l) {
|
||||||
var t = new lexer.Tokenizer(s, f, l);
|
var t = new lexer.Tokenizer(s, f, l);
|
||||||
var n = Script(t, false);
|
var n = Script(t, false, false);
|
||||||
if (!t.done)
|
if (!t.done)
|
||||||
throw t.newSyntaxError("Syntax error");
|
throw t.newSyntaxError("Syntax error");
|
||||||
|
|
||||||
|
|
@ -1400,33 +1832,78 @@ function parse(s, f, l) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* parseStdin :: (source, {line number}) -> node
|
* parseStdin :: (source, {line number}, string, (string) -> boolean) -> program node
|
||||||
*/
|
*/
|
||||||
function parseStdin(s, ln) {
|
function parseStdin(s, ln, prefix, isCommand) {
|
||||||
|
// the special .begin command is only recognized at the beginning
|
||||||
|
if (s.match(/^[\s]*\.begin[\s]*$/)) {
|
||||||
|
++ln.value;
|
||||||
|
return parseMultiline(ln, prefix);
|
||||||
|
}
|
||||||
|
|
||||||
|
// commands at the beginning are treated as the entire input
|
||||||
|
if (isCommand(s.trim()))
|
||||||
|
s = "";
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
try {
|
try {
|
||||||
var t = new lexer.Tokenizer(s, "stdin", ln.value);
|
var t = new lexer.Tokenizer(s, "stdin", ln.value);
|
||||||
var n = Script(t, false);
|
var n = Script(t, false, false);
|
||||||
ln.value = t.lineno;
|
ln.value = t.lineno;
|
||||||
return n;
|
return n;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (!t.unexpectedEOF)
|
if (!t.unexpectedEOF)
|
||||||
throw e;
|
throw e;
|
||||||
var more = readline();
|
|
||||||
|
// commands in the middle are not treated as part of the input
|
||||||
|
var more;
|
||||||
|
do {
|
||||||
|
if (prefix)
|
||||||
|
putstr(prefix);
|
||||||
|
more = readline();
|
||||||
if (!more)
|
if (!more)
|
||||||
throw e;
|
throw e;
|
||||||
|
} while (isCommand(more.trim()));
|
||||||
|
|
||||||
s += "\n" + more;
|
s += "\n" + more;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.parse = parse;
|
/*
|
||||||
exports.parseStdin = parseStdin;
|
* parseMultiline :: ({line number}, string | null) -> program node
|
||||||
exports.Node = Node;
|
*/
|
||||||
exports.DECLARED_FORM = DECLARED_FORM;
|
function parseMultiline(ln, prefix) {
|
||||||
exports.EXPRESSED_FORM = EXPRESSED_FORM;
|
var s = "";
|
||||||
exports.STATEMENT_FORM = STATEMENT_FORM;
|
for (;;) {
|
||||||
exports.Tokenizer = lexer.Tokenizer;
|
if (prefix)
|
||||||
exports.FunctionDefinition = FunctionDefinition;
|
putstr(prefix);
|
||||||
|
var more = readline();
|
||||||
|
if (more === null)
|
||||||
|
return null;
|
||||||
|
// the only command recognized in multiline mode is .end
|
||||||
|
if (more.match(/^[\s]*\.end[\s]*$/))
|
||||||
|
break;
|
||||||
|
s += "\n" + more;
|
||||||
|
}
|
||||||
|
var t = new lexer.Tokenizer(s, "stdin", ln.value);
|
||||||
|
var n = Script(t, false, false);
|
||||||
|
ln.value = t.lineno;
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
parse: parse,
|
||||||
|
parseStdin: parseStdin,
|
||||||
|
Node: Node,
|
||||||
|
SyntheticNode: SyntheticNode,
|
||||||
|
DECLARED_FORM: DECLARED_FORM,
|
||||||
|
EXPRESSED_FORM: EXPRESSED_FORM,
|
||||||
|
STATEMENT_FORM: STATEMENT_FORM,
|
||||||
|
Tokenizer: lexer.Tokenizer,
|
||||||
|
FunctionDefinition: FunctionDefinition,
|
||||||
|
Module: Module,
|
||||||
|
Export: Export
|
||||||
|
};
|
||||||
|
|
||||||
});
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue