2010-08-06 10:40:31 -07:00
|
|
|
/* -*- Mode: JS; tab-width: 4; indent-tabs-mode: nil; -*-
|
|
|
|
* vim: set sw=4 ts=4 et tw=78:
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
*
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is the Narcissus JavaScript engine.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Brendan Eich <brendan@mozilla.org>.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2004
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Narcissus - JS implemented in JS.
|
|
|
|
*
|
|
|
|
* Parser.
|
|
|
|
*/
|
|
|
|
|
2010-08-09 12:37:38 -07:00
|
|
|
Narcissus.parser = (function() {
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-09 12:37:38 -07:00
|
|
|
var lexer = Narcissus.lexer;
|
|
|
|
var definitions = Narcissus.definitions;
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
// Set constants in the local scope.
|
2010-08-09 12:37:38 -07:00
|
|
|
eval(definitions.consts);
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
/*
|
|
|
|
* Function.prototype.bind is not yet implemented in many browsers.
|
|
|
|
* The following definition will be removed when it is.
|
|
|
|
*
|
|
|
|
* Similar to Prototype's implementation.
|
|
|
|
*/
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
function bindMethod(method, context) {
|
|
|
|
if (arguments.length < 3 && arguments[0] === undefined)
|
|
|
|
return method;
|
|
|
|
var slice = Array.prototype.slice;
|
|
|
|
var args = slice.call(arguments, 2);
|
|
|
|
// Optimization for when there's no currying.
|
|
|
|
if (args.length === 0) {
|
|
|
|
return function() {
|
|
|
|
return method.apply(context, arguments);
|
|
|
|
}
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
return function() {
|
|
|
|
var a = slice.call(args, 0);
|
|
|
|
for (var i = 0, j = arguments.length; i < j; i++) {
|
|
|
|
a.push(arguments[i]);
|
|
|
|
}
|
|
|
|
return method.apply(context, a);
|
|
|
|
}
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
function bindSubBuilders(builder, proto) {
|
|
|
|
for (var ns in proto) {
|
|
|
|
var unbound = proto[ns];
|
|
|
|
// We do not want to bind functions like setHoists.
|
|
|
|
if (typeof unbound !== "object")
|
|
|
|
continue;
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
/*
|
|
|
|
* We store the bound sub-builder as builder's own property
|
|
|
|
* so that we can have multiple builders at the same time.
|
|
|
|
*/
|
|
|
|
var bound = builder[ns] = {};
|
|
|
|
for (var m in unbound) {
|
|
|
|
bound[m] = bindMethod(unbound[m], builder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
/*
|
|
|
|
* The vanilla AST builder.
|
|
|
|
*/
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
function DefaultBuilder() {
|
|
|
|
bindSubBuilders(this, DefaultBuilder.prototype);
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
function mkBinopBuilder(type) {
|
|
|
|
return {
|
|
|
|
build: !type ? function(t) { return new Node(t); }
|
|
|
|
: function(t) { return new Node(t, type); },
|
|
|
|
addOperand: function(n, n2) { n.push(n2); },
|
|
|
|
finish: function(n) { }
|
|
|
|
};
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
DefaultBuilder.prototype = {
|
|
|
|
IF: {
|
|
|
|
build: function(t) {
|
|
|
|
return new Node(t, IF);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setCondition: function(n, e) {
|
|
|
|
n.condition = e;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setThenPart: function(n, s) {
|
|
|
|
n.thenPart = s;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setElsePart: function(n, s) {
|
|
|
|
n.elsePart = s;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
SWITCH: {
|
|
|
|
build: function(t) {
|
|
|
|
var n = new Node(t, SWITCH);
|
|
|
|
n.cases = [];
|
|
|
|
n.defaultIndex = -1;
|
|
|
|
return n;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setDiscriminant: function(n, e) {
|
|
|
|
n.discriminant = e;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setDefaultIndex: function(n, i) {
|
|
|
|
n.defaultIndex = i;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
addCase: function(n, n2) {
|
|
|
|
n.cases.push(n2);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
CASE: {
|
|
|
|
build: function(t) {
|
|
|
|
return new Node(t, CASE);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setLabel: function(n, e) {
|
|
|
|
n.caseLabel = e;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
initializeStatements: function(n, t) {
|
|
|
|
n.statements = new Node(t, BLOCK);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
addStatement: function(n, s) {
|
|
|
|
n.statements.push(s);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
DEFAULT: {
|
|
|
|
build: function(t, p) {
|
|
|
|
return new Node(t, DEFAULT);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
initializeStatements: function(n, t) {
|
|
|
|
n.statements = new Node(t, BLOCK);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
addStatement: function(n, s) {
|
|
|
|
n.statements.push(s);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
FOR: {
|
|
|
|
build: function(t) {
|
|
|
|
var n = new Node(t, FOR);
|
|
|
|
n.isLoop = true;
|
|
|
|
n.isEach = false;
|
|
|
|
return n;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
rebuildForEach: function(n) {
|
|
|
|
n.isEach = true;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
// NB. This function is called after rebuildForEach, if that's called
|
|
|
|
// at all.
|
|
|
|
rebuildForIn: function(n) {
|
|
|
|
n.type = FOR_IN;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setCondition: function(n, e) {
|
|
|
|
n.condition = e;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setSetup: function(n, e) {
|
|
|
|
n.setup = e || null;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setUpdate: function(n, e) {
|
|
|
|
n.update = e;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setObject: function(n, e) {
|
|
|
|
n.object = e;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setIterator: function(n, e, e2) {
|
|
|
|
n.iterator = e;
|
|
|
|
n.varDecl = e2;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setBody: function(n, s) {
|
|
|
|
n.body = s;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
WHILE: {
|
|
|
|
build: function(t) {
|
|
|
|
var n = new Node(t, WHILE);
|
|
|
|
n.isLoop = true;
|
|
|
|
return n;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setCondition: function(n, e) {
|
|
|
|
n.condition = e;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setBody: function(n, s) {
|
|
|
|
n.body = s;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
DO: {
|
|
|
|
build: function(t) {
|
|
|
|
var n = new Node(t, DO);
|
|
|
|
n.isLoop = true;
|
|
|
|
return n;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setCondition: function(n, e) {
|
|
|
|
n.condition = e;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setBody: function(n, s) {
|
|
|
|
n.body = s;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
BREAK: {
|
|
|
|
build: function(t) {
|
|
|
|
return new Node(t, BREAK);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setLabel: function(n, v) {
|
|
|
|
n.label = v;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setTarget: function(n, n2) {
|
|
|
|
n.target = n2;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
CONTINUE: {
|
|
|
|
build: function(t) {
|
|
|
|
return new Node(t, CONTINUE);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setLabel: function(n, v) {
|
|
|
|
n.label = v;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setTarget: function(n, n2) {
|
|
|
|
n.target = n2;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
TRY: {
|
|
|
|
build: function(t) {
|
|
|
|
var n = new Node(t, TRY);
|
|
|
|
n.catchClauses = [];
|
|
|
|
return n;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setTryBlock: function(n, s) {
|
|
|
|
n.tryBlock = s;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
addCatch: function(n, n2) {
|
|
|
|
n.catchClauses.push(n2);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finishCatches: function(n) {
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setFinallyBlock: function(n, s) {
|
|
|
|
n.finallyBlock = s;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
CATCH: {
|
|
|
|
build: function(t) {
|
|
|
|
var n = new Node(t, CATCH);
|
|
|
|
n.guard = null;
|
|
|
|
return n;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setVarName: function(n, v) {
|
|
|
|
n.varName = v;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setGuard: function(n, e) {
|
|
|
|
n.guard = e;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setBlock: function(n, s) {
|
|
|
|
n.block = s;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
THROW: {
|
|
|
|
build: function(t) {
|
|
|
|
return new Node(t, THROW);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setException: function(n, e) {
|
|
|
|
n.exception = e;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
RETURN: {
|
|
|
|
build: function(t) {
|
|
|
|
return new Node(t, RETURN);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setValue: function(n, e) {
|
|
|
|
n.value = e;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
YIELD: {
|
|
|
|
build: function(t) {
|
|
|
|
return new Node(t, YIELD);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setValue: function(n, e) {
|
|
|
|
n.value = e;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
GENERATOR: {
|
|
|
|
build: function(t) {
|
|
|
|
return new Node(t, GENERATOR);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setExpression: function(n, e) {
|
|
|
|
n.expression = e;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setTail: function(n, n2) {
|
|
|
|
n.tail = n2;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
WITH: {
|
|
|
|
build: function(t) {
|
|
|
|
return new Node(t, WITH);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setObject: function(n, e) {
|
|
|
|
n.object = e;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setBody: function(n, s) {
|
|
|
|
n.body = s;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
DEBUGGER: {
|
|
|
|
build: function(t) {
|
|
|
|
return new Node(t, DEBUGGER);
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
SEMICOLON: {
|
|
|
|
build: function(t) {
|
|
|
|
return new Node(t, SEMICOLON);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setExpression: function(n, e) {
|
|
|
|
n.expression = e;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
LABEL: {
|
|
|
|
build: function(t) {
|
|
|
|
return new Node(t, LABEL);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setLabel: function(n, e) {
|
|
|
|
n.label = e;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setStatement: function(n, s) {
|
|
|
|
n.statement = s;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
FUNCTION: {
|
|
|
|
build: function(t) {
|
|
|
|
var n = new Node(t);
|
|
|
|
if (n.type !== FUNCTION)
|
|
|
|
n.type = (n.value === "get") ? GETTER : SETTER;
|
|
|
|
n.params = [];
|
|
|
|
return n;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setName: function(n, v) {
|
|
|
|
n.name = v;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
addParam: function(n, v) {
|
|
|
|
n.params.push(v);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setBody: function(n, s) {
|
|
|
|
n.body = s;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
hoistVars: function(x) {
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n, x) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
VAR: {
|
|
|
|
build: function(t) {
|
|
|
|
return new Node(t, VAR);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
addDecl: function(n, n2, x) {
|
|
|
|
n.push(n2);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
CONST: {
|
|
|
|
build: function(t) {
|
|
|
|
return new Node(t, VAR);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
addDecl: function(n, n2, x) {
|
|
|
|
n.push(n2);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
LET: {
|
|
|
|
build: function(t) {
|
|
|
|
return new Node(t, LET);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
addDecl: function(n, n2, x) {
|
|
|
|
n.push(n2);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
DECL: {
|
|
|
|
build: function(t) {
|
|
|
|
return new Node(t, IDENTIFIER);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setName: function(n, v) {
|
|
|
|
n.name = v;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setInitializer: function(n, e) {
|
|
|
|
n.initializer = e;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setReadOnly: function(n, b) {
|
|
|
|
n.readOnly = b;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
LET_BLOCK: {
|
|
|
|
build: function(t) {
|
|
|
|
var n = Node(t, LET_BLOCK);
|
|
|
|
n.varDecls = [];
|
|
|
|
return n;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setVariables: function(n, n2) {
|
|
|
|
n.variables = n2;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setExpression: function(n, e) {
|
|
|
|
n.expression = e;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setBlock: function(n, s) {
|
|
|
|
n.block = s;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
BLOCK: {
|
|
|
|
build: function(t, id) {
|
|
|
|
var n = new Node(t, BLOCK);
|
|
|
|
n.varDecls = [];
|
|
|
|
n.id = id;
|
|
|
|
return n;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
hoistLets: function(n) {
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
addStatement: function(n, n2) {
|
|
|
|
n.push(n2);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
EXPRESSION: {
|
|
|
|
build: function(t, tt) {
|
|
|
|
return new Node(t, tt);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
addOperand: function(n, n2) {
|
|
|
|
n.push(n2);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
ASSIGN: {
|
|
|
|
build: function(t) {
|
|
|
|
return new Node(t, ASSIGN);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
addOperand: function(n, n2) {
|
|
|
|
n.push(n2);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setAssignOp: function(n, o) {
|
|
|
|
n.assignOp = o;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
HOOK: {
|
|
|
|
build: function(t) {
|
|
|
|
return new Node(t, HOOK);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setCondition: function(n, e) {
|
|
|
|
n[0] = e;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setThenPart: function(n, n2) {
|
|
|
|
n[1] = n2;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setElsePart: function(n, n2) {
|
|
|
|
n[2] = n2;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
OR: mkBinopBuilder(OR),
|
|
|
|
AND: mkBinopBuilder(AND),
|
|
|
|
BITWISE_OR: mkBinopBuilder(BITWISE_OR),
|
|
|
|
BITWISE_XOR: mkBinopBuilder(BITWISE_XOR),
|
|
|
|
BITWISE_AND: mkBinopBuilder(BITWISE_AND),
|
|
|
|
EQUALITY: mkBinopBuilder(), // EQ | NE | STRICT_EQ | STRICT_NE
|
|
|
|
RELATIONAL: mkBinopBuilder(), // LT | LE | GE | GT
|
|
|
|
SHIFT: mkBinopBuilder(), // LSH | RSH | URSH
|
|
|
|
ADD: mkBinopBuilder(), // PLUS | MINUS
|
|
|
|
MULTIPLY: mkBinopBuilder(), // MUL | DIV | MOD
|
|
|
|
|
|
|
|
UNARY: {
|
|
|
|
// DELETE | VOID | TYPEOF | NOT | BITWISE_NOT
|
|
|
|
// UNARY_PLUS | UNARY_MINUS | INCREMENT | DECREMENT
|
|
|
|
build: function(t) {
|
|
|
|
if (t.token.type === PLUS)
|
|
|
|
t.token.type = UNARY_PLUS;
|
|
|
|
else if (t.token.type === MINUS)
|
|
|
|
t.token.type = UNARY_MINUS;
|
|
|
|
return new Node(t);
|
|
|
|
},
|
|
|
|
|
|
|
|
addOperand: function(n, n2) {
|
|
|
|
n.push(n2);
|
|
|
|
},
|
|
|
|
|
|
|
|
setPostfix: function(n) {
|
|
|
|
n.postfix = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
MEMBER: {
|
|
|
|
// NEW | DOT | INDEX
|
|
|
|
build: function(t, tt) {
|
|
|
|
return new Node(t, tt);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
rebuildNewWithArgs: function(n) {
|
|
|
|
n.type = NEW_WITH_ARGS;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
addOperand: function(n, n2) {
|
|
|
|
n.push(n2);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
PRIMARY: {
|
|
|
|
build: function(t, tt) {
|
|
|
|
// NB t.token.type must be NULL, THIS, TRUIE, FALSE, IDENTIFIER,
|
|
|
|
// NUMBER, STRING, or REGEXP.
|
|
|
|
return new Node(t, tt);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
ARRAY_INIT: {
|
|
|
|
build: function(t) {
|
|
|
|
return new Node(t, ARRAY_INIT);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
addElement: function(n, n2) {
|
|
|
|
n.push(n2);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
ARRAY_COMP: {
|
|
|
|
build: function(t) {
|
|
|
|
return new Node(t, ARRAY_COMP);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setExpression: function(n, e) {
|
|
|
|
n.expression = e
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setTail: function(n, n2) {
|
|
|
|
n.tail = n2;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
COMP_TAIL: {
|
|
|
|
build: function(t) {
|
|
|
|
return new Node(t, COMP_TAIL);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
setGuard: function(n, e) {
|
|
|
|
n.guard = e;
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
addFor: function(n, n2) {
|
|
|
|
n.push(n2);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
OBJECT_INIT: {
|
|
|
|
build: function(t) {
|
|
|
|
return new Node(t, OBJECT_INIT);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
addProperty: function(n, n2) {
|
|
|
|
n.push(n2);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
PROPERTY_INIT: {
|
|
|
|
build: function(t) {
|
|
|
|
return new Node(t, PROPERTY_INIT);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
addOperand: function(n, n2) {
|
|
|
|
n.push(n2);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
COMMA: {
|
|
|
|
build: function(t) {
|
|
|
|
return new Node(t, COMMA);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
addOperand: function(n, n2) {
|
|
|
|
n.push(n2);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
LIST: {
|
|
|
|
build: function(t) {
|
|
|
|
return new Node(t, LIST);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
addOperand: function(n, n2) {
|
|
|
|
n.push(n2);
|
|
|
|
},
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
finish: function(n) {
|
|
|
|
}
|
2010-08-06 10:40:31 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
setHoists: function(id, vds) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-08-06 19:37:24 -07:00
|
|
|
function StaticContext(inFunction, builder) {
|
2010-08-06 10:40:31 -07:00
|
|
|
this.inFunction = inFunction;
|
|
|
|
this.hasEmptyReturn = false;
|
|
|
|
this.hasReturnWithValue = false;
|
|
|
|
this.isGenerator = false;
|
|
|
|
this.blockId = 0;
|
|
|
|
this.builder = builder;
|
|
|
|
this.stmtStack = [];
|
|
|
|
this.funDecls = [];
|
|
|
|
this.varDecls = [];
|
|
|
|
}
|
|
|
|
|
2010-08-06 19:37:24 -07:00
|
|
|
StaticContext.prototype = {
|
2010-08-06 10:40:31 -07:00
|
|
|
bracketLevel: 0,
|
|
|
|
curlyLevel: 0,
|
|
|
|
parenLevel: 0,
|
|
|
|
hookLevel: 0,
|
|
|
|
ecma3OnlyMode: false,
|
|
|
|
inForLoopInit: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Script :: (tokenizer, compiler context) -> node
|
|
|
|
*
|
|
|
|
* Parses the toplevel and function bodies.
|
|
|
|
*/
|
|
|
|
function Script(t, x) {
|
|
|
|
var n = Statements(t, x);
|
|
|
|
n.type = SCRIPT;
|
|
|
|
n.funDecls = x.funDecls;
|
|
|
|
n.varDecls = x.varDecls;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Node extends Array, which we extend slightly with a top-of-stack method.
|
2010-08-09 12:37:38 -07:00
|
|
|
definitions.defineProperty(Array.prototype, "top",
|
2010-08-06 10:40:31 -07:00
|
|
|
function() {
|
|
|
|
return this.length && this[this.length-1];
|
|
|
|
}, false, false, true);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Node :: (tokenizer, optional type) -> node
|
|
|
|
*/
|
|
|
|
function Node(t, type) {
|
|
|
|
var token = t.token;
|
|
|
|
if (token) {
|
|
|
|
this.type = type || token.type;
|
|
|
|
this.value = token.value;
|
|
|
|
this.lineno = token.lineno;
|
|
|
|
// Start & end are file positions for error handling.
|
|
|
|
this.start = token.start;
|
|
|
|
this.end = token.end;
|
|
|
|
} else {
|
|
|
|
this.type = type;
|
|
|
|
this.lineno = t.lineno;
|
|
|
|
}
|
|
|
|
// Nodes use a tokenizer for debugging (getSource, filename getter).
|
|
|
|
this.tokenizer = t;
|
|
|
|
|
|
|
|
for (var i = 2; i < arguments.length; i++)
|
|
|
|
this.push(arguments[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
var Np = Node.prototype = new Array;
|
|
|
|
Np.constructor = Node;
|
|
|
|
Np.toSource = Object.prototype.toSource;
|
|
|
|
|
|
|
|
// Always use push to add operands to an expression, to update start and end.
|
|
|
|
Np.push = function (kid) {
|
|
|
|
// kid can be null e.g. [1, , 2].
|
|
|
|
if (kid !== null) {
|
|
|
|
if (kid.start < this.start)
|
|
|
|
this.start = kid.start;
|
|
|
|
if (this.end < kid.end)
|
|
|
|
this.end = kid.end;
|
|
|
|
}
|
|
|
|
return Array.prototype.push.call(this, kid);
|
|
|
|
}
|
|
|
|
|
|
|
|
Node.indentLevel = 0;
|
|
|
|
|
|
|
|
function tokenstr(tt) {
|
2010-08-09 12:37:38 -07:00
|
|
|
var t = definitions.tokens[tt];
|
|
|
|
return /^\W/.test(t) ? definitions.opTypeNames[t] : t.toUpperCase();
|
2010-08-06 10:40:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Np.toString = function () {
|
|
|
|
var a = [];
|
|
|
|
for (var i in this) {
|
2010-08-09 14:35:02 -07:00
|
|
|
if (this.hasOwnProperty(i) && i !== 'type' && i !== 'target')
|
2010-08-06 10:40:31 -07:00
|
|
|
a.push({id: i, value: this[i]});
|
|
|
|
}
|
|
|
|
a.sort(function (a,b) { return (a.id < b.id) ? -1 : 1; });
|
|
|
|
const INDENTATION = " ";
|
|
|
|
var n = ++Node.indentLevel;
|
|
|
|
var s = "{\n" + INDENTATION.repeat(n) + "type: " + tokenstr(this.type);
|
|
|
|
for (i = 0; i < a.length; i++)
|
|
|
|
s += ",\n" + INDENTATION.repeat(n) + a[i].id + ": " + a[i].value;
|
|
|
|
n = --Node.indentLevel;
|
|
|
|
s += "\n" + INDENTATION.repeat(n) + "}";
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
Np.getSource = function () {
|
|
|
|
return this.tokenizer.source.slice(this.start, this.end);
|
|
|
|
};
|
|
|
|
|
2010-08-09 12:37:38 -07:00
|
|
|
definitions.defineGetter(Np, "filename",
|
2010-08-06 10:40:31 -07:00
|
|
|
function() {
|
|
|
|
return this.tokenizer.filename;
|
|
|
|
});
|
|
|
|
|
2010-08-09 12:37:38 -07:00
|
|
|
definitions.defineProperty(String.prototype, "repeat",
|
2010-08-06 10:40:31 -07:00
|
|
|
function(n) {
|
|
|
|
var s = "", t = this + s;
|
|
|
|
while (--n >= 0)
|
|
|
|
s += t;
|
|
|
|
return s;
|
|
|
|
}, false, false, true);
|
|
|
|
|
|
|
|
// Statement stack and nested statement handler.
|
|
|
|
function nest(t, x, node, func, end) {
|
|
|
|
x.stmtStack.push(node);
|
|
|
|
var n = func(t, x);
|
|
|
|
x.stmtStack.pop();
|
|
|
|
end && t.mustMatch(end);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Statements :: (tokenizer, compiler context) -> node
|
|
|
|
*
|
|
|
|
* Parses a list of Statements.
|
|
|
|
*/
|
|
|
|
function Statements(t, x) {
|
2010-08-09 12:47:41 -07:00
|
|
|
/*
|
|
|
|
* Blocks are uniquely numbered by a blockId within a function that is
|
|
|
|
* at the top level of the program. blockId starts from 0.
|
|
|
|
*
|
|
|
|
* This is done to aid hoisting for parse-time analyses done in custom
|
|
|
|
* builders.
|
|
|
|
*
|
|
|
|
* For more details in its interaction with hoisting, see comments in
|
|
|
|
* FunctionDefinition.
|
|
|
|
*/
|
2010-08-24 19:45:21 -07:00
|
|
|
var builder = x.builder;
|
|
|
|
var b = builder.BLOCK;
|
|
|
|
var n = b.build(t, x.blockId++);
|
|
|
|
b.hoistLets(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
x.stmtStack.push(n);
|
2010-08-09 14:35:02 -07:00
|
|
|
while (!t.done && t.peek(true) !== RIGHT_CURLY)
|
2010-08-24 19:45:21 -07:00
|
|
|
b.addStatement(n, Statement(t, x));
|
2010-08-06 10:40:31 -07:00
|
|
|
x.stmtStack.pop();
|
2010-08-24 19:45:21 -07:00
|
|
|
b.finish(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
if (n.needsHoisting) {
|
2010-08-24 19:45:21 -07:00
|
|
|
builder.setHoists(n.id, n.varDecls);
|
2010-08-09 12:47:41 -07:00
|
|
|
/*
|
|
|
|
* If a block needs hoisting, we need to propagate this flag up to
|
|
|
|
* the CompilerContext.
|
|
|
|
*/
|
2010-08-06 10:40:31 -07:00
|
|
|
x.needsHoisting = true;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
function Block(t, x) {
|
|
|
|
t.mustMatch(LEFT_CURLY);
|
|
|
|
var n = Statements(t, x);
|
|
|
|
t.mustMatch(RIGHT_CURLY);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DECLARED_FORM = 0, EXPRESSED_FORM = 1, STATEMENT_FORM = 2;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Statement :: (tokenizer, compiler context) -> node
|
|
|
|
*
|
|
|
|
* Parses a Statement.
|
|
|
|
*/
|
|
|
|
function Statement(t, x) {
|
|
|
|
var i, label, n, n2, ss, tt = t.get(true);
|
2010-08-24 19:45:21 -07:00
|
|
|
var builder = x.builder, b, b2, b3;
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
// Cases for statements ending in a right curly return early, avoiding the
|
|
|
|
// common semicolon insertion magic after this switch.
|
|
|
|
switch (tt) {
|
|
|
|
case FUNCTION:
|
|
|
|
// DECLARED_FORM extends funDecls of x, STATEMENT_FORM doesn't.
|
|
|
|
return FunctionDefinition(t, x, true,
|
|
|
|
(x.stmtStack.length > 1)
|
|
|
|
? STATEMENT_FORM
|
|
|
|
: DECLARED_FORM);
|
|
|
|
|
|
|
|
case LEFT_CURLY:
|
|
|
|
n = Statements(t, x);
|
|
|
|
t.mustMatch(RIGHT_CURLY);
|
|
|
|
return n;
|
|
|
|
|
|
|
|
case IF:
|
2010-08-24 19:45:21 -07:00
|
|
|
b = builder.IF;
|
|
|
|
n = b.build(t);
|
|
|
|
b.setCondition(n, ParenExpression(t, x));
|
2010-08-06 10:40:31 -07:00
|
|
|
x.stmtStack.push(n);
|
2010-08-24 19:45:21 -07:00
|
|
|
b.setThenPart(n, Statement(t, x));
|
2010-08-06 10:40:31 -07:00
|
|
|
if (t.match(ELSE))
|
2010-08-24 19:45:21 -07:00
|
|
|
b.setElsePart(n, Statement(t, x));
|
2010-08-06 10:40:31 -07:00
|
|
|
x.stmtStack.pop();
|
2010-08-24 19:45:21 -07:00
|
|
|
b.finish(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
return n;
|
|
|
|
|
|
|
|
case SWITCH:
|
|
|
|
// This allows CASEs after a DEFAULT, which is in the standard.
|
2010-08-24 19:45:21 -07:00
|
|
|
b = builder.SWITCH;
|
|
|
|
b2 = builder.DEFAULT;
|
|
|
|
b3 = builder.CASE;
|
|
|
|
n = b.build(t);
|
|
|
|
b.setDiscriminant(n, ParenExpression(t, x));
|
2010-08-06 10:40:31 -07:00
|
|
|
x.stmtStack.push(n);
|
|
|
|
t.mustMatch(LEFT_CURLY);
|
2010-08-09 14:35:02 -07:00
|
|
|
while ((tt = t.get()) !== RIGHT_CURLY) {
|
2010-08-06 10:40:31 -07:00
|
|
|
switch (tt) {
|
|
|
|
case DEFAULT:
|
|
|
|
if (n.defaultIndex >= 0)
|
|
|
|
throw t.newSyntaxError("More than one switch default");
|
2010-08-24 19:45:21 -07:00
|
|
|
n2 = b2.build(t);
|
|
|
|
b.setDefaultIndex(n, n.cases.length);
|
2010-08-06 10:40:31 -07:00
|
|
|
t.mustMatch(COLON);
|
2010-08-24 19:45:21 -07:00
|
|
|
b2.initializeStatements(n2, t);
|
2010-08-09 14:35:02 -07:00
|
|
|
while ((tt=t.peek(true)) !== CASE && tt !== DEFAULT &&
|
|
|
|
tt !== RIGHT_CURLY)
|
2010-08-24 19:45:21 -07:00
|
|
|
b2.addStatement(n2, Statement(t, x));
|
|
|
|
b2.finish(n2);
|
2010-08-06 10:40:31 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CASE:
|
2010-08-24 19:45:21 -07:00
|
|
|
n2 = b3.build(t);
|
|
|
|
b3.setLabel(n2, Expression(t, x, COLON));
|
2010-08-06 10:40:31 -07:00
|
|
|
t.mustMatch(COLON);
|
2010-08-24 19:45:21 -07:00
|
|
|
b3.initializeStatements(n2, t);
|
2010-08-09 14:35:02 -07:00
|
|
|
while ((tt=t.peek(true)) !== CASE && tt !== DEFAULT &&
|
|
|
|
tt !== RIGHT_CURLY)
|
2010-08-24 19:45:21 -07:00
|
|
|
b3.addStatement(n2, Statement(t, x));
|
|
|
|
b3.finish(n2);
|
2010-08-06 10:40:31 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw t.newSyntaxError("Invalid switch case");
|
|
|
|
}
|
2010-08-24 19:45:21 -07:00
|
|
|
b.addCase(n, n2);
|
2010-08-06 10:40:31 -07:00
|
|
|
}
|
|
|
|
x.stmtStack.pop();
|
2010-08-24 19:45:21 -07:00
|
|
|
b.finish(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
return n;
|
|
|
|
|
|
|
|
case FOR:
|
2010-08-24 19:45:21 -07:00
|
|
|
b = builder.FOR;
|
|
|
|
n = b.build(t);
|
2010-08-09 14:35:02 -07:00
|
|
|
if (t.match(IDENTIFIER) && t.token.value === "each")
|
2010-08-24 19:45:21 -07:00
|
|
|
b.rebuildForEach(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
t.mustMatch(LEFT_PAREN);
|
2010-08-09 14:35:02 -07:00
|
|
|
if ((tt = t.peek()) !== SEMICOLON) {
|
2010-08-06 10:40:31 -07:00
|
|
|
x.inForLoopInit = true;
|
2010-08-09 14:35:02 -07:00
|
|
|
if (tt === VAR || tt === CONST) {
|
2010-08-06 10:40:31 -07:00
|
|
|
t.get();
|
|
|
|
n2 = Variables(t, x);
|
2010-08-09 14:35:02 -07:00
|
|
|
} else if (tt === LET) {
|
2010-08-06 10:40:31 -07:00
|
|
|
t.get();
|
2010-08-09 14:35:02 -07:00
|
|
|
if (t.peek() === LEFT_PAREN) {
|
2010-08-06 10:40:31 -07:00
|
|
|
n2 = LetBlock(t, x, false);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Let in for head, we need to add an implicit block
|
|
|
|
* around the rest of the for.
|
|
|
|
*/
|
2010-08-24 19:45:21 -07:00
|
|
|
var forBlock = builder.BLOCK.build(t, x.blockId++);
|
2010-08-06 10:40:31 -07:00
|
|
|
x.stmtStack.push(forBlock);
|
|
|
|
n2 = Variables(t, x, forBlock);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
n2 = Expression(t, x);
|
|
|
|
}
|
|
|
|
x.inForLoopInit = false;
|
|
|
|
}
|
|
|
|
if (n2 && t.match(IN)) {
|
2010-08-24 19:45:21 -07:00
|
|
|
b.rebuildForIn(n);
|
|
|
|
b.setObject(n, Expression(t, x), forBlock);
|
2010-08-09 14:35:02 -07:00
|
|
|
if (n2.type === VAR || n2.type === LET) {
|
|
|
|
if (n2.length !== 1) {
|
2010-08-06 10:40:31 -07:00
|
|
|
throw new SyntaxError("Invalid for..in left-hand side",
|
|
|
|
t.filename, n2.lineno);
|
|
|
|
}
|
2010-08-24 19:45:21 -07:00
|
|
|
b.setIterator(n, n2[0], n2, forBlock);
|
2010-08-06 10:40:31 -07:00
|
|
|
} else {
|
2010-08-24 19:45:21 -07:00
|
|
|
b.setIterator(n, n2, null, forBlock);
|
2010-08-06 10:40:31 -07:00
|
|
|
}
|
|
|
|
} else {
|
2010-08-24 19:45:21 -07:00
|
|
|
b.setSetup(n, n2);
|
2010-08-06 10:40:31 -07:00
|
|
|
t.mustMatch(SEMICOLON);
|
|
|
|
if (n.isEach)
|
|
|
|
throw t.newSyntaxError("Invalid for each..in loop");
|
2010-08-24 19:45:21 -07:00
|
|
|
b.setCondition(n, (t.peek() === SEMICOLON)
|
2010-08-06 10:40:31 -07:00
|
|
|
? null
|
|
|
|
: Expression(t, x));
|
|
|
|
t.mustMatch(SEMICOLON);
|
2010-08-24 19:45:21 -07:00
|
|
|
b.setUpdate(n, (t.peek() === RIGHT_PAREN)
|
2010-08-06 10:40:31 -07:00
|
|
|
? null
|
|
|
|
: Expression(t, x));
|
|
|
|
}
|
|
|
|
t.mustMatch(RIGHT_PAREN);
|
2010-08-24 19:45:21 -07:00
|
|
|
b.setBody(n, nest(t, x, n, Statement));
|
2010-08-06 10:40:31 -07:00
|
|
|
if (forBlock) {
|
2010-08-24 19:45:21 -07:00
|
|
|
builder.BLOCK.finish(forBlock);
|
2010-08-06 10:40:31 -07:00
|
|
|
x.stmtStack.pop();
|
|
|
|
}
|
2010-08-24 19:45:21 -07:00
|
|
|
b.finish(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
return n;
|
|
|
|
|
|
|
|
case WHILE:
|
2010-08-24 19:45:21 -07:00
|
|
|
b = builder.WHILE;
|
|
|
|
n = b.build(t);
|
|
|
|
b.setCondition(n, ParenExpression(t, x));
|
|
|
|
b.setBody(n, nest(t, x, n, Statement));
|
|
|
|
b.finish(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
return n;
|
|
|
|
|
|
|
|
case DO:
|
2010-08-24 19:45:21 -07:00
|
|
|
b = builder.DO;
|
|
|
|
n = b.build(t);
|
|
|
|
b.setBody(n, nest(t, x, n, Statement, WHILE));
|
|
|
|
b.setCondition(n, ParenExpression(t, x));
|
|
|
|
b.finish(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
if (!x.ecmaStrictMode) {
|
|
|
|
// <script language="JavaScript"> (without version hints) may need
|
|
|
|
// automatic semicolon insertion without a newline after do-while.
|
|
|
|
// See http://bugzilla.mozilla.org/show_bug.cgi?id=238945.
|
|
|
|
t.match(SEMICOLON);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BREAK:
|
|
|
|
case CONTINUE:
|
2010-08-24 19:45:21 -07:00
|
|
|
if (tt === BREAK) {
|
|
|
|
b = builder.BREAK;
|
|
|
|
} else {
|
|
|
|
b = builder.CONTINUE;
|
|
|
|
}
|
|
|
|
n = b.build(t);
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-09 14:35:02 -07:00
|
|
|
if (t.peekOnSameLine() === IDENTIFIER) {
|
2010-08-06 10:40:31 -07:00
|
|
|
t.get();
|
2010-08-24 19:45:21 -07:00
|
|
|
b.setLabel(n, t.token.value);
|
2010-08-06 10:40:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ss = x.stmtStack;
|
|
|
|
i = ss.length;
|
|
|
|
label = n.label;
|
|
|
|
|
|
|
|
if (label) {
|
|
|
|
do {
|
|
|
|
if (--i < 0)
|
|
|
|
throw t.newSyntaxError("Label not found");
|
2010-08-09 14:35:02 -07:00
|
|
|
} while (ss[i].label !== label);
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Both break and continue to label need to be handled specially
|
|
|
|
* within a labeled loop, so that they target that loop. If not in
|
|
|
|
* a loop, then break targets its labeled statement. Labels can be
|
|
|
|
* nested so we skip all labels immediately enclosing the nearest
|
|
|
|
* non-label statement.
|
|
|
|
*/
|
2010-08-09 14:35:02 -07:00
|
|
|
while (i < ss.length - 1 && ss[i+1].type === LABEL)
|
2010-08-06 10:40:31 -07:00
|
|
|
i++;
|
|
|
|
if (i < ss.length - 1 && ss[i+1].isLoop)
|
|
|
|
i++;
|
2010-08-09 14:35:02 -07:00
|
|
|
else if (tt === CONTINUE)
|
2010-08-06 10:40:31 -07:00
|
|
|
throw t.newSyntaxError("Invalid continue");
|
|
|
|
} else {
|
|
|
|
do {
|
|
|
|
if (--i < 0) {
|
2010-08-09 14:35:02 -07:00
|
|
|
throw t.newSyntaxError("Invalid " + ((tt === BREAK)
|
2010-08-06 10:40:31 -07:00
|
|
|
? "break"
|
|
|
|
: "continue"));
|
|
|
|
}
|
2010-08-09 14:35:02 -07:00
|
|
|
} while (!ss[i].isLoop && !(tt === BREAK && ss[i].type === SWITCH));
|
2010-08-06 10:40:31 -07:00
|
|
|
}
|
2010-08-24 19:45:21 -07:00
|
|
|
b.setTarget(n, ss[i]);
|
|
|
|
b.finish(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TRY:
|
2010-08-24 19:45:21 -07:00
|
|
|
b = builder.TRY;
|
|
|
|
b2 = builder.CATCH;
|
|
|
|
n = b.build(t);
|
|
|
|
b.setTryBlock(n, Block(t, x));
|
2010-08-06 10:40:31 -07:00
|
|
|
while (t.match(CATCH)) {
|
2010-08-24 19:45:21 -07:00
|
|
|
n2 = b2.build(t);
|
2010-08-06 10:40:31 -07:00
|
|
|
t.mustMatch(LEFT_PAREN);
|
|
|
|
switch (t.get()) {
|
|
|
|
case LEFT_BRACKET:
|
|
|
|
case LEFT_CURLY:
|
|
|
|
// Destructured catch identifiers.
|
|
|
|
t.unget();
|
2010-08-24 19:45:21 -07:00
|
|
|
b2.setVarName(n2, DestructuringExpression(t, x, true));
|
2010-08-06 10:40:31 -07:00
|
|
|
case IDENTIFIER:
|
2010-08-24 19:45:21 -07:00
|
|
|
b2.setVarName(n2, t.token.value);
|
2010-08-06 10:40:31 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw t.newSyntaxError("missing identifier in catch");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (t.match(IF)) {
|
|
|
|
if (x.ecma3OnlyMode)
|
|
|
|
throw t.newSyntaxError("Illegal catch guard");
|
|
|
|
if (n.catchClauses.length && !n.catchClauses.top().guard)
|
|
|
|
throw t.newSyntaxError("Guarded catch after unguarded");
|
2010-08-24 19:45:21 -07:00
|
|
|
b2.setGuard(n2, Expression(t, x));
|
2010-08-06 10:40:31 -07:00
|
|
|
} else {
|
2010-08-24 19:45:21 -07:00
|
|
|
b2.setGuard(n2, null);
|
2010-08-06 10:40:31 -07:00
|
|
|
}
|
|
|
|
t.mustMatch(RIGHT_PAREN);
|
2010-08-24 19:45:21 -07:00
|
|
|
b2.setBlock(n2, Block(t, x));
|
|
|
|
b2.finish(n2);
|
|
|
|
b.addCatch(n, n2);
|
2010-08-06 10:40:31 -07:00
|
|
|
}
|
2010-08-24 19:45:21 -07:00
|
|
|
b.finishCatches(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
if (t.match(FINALLY))
|
2010-08-24 19:45:21 -07:00
|
|
|
b.setFinallyBlock(n, Block(t, x));
|
2010-08-06 10:40:31 -07:00
|
|
|
if (!n.catchClauses.length && !n.finallyBlock)
|
|
|
|
throw t.newSyntaxError("Invalid try statement");
|
2010-08-24 19:45:21 -07:00
|
|
|
b.finish(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
return n;
|
|
|
|
|
|
|
|
case CATCH:
|
|
|
|
case FINALLY:
|
2010-08-09 12:37:38 -07:00
|
|
|
throw t.newSyntaxError(definitions.tokens[tt] + " without preceding try");
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
case THROW:
|
2010-08-24 19:45:21 -07:00
|
|
|
b = builder.THROW;
|
|
|
|
n = b.build(t);
|
|
|
|
b.setException(n, Expression(t, x));
|
|
|
|
b.finish(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RETURN:
|
|
|
|
n = returnOrYield(t, x);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WITH:
|
2010-08-24 19:45:21 -07:00
|
|
|
b = builder.WITH;
|
|
|
|
n = b.build(t);
|
|
|
|
b.setObject(n, ParenExpression(t, x));
|
|
|
|
b.setBody(n, nest(t, x, n, Statement));
|
|
|
|
b.finish(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
return n;
|
|
|
|
|
|
|
|
case VAR:
|
|
|
|
case CONST:
|
|
|
|
n = Variables(t, x);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LET:
|
2010-08-09 14:35:02 -07:00
|
|
|
if (t.peek() === LEFT_PAREN)
|
2010-08-06 10:40:31 -07:00
|
|
|
n = LetBlock(t, x, true);
|
|
|
|
else
|
|
|
|
n = Variables(t, x);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DEBUGGER:
|
2010-08-24 19:45:21 -07:00
|
|
|
n = builder.DEBUGGER.build(t);
|
2010-08-06 10:40:31 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case NEWLINE:
|
|
|
|
case SEMICOLON:
|
2010-08-24 19:45:21 -07:00
|
|
|
b = builder.SEMICOLON;
|
|
|
|
n = b.build(t);
|
|
|
|
b.setExpression(n, null);
|
|
|
|
b.finish(t);
|
2010-08-06 10:40:31 -07:00
|
|
|
return n;
|
|
|
|
|
|
|
|
default:
|
2010-08-09 14:35:02 -07:00
|
|
|
if (tt === IDENTIFIER) {
|
2010-08-06 10:40:31 -07:00
|
|
|
tt = t.peek();
|
|
|
|
// Labeled statement.
|
2010-08-09 14:35:02 -07:00
|
|
|
if (tt === COLON) {
|
2010-08-06 10:40:31 -07:00
|
|
|
label = t.token.value;
|
|
|
|
ss = x.stmtStack;
|
|
|
|
for (i = ss.length-1; i >= 0; --i) {
|
2010-08-09 14:35:02 -07:00
|
|
|
if (ss[i].label === label)
|
2010-08-06 10:40:31 -07:00
|
|
|
throw t.newSyntaxError("Duplicate label");
|
|
|
|
}
|
|
|
|
t.get();
|
2010-08-24 19:45:21 -07:00
|
|
|
b = builder.LABEL;
|
|
|
|
n = b.build(t);
|
|
|
|
b.setLabel(n, label)
|
|
|
|
b.setStatement(n, nest(t, x, n, Statement));
|
|
|
|
b.finish(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expression statement.
|
|
|
|
// We unget the current token to parse the expression as a whole.
|
2010-08-24 19:45:21 -07:00
|
|
|
b = builder.SEMICOLON;
|
|
|
|
n = b.build(t);
|
2010-08-06 10:40:31 -07:00
|
|
|
t.unget();
|
2010-08-24 19:45:21 -07:00
|
|
|
b.setExpression(n, Expression(t, x));
|
2010-08-06 10:40:31 -07:00
|
|
|
n.end = n.expression.end;
|
2010-08-24 19:45:21 -07:00
|
|
|
b.finish(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
MagicalSemicolon(t);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
function MagicalSemicolon(t) {
|
|
|
|
var tt;
|
2010-08-09 14:35:02 -07:00
|
|
|
if (t.lineno === t.token.lineno) {
|
2010-08-06 10:40:31 -07:00
|
|
|
tt = t.peekOnSameLine();
|
2010-08-09 14:35:02 -07:00
|
|
|
if (tt !== END && tt !== NEWLINE && tt !== SEMICOLON && tt !== RIGHT_CURLY)
|
2010-08-06 10:40:31 -07:00
|
|
|
throw t.newSyntaxError("missing ; before statement");
|
|
|
|
}
|
|
|
|
t.match(SEMICOLON);
|
|
|
|
}
|
|
|
|
|
|
|
|
function returnOrYield(t, x) {
|
2010-08-24 19:45:21 -07:00
|
|
|
var n, b, tt = t.token.type, tt2;
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-09 14:35:02 -07:00
|
|
|
if (tt === RETURN) {
|
2010-08-06 10:40:31 -07:00
|
|
|
if (!x.inFunction)
|
|
|
|
throw t.newSyntaxError("Return not in function");
|
2010-08-24 19:45:21 -07:00
|
|
|
b = x.builder.RETURN;
|
2010-08-09 14:35:02 -07:00
|
|
|
} else /* (tt === YIELD) */ {
|
2010-08-06 10:40:31 -07:00
|
|
|
if (!x.inFunction)
|
|
|
|
throw t.newSyntaxError("Yield not in function");
|
|
|
|
x.isGenerator = true;
|
2010-08-24 19:45:21 -07:00
|
|
|
b = x.builder.YIELD;
|
2010-08-06 10:40:31 -07:00
|
|
|
}
|
2010-08-24 19:45:21 -07:00
|
|
|
n = b.build(t);
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
tt2 = t.peek(true);
|
2010-08-09 14:35:02 -07:00
|
|
|
if (tt2 !== END && tt2 !== NEWLINE && tt2 !== SEMICOLON && tt2 !== RIGHT_CURLY
|
|
|
|
&& (tt !== YIELD ||
|
|
|
|
(tt2 !== tt && tt2 !== RIGHT_BRACKET && tt2 !== RIGHT_PAREN &&
|
|
|
|
tt2 !== COLON && tt2 !== COMMA))) {
|
|
|
|
if (tt === RETURN) {
|
2010-08-24 19:45:21 -07:00
|
|
|
b.setValue(n, Expression(t, x));
|
2010-08-06 10:40:31 -07:00
|
|
|
x.hasReturnWithValue = true;
|
|
|
|
} else {
|
2010-08-24 19:45:21 -07:00
|
|
|
b.setValue(n, AssignExpression(t, x));
|
2010-08-06 10:40:31 -07:00
|
|
|
}
|
2010-08-09 14:35:02 -07:00
|
|
|
} else if (tt === RETURN) {
|
2010-08-06 10:40:31 -07:00
|
|
|
x.hasEmptyReturn = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disallow return v; in generator.
|
|
|
|
if (x.hasReturnWithValue && x.isGenerator)
|
|
|
|
throw t.newSyntaxError("Generator returns a value");
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
b.finish(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FunctionDefinition :: (tokenizer, compiler context, boolean,
|
|
|
|
* DECLARED_FORM or EXPRESSED_FORM or STATEMENT_FORM)
|
|
|
|
* -> node
|
|
|
|
*/
|
|
|
|
function FunctionDefinition(t, x, requireName, functionForm) {
|
2010-08-24 19:45:21 -07:00
|
|
|
var tt, x2, rp;
|
|
|
|
var builder = x.builder;
|
|
|
|
var b = builder.FUNCTION;
|
|
|
|
var f = b.build(t);
|
2010-08-06 10:40:31 -07:00
|
|
|
if (t.match(IDENTIFIER))
|
2010-08-24 19:45:21 -07:00
|
|
|
b.setName(f, t.token.value);
|
2010-08-06 10:40:31 -07:00
|
|
|
else if (requireName)
|
|
|
|
throw t.newSyntaxError("missing function identifier");
|
|
|
|
|
|
|
|
t.mustMatch(LEFT_PAREN);
|
|
|
|
if (!t.match(RIGHT_PAREN)) {
|
|
|
|
do {
|
|
|
|
switch (t.get()) {
|
|
|
|
case LEFT_BRACKET:
|
|
|
|
case LEFT_CURLY:
|
|
|
|
// Destructured formal parameters.
|
|
|
|
t.unget();
|
2010-08-24 19:45:21 -07:00
|
|
|
b.addParam(f, DestructuringExpression(t, x));
|
2010-08-06 10:40:31 -07:00
|
|
|
break;
|
|
|
|
case IDENTIFIER:
|
2010-08-24 19:45:21 -07:00
|
|
|
b.addParam(f, t.token.value);
|
2010-08-06 10:40:31 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw t.newSyntaxError("missing formal parameter");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (t.match(COMMA));
|
|
|
|
t.mustMatch(RIGHT_PAREN);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do we have an expression closure or a normal body?
|
2010-08-24 19:45:21 -07:00
|
|
|
tt = t.get();
|
2010-08-09 14:35:02 -07:00
|
|
|
if (tt !== LEFT_CURLY)
|
2010-08-06 10:40:31 -07:00
|
|
|
t.unget();
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
x2 = new StaticContext(true, builder);
|
|
|
|
rp = t.save();
|
2010-08-06 10:40:31 -07:00
|
|
|
if (x.inFunction) {
|
|
|
|
/*
|
2010-08-09 12:47:41 -07:00
|
|
|
* Inner functions don't reset block numbering, only functions at
|
|
|
|
* the top level of the program do.
|
2010-08-06 10:40:31 -07:00
|
|
|
*/
|
|
|
|
x2.blockId = x.blockId;
|
|
|
|
}
|
|
|
|
|
2010-08-09 14:35:02 -07:00
|
|
|
if (tt !== LEFT_CURLY) {
|
2010-08-24 19:45:21 -07:00
|
|
|
b.setBody(f, AssignExpression(t, x));
|
2010-08-06 10:40:31 -07:00
|
|
|
if (x.isGenerator)
|
|
|
|
throw t.newSyntaxError("Generator returns a value");
|
|
|
|
} else {
|
2010-08-24 19:45:21 -07:00
|
|
|
b.hoistVars(x2.blockId);
|
|
|
|
b.setBody(f, Script(t, x2));
|
2010-08-06 10:40:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-08-09 12:47:41 -07:00
|
|
|
* Hoisting makes parse-time binding analysis tricky. A taxonomy of hoists:
|
|
|
|
*
|
|
|
|
* 1. vars hoist to the top of their function:
|
|
|
|
*
|
|
|
|
* var x = 'global';
|
|
|
|
* function f() {
|
|
|
|
* x = 'f';
|
|
|
|
* if (false)
|
|
|
|
* var x;
|
|
|
|
* }
|
|
|
|
* f();
|
|
|
|
* print(x); // "global"
|
|
|
|
*
|
|
|
|
* 2. lets hoist to the top of their block:
|
|
|
|
*
|
|
|
|
* function f() { // id: 0
|
|
|
|
* var x = 'f';
|
|
|
|
* {
|
|
|
|
* {
|
|
|
|
* print(x); // "undefined"
|
|
|
|
* }
|
|
|
|
* let x;
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* f();
|
2010-08-06 10:40:31 -07:00
|
|
|
*
|
2010-08-09 12:47:41 -07:00
|
|
|
* 3. inner functions at function top-level hoist to the beginning
|
|
|
|
* of the function.
|
2010-08-06 10:40:31 -07:00
|
|
|
*
|
2010-08-09 12:47:41 -07:00
|
|
|
* If the builder used is doing parse-time analyses, hoisting may
|
|
|
|
* invalidate earlier conclusions it makes about variable scope.
|
2010-08-06 10:40:31 -07:00
|
|
|
*
|
2010-08-09 12:47:41 -07:00
|
|
|
* The builder can opt to set the needsHoisting flag in a
|
|
|
|
* CompilerContext (in the case of var and function hoisting) or in a
|
|
|
|
* node of type BLOCK (in the case of let hoisting). This signals for
|
|
|
|
* the parser to reparse sections of code.
|
2010-08-06 10:40:31 -07:00
|
|
|
*
|
2010-08-09 12:47:41 -07:00
|
|
|
* To avoid exponential blowup, if a function at the program top-level
|
|
|
|
* has any hoists in its child blocks or inner functions, we reparse
|
|
|
|
* the entire toplevel function. Each toplevel function is parsed at
|
|
|
|
* most twice.
|
|
|
|
*
|
|
|
|
* The list of declarations can be tied to block ids to aid talking
|
|
|
|
* about declarations of blocks that have not yet been fully parsed.
|
|
|
|
*
|
|
|
|
* Blocks are already uniquely numbered; see the comment in
|
|
|
|
* Statements.
|
2010-08-06 10:40:31 -07:00
|
|
|
*/
|
|
|
|
if (x2.needsHoisting) {
|
2010-08-09 12:47:41 -07:00
|
|
|
/*
|
|
|
|
* Order is important here! Builders expect funDecls to come after
|
|
|
|
* varDecls!
|
|
|
|
*/
|
2010-08-24 19:45:21 -07:00
|
|
|
builder.setHoists(f.body.id, x2.varDecls.concat(x2.funDecls));
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
if (x.inFunction) {
|
2010-08-09 12:47:41 -07:00
|
|
|
/*
|
|
|
|
* If an inner function needs hoisting, we need to propagate
|
|
|
|
* this flag up to the parent function.
|
|
|
|
*/
|
2010-08-06 10:40:31 -07:00
|
|
|
x.needsHoisting = true;
|
|
|
|
} else {
|
2010-08-09 12:47:41 -07:00
|
|
|
// Only re-parse functions at the top level of the program.
|
2010-08-24 19:45:21 -07:00
|
|
|
x2 = new StaticContext(true, builder);
|
2010-08-06 10:40:31 -07:00
|
|
|
t.rewind(rp);
|
2010-08-09 12:47:41 -07:00
|
|
|
/*
|
|
|
|
* Set a flag in case the builder wants to have different behavior
|
|
|
|
* on the second pass.
|
|
|
|
*/
|
2010-08-24 19:45:21 -07:00
|
|
|
builder.secondPass = true;
|
|
|
|
b.hoistVars(f.body.id, true);
|
|
|
|
b.setBody(f, Script(t, x2));
|
|
|
|
builder.secondPass = false;
|
2010-08-06 10:40:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-09 14:35:02 -07:00
|
|
|
if (tt === LEFT_CURLY)
|
2010-08-06 10:40:31 -07:00
|
|
|
t.mustMatch(RIGHT_CURLY);
|
|
|
|
|
|
|
|
f.end = t.token.end;
|
|
|
|
f.functionForm = functionForm;
|
2010-08-09 14:35:02 -07:00
|
|
|
if (functionForm === DECLARED_FORM)
|
2010-08-06 10:40:31 -07:00
|
|
|
x.funDecls.push(f);
|
2010-08-24 19:45:21 -07:00
|
|
|
b.finish(f, x);
|
2010-08-06 10:40:31 -07:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Variables :: (tokenizer, compiler context) -> node
|
|
|
|
*
|
|
|
|
* Parses a comma-separated list of var declarations (and maybe
|
|
|
|
* initializations).
|
|
|
|
*/
|
|
|
|
function Variables(t, x, letBlock) {
|
2010-08-24 19:45:21 -07:00
|
|
|
var b, bDecl, bAssign, n, n2, n3, ss, i, s, tt, id, data;
|
|
|
|
var builder = x.builder;
|
|
|
|
var bDecl = builder.DECL;
|
|
|
|
var bAssign = builder.ASSIGN;
|
|
|
|
|
2010-08-06 10:40:31 -07:00
|
|
|
switch (t.token.type) {
|
|
|
|
case VAR:
|
2010-08-24 19:45:21 -07:00
|
|
|
b = builder.VAR;
|
2010-08-06 10:40:31 -07:00
|
|
|
s = x;
|
|
|
|
break;
|
|
|
|
case CONST:
|
2010-08-24 19:45:21 -07:00
|
|
|
b = builder.CONST;
|
2010-08-06 10:40:31 -07:00
|
|
|
s = x;
|
|
|
|
break;
|
|
|
|
case LET:
|
|
|
|
case LEFT_PAREN:
|
2010-08-24 19:45:21 -07:00
|
|
|
b = builder.LET;
|
2010-08-06 10:40:31 -07:00
|
|
|
if (!letBlock) {
|
|
|
|
ss = x.stmtStack;
|
|
|
|
i = ss.length;
|
|
|
|
while (ss[--i].type !== BLOCK) ; // a BLOCK *must* be found.
|
|
|
|
/*
|
|
|
|
* Lets at the function toplevel are just vars, at least in
|
|
|
|
* SpiderMonkey.
|
|
|
|
*/
|
2010-08-09 14:35:02 -07:00
|
|
|
if (i === 0) {
|
2010-08-24 19:45:21 -07:00
|
|
|
b = builder.VAR;
|
2010-08-06 10:40:31 -07:00
|
|
|
s = x;
|
|
|
|
} else {
|
|
|
|
s = ss[i];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s = letBlock;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2010-08-24 19:45:21 -07:00
|
|
|
|
|
|
|
n = b.build(t);
|
2010-08-06 10:40:31 -07:00
|
|
|
initializers = [];
|
2010-08-24 19:45:21 -07:00
|
|
|
|
2010-08-06 10:40:31 -07:00
|
|
|
do {
|
2010-08-24 19:45:21 -07:00
|
|
|
tt = t.get();
|
2010-08-06 10:40:31 -07:00
|
|
|
/*
|
|
|
|
* FIXME Should have a special DECLARATION node instead of overloading
|
|
|
|
* IDENTIFIER to mean both identifier declarations and destructured
|
|
|
|
* declarations.
|
|
|
|
*/
|
2010-08-24 19:45:21 -07:00
|
|
|
n2 = bDecl.build(t);
|
2010-08-09 14:35:02 -07:00
|
|
|
if (tt === LEFT_BRACKET || tt === LEFT_CURLY) {
|
2010-08-06 10:40:31 -07:00
|
|
|
// Pass in s if we need to add each pattern matched into
|
|
|
|
// its varDecls, else pass in x.
|
2010-08-24 19:45:21 -07:00
|
|
|
data = null;
|
2010-08-06 10:40:31 -07:00
|
|
|
// Need to unget to parse the full destructured expression.
|
|
|
|
t.unget();
|
2010-08-24 19:45:21 -07:00
|
|
|
bDecl.setName(n2, DestructuringExpression(t, x, true, s));
|
2010-08-09 14:35:02 -07:00
|
|
|
if (x.inForLoopInit && t.peek() === IN) {
|
2010-08-24 19:45:21 -07:00
|
|
|
b.addDecl(n, n2, s);
|
2010-08-06 10:40:31 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
t.mustMatch(ASSIGN);
|
|
|
|
if (t.token.assignOp)
|
|
|
|
throw t.newSyntaxError("Invalid variable initialization");
|
|
|
|
|
|
|
|
// Parse the init as a normal assignment.
|
2010-08-24 19:45:21 -07:00
|
|
|
n3 = bAssign.build(t);
|
|
|
|
bAssign.addOperand(n3, n2.name);
|
|
|
|
bAssign.addOperand(n3, AssignExpression(t, x));
|
|
|
|
bAssign.finish(n3);
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
// But only add the rhs as the initializer.
|
2010-08-24 19:45:21 -07:00
|
|
|
bDecl.setInitializer(n2, n3[1]);
|
|
|
|
bDecl.finish(n2);
|
|
|
|
b.addDecl(n, n2, s);
|
2010-08-06 10:40:31 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-08-09 14:35:02 -07:00
|
|
|
if (tt !== IDENTIFIER)
|
2010-08-06 10:40:31 -07:00
|
|
|
throw t.newSyntaxError("missing variable name");
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
bDecl.setName(n2, t.token.value);
|
|
|
|
bDecl.setReadOnly(n2, n.type === CONST);
|
|
|
|
b.addDecl(n, n2, s);
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
if (t.match(ASSIGN)) {
|
|
|
|
if (t.token.assignOp)
|
|
|
|
throw t.newSyntaxError("Invalid variable initialization");
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
// Parse the init as a normal assignment.
|
|
|
|
id = new Node(n2.tokenizer, IDENTIFIER);
|
|
|
|
n3 = bAssign.build(t);
|
2010-08-06 10:40:31 -07:00
|
|
|
id.name = id.value = n2.name;
|
2010-08-24 19:45:21 -07:00
|
|
|
bAssign.addOperand(n3, id);
|
|
|
|
bAssign.addOperand(n3, AssignExpression(t, x));
|
|
|
|
bAssign.finish(n3);
|
2010-08-06 10:40:31 -07:00
|
|
|
initializers.push(n3);
|
|
|
|
|
|
|
|
// But only add the rhs as the initializer.
|
2010-08-24 19:45:21 -07:00
|
|
|
bDecl.setInitializer(n2, n3[1]);
|
2010-08-06 10:40:31 -07:00
|
|
|
}
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
bDecl.finish(n2);
|
2010-08-06 10:40:31 -07:00
|
|
|
s.varDecls.push(n2);
|
|
|
|
} while (t.match(COMMA));
|
2010-08-24 19:45:21 -07:00
|
|
|
b.finish(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* LetBlock :: (tokenizer, compiler context, boolean) -> node
|
|
|
|
*
|
|
|
|
* Does not handle let inside of for loop init.
|
|
|
|
*/
|
|
|
|
function LetBlock(t, x, isStatement) {
|
|
|
|
var n, n2, binds;
|
2010-08-24 19:45:21 -07:00
|
|
|
var builder = x.builder;
|
|
|
|
var b = builder.LET_BLOCK, b2;
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
// t.token.type must be LET
|
2010-08-24 19:45:21 -07:00
|
|
|
n = b.build(t);
|
2010-08-06 10:40:31 -07:00
|
|
|
t.mustMatch(LEFT_PAREN);
|
2010-08-24 19:45:21 -07:00
|
|
|
b.setVariables(n, Variables(t, x, n));
|
2010-08-06 10:40:31 -07:00
|
|
|
t.mustMatch(RIGHT_PAREN);
|
|
|
|
|
2010-08-09 14:35:02 -07:00
|
|
|
if (isStatement && t.peek() !== LEFT_CURLY) {
|
2010-08-06 10:40:31 -07:00
|
|
|
/*
|
|
|
|
* If this is really an expression in let statement guise, then we
|
|
|
|
* need to wrap the LET_BLOCK node in a SEMICOLON node so that we pop
|
|
|
|
* the return value of the expression.
|
|
|
|
*/
|
2010-08-24 19:45:21 -07:00
|
|
|
b2 = builder.SEMICOLON;
|
|
|
|
n2 = b2.build(t);
|
|
|
|
b2.setExpression(n2, n);
|
|
|
|
b2.finish(n2);
|
2010-08-06 10:40:31 -07:00
|
|
|
isStatement = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isStatement) {
|
|
|
|
n2 = Block(t, x);
|
2010-08-24 19:45:21 -07:00
|
|
|
b.setBlock(n, n2);
|
2010-08-06 10:40:31 -07:00
|
|
|
} else {
|
|
|
|
n2 = AssignExpression(t, x);
|
2010-08-24 19:45:21 -07:00
|
|
|
b.setExpression(n, n2);
|
2010-08-06 10:40:31 -07:00
|
|
|
}
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
b.finish(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkDestructuring(t, x, n, simpleNamesOnly, data) {
|
2010-08-09 14:35:02 -07:00
|
|
|
if (n.type === ARRAY_COMP)
|
2010-08-06 10:40:31 -07:00
|
|
|
throw t.newSyntaxError("Invalid array comprehension left-hand side");
|
2010-08-09 14:35:02 -07:00
|
|
|
if (n.type !== ARRAY_INIT && n.type !== OBJECT_INIT)
|
2010-08-06 10:40:31 -07:00
|
|
|
return;
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
var nn, n2, lhs, rhs, b = x.builder.DECL;
|
2010-08-06 10:40:31 -07:00
|
|
|
for (var i = 0, j = n.length; i < j; i++) {
|
2010-08-24 19:45:21 -07:00
|
|
|
nn = n[i];
|
2010-08-06 10:40:31 -07:00
|
|
|
if (!nn)
|
|
|
|
continue;
|
2010-08-09 14:35:02 -07:00
|
|
|
if (nn.type === PROPERTY_INIT)
|
2010-08-06 10:40:31 -07:00
|
|
|
lhs = nn[0], rhs = nn[1];
|
|
|
|
else
|
|
|
|
lhs = null, rhs = null;
|
2010-08-09 14:35:02 -07:00
|
|
|
if (rhs && (rhs.type === ARRAY_INIT || rhs.type === OBJECT_INIT))
|
2010-08-06 10:40:31 -07:00
|
|
|
checkDestructuring(t, x, rhs, simpleNamesOnly, data);
|
|
|
|
if (lhs && simpleNamesOnly) {
|
|
|
|
// In declarations, lhs must be simple names
|
2010-08-09 14:35:02 -07:00
|
|
|
if (lhs.type !== IDENTIFIER) {
|
2010-08-06 10:40:31 -07:00
|
|
|
throw t.newSyntaxError("missing name in pattern");
|
|
|
|
} else if (data) {
|
2010-08-24 19:45:21 -07:00
|
|
|
n2 = b.build(t);
|
|
|
|
b.setName(n2, lhs.value);
|
2010-08-06 10:40:31 -07:00
|
|
|
// Don't need to set initializer because it's just for
|
|
|
|
// hoisting anyways.
|
2010-08-24 19:45:21 -07:00
|
|
|
b.finish(n2);
|
2010-08-06 10:40:31 -07:00
|
|
|
// Each pattern needs to be added to varDecls.
|
|
|
|
data.varDecls.push(n2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function DestructuringExpression(t, x, simpleNamesOnly, data) {
|
|
|
|
var n = PrimaryExpression(t, x);
|
|
|
|
checkDestructuring(t, x, n, simpleNamesOnly, data);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
function GeneratorExpression(t, x, e) {
|
2010-08-24 19:45:21 -07:00
|
|
|
var n, b = x.builder.GENERATOR;
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
n = b.build(t);
|
|
|
|
b.setExpression(n, e);
|
|
|
|
b.setTail(n, comprehensionTail(t, x));
|
|
|
|
b.finish(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
function comprehensionTail(t, x) {
|
|
|
|
var body, n;
|
2010-08-24 19:45:21 -07:00
|
|
|
var builder = x.builder;
|
|
|
|
var b = builder.COMP_TAIL;
|
|
|
|
var bFor = builder.FOR;
|
|
|
|
var bDecl = builder.DECL;
|
|
|
|
var bVar = builder.VAR;
|
|
|
|
|
2010-08-06 10:40:31 -07:00
|
|
|
// t.token.type must be FOR
|
2010-08-24 19:45:21 -07:00
|
|
|
body = b.build(t);
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
do {
|
2010-08-24 19:45:21 -07:00
|
|
|
n = bFor.build(t);
|
2010-08-06 10:40:31 -07:00
|
|
|
// Comprehension tails are always for..in loops.
|
2010-08-24 19:45:21 -07:00
|
|
|
bFor.rebuildForIn(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
if (t.match(IDENTIFIER)) {
|
|
|
|
// But sometimes they're for each..in.
|
2010-08-09 14:35:02 -07:00
|
|
|
if (t.token.value === "each")
|
2010-08-24 19:45:21 -07:00
|
|
|
bFor.rebuildForEach(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
else
|
|
|
|
t.unget();
|
|
|
|
}
|
|
|
|
t.mustMatch(LEFT_PAREN);
|
|
|
|
switch(t.get()) {
|
|
|
|
case LEFT_BRACKET:
|
|
|
|
case LEFT_CURLY:
|
|
|
|
t.unget();
|
|
|
|
// Destructured left side of for in comprehension tails.
|
2010-08-24 19:45:21 -07:00
|
|
|
b2.setIterator(n, DestructuringExpression(t, x), null);
|
2010-08-06 10:40:31 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IDENTIFIER:
|
2010-08-24 19:45:21 -07:00
|
|
|
var n3 = bDecl.build(t);
|
|
|
|
bDecl.setName(n3, n3.value);
|
|
|
|
bDecl.finish(n3);
|
|
|
|
var n2 = bVar.build(t);
|
|
|
|
bVar.addDecl(n2, n3);
|
|
|
|
bVar.finish(n2);
|
|
|
|
bFor.setIterator(n, n3, n2);
|
2010-08-06 10:40:31 -07:00
|
|
|
/*
|
|
|
|
* Don't add to varDecls since the semantics of comprehensions is
|
|
|
|
* such that the variables are in their own function when
|
|
|
|
* desugared.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw t.newSyntaxError("missing identifier");
|
|
|
|
}
|
|
|
|
t.mustMatch(IN);
|
2010-08-24 19:45:21 -07:00
|
|
|
bFor.setObject(n, Expression(t, x));
|
2010-08-06 10:40:31 -07:00
|
|
|
t.mustMatch(RIGHT_PAREN);
|
2010-08-24 19:45:21 -07:00
|
|
|
b.addFor(body, n);
|
2010-08-06 10:40:31 -07:00
|
|
|
} while (t.match(FOR));
|
|
|
|
|
|
|
|
// Optional guard.
|
|
|
|
if (t.match(IF))
|
2010-08-24 19:45:21 -07:00
|
|
|
b.setGuard(body, ParenExpression(t, x));
|
2010-08-06 10:40:31 -07:00
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
b.finish(body);
|
2010-08-06 10:40:31 -07:00
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ParenExpression(t, x) {
|
|
|
|
t.mustMatch(LEFT_PAREN);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Always accept the 'in' operator in a parenthesized expression,
|
|
|
|
* where it's unambiguous, even if we might be parsing the init of a
|
|
|
|
* for statement.
|
|
|
|
*/
|
|
|
|
var oldLoopInit = x.inForLoopInit;
|
|
|
|
x.inForLoopInit = false;
|
|
|
|
var n = Expression(t, x);
|
|
|
|
x.inForLoopInit = oldLoopInit;
|
|
|
|
|
|
|
|
var err = "expression must be parenthesized";
|
|
|
|
if (t.match(FOR)) {
|
2010-08-09 14:35:02 -07:00
|
|
|
if (n.type === YIELD && !n.parenthesized)
|
2010-08-06 10:40:31 -07:00
|
|
|
throw t.newSyntaxError("Yield " + err);
|
2010-08-09 14:35:02 -07:00
|
|
|
if (n.type === COMMA && !n.parenthesized)
|
2010-08-06 10:40:31 -07:00
|
|
|
throw t.newSyntaxError("Generator " + err);
|
|
|
|
n = GeneratorExpression(t, x, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
t.mustMatch(RIGHT_PAREN);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-08-24 19:45:21 -07:00
|
|
|
* Expression :: (tokenizer, compiler context) -> node
|
2010-08-06 10:40:31 -07:00
|
|
|
*
|
|
|
|
* Top-down expression parser matched against SpiderMonkey.
|
|
|
|
*/
|
|
|
|
function Expression(t, x) {
|
2010-08-24 19:45:21 -07:00
|
|
|
var n, n2, b = x.builder.COMMA;
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
n = AssignExpression(t, x);
|
|
|
|
if (t.match(COMMA)) {
|
2010-08-24 19:45:21 -07:00
|
|
|
n2 = b.build(t);
|
|
|
|
b.addOperand(n2, n);
|
2010-08-06 10:40:31 -07:00
|
|
|
n = n2;
|
|
|
|
do {
|
|
|
|
n2 = n[n.length-1];
|
2010-08-09 14:35:02 -07:00
|
|
|
if (n2.type === YIELD && !n2.parenthesized)
|
2010-08-06 10:40:31 -07:00
|
|
|
throw t.newSyntaxError("Yield expression must be parenthesized");
|
2010-08-24 19:45:21 -07:00
|
|
|
b.addOperand(n, AssignExpression(t, x));
|
2010-08-06 10:40:31 -07:00
|
|
|
} while (t.match(COMMA));
|
2010-08-24 19:45:21 -07:00
|
|
|
b.finish(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
function AssignExpression(t, x) {
|
2010-08-24 19:45:21 -07:00
|
|
|
var n, lhs, b = x.builder.ASSIGN;
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
// Have to treat yield like an operand because it could be the leftmost
|
|
|
|
// operand of the expression.
|
|
|
|
if (t.match(YIELD, true))
|
|
|
|
return returnOrYield(t, x);
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
n = b.build(t);
|
2010-08-06 10:40:31 -07:00
|
|
|
lhs = ConditionalExpression(t, x);
|
|
|
|
|
|
|
|
if (!t.match(ASSIGN)) {
|
2010-08-24 19:45:21 -07:00
|
|
|
b.finish(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (lhs.type) {
|
|
|
|
case OBJECT_INIT:
|
|
|
|
case ARRAY_INIT:
|
|
|
|
checkDestructuring(t, x, lhs);
|
|
|
|
// FALL THROUGH
|
|
|
|
case IDENTIFIER: case DOT: case INDEX: case CALL:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw t.newSyntaxError("Bad left-hand side of assignment");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
b.setAssignOp(n, t.token.assignOp);
|
|
|
|
b.addOperand(n, lhs);
|
|
|
|
b.addOperand(n, AssignExpression(t, x));
|
|
|
|
b.finish(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ConditionalExpression(t, x) {
|
2010-08-24 19:45:21 -07:00
|
|
|
var n, n2, b = x.builder.HOOK;
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
n = OrExpression(t, x);
|
|
|
|
if (t.match(HOOK)) {
|
|
|
|
n2 = n;
|
2010-08-24 19:45:21 -07:00
|
|
|
n = b.build(t);
|
|
|
|
b.setCondition(n, n2);
|
2010-08-06 10:40:31 -07:00
|
|
|
/*
|
|
|
|
* Always accept the 'in' operator in the middle clause of a ternary,
|
|
|
|
* where it's unambiguous, even if we might be parsing the init of a
|
|
|
|
* for statement.
|
|
|
|
*/
|
|
|
|
var oldLoopInit = x.inForLoopInit;
|
|
|
|
x.inForLoopInit = false;
|
2010-08-24 19:45:21 -07:00
|
|
|
b.setThenPart(n, AssignExpression(t, x));
|
2010-08-06 10:40:31 -07:00
|
|
|
x.inForLoopInit = oldLoopInit;
|
|
|
|
if (!t.match(COLON))
|
|
|
|
throw t.newSyntaxError("missing : after ?");
|
2010-08-24 19:45:21 -07:00
|
|
|
b.setElsePart(n, AssignExpression(t, x));
|
|
|
|
b.finish(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
function OrExpression(t, x) {
|
2010-08-24 19:45:21 -07:00
|
|
|
var n, n2, b = x.builder.OR;
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
n = AndExpression(t, x);
|
|
|
|
while (t.match(OR)) {
|
2010-08-24 19:45:21 -07:00
|
|
|
n2 = b.build(t);
|
|
|
|
b.addOperand(n2, n);
|
|
|
|
b.addOperand(n2, AndExpression(t, x));
|
|
|
|
b.finish(n2);
|
2010-08-06 10:40:31 -07:00
|
|
|
n = n2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
function AndExpression(t, x) {
|
2010-08-24 19:45:21 -07:00
|
|
|
var n, n2, b = x.builder.AND;
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
n = BitwiseOrExpression(t, x);
|
|
|
|
while (t.match(AND)) {
|
2010-08-24 19:45:21 -07:00
|
|
|
n2 = b.build(t);
|
|
|
|
b.addOperand(n2, n);
|
|
|
|
b.addOperand(n2, BitwiseOrExpression(t, x));
|
|
|
|
b.finish(n2);
|
2010-08-06 10:40:31 -07:00
|
|
|
n = n2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
function BitwiseOrExpression(t, x) {
|
2010-08-24 19:45:21 -07:00
|
|
|
var n, n2, b = x.builder.BITWISE_OR;
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
n = BitwiseXorExpression(t, x);
|
|
|
|
while (t.match(BITWISE_OR)) {
|
2010-08-24 19:45:21 -07:00
|
|
|
n2 = b.build(t);
|
|
|
|
b.addOperand(n2, n);
|
|
|
|
b.addOperand(n2, BitwiseXorExpression(t, x));
|
|
|
|
b.finish(n2);
|
2010-08-06 10:40:31 -07:00
|
|
|
n = n2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
function BitwiseXorExpression(t, x) {
|
2010-08-24 19:45:21 -07:00
|
|
|
var n, n2, b = x.builder.BITWISE_XOR;
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
n = BitwiseAndExpression(t, x);
|
|
|
|
while (t.match(BITWISE_XOR)) {
|
2010-08-24 19:45:21 -07:00
|
|
|
n2 = b.build(t);
|
|
|
|
b.addOperand(n2, n);
|
|
|
|
b.addOperand(n2, BitwiseAndExpression(t, x));
|
|
|
|
b.finish(n2);
|
2010-08-06 10:40:31 -07:00
|
|
|
n = n2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
function BitwiseAndExpression(t, x) {
|
2010-08-24 19:45:21 -07:00
|
|
|
var n, n2, b = x.builder.BITWISE_AND;
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
n = EqualityExpression(t, x);
|
|
|
|
while (t.match(BITWISE_AND)) {
|
2010-08-24 19:45:21 -07:00
|
|
|
n2 = b.build(t);
|
|
|
|
b.addOperand(n2, n);
|
|
|
|
b.addOperand(n2, EqualityExpression(t, x));
|
|
|
|
b.finish(n2);
|
2010-08-06 10:40:31 -07:00
|
|
|
n = n2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
function EqualityExpression(t, x) {
|
2010-08-24 19:45:21 -07:00
|
|
|
var n, n2, b = x.builder.EQUALITY;
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
n = RelationalExpression(t, x);
|
|
|
|
while (t.match(EQ) || t.match(NE) ||
|
|
|
|
t.match(STRICT_EQ) || t.match(STRICT_NE)) {
|
2010-08-24 19:45:21 -07:00
|
|
|
n2 = b.build(t);
|
|
|
|
b.addOperand(n2, n);
|
|
|
|
b.addOperand(n2, RelationalExpression(t, x));
|
|
|
|
b.finish(n2);
|
2010-08-06 10:40:31 -07:00
|
|
|
n = n2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
function RelationalExpression(t, x) {
|
2010-08-24 19:45:21 -07:00
|
|
|
var n, n2, b = x.builder.RELATIONAL;
|
2010-08-06 10:40:31 -07:00
|
|
|
var oldLoopInit = x.inForLoopInit;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Uses of the in operator in shiftExprs are always unambiguous,
|
|
|
|
* so unset the flag that prohibits recognizing it.
|
|
|
|
*/
|
|
|
|
x.inForLoopInit = false;
|
|
|
|
n = ShiftExpression(t, x);
|
|
|
|
while ((t.match(LT) || t.match(LE) || t.match(GE) || t.match(GT) ||
|
2010-08-09 14:35:02 -07:00
|
|
|
(oldLoopInit === false && t.match(IN)) ||
|
2010-08-06 10:40:31 -07:00
|
|
|
t.match(INSTANCEOF))) {
|
2010-08-24 19:45:21 -07:00
|
|
|
n2 = b.build(t);
|
|
|
|
b.addOperand(n2, n);
|
|
|
|
b.addOperand(n2, ShiftExpression(t, x));
|
|
|
|
b.finish(n2);
|
2010-08-06 10:40:31 -07:00
|
|
|
n = n2;
|
|
|
|
}
|
|
|
|
x.inForLoopInit = oldLoopInit;
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ShiftExpression(t, x) {
|
2010-08-24 19:45:21 -07:00
|
|
|
var n, n2, b = x.builder.SHIFT;
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
n = AddExpression(t, x);
|
|
|
|
while (t.match(LSH) || t.match(RSH) || t.match(URSH)) {
|
2010-08-24 19:45:21 -07:00
|
|
|
n2 = b.build(t);
|
|
|
|
b.addOperand(n2, n);
|
|
|
|
b.addOperand(n2, AddExpression(t, x));
|
|
|
|
b.finish(n2);
|
2010-08-06 10:40:31 -07:00
|
|
|
n = n2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
function AddExpression(t, x) {
|
2010-08-24 19:45:21 -07:00
|
|
|
var n, n2, b = x.builder.ADD;
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
n = MultiplyExpression(t, x);
|
|
|
|
while (t.match(PLUS) || t.match(MINUS)) {
|
2010-08-24 19:45:21 -07:00
|
|
|
n2 = b.build(t);
|
|
|
|
b.addOperand(n2, n);
|
|
|
|
b.addOperand(n2, MultiplyExpression(t, x));
|
|
|
|
b.finish(n2);
|
2010-08-06 10:40:31 -07:00
|
|
|
n = n2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
function MultiplyExpression(t, x) {
|
2010-08-24 19:45:21 -07:00
|
|
|
var n, n2, b = x.builder.MULTIPLY;
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
n = UnaryExpression(t, x);
|
|
|
|
while (t.match(MUL) || t.match(DIV) || t.match(MOD)) {
|
2010-08-24 19:45:21 -07:00
|
|
|
n2 = b.build(t);
|
|
|
|
b.addOperand(n2, n);
|
|
|
|
b.addOperand(n2, UnaryExpression(t, x));
|
|
|
|
b.finish(n2);
|
2010-08-06 10:40:31 -07:00
|
|
|
n = n2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
function UnaryExpression(t, x) {
|
2010-08-24 19:45:21 -07:00
|
|
|
var n, n2, tt, b = x.builder.UNARY;
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
switch (tt = t.get(true)) {
|
|
|
|
case DELETE: case VOID: case TYPEOF:
|
|
|
|
case NOT: case BITWISE_NOT: case PLUS: case MINUS:
|
2010-08-24 19:45:21 -07:00
|
|
|
n = b.build(t);
|
|
|
|
b.addOperand(n, UnaryExpression(t, x));
|
2010-08-06 10:40:31 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case INCREMENT:
|
|
|
|
case DECREMENT:
|
|
|
|
// Prefix increment/decrement.
|
2010-08-24 19:45:21 -07:00
|
|
|
n = b.build(t)
|
|
|
|
b.addOperand(n, MemberExpression(t, x, true));
|
2010-08-06 10:40:31 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
t.unget();
|
|
|
|
n = MemberExpression(t, x, true);
|
|
|
|
|
|
|
|
// Don't look across a newline boundary for a postfix {in,de}crement.
|
2010-08-09 14:35:02 -07:00
|
|
|
if (t.tokens[(t.tokenIndex + t.lookahead - 1) & 3].lineno ===
|
2010-08-06 10:40:31 -07:00
|
|
|
t.lineno) {
|
|
|
|
if (t.match(INCREMENT) || t.match(DECREMENT)) {
|
2010-08-24 19:45:21 -07:00
|
|
|
n2 = b.build(t);
|
|
|
|
b.setPostfix(n2);
|
|
|
|
b.finish(n);
|
|
|
|
b.addOperand(n2, n);
|
2010-08-06 10:40:31 -07:00
|
|
|
n = n2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
b.finish(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
function MemberExpression(t, x, allowCallSyntax) {
|
2010-08-24 19:45:21 -07:00
|
|
|
var n, n2, tt, b = x.builder.MEMBER;
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
if (t.match(NEW)) {
|
2010-08-24 19:45:21 -07:00
|
|
|
n = b.build(t);
|
|
|
|
b.addOperand(n, MemberExpression(t, x, false));
|
2010-08-06 10:40:31 -07:00
|
|
|
if (t.match(LEFT_PAREN)) {
|
2010-08-24 19:45:21 -07:00
|
|
|
b.rebuildNewWithArgs(n);
|
|
|
|
b.addOperand(n, ArgumentList(t, x));
|
2010-08-06 10:40:31 -07:00
|
|
|
}
|
2010-08-24 19:45:21 -07:00
|
|
|
b.finish(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
} else {
|
|
|
|
n = PrimaryExpression(t, x);
|
|
|
|
}
|
|
|
|
|
2010-08-09 14:35:02 -07:00
|
|
|
while ((tt = t.get()) !== END) {
|
2010-08-06 10:40:31 -07:00
|
|
|
switch (tt) {
|
|
|
|
case DOT:
|
2010-08-24 19:45:21 -07:00
|
|
|
n2 = b.build(t);
|
|
|
|
b.addOperand(n2, n);
|
2010-08-06 10:40:31 -07:00
|
|
|
t.mustMatch(IDENTIFIER);
|
2010-08-24 19:45:21 -07:00
|
|
|
b.addOperand(n2, b.build(t));
|
2010-08-06 10:40:31 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LEFT_BRACKET:
|
2010-08-24 19:45:21 -07:00
|
|
|
n2 = b.build(t, INDEX);
|
|
|
|
b.addOperand(n2, n);
|
|
|
|
b.addOperand(n2, Expression(t, x));
|
2010-08-06 10:40:31 -07:00
|
|
|
t.mustMatch(RIGHT_BRACKET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LEFT_PAREN:
|
|
|
|
if (allowCallSyntax) {
|
2010-08-24 19:45:21 -07:00
|
|
|
n2 = b.build(t, CALL);
|
|
|
|
b.addOperand(n2, n);
|
|
|
|
b.addOperand(n2, ArgumentList(t, x));
|
2010-08-06 10:40:31 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FALL THROUGH
|
|
|
|
default:
|
|
|
|
t.unget();
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
b.finish(n2);
|
2010-08-06 10:40:31 -07:00
|
|
|
n = n2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ArgumentList(t, x) {
|
2010-08-24 19:45:21 -07:00
|
|
|
var n, n2, b = x.builder.LIST;
|
2010-08-06 10:40:31 -07:00
|
|
|
var err = "expression must be parenthesized";
|
|
|
|
|
2010-08-24 19:45:21 -07:00
|
|
|
n = b.build(t);
|
2010-08-06 10:40:31 -07:00
|
|
|
if (t.match(RIGHT_PAREN, true))
|
|
|
|
return n;
|
|
|
|
do {
|
|
|
|
n2 = AssignExpression(t, x);
|
2010-08-09 14:35:02 -07:00
|
|
|
if (n2.type === YIELD && !n2.parenthesized && t.peek() === COMMA)
|
2010-08-06 10:40:31 -07:00
|
|
|
throw t.newSyntaxError("Yield " + err);
|
|
|
|
if (t.match(FOR)) {
|
|
|
|
n2 = GeneratorExpression(t, x, n2);
|
2010-08-09 14:35:02 -07:00
|
|
|
if (n.length > 1 || t.peek(true) === COMMA)
|
2010-08-06 10:40:31 -07:00
|
|
|
throw t.newSyntaxError("Generator " + err);
|
|
|
|
}
|
2010-08-24 19:45:21 -07:00
|
|
|
b.addOperand(n, n2);
|
2010-08-06 10:40:31 -07:00
|
|
|
} while (t.match(COMMA));
|
|
|
|
t.mustMatch(RIGHT_PAREN);
|
2010-08-24 19:45:21 -07:00
|
|
|
b.finish(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
function PrimaryExpression(t, x) {
|
|
|
|
var n, n2, n3, tt = t.get(true);
|
2010-08-24 19:45:21 -07:00
|
|
|
var builder = x.builder;
|
|
|
|
var bArrayInit = builder.ARRAY_INIT;
|
|
|
|
var bArrayComp = builder.ARRAY_COMP;
|
|
|
|
var bPrimary = builder.PRIMARY;
|
|
|
|
var bObjInit = builder.OBJECT_INIT;
|
|
|
|
var bPropInit = builder.PROPERTY_INIT;
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
switch (tt) {
|
|
|
|
case FUNCTION:
|
|
|
|
n = FunctionDefinition(t, x, false, EXPRESSED_FORM);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LEFT_BRACKET:
|
2010-08-24 19:45:21 -07:00
|
|
|
n = bArrayInit.build(t);
|
2010-08-09 14:35:02 -07:00
|
|
|
while ((tt = t.peek()) !== RIGHT_BRACKET) {
|
|
|
|
if (tt === COMMA) {
|
2010-08-06 10:40:31 -07:00
|
|
|
t.get();
|
2010-08-24 19:45:21 -07:00
|
|
|
bArrayInit.addElement(n, null);
|
2010-08-06 10:40:31 -07:00
|
|
|
continue;
|
|
|
|
}
|
2010-08-24 19:45:21 -07:00
|
|
|
bArrayInit.addElement(n, AssignExpression(t, x));
|
2010-08-09 14:35:02 -07:00
|
|
|
if (tt !== COMMA && !t.match(COMMA))
|
2010-08-06 10:40:31 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we matched exactly one element and got a FOR, we have an
|
|
|
|
// array comprehension.
|
2010-08-09 14:35:02 -07:00
|
|
|
if (n.length === 1 && t.match(FOR)) {
|
2010-08-24 19:45:21 -07:00
|
|
|
n2 = bArrayComp.build(t);
|
|
|
|
bArrayComp.setExpression(n2, n[0]);
|
|
|
|
bArrayComp.setTail(n2, comprehensionTail(t, x));
|
2010-08-06 10:40:31 -07:00
|
|
|
n = n2;
|
|
|
|
}
|
|
|
|
t.mustMatch(RIGHT_BRACKET);
|
2010-08-24 19:45:21 -07:00
|
|
|
bPrimary.finish(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LEFT_CURLY:
|
2010-08-24 19:45:21 -07:00
|
|
|
var id, fd;
|
|
|
|
n = bObjInit.build(t);
|
2010-08-06 10:40:31 -07:00
|
|
|
|
|
|
|
object_init:
|
|
|
|
if (!t.match(RIGHT_CURLY)) {
|
|
|
|
do {
|
|
|
|
tt = t.get();
|
2010-08-09 14:35:02 -07:00
|
|
|
if ((t.token.value === "get" || t.token.value === "set") &&
|
|
|
|
t.peek() === IDENTIFIER) {
|
2010-08-06 10:40:31 -07:00
|
|
|
if (x.ecma3OnlyMode)
|
|
|
|
throw t.newSyntaxError("Illegal property accessor");
|
2010-08-24 19:45:21 -07:00
|
|
|
fd = FunctionDefinition(t, x, true, EXPRESSED_FORM);
|
|
|
|
bObjInit.addProperty(n, fd);
|
2010-08-06 10:40:31 -07:00
|
|
|
} else {
|
|
|
|
switch (tt) {
|
|
|
|
case IDENTIFIER: case NUMBER: case STRING:
|
2010-08-24 19:45:21 -07:00
|
|
|
id = bPrimary.build(t, IDENTIFIER);
|
|
|
|
bPrimary.finish(id);
|
2010-08-06 10:40:31 -07:00
|
|
|
break;
|
|
|
|
case RIGHT_CURLY:
|
|
|
|
if (x.ecma3OnlyMode)
|
|
|
|
throw t.newSyntaxError("Illegal trailing ,");
|
|
|
|
break object_init;
|
|
|
|
default:
|
2010-08-09 12:37:38 -07:00
|
|
|
if (t.token.value in definitions.keywords) {
|
2010-08-24 19:45:21 -07:00
|
|
|
id = bPrimary.build(t, IDENTIFIER);
|
|
|
|
bPrimary.finish(id);
|
2010-08-06 10:40:31 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
throw t.newSyntaxError("Invalid property name");
|
|
|
|
}
|
|
|
|
if (t.match(COLON)) {
|
2010-08-24 19:45:21 -07:00
|
|
|
n2 = bPropInit.build(t);
|
|
|
|
bPropInit.addOperand(n2, id);
|
|
|
|
bPropInit.addOperand(n2, AssignExpression(t, x));
|
|
|
|
bPropInit.finish(n2);
|
|
|
|
bObjInit.addProperty(n, n2);
|
2010-08-06 10:40:31 -07:00
|
|
|
} else {
|
|
|
|
// Support, e.g., |var {x, y} = o| as destructuring shorthand
|
|
|
|
// for |var {x: x, y: y} = o|, per proposed JS2/ES4 for JS1.8.
|
2010-08-09 14:35:02 -07:00
|
|
|
if (t.peek() !== COMMA && t.peek() !== RIGHT_CURLY)
|
2010-08-06 10:40:31 -07:00
|
|
|
throw t.newSyntaxError("missing : after property");
|
2010-08-24 19:45:21 -07:00
|
|
|
bObjInit.addProperty(n, id);
|
2010-08-06 10:40:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (t.match(COMMA));
|
|
|
|
t.mustMatch(RIGHT_CURLY);
|
|
|
|
}
|
2010-08-24 19:45:21 -07:00
|
|
|
bObjInit.finish(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LEFT_PAREN:
|
|
|
|
// ParenExpression does its own matching on parentheses, so we need to
|
|
|
|
// unget.
|
|
|
|
t.unget();
|
|
|
|
n = ParenExpression(t, x);
|
|
|
|
n.parenthesized = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LET:
|
|
|
|
n = LetBlock(t, x, false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NULL: case THIS: case TRUE: case FALSE:
|
|
|
|
case IDENTIFIER: case NUMBER: case STRING: case REGEXP:
|
2010-08-24 19:45:21 -07:00
|
|
|
n = bPrimary.build(t);
|
|
|
|
bPrimary.finish(n);
|
2010-08-06 10:40:31 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw t.newSyntaxError("missing operand");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* parse :: (builder, file ptr, path, line number) -> node
|
|
|
|
*/
|
|
|
|
function parse(b, s, f, l) {
|
2010-08-09 12:37:38 -07:00
|
|
|
var t = new lexer.Tokenizer(s, f, l);
|
2010-08-06 19:37:24 -07:00
|
|
|
var x = new StaticContext(false, b);
|
2010-08-06 10:40:31 -07:00
|
|
|
var n = Script(t, x);
|
|
|
|
if (!t.done)
|
|
|
|
throw t.newSyntaxError("Syntax error");
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2010-08-09 12:37:38 -07:00
|
|
|
parse: parse,
|
2010-08-24 19:45:21 -07:00
|
|
|
Node: Node,
|
|
|
|
DefaultBuilder: DefaultBuilder,
|
|
|
|
bindSubBuilders: bindSubBuilders,
|
2010-08-09 12:37:38 -07:00
|
|
|
DECLARED_FORM: DECLARED_FORM,
|
|
|
|
EXPRESSED_FORM: EXPRESSED_FORM,
|
|
|
|
STATEMENT_FORM: STATEMENT_FORM,
|
|
|
|
Tokenizer: lexer.Tokenizer,
|
|
|
|
FunctionDefinition: FunctionDefinition
|
2010-08-06 10:40:31 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
}());
|