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,324 +52,628 @@
|
||||||
|
|
||||||
define(function(require, exports, module) {
|
define(function(require, exports, module) {
|
||||||
|
|
||||||
exports.options = {
|
var narcissus = {
|
||||||
version: 185,
|
options: {
|
||||||
};
|
version: 185,
|
||||||
|
// Global variables to hide from the interpreter
|
||||||
(function() {
|
hiddenHostGlobals: { Narcissus: true },
|
||||||
exports.hostGlobal = this
|
// Desugar SpiderMonkey language extensions?
|
||||||
})();
|
desugarExtensions: false
|
||||||
|
|
||||||
var tokens = [
|
|
||||||
// End of source.
|
|
||||||
"END",
|
|
||||||
|
|
||||||
// Operators and punctuators. Some pair-wise order matters, e.g. (+, -)
|
|
||||||
// and (UNARY_PLUS, UNARY_MINUS).
|
|
||||||
"\n", ";",
|
|
||||||
",",
|
|
||||||
"=",
|
|
||||||
"?", ":", "CONDITIONAL",
|
|
||||||
"||",
|
|
||||||
"&&",
|
|
||||||
"|",
|
|
||||||
"^",
|
|
||||||
"&",
|
|
||||||
"==", "!=", "===", "!==",
|
|
||||||
"<", "<=", ">=", ">",
|
|
||||||
"<<", ">>", ">>>",
|
|
||||||
"+", "-",
|
|
||||||
"*", "/", "%",
|
|
||||||
"!", "~", "UNARY_PLUS", "UNARY_MINUS",
|
|
||||||
"++", "--",
|
|
||||||
".",
|
|
||||||
"[", "]",
|
|
||||||
"{", "}",
|
|
||||||
"(", ")",
|
|
||||||
|
|
||||||
// Nonterminal tree node type codes.
|
|
||||||
"SCRIPT", "BLOCK", "LABEL", "FOR_IN", "CALL", "NEW_WITH_ARGS", "INDEX",
|
|
||||||
"ARRAY_INIT", "OBJECT_INIT", "PROPERTY_INIT", "GETTER", "SETTER",
|
|
||||||
"GROUP", "LIST", "LET_BLOCK", "ARRAY_COMP", "GENERATOR", "COMP_TAIL",
|
|
||||||
|
|
||||||
// Terminals.
|
|
||||||
"IDENTIFIER", "NUMBER", "STRING", "REGEXP",
|
|
||||||
|
|
||||||
// Keywords.
|
|
||||||
"break",
|
|
||||||
"case", "catch", "const", "continue",
|
|
||||||
"debugger", "default", "delete", "do",
|
|
||||||
"else",
|
|
||||||
"false", "finally", "for", "function",
|
|
||||||
"if", "in", "instanceof",
|
|
||||||
"let",
|
|
||||||
"new", "null",
|
|
||||||
"return",
|
|
||||||
"switch",
|
|
||||||
"this", "throw", "true", "try", "typeof",
|
|
||||||
"var", "void",
|
|
||||||
"yield",
|
|
||||||
"while", "with",
|
|
||||||
];
|
|
||||||
|
|
||||||
var statementStartTokens = [
|
|
||||||
"break",
|
|
||||||
"const", "continue",
|
|
||||||
"debugger", "do",
|
|
||||||
"for",
|
|
||||||
"if",
|
|
||||||
"return",
|
|
||||||
"switch",
|
|
||||||
"throw", "try",
|
|
||||||
"var",
|
|
||||||
"yield",
|
|
||||||
"while", "with",
|
|
||||||
];
|
|
||||||
|
|
||||||
// Operator and punctuator mapping from token to tree node type name.
|
|
||||||
// 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
|
|
||||||
// tokens != and !).
|
|
||||||
var opTypeNames = {
|
|
||||||
'\n': "NEWLINE",
|
|
||||||
';': "SEMICOLON",
|
|
||||||
',': "COMMA",
|
|
||||||
'?': "HOOK",
|
|
||||||
':': "COLON",
|
|
||||||
'||': "OR",
|
|
||||||
'&&': "AND",
|
|
||||||
'|': "BITWISE_OR",
|
|
||||||
'^': "BITWISE_XOR",
|
|
||||||
'&': "BITWISE_AND",
|
|
||||||
'===': "STRICT_EQ",
|
|
||||||
'==': "EQ",
|
|
||||||
'=': "ASSIGN",
|
|
||||||
'!==': "STRICT_NE",
|
|
||||||
'!=': "NE",
|
|
||||||
'<<': "LSH",
|
|
||||||
'<=': "LE",
|
|
||||||
'<': "LT",
|
|
||||||
'>>>': "URSH",
|
|
||||||
'>>': "RSH",
|
|
||||||
'>=': "GE",
|
|
||||||
'>': "GT",
|
|
||||||
'++': "INCREMENT",
|
|
||||||
'--': "DECREMENT",
|
|
||||||
'+': "PLUS",
|
|
||||||
'-': "MINUS",
|
|
||||||
'*': "MUL",
|
|
||||||
'/': "DIV",
|
|
||||||
'%': "MOD",
|
|
||||||
'!': "NOT",
|
|
||||||
'~': "BITWISE_NOT",
|
|
||||||
'.': "DOT",
|
|
||||||
'[': "LEFT_BRACKET",
|
|
||||||
']': "RIGHT_BRACKET",
|
|
||||||
'{': "LEFT_CURLY",
|
|
||||||
'}': "RIGHT_CURLY",
|
|
||||||
'(': "LEFT_PAREN",
|
|
||||||
')': "RIGHT_PAREN"
|
|
||||||
};
|
|
||||||
|
|
||||||
// Hash of keyword identifier to tokens index. NB: we must null __proto__ to
|
|
||||||
// avoid toString, etc. namespace pollution.
|
|
||||||
var keywords = {__proto__: null};
|
|
||||||
|
|
||||||
// Define const END, etc., based on the token names. Also map name to index.
|
|
||||||
var tokenIds = {};
|
|
||||||
|
|
||||||
// Building up a string to be eval'd in different contexts.
|
|
||||||
var consts = "const ";
|
|
||||||
for (var i = 0, j = tokens.length; i < j; i++) {
|
|
||||||
if (i > 0)
|
|
||||||
consts += ", ";
|
|
||||||
var t = tokens[i];
|
|
||||||
var name;
|
|
||||||
if (/^[a-z]/.test(t)) {
|
|
||||||
name = t.toUpperCase();
|
|
||||||
keywords[t] = i;
|
|
||||||
} else {
|
|
||||||
name = (/^\W/.test(t) ? opTypeNames[t] : t);
|
|
||||||
}
|
|
||||||
consts += name + " = " + i;
|
|
||||||
tokenIds[name] = i;
|
|
||||||
tokens[t] = i;
|
|
||||||
}
|
|
||||||
consts += ";";
|
|
||||||
|
|
||||||
var isStatementStartCode = {__proto__: null};
|
|
||||||
for (i = 0, j = statementStartTokens.length; i < j; i++)
|
|
||||||
isStatementStartCode[keywords[statementStartTokens[i]]] = true;
|
|
||||||
|
|
||||||
// Map assignment operators to their indexes in the tokens array.
|
|
||||||
var assignOps = ['|', '^', '&', '<<', '>>', '>>>', '+', '-', '*', '/', '%'];
|
|
||||||
|
|
||||||
for (i = 0, j = assignOps.length; i < j; i++) {
|
|
||||||
t = assignOps[i];
|
|
||||||
assignOps[t] = tokens[t];
|
|
||||||
}
|
|
||||||
|
|
||||||
function defineGetter(obj, prop, fn, dontDelete, dontEnum) {
|
|
||||||
Object.defineProperty(obj, prop,
|
|
||||||
{ get: fn, configurable: !dontDelete, enumerable: !dontEnum });
|
|
||||||
}
|
|
||||||
|
|
||||||
function defineProperty(obj, prop, val, dontDelete, readOnly, dontEnum) {
|
|
||||||
Object.defineProperty(obj, prop,
|
|
||||||
{ value: val, writable: !readOnly, configurable: !dontDelete,
|
|
||||||
enumerable: !dontEnum });
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns true if fn is a native function. (Note: SpiderMonkey specific.)
|
|
||||||
function isNativeCode(fn) {
|
|
||||||
// Relies on the toString method to identify native code.
|
|
||||||
return ((typeof fn) === "function") && fn.toString().match(/\[native code\]/);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getPropertyDescriptor(obj, name) {
|
|
||||||
while (obj) {
|
|
||||||
if (({}).hasOwnProperty.call(obj, name))
|
|
||||||
return Object.getOwnPropertyDescriptor(obj, name);
|
|
||||||
obj = Object.getPrototypeOf(obj);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getOwnProperties(obj) {
|
|
||||||
var map = {};
|
|
||||||
for (var name in Object.getOwnPropertyNames(obj))
|
|
||||||
map[name] = Object.getOwnPropertyDescriptor(obj, name);
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
|
|
||||||
function makePassthruHandler(obj) {
|
|
||||||
// Handler copied from
|
|
||||||
// http://wiki.ecmascript.org/doku.php?id=harmony:proxies&s=proxy%20object#examplea_no-op_forwarding_proxy
|
|
||||||
return {
|
|
||||||
getOwnPropertyDescriptor: function(name) {
|
|
||||||
var desc = Object.getOwnPropertyDescriptor(obj, name);
|
|
||||||
|
|
||||||
// a trapping proxy's properties must always be configurable
|
|
||||||
desc.configurable = true;
|
|
||||||
return desc;
|
|
||||||
},
|
},
|
||||||
getPropertyDescriptor: function(name) {
|
hostSupportsEvalConst: (function() {
|
||||||
var desc = getPropertyDescriptor(obj, name);
|
try {
|
||||||
|
return eval("(function(s) { eval(s); return x })('const x = true;')");
|
||||||
// a trapping proxy's properties must always be configurable
|
} catch (e) {
|
||||||
desc.configurable = true;
|
return false;
|
||||||
return desc;
|
|
||||||
},
|
|
||||||
getOwnPropertyNames: function() {
|
|
||||||
return Object.getOwnPropertyNames(obj);
|
|
||||||
},
|
|
||||||
defineProperty: function(name, desc) {
|
|
||||||
Object.defineProperty(obj, name, desc);
|
|
||||||
},
|
|
||||||
"delete": function(name) { return delete obj[name]; },
|
|
||||||
fix: function() {
|
|
||||||
if (Object.isFrozen(obj)) {
|
|
||||||
return getOwnProperties(obj);
|
|
||||||
}
|
}
|
||||||
|
})(),
|
||||||
// As long as obj is not frozen, the proxy won't allow itself to be fixed.
|
hostGlobal: this
|
||||||
return undefined; // will cause a TypeError to be thrown
|
|
||||||
},
|
|
||||||
|
|
||||||
has: function(name) { return name in obj; },
|
|
||||||
hasOwn: function(name) { return ({}).hasOwnProperty.call(obj, name); },
|
|
||||||
get: function(receiver, name) { return obj[name]; },
|
|
||||||
|
|
||||||
// bad behavior when set fails in non-strict mode
|
|
||||||
set: function(receiver, name, val) { obj[name] = val; return true; },
|
|
||||||
enumerate: function() {
|
|
||||||
var result = [];
|
|
||||||
for (name in obj) { result.push(name); };
|
|
||||||
return result;
|
|
||||||
},
|
|
||||||
keys: function() { return Object.keys(obj); }
|
|
||||||
};
|
};
|
||||||
}
|
Narcissus = narcissus;
|
||||||
|
|
||||||
// default function used when looking for a property in the global object
|
var tokens = [
|
||||||
function noPropFound() { return undefined; }
|
// End of source.
|
||||||
|
"END",
|
||||||
|
|
||||||
var hasOwnProperty = ({}).hasOwnProperty;
|
// Operators and punctuators. Some pair-wise order matters, e.g. (+, -)
|
||||||
|
// and (UNARY_PLUS, UNARY_MINUS).
|
||||||
|
"\n", ";",
|
||||||
|
",",
|
||||||
|
"=",
|
||||||
|
"?", ":", "CONDITIONAL",
|
||||||
|
"||",
|
||||||
|
"&&",
|
||||||
|
"|",
|
||||||
|
"^",
|
||||||
|
"&",
|
||||||
|
"==", "!=", "===", "!==",
|
||||||
|
"<", "<=", ">=", ">",
|
||||||
|
"<<", ">>", ">>>",
|
||||||
|
"+", "-",
|
||||||
|
"*", "/", "%",
|
||||||
|
"!", "~", "UNARY_PLUS", "UNARY_MINUS",
|
||||||
|
"++", "--",
|
||||||
|
".",
|
||||||
|
"[", "]",
|
||||||
|
"{", "}",
|
||||||
|
"(", ")",
|
||||||
|
|
||||||
function StringMap() {
|
// Nonterminal tree node type codes.
|
||||||
this.table = Object.create(null, {});
|
"SCRIPT", "BLOCK", "LABEL", "FOR_IN", "CALL", "NEW_WITH_ARGS", "INDEX",
|
||||||
this.size = 0;
|
"ARRAY_INIT", "OBJECT_INIT", "PROPERTY_INIT", "GETTER", "SETTER",
|
||||||
}
|
"GROUP", "LIST", "LET_BLOCK", "ARRAY_COMP", "GENERATOR", "COMP_TAIL",
|
||||||
|
|
||||||
StringMap.prototype = {
|
// Terminals.
|
||||||
has: function(x) { return hasOwnProperty.call(this.table, x); },
|
"IDENTIFIER", "NUMBER", "STRING", "REGEXP",
|
||||||
set: function(x, v) {
|
|
||||||
if (!hasOwnProperty.call(this.table, x))
|
// Keywords.
|
||||||
this.size++;
|
"break",
|
||||||
this.table[x] = v;
|
"case", "catch", "const", "continue",
|
||||||
},
|
"debugger", "default", "delete", "do",
|
||||||
get: function(x) { return this.table[x]; },
|
"else", "export",
|
||||||
getDef: function(x, thunk) {
|
"false", "finally", "for", "function",
|
||||||
if (!hasOwnProperty.call(this.table, x)) {
|
"if", "import", "in", "instanceof",
|
||||||
this.size++;
|
"let", "module",
|
||||||
this.table[x] = thunk();
|
"new", "null",
|
||||||
|
"return",
|
||||||
|
"switch",
|
||||||
|
"this", "throw", "true", "try", "typeof",
|
||||||
|
"var", "void",
|
||||||
|
"yield",
|
||||||
|
"while", "with",
|
||||||
|
];
|
||||||
|
|
||||||
|
var statementStartTokens = [
|
||||||
|
"break",
|
||||||
|
"const", "continue",
|
||||||
|
"debugger", "do",
|
||||||
|
"for",
|
||||||
|
"if",
|
||||||
|
"return",
|
||||||
|
"switch",
|
||||||
|
"throw", "try",
|
||||||
|
"var",
|
||||||
|
"yield",
|
||||||
|
"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.
|
||||||
|
// 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
|
||||||
|
// tokens != and !).
|
||||||
|
var opTypeNames = {
|
||||||
|
'\n': "NEWLINE",
|
||||||
|
';': "SEMICOLON",
|
||||||
|
',': "COMMA",
|
||||||
|
'?': "HOOK",
|
||||||
|
':': "COLON",
|
||||||
|
'||': "OR",
|
||||||
|
'&&': "AND",
|
||||||
|
'|': "BITWISE_OR",
|
||||||
|
'^': "BITWISE_XOR",
|
||||||
|
'&': "BITWISE_AND",
|
||||||
|
'===': "STRICT_EQ",
|
||||||
|
'==': "EQ",
|
||||||
|
'=': "ASSIGN",
|
||||||
|
'!==': "STRICT_NE",
|
||||||
|
'!=': "NE",
|
||||||
|
'<<': "LSH",
|
||||||
|
'<=': "LE",
|
||||||
|
'<': "LT",
|
||||||
|
'>>>': "URSH",
|
||||||
|
'>>': "RSH",
|
||||||
|
'>=': "GE",
|
||||||
|
'>': "GT",
|
||||||
|
'++': "INCREMENT",
|
||||||
|
'--': "DECREMENT",
|
||||||
|
'+': "PLUS",
|
||||||
|
'-': "MINUS",
|
||||||
|
'*': "MUL",
|
||||||
|
'/': "DIV",
|
||||||
|
'%': "MOD",
|
||||||
|
'!': "NOT",
|
||||||
|
'~': "BITWISE_NOT",
|
||||||
|
'.': "DOT",
|
||||||
|
'[': "LEFT_BRACKET",
|
||||||
|
']': "RIGHT_BRACKET",
|
||||||
|
'{': "LEFT_CURLY",
|
||||||
|
'}': "RIGHT_CURLY",
|
||||||
|
'(': "LEFT_PAREN",
|
||||||
|
')': "RIGHT_PAREN"
|
||||||
|
};
|
||||||
|
|
||||||
|
// Hash of keyword identifier to tokens index. NB: we must null __proto__ to
|
||||||
|
// avoid toString, etc. namespace pollution.
|
||||||
|
var keywords = {__proto__: null};
|
||||||
|
|
||||||
|
// Define const END, etc., based on the token names. Also map name to index.
|
||||||
|
var tokenIds = {};
|
||||||
|
|
||||||
|
// Building up a string to be eval'd in different contexts.
|
||||||
|
var consts = Narcissus.hostSupportsEvalConst ? "const " : "var ";
|
||||||
|
for (var i = 0, j = tokens.length; i < j; i++) {
|
||||||
|
if (i > 0)
|
||||||
|
consts += ", ";
|
||||||
|
var t = tokens[i];
|
||||||
|
var name;
|
||||||
|
if (/^[a-z]/.test(t)) {
|
||||||
|
name = t.toUpperCase();
|
||||||
|
keywords[t] = i;
|
||||||
|
} else {
|
||||||
|
name = (/^\W/.test(t) ? opTypeNames[t] : t);
|
||||||
}
|
}
|
||||||
return this.table[x];
|
consts += name + " = " + i;
|
||||||
},
|
tokenIds[name] = i;
|
||||||
forEach: function(f) {
|
tokens[t] = i;
|
||||||
var table = this.table;
|
}
|
||||||
for (var key in table)
|
consts += ";";
|
||||||
f.call(this, key, table[key]);
|
|
||||||
},
|
|
||||||
toString: function() { return "[object StringMap]" }
|
|
||||||
};
|
|
||||||
|
|
||||||
// non-destructive stack
|
var isStatementStartCode = {__proto__: null};
|
||||||
function Stack(elts) {
|
for (i = 0, j = statementStartTokens.length; i < j; i++)
|
||||||
this.elts = elts || null;
|
isStatementStartCode[keywords[statementStartTokens[i]]] = true;
|
||||||
}
|
|
||||||
|
|
||||||
Stack.prototype = {
|
// Map assignment operators to their indexes in the tokens array.
|
||||||
push: function(x) {
|
var assignOps = ['|', '^', '&', '<<', '>>', '>>>', '+', '-', '*', '/', '%'];
|
||||||
return new Stack({ top: x, rest: this.elts });
|
|
||||||
},
|
for (i = 0, j = assignOps.length; i < j; i++) {
|
||||||
top: function() {
|
t = assignOps[i];
|
||||||
if (!this.elts)
|
assignOps[t] = tokens[t];
|
||||||
throw new Error("empty stack");
|
}
|
||||||
return this.elts.top;
|
|
||||||
},
|
function defineGetter(obj, prop, fn, dontDelete, dontEnum) {
|
||||||
isEmpty: function() {
|
Object.defineProperty(obj, prop,
|
||||||
return this.top === null;
|
{ get: fn, configurable: !dontDelete, enumerable: !dontEnum });
|
||||||
},
|
}
|
||||||
find: function(test) {
|
|
||||||
for (var elts = this.elts; elts; elts = elts.rest) {
|
function defineGetterSetter(obj, prop, getter, setter, dontDelete, dontEnum) {
|
||||||
if (test(elts.top))
|
Object.defineProperty(obj, prop, {
|
||||||
return elts.top;
|
get: getter,
|
||||||
}
|
set: setter,
|
||||||
return null;
|
configurable: !dontDelete,
|
||||||
},
|
enumerable: !dontEnum
|
||||||
has: function(x) {
|
});
|
||||||
return Boolean(this.find(function(elt) { return elt === x }));
|
}
|
||||||
},
|
|
||||||
forEach: function(f) {
|
function defineMemoGetter(obj, prop, fn, dontDelete, dontEnum) {
|
||||||
for (var elts = this.elts; elts; elts = elts.rest) {
|
Object.defineProperty(obj, prop, {
|
||||||
f(elts.top);
|
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) {
|
||||||
|
Object.defineProperty(obj, prop,
|
||||||
|
{ value: val, writable: !readOnly, configurable: !dontDelete,
|
||||||
|
enumerable: !dontEnum });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns true if fn is a native function. (Note: SpiderMonkey specific.)
|
||||||
|
function isNativeCode(fn) {
|
||||||
|
// Relies on the toString method to identify native code.
|
||||||
|
return ((typeof fn) === "function") && fn.toString().match(/\[native code\]/);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPropertyDescriptor(obj, name) {
|
||||||
|
while (obj) {
|
||||||
|
if (({}).hasOwnProperty.call(obj, name))
|
||||||
|
return Object.getOwnPropertyDescriptor(obj, name);
|
||||||
|
obj = Object.getPrototypeOf(obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
exports.tokens = tokens;
|
function getPropertyNames(obj) {
|
||||||
exports.opTypeNames = opTypeNames;
|
var table = Object.create(null, {});
|
||||||
exports.keywords = keywords;
|
while (obj) {
|
||||||
exports.isStatementStartCode = isStatementStartCode;
|
var names = Object.getOwnPropertyNames(obj);
|
||||||
exports.tokenIds = tokenIds;
|
for (var i = 0, n = names.length; i < n; i++)
|
||||||
exports.consts = consts;
|
table[names[i]] = true;
|
||||||
exports.assignOps = assignOps;
|
obj = Object.getPrototypeOf(obj);
|
||||||
exports.defineGetter = defineGetter;
|
}
|
||||||
exports.defineProperty = defineProperty;
|
return Object.keys(table);
|
||||||
exports.isNativeCode = isNativeCode;
|
}
|
||||||
exports.makePassthruHandler = makePassthruHandler;
|
|
||||||
exports.noPropFound = noPropFound;
|
|
||||||
exports.StringMap = StringMap;
|
|
||||||
exports.Stack = Stack;
|
|
||||||
|
|
||||||
|
function getOwnProperties(obj) {
|
||||||
|
var map = {};
|
||||||
|
for (var name in Object.getOwnPropertyNames(obj))
|
||||||
|
map[name] = Object.getOwnPropertyDescriptor(obj, name);
|
||||||
|
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) {
|
||||||
|
// Handler copied from
|
||||||
|
// http://wiki.ecmascript.org/doku.php?id=harmony:proxies&s=proxy%20object#examplea_no-op_forwarding_proxy
|
||||||
|
return {
|
||||||
|
getOwnPropertyDescriptor: function(name) {
|
||||||
|
var desc = Object.getOwnPropertyDescriptor(obj, name);
|
||||||
|
|
||||||
|
// a trapping proxy's properties must always be configurable
|
||||||
|
desc.configurable = true;
|
||||||
|
return desc;
|
||||||
|
},
|
||||||
|
getPropertyDescriptor: function(name) {
|
||||||
|
var desc = getPropertyDescriptor(obj, name);
|
||||||
|
|
||||||
|
// a trapping proxy's properties must always be configurable
|
||||||
|
desc.configurable = true;
|
||||||
|
return desc;
|
||||||
|
},
|
||||||
|
getOwnPropertyNames: function() {
|
||||||
|
return Object.getOwnPropertyNames(obj);
|
||||||
|
},
|
||||||
|
defineProperty: function(name, desc) {
|
||||||
|
Object.defineProperty(obj, name, desc);
|
||||||
|
},
|
||||||
|
"delete": function(name) { return delete obj[name]; },
|
||||||
|
fix: function() {
|
||||||
|
if (Object.isFrozen(obj)) {
|
||||||
|
return getOwnProperties(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
// As long as obj is not frozen, the proxy won't allow itself to be fixed.
|
||||||
|
return undefined; // will cause a TypeError to be thrown
|
||||||
|
},
|
||||||
|
|
||||||
|
has: function(name) { return name in obj; },
|
||||||
|
hasOwn: function(name) { return ({}).hasOwnProperty.call(obj, name); },
|
||||||
|
get: function(receiver, name) { return obj[name]; },
|
||||||
|
|
||||||
|
// bad behavior when set fails in non-strict mode
|
||||||
|
set: function(receiver, name, val) { obj[name] = val; return true; },
|
||||||
|
enumerate: function() {
|
||||||
|
var result = [];
|
||||||
|
for (name in obj) { result.push(name); };
|
||||||
|
return result;
|
||||||
|
},
|
||||||
|
keys: function() { return Object.keys(obj); }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
var hasOwnProperty = ({}).hasOwnProperty;
|
||||||
|
|
||||||
|
function hasOwn(obj, name) {
|
||||||
|
return hasOwnProperty.call(obj, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 = {
|
||||||
|
has: function(x) { return hasOwnProperty.call(this.table, x); },
|
||||||
|
set: function(x, v) {
|
||||||
|
if (!hasOwnProperty.call(this.table, x))
|
||||||
|
this.size++;
|
||||||
|
this.table[x] = v;
|
||||||
|
},
|
||||||
|
get: function(x) { return this.table[x]; },
|
||||||
|
getDef: function(x, thunk) {
|
||||||
|
if (!hasOwnProperty.call(this.table, x)) {
|
||||||
|
this.size++;
|
||||||
|
this.table[x] = thunk();
|
||||||
|
}
|
||||||
|
return this.table[x];
|
||||||
|
},
|
||||||
|
forEach: function(f) {
|
||||||
|
var table = this.table;
|
||||||
|
for (var key in table)
|
||||||
|
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]" }
|
||||||
|
};
|
||||||
|
|
||||||
|
// 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
|
||||||
|
function Stack(elts) {
|
||||||
|
this.elts = elts || null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Stack.prototype = {
|
||||||
|
push: function(x) {
|
||||||
|
return new Stack({ top: x, rest: this.elts });
|
||||||
|
},
|
||||||
|
top: function() {
|
||||||
|
if (!this.elts)
|
||||||
|
throw new Error("empty stack");
|
||||||
|
return this.elts.top;
|
||||||
|
},
|
||||||
|
isEmpty: function() {
|
||||||
|
return this.top === null;
|
||||||
|
},
|
||||||
|
find: function(test) {
|
||||||
|
for (var elts = this.elts; elts; elts = elts.rest) {
|
||||||
|
if (test(elts.top))
|
||||||
|
return elts.top;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
has: function(x) {
|
||||||
|
return Boolean(this.find(function(elt) { return elt === x }));
|
||||||
|
},
|
||||||
|
forEach: function(f) {
|
||||||
|
for (var elts = this.elts; elts; elts = elts.rest) {
|
||||||
|
f(elts.top);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
tokens: tokens,
|
||||||
|
whitespace: whitespace,
|
||||||
|
opTypeNames: opTypeNames,
|
||||||
|
keywords: keywords,
|
||||||
|
isStatementStartCode: isStatementStartCode,
|
||||||
|
tokenIds: tokenIds,
|
||||||
|
consts: consts,
|
||||||
|
assignOps: assignOps,
|
||||||
|
defineGetter: defineGetter,
|
||||||
|
defineGetterSetter: defineGetterSetter,
|
||||||
|
defineMemoGetter: defineMemoGetter,
|
||||||
|
defineProperty: defineProperty,
|
||||||
|
isNativeCode: isNativeCode,
|
||||||
|
mirrorHandler: mirrorHandler,
|
||||||
|
mixinHandler: mixinHandler,
|
||||||
|
whitelistHandler: whitelistHandler,
|
||||||
|
blacklistHandler: blacklistHandler,
|
||||||
|
makePassthruHandler: makePassthruHandler,
|
||||||
|
StringMap: StringMap,
|
||||||
|
ObjectMap: ObjectMap,
|
||||||
|
Stack: Stack
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
@ -47,416 +47,503 @@
|
||||||
* Lexical scanner.
|
* Lexical scanner.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
define(function(require, exports, module) {
|
define(function(require, exports, module) {
|
||||||
|
|
||||||
var definitions = require("ace/narcissus/jsdefs");
|
var definitions = require("ace/narcissus/jsdefs");
|
||||||
|
|
||||||
// Set constants in the local scope.
|
// Set constants in the local scope.
|
||||||
eval(definitions.consts);
|
eval(definitions.consts);
|
||||||
|
|
||||||
// Build up a trie of operator tokens.
|
// Banned keywords by language version
|
||||||
var opTokens = {};
|
const blackLists = { 160: {}, 185: {}, harmony: {} };
|
||||||
for (var op in definitions.opTypeNames) {
|
blackLists[160][LET] = true;
|
||||||
if (op === '\n' || op === '.')
|
blackLists[160][MODULE] = true;
|
||||||
continue;
|
blackLists[160][YIELD] = true;
|
||||||
|
blackLists[185][MODULE] = true;
|
||||||
|
|
||||||
var node = opTokens;
|
// Build up a trie of operator tokens.
|
||||||
for (var i = 0; i < op.length; i++) {
|
var opTokens = {};
|
||||||
var ch = op[i];
|
for (var op in definitions.opTypeNames) {
|
||||||
if (!(ch in node))
|
if (op === '\n' || op === '.')
|
||||||
node[ch] = {};
|
continue;
|
||||||
node = node[ch];
|
|
||||||
node.op = op;
|
var node = opTokens;
|
||||||
|
for (var i = 0; i < op.length; i++) {
|
||||||
|
var ch = op[i];
|
||||||
|
if (!(ch in node))
|
||||||
|
node[ch] = {};
|
||||||
|
node = node[ch];
|
||||||
|
node.op = op;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Tokenizer :: (source, filename, line number) -> Tokenizer
|
* Tokenizer :: (source, filename, line number) -> Tokenizer
|
||||||
*/
|
*/
|
||||||
function Tokenizer(s, f, l) {
|
function Tokenizer(s, f, l) {
|
||||||
this.cursor = 0;
|
this.cursor = 0;
|
||||||
this.source = String(s);
|
this.source = String(s);
|
||||||
this.tokens = [];
|
this.tokens = [];
|
||||||
this.tokenIndex = 0;
|
this.tokenIndex = 0;
|
||||||
this.lookahead = 0;
|
this.lookahead = 0;
|
||||||
this.scanNewlines = false;
|
|
||||||
this.unexpectedEOF = false;
|
|
||||||
this.filename = f || "";
|
|
||||||
this.lineno = l || 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
Tokenizer.prototype = {
|
|
||||||
get done() {
|
|
||||||
// We need to set scanOperand to true here because the first thing
|
|
||||||
// might be a regexp.
|
|
||||||
return this.peek(true) === END;
|
|
||||||
},
|
|
||||||
|
|
||||||
get token() {
|
|
||||||
return this.tokens[this.tokenIndex];
|
|
||||||
},
|
|
||||||
|
|
||||||
match: function (tt, scanOperand) {
|
|
||||||
return this.get(scanOperand) === tt || this.unget();
|
|
||||||
},
|
|
||||||
|
|
||||||
mustMatch: function (tt) {
|
|
||||||
if (!this.match(tt)) {
|
|
||||||
throw this.newSyntaxError("Missing " +
|
|
||||||
definitions.tokens[tt].toLowerCase());
|
|
||||||
}
|
|
||||||
return this.token;
|
|
||||||
},
|
|
||||||
|
|
||||||
peek: function (scanOperand) {
|
|
||||||
var tt, next;
|
|
||||||
if (this.lookahead) {
|
|
||||||
next = this.tokens[(this.tokenIndex + this.lookahead) & 3];
|
|
||||||
tt = (this.scanNewlines && next.lineno !== this.lineno)
|
|
||||||
? NEWLINE
|
|
||||||
: next.type;
|
|
||||||
} else {
|
|
||||||
tt = this.get(scanOperand);
|
|
||||||
this.unget();
|
|
||||||
}
|
|
||||||
return tt;
|
|
||||||
},
|
|
||||||
|
|
||||||
peekOnSameLine: function (scanOperand) {
|
|
||||||
this.scanNewlines = true;
|
|
||||||
var tt = this.peek(scanOperand);
|
|
||||||
this.scanNewlines = false;
|
this.scanNewlines = false;
|
||||||
return tt;
|
this.unexpectedEOF = false;
|
||||||
},
|
this.filename = f || "";
|
||||||
|
this.lineno = l || 1;
|
||||||
|
this.blackList = blackLists[Narcissus.options.version];
|
||||||
|
this.blockComments = null;
|
||||||
|
}
|
||||||
|
|
||||||
// Eat comments and whitespace.
|
Tokenizer.prototype = {
|
||||||
skip: function () {
|
get done() {
|
||||||
var input = this.source;
|
// We need to set scanOperand to true here because the first thing
|
||||||
for (;;) {
|
// might be a regexp.
|
||||||
var ch = input[this.cursor++];
|
return this.peek(true) === END;
|
||||||
var next = input[this.cursor];
|
},
|
||||||
if (ch === '\n' && !this.scanNewlines) {
|
|
||||||
this.lineno++;
|
|
||||||
} else if (ch === '/' && next === '*') {
|
|
||||||
this.cursor++;
|
|
||||||
for (;;) {
|
|
||||||
ch = input[this.cursor++];
|
|
||||||
if (ch === undefined)
|
|
||||||
throw this.newSyntaxError("Unterminated comment");
|
|
||||||
|
|
||||||
if (ch === '*') {
|
get token() {
|
||||||
|
return this.tokens[this.tokenIndex];
|
||||||
|
},
|
||||||
|
|
||||||
|
match: function (tt, scanOperand) {
|
||||||
|
return this.get(scanOperand) === tt || this.unget();
|
||||||
|
},
|
||||||
|
|
||||||
|
mustMatch: function (tt) {
|
||||||
|
if (!this.match(tt)) {
|
||||||
|
throw this.newSyntaxError("Missing " +
|
||||||
|
definitions.tokens[tt].toLowerCase());
|
||||||
|
}
|
||||||
|
return this.token;
|
||||||
|
},
|
||||||
|
|
||||||
|
peek: function (scanOperand) {
|
||||||
|
var tt, next;
|
||||||
|
if (this.lookahead) {
|
||||||
|
next = this.tokens[(this.tokenIndex + this.lookahead) & 3];
|
||||||
|
tt = (this.scanNewlines && next.lineno !== this.lineno)
|
||||||
|
? NEWLINE
|
||||||
|
: next.type;
|
||||||
|
} else {
|
||||||
|
tt = this.get(scanOperand);
|
||||||
|
this.unget();
|
||||||
|
}
|
||||||
|
return tt;
|
||||||
|
},
|
||||||
|
|
||||||
|
peekOnSameLine: function (scanOperand) {
|
||||||
|
this.scanNewlines = true;
|
||||||
|
var tt = this.peek(scanOperand);
|
||||||
|
this.scanNewlines = false;
|
||||||
|
return tt;
|
||||||
|
},
|
||||||
|
|
||||||
|
lastBlockComment: function() {
|
||||||
|
var length = this.blockComments.length;
|
||||||
|
return length ? this.blockComments[length - 1] : null;
|
||||||
|
},
|
||||||
|
|
||||||
|
// Eat comments and whitespace.
|
||||||
|
skip: function () {
|
||||||
|
var input = this.source;
|
||||||
|
this.blockComments = [];
|
||||||
|
for (;;) {
|
||||||
|
var ch = 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) {
|
||||||
|
this.lineno++;
|
||||||
|
} else if (ch === '/' && next === '*') {
|
||||||
|
var commentStart = ++this.cursor;
|
||||||
|
for (;;) {
|
||||||
|
ch = input[this.cursor++];
|
||||||
|
if (ch === undefined)
|
||||||
|
throw this.newSyntaxError("Unterminated comment");
|
||||||
|
|
||||||
|
if (ch === '*') {
|
||||||
|
next = input[this.cursor];
|
||||||
|
if (next === '/') {
|
||||||
|
var commentEnd = this.cursor - 1;
|
||||||
|
this.cursor++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else if (ch === '\n') {
|
||||||
|
this.lineno++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.blockComments.push(input.substring(commentStart, commentEnd));
|
||||||
|
} else if (ch === '/' && next === '/') {
|
||||||
|
this.cursor++;
|
||||||
|
for (;;) {
|
||||||
|
ch = input[this.cursor++];
|
||||||
next = input[this.cursor];
|
next = input[this.cursor];
|
||||||
if (next === '/') {
|
if (ch === undefined)
|
||||||
this.cursor++;
|
return;
|
||||||
|
|
||||||
|
if (ch === '\r') {
|
||||||
|
// check for \r\n
|
||||||
|
if (next !== '\n') ch = '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ch === '\n') {
|
||||||
|
if (this.scanNewlines) {
|
||||||
|
this.cursor--;
|
||||||
|
} else {
|
||||||
|
this.lineno++;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else if (ch === '\n') {
|
|
||||||
this.lineno++;
|
|
||||||
}
|
}
|
||||||
|
} else if (!(ch in definitions.whitespace)) {
|
||||||
|
this.cursor--;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
} else if (ch === '/' && next === '/') {
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
// Lex the exponential part of a number, if present. Return true iff an
|
||||||
|
// exponential part was found.
|
||||||
|
lexExponent: function() {
|
||||||
|
var input = this.source;
|
||||||
|
var next = input[this.cursor];
|
||||||
|
if (next === 'e' || next === 'E') {
|
||||||
this.cursor++;
|
this.cursor++;
|
||||||
for (;;) {
|
ch = input[this.cursor++];
|
||||||
|
if (ch === '+' || ch === '-')
|
||||||
ch = input[this.cursor++];
|
ch = input[this.cursor++];
|
||||||
if (ch === undefined)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (ch === '\n') {
|
if (ch < '0' || ch > '9')
|
||||||
this.lineno++;
|
throw this.newSyntaxError("Missing exponent");
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (ch !== ' ' && ch !== '\t') {
|
|
||||||
this.cursor--;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
// Lex the exponential part of a number, if present. Return true iff an
|
|
||||||
// exponential part was found.
|
|
||||||
lexExponent: function() {
|
|
||||||
var input = this.source;
|
|
||||||
var next = input[this.cursor];
|
|
||||||
if (next === 'e' || next === 'E') {
|
|
||||||
this.cursor++;
|
|
||||||
ch = input[this.cursor++];
|
|
||||||
if (ch === '+' || ch === '-')
|
|
||||||
ch = input[this.cursor++];
|
|
||||||
|
|
||||||
if (ch < '0' || ch > '9')
|
|
||||||
throw this.newSyntaxError("Missing exponent");
|
|
||||||
|
|
||||||
do {
|
|
||||||
ch = input[this.cursor++];
|
|
||||||
} while (ch >= '0' && ch <= '9');
|
|
||||||
this.cursor--;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
},
|
|
||||||
|
|
||||||
lexZeroNumber: function (ch) {
|
|
||||||
var token = this.token, input = this.source;
|
|
||||||
token.type = NUMBER;
|
|
||||||
|
|
||||||
ch = input[this.cursor++];
|
|
||||||
if (ch === '.') {
|
|
||||||
do {
|
|
||||||
ch = input[this.cursor++];
|
|
||||||
} while (ch >= '0' && ch <= '9');
|
|
||||||
this.cursor--;
|
|
||||||
|
|
||||||
this.lexExponent();
|
|
||||||
token.value = parseFloat(token.start, this.cursor);
|
|
||||||
} else if (ch === 'x' || ch === 'X') {
|
|
||||||
do {
|
|
||||||
ch = input[this.cursor++];
|
|
||||||
} while ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') ||
|
|
||||||
(ch >= 'A' && ch <= 'F'));
|
|
||||||
this.cursor--;
|
|
||||||
|
|
||||||
token.value = parseInt(input.substring(token.start, this.cursor));
|
|
||||||
} else if (ch >= '0' && ch <= '7') {
|
|
||||||
do {
|
|
||||||
ch = input[this.cursor++];
|
|
||||||
} while (ch >= '0' && ch <= '7');
|
|
||||||
this.cursor--;
|
|
||||||
|
|
||||||
token.value = parseInt(input.substring(token.start, this.cursor));
|
|
||||||
} else {
|
|
||||||
this.cursor--;
|
|
||||||
this.lexExponent(); // 0E1, &c.
|
|
||||||
token.value = 0;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
lexNumber: function (ch) {
|
|
||||||
var token = this.token, input = this.source;
|
|
||||||
token.type = NUMBER;
|
|
||||||
|
|
||||||
var floating = false;
|
|
||||||
do {
|
|
||||||
ch = input[this.cursor++];
|
|
||||||
if (ch === '.' && !floating) {
|
|
||||||
floating = true;
|
|
||||||
ch = input[this.cursor++];
|
|
||||||
}
|
|
||||||
} while (ch >= '0' && ch <= '9');
|
|
||||||
|
|
||||||
this.cursor--;
|
|
||||||
|
|
||||||
var exponent = this.lexExponent();
|
|
||||||
floating = floating || exponent;
|
|
||||||
|
|
||||||
var str = input.substring(token.start, this.cursor);
|
|
||||||
token.value = floating ? parseFloat(str) : parseInt(str);
|
|
||||||
},
|
|
||||||
|
|
||||||
lexDot: function (ch) {
|
|
||||||
var token = this.token, input = this.source;
|
|
||||||
var next = input[this.cursor];
|
|
||||||
if (next >= '0' && next <= '9') {
|
|
||||||
do {
|
|
||||||
ch = input[this.cursor++];
|
|
||||||
} while (ch >= '0' && ch <= '9');
|
|
||||||
this.cursor--;
|
|
||||||
|
|
||||||
this.lexExponent();
|
|
||||||
|
|
||||||
token.type = NUMBER;
|
|
||||||
token.value = parseFloat(token.start, this.cursor);
|
|
||||||
} else {
|
|
||||||
token.type = DOT;
|
|
||||||
token.assignOp = null;
|
|
||||||
token.value = '.';
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
lexString: function (ch) {
|
|
||||||
var token = this.token, input = this.source;
|
|
||||||
token.type = STRING;
|
|
||||||
|
|
||||||
var hasEscapes = false;
|
|
||||||
var delim = ch;
|
|
||||||
if (input.length <= this.cursor)
|
|
||||||
throw this.newSyntaxError("Unterminated string literal");
|
|
||||||
while ((ch = input[this.cursor++]) !== delim) {
|
|
||||||
if (this.cursor == input.length)
|
|
||||||
throw this.newSyntaxError("Unterminated string literal");
|
|
||||||
if (ch === '\\') {
|
|
||||||
hasEscapes = true;
|
|
||||||
if (++this.cursor == input.length)
|
|
||||||
throw this.newSyntaxError("Unterminated string literal");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
token.value = hasEscapes
|
|
||||||
? eval(input.substring(token.start, this.cursor))
|
|
||||||
: input.substring(token.start + 1, this.cursor - 1);
|
|
||||||
},
|
|
||||||
|
|
||||||
lexRegExp: function (ch) {
|
|
||||||
var token = this.token, input = this.source;
|
|
||||||
token.type = REGEXP;
|
|
||||||
|
|
||||||
do {
|
|
||||||
ch = input[this.cursor++];
|
|
||||||
if (ch === '\\') {
|
|
||||||
this.cursor++;
|
|
||||||
} else if (ch === '[') {
|
|
||||||
do {
|
do {
|
||||||
if (ch === undefined)
|
|
||||||
throw this.newSyntaxError("Unterminated character class");
|
|
||||||
|
|
||||||
if (ch === '\\')
|
|
||||||
this.cursor++;
|
|
||||||
|
|
||||||
ch = input[this.cursor++];
|
ch = input[this.cursor++];
|
||||||
} while (ch !== ']');
|
} while (ch >= '0' && ch <= '9');
|
||||||
} else if (ch === undefined) {
|
this.cursor--;
|
||||||
throw this.newSyntaxError("Unterminated regex");
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
} while (ch !== '/');
|
|
||||||
|
|
||||||
do {
|
return false;
|
||||||
|
},
|
||||||
|
|
||||||
|
lexZeroNumber: function (ch) {
|
||||||
|
var token = this.token, input = this.source;
|
||||||
|
token.type = NUMBER;
|
||||||
|
|
||||||
ch = input[this.cursor++];
|
ch = input[this.cursor++];
|
||||||
} while (ch >= 'a' && ch <= 'z');
|
if (ch === '.') {
|
||||||
|
do {
|
||||||
|
ch = input[this.cursor++];
|
||||||
|
} while (ch >= '0' && ch <= '9');
|
||||||
|
this.cursor--;
|
||||||
|
|
||||||
this.cursor--;
|
this.lexExponent();
|
||||||
|
token.value = parseFloat(token.start, this.cursor);
|
||||||
|
} else if (ch === 'x' || ch === 'X') {
|
||||||
|
do {
|
||||||
|
ch = input[this.cursor++];
|
||||||
|
} while ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') ||
|
||||||
|
(ch >= 'A' && ch <= 'F'));
|
||||||
|
this.cursor--;
|
||||||
|
|
||||||
token.value = eval(input.substring(token.start, this.cursor));
|
token.value = parseInt(input.substring(token.start, this.cursor));
|
||||||
},
|
} else if (ch >= '0' && ch <= '7') {
|
||||||
|
do {
|
||||||
|
ch = input[this.cursor++];
|
||||||
|
} while (ch >= '0' && ch <= '7');
|
||||||
|
this.cursor--;
|
||||||
|
|
||||||
lexOp: function (ch) {
|
token.value = parseInt(input.substring(token.start, this.cursor));
|
||||||
var token = this.token, input = this.source;
|
} else {
|
||||||
|
this.cursor--;
|
||||||
|
this.lexExponent(); // 0E1, &c.
|
||||||
|
token.value = 0;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
// A bit ugly, but it seems wasteful to write a trie lookup routine
|
lexNumber: function (ch) {
|
||||||
// for only 3 characters...
|
var token = this.token, input = this.source;
|
||||||
var node = opTokens[ch];
|
token.type = NUMBER;
|
||||||
var next = input[this.cursor];
|
|
||||||
if (next in node) {
|
var floating = false;
|
||||||
node = node[next];
|
do {
|
||||||
this.cursor++;
|
ch = input[this.cursor++];
|
||||||
next = input[this.cursor];
|
if (ch === '.' && !floating) {
|
||||||
|
floating = true;
|
||||||
|
ch = input[this.cursor++];
|
||||||
|
}
|
||||||
|
} while (ch >= '0' && ch <= '9');
|
||||||
|
|
||||||
|
this.cursor--;
|
||||||
|
|
||||||
|
var exponent = this.lexExponent();
|
||||||
|
floating = floating || exponent;
|
||||||
|
|
||||||
|
var str = input.substring(token.start, this.cursor);
|
||||||
|
token.value = floating ? parseFloat(str) : parseInt(str);
|
||||||
|
},
|
||||||
|
|
||||||
|
lexDot: function (ch) {
|
||||||
|
var token = this.token, input = this.source;
|
||||||
|
var next = input[this.cursor];
|
||||||
|
if (next >= '0' && next <= '9') {
|
||||||
|
do {
|
||||||
|
ch = input[this.cursor++];
|
||||||
|
} while (ch >= '0' && ch <= '9');
|
||||||
|
this.cursor--;
|
||||||
|
|
||||||
|
this.lexExponent();
|
||||||
|
|
||||||
|
token.type = NUMBER;
|
||||||
|
token.value = parseFloat(token.start, this.cursor);
|
||||||
|
} else {
|
||||||
|
token.type = DOT;
|
||||||
|
token.assignOp = null;
|
||||||
|
token.value = '.';
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
lexString: function (ch) {
|
||||||
|
var token = this.token, input = this.source;
|
||||||
|
token.type = STRING;
|
||||||
|
|
||||||
|
var hasEscapes = false;
|
||||||
|
var delim = ch;
|
||||||
|
if (input.length <= this.cursor)
|
||||||
|
throw this.newSyntaxError("Unterminated string literal");
|
||||||
|
while ((ch = input[this.cursor++]) !== delim) {
|
||||||
|
if (this.cursor == input.length)
|
||||||
|
throw this.newSyntaxError("Unterminated string literal");
|
||||||
|
if (ch === '\\') {
|
||||||
|
hasEscapes = true;
|
||||||
|
if (++this.cursor == input.length)
|
||||||
|
throw this.newSyntaxError("Unterminated string literal");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
token.value = hasEscapes
|
||||||
|
? eval(input.substring(token.start, this.cursor))
|
||||||
|
: input.substring(token.start + 1, this.cursor - 1);
|
||||||
|
},
|
||||||
|
|
||||||
|
lexRegExp: function (ch) {
|
||||||
|
var token = this.token, input = this.source;
|
||||||
|
token.type = REGEXP;
|
||||||
|
|
||||||
|
do {
|
||||||
|
ch = input[this.cursor++];
|
||||||
|
if (ch === '\\') {
|
||||||
|
this.cursor++;
|
||||||
|
} else if (ch === '[') {
|
||||||
|
do {
|
||||||
|
if (ch === undefined)
|
||||||
|
throw this.newSyntaxError("Unterminated character class");
|
||||||
|
|
||||||
|
if (ch === '\\')
|
||||||
|
this.cursor++;
|
||||||
|
|
||||||
|
ch = input[this.cursor++];
|
||||||
|
} while (ch !== ']');
|
||||||
|
} else if (ch === undefined) {
|
||||||
|
throw this.newSyntaxError("Unterminated regex");
|
||||||
|
}
|
||||||
|
} while (ch !== '/');
|
||||||
|
|
||||||
|
do {
|
||||||
|
ch = input[this.cursor++];
|
||||||
|
} while (ch >= 'a' && ch <= 'z');
|
||||||
|
|
||||||
|
this.cursor--;
|
||||||
|
|
||||||
|
token.value = eval(input.substring(token.start, this.cursor));
|
||||||
|
},
|
||||||
|
|
||||||
|
lexOp: function (ch) {
|
||||||
|
var token = this.token, input = this.source;
|
||||||
|
|
||||||
|
// A bit ugly, but it seems wasteful to write a trie lookup routine
|
||||||
|
// for only 3 characters...
|
||||||
|
var node = opTokens[ch];
|
||||||
|
var next = input[this.cursor];
|
||||||
if (next in node) {
|
if (next in node) {
|
||||||
node = node[next];
|
node = node[next];
|
||||||
this.cursor++;
|
this.cursor++;
|
||||||
next = input[this.cursor];
|
next = input[this.cursor];
|
||||||
|
if (next in node) {
|
||||||
|
node = node[next];
|
||||||
|
this.cursor++;
|
||||||
|
next = input[this.cursor];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
var op = node.op;
|
var op = node.op;
|
||||||
if (definitions.assignOps[op] && input[this.cursor] === '=') {
|
if (definitions.assignOps[op] && input[this.cursor] === '=') {
|
||||||
this.cursor++;
|
this.cursor++;
|
||||||
token.type = ASSIGN;
|
token.type = ASSIGN;
|
||||||
token.assignOp = definitions.tokenIds[definitions.opTypeNames[op]];
|
token.assignOp = definitions.tokenIds[definitions.opTypeNames[op]];
|
||||||
op += '=';
|
op += '=';
|
||||||
} else {
|
} else {
|
||||||
token.type = definitions.tokenIds[definitions.opTypeNames[op]];
|
token.type = definitions.tokenIds[definitions.opTypeNames[op]];
|
||||||
token.assignOp = null;
|
token.assignOp = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
token.value = op;
|
token.value = op;
|
||||||
},
|
},
|
||||||
|
|
||||||
// FIXME: Unicode escape sequences
|
// FIXME: Unicode escape sequences
|
||||||
// FIXME: Unicode identifiers
|
lexIdent: function (ch) {
|
||||||
lexIdent: function (ch) {
|
var token = this.token;
|
||||||
var token = this.token, input = this.source;
|
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.
|
token.type = definitions.keywords[id] || IDENTIFIER;
|
||||||
|
if (token.type in this.blackList) {
|
||||||
|
// banned keyword, this is an identifier
|
||||||
|
token.type = IDENTIFIER;
|
||||||
|
}
|
||||||
|
token.value = id;
|
||||||
|
},
|
||||||
|
|
||||||
var id = input.substring(token.start, this.cursor);
|
/*
|
||||||
token.type = definitions.keywords[id] || IDENTIFIER;
|
* Tokenizer.get :: void -> token type
|
||||||
token.value = id;
|
*
|
||||||
},
|
* Consume input *only* if there is no lookahead.
|
||||||
|
* Dispatch to the appropriate lexing function depending on the input.
|
||||||
|
*/
|
||||||
|
get: function (scanOperand) {
|
||||||
|
var token;
|
||||||
|
while (this.lookahead) {
|
||||||
|
--this.lookahead;
|
||||||
|
this.tokenIndex = (this.tokenIndex + 1) & 3;
|
||||||
|
token = this.tokens[this.tokenIndex];
|
||||||
|
if (token.type !== NEWLINE || this.scanNewlines)
|
||||||
|
return token.type;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.skip();
|
||||||
|
|
||||||
/*
|
|
||||||
* Tokenizer.get :: void -> token type
|
|
||||||
*
|
|
||||||
* Consume input *only* if there is no lookahead.
|
|
||||||
* Dispatch to the appropriate lexing function depending on the input.
|
|
||||||
*/
|
|
||||||
get: function (scanOperand) {
|
|
||||||
var token;
|
|
||||||
while (this.lookahead) {
|
|
||||||
--this.lookahead;
|
|
||||||
this.tokenIndex = (this.tokenIndex + 1) & 3;
|
this.tokenIndex = (this.tokenIndex + 1) & 3;
|
||||||
token = this.tokens[this.tokenIndex];
|
token = this.tokens[this.tokenIndex];
|
||||||
if (token.type !== NEWLINE || this.scanNewlines)
|
if (!token)
|
||||||
return token.type;
|
this.tokens[this.tokenIndex] = token = {};
|
||||||
}
|
|
||||||
|
|
||||||
this.skip();
|
var input = this.source;
|
||||||
|
if (this.cursor >= input.length)
|
||||||
|
return token.type = END;
|
||||||
|
|
||||||
this.tokenIndex = (this.tokenIndex + 1) & 3;
|
token.start = this.cursor;
|
||||||
token = this.tokens[this.tokenIndex];
|
token.lineno = this.lineno;
|
||||||
if (!token)
|
|
||||||
this.tokens[this.tokenIndex] = token = {};
|
|
||||||
|
|
||||||
var input = this.source;
|
var ich = this.getValidIdentifierChar(true);
|
||||||
if (this.cursor === input.length)
|
var ch = (ich === null) ? input[this.cursor++] : null;
|
||||||
return token.type = END;
|
if (ich !== null) {
|
||||||
|
this.lexIdent(ich);
|
||||||
|
} else if (scanOperand && ch === '/') {
|
||||||
|
this.lexRegExp(ch);
|
||||||
|
} else if (ch in opTokens) {
|
||||||
|
this.lexOp(ch);
|
||||||
|
} else if (ch === '.') {
|
||||||
|
this.lexDot(ch);
|
||||||
|
} else if (ch >= '1' && ch <= '9') {
|
||||||
|
this.lexNumber(ch);
|
||||||
|
} else if (ch === '0') {
|
||||||
|
this.lexZeroNumber(ch);
|
||||||
|
} else if (ch === '"' || ch === "'") {
|
||||||
|
this.lexString(ch);
|
||||||
|
} 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.value = '\n';
|
||||||
|
this.lineno++;
|
||||||
|
} else {
|
||||||
|
throw this.newSyntaxError("Illegal token");
|
||||||
|
}
|
||||||
|
|
||||||
token.start = this.cursor;
|
token.end = this.cursor;
|
||||||
token.lineno = this.lineno;
|
return token.type;
|
||||||
|
},
|
||||||
|
|
||||||
var ch = input[this.cursor++];
|
/*
|
||||||
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch === '$' || ch === '_') {
|
* Tokenizer.unget :: void -> undefined
|
||||||
this.lexIdent(ch);
|
*
|
||||||
} else if (scanOperand && ch === '/') {
|
* Match depends on unget returning undefined.
|
||||||
this.lexRegExp(ch);
|
*/
|
||||||
} else if (ch in opTokens) {
|
unget: function () {
|
||||||
this.lexOp(ch);
|
if (++this.lookahead === 4) throw "PANIC: too much lookahead!";
|
||||||
} else if (ch === '.') {
|
this.tokenIndex = (this.tokenIndex - 1) & 3;
|
||||||
this.lexDot(ch);
|
},
|
||||||
} else if (ch >= '1' && ch <= '9') {
|
|
||||||
this.lexNumber(ch);
|
|
||||||
} else if (ch === '0') {
|
|
||||||
this.lexZeroNumber(ch);
|
|
||||||
} else if (ch === '"' || ch === "'") {
|
|
||||||
this.lexString(ch);
|
|
||||||
} else if (this.scanNewlines && ch === '\n') {
|
|
||||||
token.type = NEWLINE;
|
|
||||||
token.value = '\n';
|
|
||||||
this.lineno++;
|
|
||||||
} else {
|
|
||||||
throw this.newSyntaxError("Illegal token");
|
|
||||||
}
|
|
||||||
|
|
||||||
token.end = this.cursor;
|
newSyntaxError: function (m) {
|
||||||
return token.type;
|
m = (this.filename ? this.filename + ":" : "") + this.lineno + ": " + m;
|
||||||
},
|
var e = new SyntaxError(m, this.filename, this.lineno);
|
||||||
|
e.source = this.source;
|
||||||
|
e.cursor = this.lookahead
|
||||||
|
? this.tokens[(this.tokenIndex + this.lookahead) & 3].start
|
||||||
|
: this.cursor;
|
||||||
|
return e;
|
||||||
|
},
|
||||||
|
|
||||||
/*
|
/* Gets a single valid identifier char from the input stream, or null
|
||||||
* Tokenizer.unget :: void -> undefined
|
* if there is none.
|
||||||
*
|
* Since JavaScript provides no convenient way to determine if a
|
||||||
* Match depends on unget returning undefined.
|
* character is in a particular Unicode category, we use
|
||||||
*/
|
* metacircularity to accomplish this (oh yeaaaah!) */
|
||||||
unget: function () {
|
getValidIdentifierChar: function(first) {
|
||||||
if (++this.lookahead === 4) throw "PANIC: too much lookahead!";
|
var input = this.source;
|
||||||
this.tokenIndex = (this.tokenIndex - 1) & 3;
|
if (this.cursor >= input.length) return null;
|
||||||
},
|
var ch = input[this.cursor];
|
||||||
|
|
||||||
newSyntaxError: function (m) {
|
// first check for \u escapes
|
||||||
var e = new SyntaxError(m, this.filename, this.lineno);
|
if (ch === '\\' && input[this.cursor+1] === 'u') {
|
||||||
e.source = this.source;
|
// get the character value
|
||||||
e.lineno = this.lineno;
|
try {
|
||||||
e.cursor = this.lookahead
|
ch = String.fromCharCode(parseInt(
|
||||||
? this.tokens[(this.tokenIndex + this.lookahead) & 3].start
|
input.substring(this.cursor + 2, this.cursor + 6),
|
||||||
: this.cursor;
|
16));
|
||||||
return e;
|
} catch (ex) {
|
||||||
},
|
return null;
|
||||||
};
|
}
|
||||||
|
this.cursor += 5;
|
||||||
|
}
|
||||||
|
|
||||||
exports.Tokenizer = Tokenizer;
|
// 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);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
});
|
|
||||||
|
return { Tokenizer: Tokenizer };
|
||||||
|
|
||||||
|
});
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue