use directories for structure
This commit is contained in:
605
node_modules/uglify-js/lib/ast.js
generated
vendored
605
node_modules/uglify-js/lib/ast.js
generated
vendored
@@ -1,7 +1,7 @@
|
||||
/***********************************************************************
|
||||
|
||||
A JavaScript tokenizer / parser / beautifier / compressor.
|
||||
https://github.com/mishoo/UglifyJS2
|
||||
https://github.com/mishoo/UglifyJS
|
||||
|
||||
-------------------------------- (C) ---------------------------------
|
||||
|
||||
@@ -110,12 +110,16 @@ var AST_Node = DEFNODE("Node", "start end", {
|
||||
start: "[AST_Token] The first token of this node",
|
||||
end: "[AST_Token] The last token of this node"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this);
|
||||
},
|
||||
walk: function(visitor) {
|
||||
return this._walk(visitor); // not sure the indirection will be any help
|
||||
}
|
||||
visitor.visit(this);
|
||||
},
|
||||
_validate: noop,
|
||||
validate: function() {
|
||||
var ctor = this.CTOR;
|
||||
do {
|
||||
ctor.prototype._validate.call(this);
|
||||
} while (ctor = ctor.BASE);
|
||||
},
|
||||
}, null);
|
||||
|
||||
(AST_Node.log_function = function(fn, verbose) {
|
||||
@@ -138,6 +142,32 @@ var AST_Node = DEFNODE("Node", "start end", {
|
||||
}
|
||||
})();
|
||||
|
||||
var restore_transforms = [];
|
||||
AST_Node.enable_validation = function() {
|
||||
AST_Node.disable_validation();
|
||||
(function validate_transform(ctor) {
|
||||
var transform = ctor.prototype.transform;
|
||||
ctor.prototype.transform = function(tw, in_list) {
|
||||
var node = transform.call(this, tw, in_list);
|
||||
if (node instanceof AST_Node) {
|
||||
node.validate();
|
||||
} else if (!(node === null || in_list && List.is_op(node))) {
|
||||
throw new Error("invalid transformed value: " + node);
|
||||
}
|
||||
return node;
|
||||
};
|
||||
restore_transforms.push(function() {
|
||||
ctor.prototype.transform = transform;
|
||||
});
|
||||
ctor.SUBCLASSES.forEach(validate_transform);
|
||||
})(this);
|
||||
};
|
||||
|
||||
AST_Node.disable_validation = function() {
|
||||
var restore;
|
||||
while (restore = restore_transforms.pop()) restore();
|
||||
};
|
||||
|
||||
/* -----[ statements ]----- */
|
||||
|
||||
var AST_Statement = DEFNODE("Statement", null, {
|
||||
@@ -154,26 +184,37 @@ var AST_Directive = DEFNODE("Directive", "value quote", {
|
||||
value: "[string] The value of this directive as a plain string (it's not an AST_String!)",
|
||||
quote: "[string] the original quote character"
|
||||
},
|
||||
_validate: function() {
|
||||
if (typeof this.value != "string") throw new Error("value must be string");
|
||||
},
|
||||
}, AST_Statement);
|
||||
|
||||
function must_be_expression(node, prop) {
|
||||
if (!(node[prop] instanceof AST_Node)) throw new Error(prop + " must be AST_Node");
|
||||
if (node[prop] instanceof AST_Statement && !(node[prop] instanceof AST_Function)) {
|
||||
throw new Error(prop + " cannot be AST_Statement");
|
||||
}
|
||||
}
|
||||
|
||||
var AST_SimpleStatement = DEFNODE("SimpleStatement", "body", {
|
||||
$documentation: "A statement consisting of an expression, i.e. a = 1 + 2",
|
||||
$propdoc: {
|
||||
body: "[AST_Node] an expression node (should not be instanceof AST_Statement)"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.body._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.body.walk(visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
must_be_expression(this, "body");
|
||||
},
|
||||
}, AST_Statement);
|
||||
|
||||
function walk_body(node, visitor) {
|
||||
var body = node.body;
|
||||
if (body instanceof AST_Statement) {
|
||||
body._walk(visitor);
|
||||
} else body.forEach(function(node) {
|
||||
node._walk(visitor);
|
||||
node.body.forEach(function(node) {
|
||||
node.walk(visitor);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -182,11 +223,18 @@ var AST_Block = DEFNODE("Block", "body", {
|
||||
$propdoc: {
|
||||
body: "[AST_Statement*] an array of statements"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
walk_body(this, visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
walk_body(node, visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
this.body.forEach(function(node) {
|
||||
if (!(node instanceof AST_Statement)) throw new Error("body must be AST_Statement[]");
|
||||
if (node instanceof AST_Function) throw new Error("body cannot contain AST_Function");
|
||||
});
|
||||
},
|
||||
}, AST_Statement);
|
||||
|
||||
var AST_BlockStatement = DEFNODE("BlockStatement", null, {
|
||||
@@ -201,7 +249,11 @@ var AST_StatementWithBody = DEFNODE("StatementWithBody", "body", {
|
||||
$documentation: "Base class for all statements that contain one nested body: `For`, `ForIn`, `Do`, `While`, `With`",
|
||||
$propdoc: {
|
||||
body: "[AST_Statement] the body; this should always be present, even if it's an AST_EmptyStatement"
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (!(this.body instanceof AST_Statement)) throw new Error("body must be AST_Statement");
|
||||
if (this.body instanceof AST_Function) throw new Error("body cannot be AST_Function");
|
||||
},
|
||||
}, AST_Statement);
|
||||
|
||||
var AST_LabeledStatement = DEFNODE("LabeledStatement", "label", {
|
||||
@@ -209,10 +261,11 @@ var AST_LabeledStatement = DEFNODE("LabeledStatement", "label", {
|
||||
$propdoc: {
|
||||
label: "[AST_Label] a label definition"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.label._walk(visitor);
|
||||
this.body._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.label.walk(visitor);
|
||||
node.body.walk(visitor);
|
||||
});
|
||||
},
|
||||
clone: function(deep) {
|
||||
@@ -228,7 +281,10 @@ var AST_LabeledStatement = DEFNODE("LabeledStatement", "label", {
|
||||
}));
|
||||
}
|
||||
return node;
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (!(this.label instanceof AST_Label)) throw new Error("label must be AST_Label");
|
||||
},
|
||||
}, AST_StatementWithBody);
|
||||
|
||||
var AST_IterationStatement = DEFNODE("IterationStatement", null, {
|
||||
@@ -239,25 +295,30 @@ var AST_DWLoop = DEFNODE("DWLoop", "condition", {
|
||||
$documentation: "Base class for do/while statements",
|
||||
$propdoc: {
|
||||
condition: "[AST_Node] the loop condition. Should not be instanceof AST_Statement"
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
must_be_expression(this, "condition");
|
||||
},
|
||||
}, AST_IterationStatement);
|
||||
|
||||
var AST_Do = DEFNODE("Do", null, {
|
||||
$documentation: "A `do` statement",
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.body._walk(visitor);
|
||||
this.condition._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.body.walk(visitor);
|
||||
node.condition.walk(visitor);
|
||||
});
|
||||
}
|
||||
}, AST_DWLoop);
|
||||
|
||||
var AST_While = DEFNODE("While", null, {
|
||||
$documentation: "A `while` statement",
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.condition._walk(visitor);
|
||||
this.body._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.condition.walk(visitor);
|
||||
node.body.walk(visitor);
|
||||
});
|
||||
}
|
||||
}, AST_DWLoop);
|
||||
@@ -269,14 +330,26 @@ var AST_For = DEFNODE("For", "init condition step", {
|
||||
condition: "[AST_Node?] the `for` termination clause, or null if empty",
|
||||
step: "[AST_Node?] the `for` update clause, or null if empty"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
if (this.init) this.init._walk(visitor);
|
||||
if (this.condition) this.condition._walk(visitor);
|
||||
if (this.step) this.step._walk(visitor);
|
||||
this.body._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
if (node.init) node.init.walk(visitor);
|
||||
if (node.condition) node.condition.walk(visitor);
|
||||
if (node.step) node.step.walk(visitor);
|
||||
node.body.walk(visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (this.init != null) {
|
||||
if (!(this.init instanceof AST_Node)) throw new Error("init must be AST_Node");
|
||||
if (this.init instanceof AST_Statement
|
||||
&& !(this.init instanceof AST_Definitions || this.init instanceof AST_Function)) {
|
||||
throw new Error("init cannot be AST_Statement");
|
||||
}
|
||||
}
|
||||
if (this.condition != null) must_be_expression(this, "condition");
|
||||
if (this.step != null) must_be_expression(this, "step");
|
||||
},
|
||||
}, AST_IterationStatement);
|
||||
|
||||
var AST_ForIn = DEFNODE("ForIn", "init object", {
|
||||
@@ -285,13 +358,22 @@ var AST_ForIn = DEFNODE("ForIn", "init object", {
|
||||
init: "[AST_Node] the `for/in` initialization code",
|
||||
object: "[AST_Node] the object that we're looping through"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.init._walk(visitor);
|
||||
this.object._walk(visitor);
|
||||
this.body._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.init.walk(visitor);
|
||||
node.object.walk(visitor);
|
||||
node.body.walk(visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (this.init instanceof AST_Definitions) {
|
||||
if (this.init.definitions.length != 1) throw new Error("init must have single declaration");
|
||||
} else if (!(this.init instanceof AST_PropAccess || this.init instanceof AST_SymbolRef)) {
|
||||
throw new Error("init must be assignable");
|
||||
}
|
||||
must_be_expression(this, "object");
|
||||
},
|
||||
}, AST_IterationStatement);
|
||||
|
||||
var AST_With = DEFNODE("With", "expression", {
|
||||
@@ -299,12 +381,16 @@ var AST_With = DEFNODE("With", "expression", {
|
||||
$propdoc: {
|
||||
expression: "[AST_Node] the `with` expression"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.expression._walk(visitor);
|
||||
this.body._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.expression.walk(visitor);
|
||||
node.body.walk(visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
must_be_expression(this, "expression");
|
||||
},
|
||||
}, AST_StatementWithBody);
|
||||
|
||||
/* -----[ scope and functions ]----- */
|
||||
@@ -329,7 +415,12 @@ var AST_Scope = DEFNODE("Scope", "variables functions uses_with uses_eval parent
|
||||
},
|
||||
pinned: function() {
|
||||
return this.uses_eval || this.uses_with;
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (this.parent_scope != null) {
|
||||
if (!(this.parent_scope instanceof AST_Scope)) throw new Error("parent_scope must be AST_Scope");
|
||||
}
|
||||
},
|
||||
}, AST_Block);
|
||||
|
||||
var AST_Toplevel = DEFNODE("Toplevel", "globals", {
|
||||
@@ -351,7 +442,7 @@ var AST_Toplevel = DEFNODE("Toplevel", "globals", {
|
||||
filename: "wrap=" + JSON.stringify(name)
|
||||
}).transform(new TreeTransformer(function(node) {
|
||||
if (node instanceof AST_Directive && node.value == "$ORIG") {
|
||||
return MAP.splice(body);
|
||||
return List.splice(body);
|
||||
}
|
||||
}));
|
||||
},
|
||||
@@ -370,7 +461,7 @@ var AST_Toplevel = DEFNODE("Toplevel", "globals", {
|
||||
filename: "enclose=" + JSON.stringify(args_values)
|
||||
}).transform(new TreeTransformer(function(node) {
|
||||
if (node instanceof AST_Directive && node.value == "$ORIG") {
|
||||
return MAP.splice(body);
|
||||
return List.splice(body);
|
||||
}
|
||||
}));
|
||||
}
|
||||
@@ -383,27 +474,44 @@ var AST_Lambda = DEFNODE("Lambda", "name argnames uses_arguments length_read", {
|
||||
argnames: "[AST_SymbolFunarg*] array of function arguments",
|
||||
uses_arguments: "[boolean/S] tells whether this function accesses the arguments array"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
if (this.name) this.name._walk(visitor);
|
||||
this.argnames.forEach(function(argname) {
|
||||
argname._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
if (node.name) node.name.walk(visitor);
|
||||
node.argnames.forEach(function(argname) {
|
||||
argname.walk(visitor);
|
||||
});
|
||||
walk_body(this, visitor);
|
||||
walk_body(node, visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
this.argnames.forEach(function(node) {
|
||||
if (!(node instanceof AST_SymbolFunarg)) throw new Error("argnames must be AST_SymbolFunarg[]");
|
||||
});
|
||||
},
|
||||
}, AST_Scope);
|
||||
|
||||
var AST_Accessor = DEFNODE("Accessor", null, {
|
||||
$documentation: "A setter/getter function. The `name` property is always null."
|
||||
$documentation: "A setter/getter function. The `name` property is always null.",
|
||||
_validate: function() {
|
||||
if (this.name != null) throw new Error("name must be null");
|
||||
},
|
||||
}, AST_Lambda);
|
||||
|
||||
var AST_Function = DEFNODE("Function", "inlined", {
|
||||
$documentation: "A function expression"
|
||||
$documentation: "A function expression",
|
||||
_validate: function() {
|
||||
if (this.name != null) {
|
||||
if (!(this.name instanceof AST_SymbolLambda)) throw new Error("name must be AST_SymbolLambda");
|
||||
}
|
||||
},
|
||||
}, AST_Lambda);
|
||||
|
||||
var AST_Defun = DEFNODE("Defun", "inlined", {
|
||||
$documentation: "A function definition"
|
||||
$documentation: "A function definition",
|
||||
_validate: function() {
|
||||
if (!(this.name instanceof AST_SymbolDefun)) throw new Error("name must be AST_SymbolDefun");
|
||||
},
|
||||
}, AST_Lambda);
|
||||
|
||||
/* -----[ JUMPS ]----- */
|
||||
@@ -417,19 +525,26 @@ var AST_Exit = DEFNODE("Exit", "value", {
|
||||
$propdoc: {
|
||||
value: "[AST_Node?] the value returned or thrown by this statement; could be null for AST_Return"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, this.value && function() {
|
||||
this.value._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
if (node.value) node.value.walk(visitor);
|
||||
});
|
||||
}
|
||||
}, AST_Jump);
|
||||
|
||||
var AST_Return = DEFNODE("Return", null, {
|
||||
$documentation: "A `return` statement"
|
||||
$documentation: "A `return` statement",
|
||||
_validate: function() {
|
||||
if (this.value != null) must_be_expression(this, "value");
|
||||
},
|
||||
}, AST_Exit);
|
||||
|
||||
var AST_Throw = DEFNODE("Throw", null, {
|
||||
$documentation: "A `throw` statement"
|
||||
$documentation: "A `throw` statement",
|
||||
_validate: function() {
|
||||
must_be_expression(this, "value");
|
||||
},
|
||||
}, AST_Exit);
|
||||
|
||||
var AST_LoopControl = DEFNODE("LoopControl", "label", {
|
||||
@@ -437,11 +552,17 @@ var AST_LoopControl = DEFNODE("LoopControl", "label", {
|
||||
$propdoc: {
|
||||
label: "[AST_LabelRef?] the label, or null if none",
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, this.label && function() {
|
||||
this.label._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
if (node.label) node.label.walk(visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (this.label != null) {
|
||||
if (!(this.label instanceof AST_LabelRef)) throw new Error("label must be AST_LabelRef");
|
||||
}
|
||||
},
|
||||
}, AST_Jump);
|
||||
|
||||
var AST_Break = DEFNODE("Break", null, {
|
||||
@@ -460,13 +581,21 @@ var AST_If = DEFNODE("If", "condition alternative", {
|
||||
condition: "[AST_Node] the `if` condition",
|
||||
alternative: "[AST_Statement?] the `else` part, or null if not present"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.condition._walk(visitor);
|
||||
this.body._walk(visitor);
|
||||
if (this.alternative) this.alternative._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.condition.walk(visitor);
|
||||
node.body.walk(visitor);
|
||||
if (node.alternative) node.alternative.walk(visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
must_be_expression(this, "condition");
|
||||
if (this.alternative != null) {
|
||||
if (!(this.alternative instanceof AST_Statement)) throw new Error("alternative must be AST_Statement");
|
||||
if (this.alternative instanceof AST_Function) throw new error("alternative cannot be AST_Function");
|
||||
}
|
||||
},
|
||||
}, AST_StatementWithBody);
|
||||
|
||||
/* -----[ SWITCH ]----- */
|
||||
@@ -476,12 +605,16 @@ var AST_Switch = DEFNODE("Switch", "expression", {
|
||||
$propdoc: {
|
||||
expression: "[AST_Node] the `switch` “discriminant”"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.expression._walk(visitor);
|
||||
walk_body(this, visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.expression.walk(visitor);
|
||||
walk_body(node, visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
must_be_expression(this, "expression");
|
||||
},
|
||||
}, AST_Block);
|
||||
|
||||
var AST_SwitchBranch = DEFNODE("SwitchBranch", null, {
|
||||
@@ -497,12 +630,16 @@ var AST_Case = DEFNODE("Case", "expression", {
|
||||
$propdoc: {
|
||||
expression: "[AST_Node] the `case` expression"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.expression._walk(visitor);
|
||||
walk_body(this, visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.expression.walk(visitor);
|
||||
walk_body(node, visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
must_be_expression(this, "expression");
|
||||
},
|
||||
}, AST_SwitchBranch);
|
||||
|
||||
/* -----[ EXCEPTIONS ]----- */
|
||||
@@ -513,13 +650,22 @@ var AST_Try = DEFNODE("Try", "bcatch bfinally", {
|
||||
bcatch: "[AST_Catch?] the catch block, or null if not present",
|
||||
bfinally: "[AST_Finally?] the finally block, or null if not present"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
walk_body(this, visitor);
|
||||
if (this.bcatch) this.bcatch._walk(visitor);
|
||||
if (this.bfinally) this.bfinally._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
walk_body(node, visitor);
|
||||
if (node.bcatch) node.bcatch.walk(visitor);
|
||||
if (node.bfinally) node.bfinally.walk(visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (this.bcatch != null) {
|
||||
if (!(this.bcatch instanceof AST_Catch)) throw new Error("bcatch must be AST_Catch");
|
||||
}
|
||||
if (this.bfinally != null) {
|
||||
if (!(this.bfinally instanceof AST_Finally)) throw new Error("bfinally must be AST_Finally");
|
||||
}
|
||||
},
|
||||
}, AST_Block);
|
||||
|
||||
var AST_Catch = DEFNODE("Catch", "argname", {
|
||||
@@ -527,12 +673,16 @@ var AST_Catch = DEFNODE("Catch", "argname", {
|
||||
$propdoc: {
|
||||
argname: "[AST_SymbolCatch] symbol for the exception"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.argname._walk(visitor);
|
||||
walk_body(this, visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.argname.walk(visitor);
|
||||
walk_body(node, visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (!(this.argname instanceof AST_SymbolCatch)) throw new Error("argname must be AST_SymbolCatch");
|
||||
},
|
||||
}, AST_Block);
|
||||
|
||||
var AST_Finally = DEFNODE("Finally", null, {
|
||||
@@ -546,17 +696,23 @@ var AST_Definitions = DEFNODE("Definitions", "definitions", {
|
||||
$propdoc: {
|
||||
definitions: "[AST_VarDef*] array of variable definitions"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.definitions.forEach(function(defn) {
|
||||
defn._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.definitions.forEach(function(defn) {
|
||||
defn.walk(visitor);
|
||||
});
|
||||
});
|
||||
}
|
||||
}, AST_Statement);
|
||||
|
||||
var AST_Var = DEFNODE("Var", null, {
|
||||
$documentation: "A `var` statement"
|
||||
$documentation: "A `var` statement",
|
||||
_validate: function() {
|
||||
this.definitions.forEach(function(node) {
|
||||
if (!(node instanceof AST_VarDef)) throw new Error("definitions must be AST_VarDef[]");
|
||||
});
|
||||
},
|
||||
}, AST_Definitions);
|
||||
|
||||
var AST_VarDef = DEFNODE("VarDef", "name value", {
|
||||
@@ -565,30 +721,49 @@ var AST_VarDef = DEFNODE("VarDef", "name value", {
|
||||
name: "[AST_SymbolVar] name of the variable",
|
||||
value: "[AST_Node?] initializer, or null of there's no initializer"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.name._walk(visitor);
|
||||
if (this.value) this.value._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.name.walk(visitor);
|
||||
if (node.value) node.value.walk(visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (!(this.name instanceof AST_SymbolVar)) throw new Error("name must be AST_SymbolVar");
|
||||
if (this.value != null) must_be_expression(this, "value");
|
||||
},
|
||||
});
|
||||
|
||||
/* -----[ OTHER ]----- */
|
||||
|
||||
var AST_Call = DEFNODE("Call", "expression args", {
|
||||
function must_be_expressions(node, prop) {
|
||||
node[prop].forEach(function(node) {
|
||||
if (!(node instanceof AST_Node)) throw new Error(prop + " must be AST_Node[]");
|
||||
if (node instanceof AST_Statement && !(node instanceof AST_Function)) {
|
||||
throw new Error(prop + " cannot contain AST_Statement");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
var AST_Call = DEFNODE("Call", "expression args pure", {
|
||||
$documentation: "A function call expression",
|
||||
$propdoc: {
|
||||
expression: "[AST_Node] expression to invoke as function",
|
||||
args: "[AST_Node*] array of arguments"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.expression._walk(visitor);
|
||||
this.args.forEach(function(node) {
|
||||
node._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.expression.walk(visitor);
|
||||
node.args.forEach(function(arg) {
|
||||
arg.walk(visitor);
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
must_be_expression(this, "expression");
|
||||
must_be_expressions(this, "args");
|
||||
},
|
||||
});
|
||||
|
||||
var AST_New = DEFNODE("New", null, {
|
||||
@@ -600,13 +775,18 @@ var AST_Sequence = DEFNODE("Sequence", "expressions", {
|
||||
$propdoc: {
|
||||
expressions: "[AST_Node*] array of expressions (at least two)"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.expressions.forEach(function(node) {
|
||||
node._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.expressions.forEach(function(expr) {
|
||||
expr.walk(visitor);
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (this.expressions.length < 2) throw new Error("expressions must contain multiple elements");
|
||||
must_be_expressions(this, "expressions");
|
||||
},
|
||||
});
|
||||
|
||||
var AST_PropAccess = DEFNODE("PropAccess", "expression property", {
|
||||
@@ -618,7 +798,7 @@ var AST_PropAccess = DEFNODE("PropAccess", "expression property", {
|
||||
getProperty: function() {
|
||||
var p = this.property;
|
||||
if (p instanceof AST_Constant) {
|
||||
return p.getValue();
|
||||
return p.value;
|
||||
}
|
||||
if (p instanceof AST_UnaryPrefix
|
||||
&& p.operator == "void"
|
||||
@@ -626,26 +806,37 @@ var AST_PropAccess = DEFNODE("PropAccess", "expression property", {
|
||||
return;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
must_be_expression(this, "expression");
|
||||
},
|
||||
});
|
||||
|
||||
var AST_Dot = DEFNODE("Dot", null, {
|
||||
$documentation: "A dotted property access expression",
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.expression._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.expression.walk(visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (typeof this.property != "string") throw new Error("property must be string");
|
||||
},
|
||||
}, AST_PropAccess);
|
||||
|
||||
var AST_Sub = DEFNODE("Sub", null, {
|
||||
$documentation: "Index-style property access, i.e. `a[\"foo\"]`",
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.expression._walk(visitor);
|
||||
this.property._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.expression.walk(visitor);
|
||||
node.property.walk(visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
must_be_expression(this, "property");
|
||||
},
|
||||
}, AST_PropAccess);
|
||||
|
||||
var AST_Unary = DEFNODE("Unary", "operator expression", {
|
||||
@@ -654,11 +845,16 @@ var AST_Unary = DEFNODE("Unary", "operator expression", {
|
||||
operator: "[string] the operator",
|
||||
expression: "[AST_Node] expression that this unary operator applies to"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.expression._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.expression.walk(visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (typeof this.operator != "string") throw new Error("operator must be string");
|
||||
must_be_expression(this, "expression");
|
||||
},
|
||||
});
|
||||
|
||||
var AST_UnaryPrefix = DEFNODE("UnaryPrefix", null, {
|
||||
@@ -676,12 +872,18 @@ var AST_Binary = DEFNODE("Binary", "operator left right", {
|
||||
operator: "[string] the operator",
|
||||
right: "[AST_Node] right-hand side expression"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.left._walk(visitor);
|
||||
this.right._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.left.walk(visitor);
|
||||
node.right.walk(visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
must_be_expression(this, "left");
|
||||
if (typeof this.operator != "string") throw new Error("operator must be string");
|
||||
must_be_expression(this, "right");
|
||||
},
|
||||
});
|
||||
|
||||
var AST_Conditional = DEFNODE("Conditional", "condition consequent alternative", {
|
||||
@@ -691,17 +893,26 @@ var AST_Conditional = DEFNODE("Conditional", "condition consequent alternative",
|
||||
consequent: "[AST_Node]",
|
||||
alternative: "[AST_Node]"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.condition._walk(visitor);
|
||||
this.consequent._walk(visitor);
|
||||
this.alternative._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.condition.walk(visitor);
|
||||
node.consequent.walk(visitor);
|
||||
node.alternative.walk(visitor);
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
must_be_expression(this, "condition");
|
||||
must_be_expression(this, "consequent");
|
||||
must_be_expression(this, "alternative");
|
||||
},
|
||||
});
|
||||
|
||||
var AST_Assign = DEFNODE("Assign", null, {
|
||||
$documentation: "An assignment expression — `a = b + 5`",
|
||||
_validate: function() {
|
||||
if (this.operator.indexOf("=") < 0) throw new Error('operator must contain "="');
|
||||
},
|
||||
}, AST_Binary);
|
||||
|
||||
/* -----[ LITERALS ]----- */
|
||||
@@ -711,13 +922,17 @@ var AST_Array = DEFNODE("Array", "elements", {
|
||||
$propdoc: {
|
||||
elements: "[AST_Node*] array of elements"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.elements.forEach(function(element) {
|
||||
element._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.elements.forEach(function(element) {
|
||||
element.walk(visitor);
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
must_be_expressions(this, "elements");
|
||||
},
|
||||
});
|
||||
|
||||
var AST_Object = DEFNODE("Object", "properties", {
|
||||
@@ -725,13 +940,19 @@ var AST_Object = DEFNODE("Object", "properties", {
|
||||
$propdoc: {
|
||||
properties: "[AST_ObjectProperty*] array of properties"
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.properties.forEach(function(prop) {
|
||||
prop._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.properties.forEach(function(prop) {
|
||||
prop.walk(visitor);
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
this.properties.forEach(function(node) {
|
||||
if (!(node instanceof AST_ObjectProperty)) throw new Error("properties must be AST_ObjectProperty[]");
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
var AST_ObjectProperty = DEFNODE("ObjectProperty", "key value", {
|
||||
@@ -740,9 +961,10 @@ var AST_ObjectProperty = DEFNODE("ObjectProperty", "key value", {
|
||||
key: "[string|AST_SymbolAccessor] property name. For ObjectKeyVal this is a string. For getters and setters this is an AST_SymbolAccessor.",
|
||||
value: "[AST_Node] property value. For getters and setters this is an AST_Accessor."
|
||||
},
|
||||
_walk: function(visitor) {
|
||||
return visitor._visit(this, function() {
|
||||
this.value._walk(visitor);
|
||||
walk: function(visitor) {
|
||||
var node = this;
|
||||
visitor.visit(node, function() {
|
||||
node.value.walk(visitor);
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -751,15 +973,27 @@ var AST_ObjectKeyVal = DEFNODE("ObjectKeyVal", "quote", {
|
||||
$documentation: "A key: value object property",
|
||||
$propdoc: {
|
||||
quote: "[string] the original quote character"
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (typeof this.key != "string") throw new Error("key must be string");
|
||||
must_be_expression(this, "value");
|
||||
},
|
||||
}, AST_ObjectProperty);
|
||||
|
||||
var AST_ObjectSetter = DEFNODE("ObjectSetter", null, {
|
||||
$documentation: "An object setter property",
|
||||
_validate: function() {
|
||||
if (!(this.key instanceof AST_SymbolAccessor)) throw new Error("key must be AST_SymbolAccessor");
|
||||
if (!(this.value instanceof AST_Accessor)) throw new Error("value must be AST_Accessor");
|
||||
},
|
||||
}, AST_ObjectProperty);
|
||||
|
||||
var AST_ObjectGetter = DEFNODE("ObjectGetter", null, {
|
||||
$documentation: "An object getter property",
|
||||
_validate: function() {
|
||||
if (!(this.key instanceof AST_SymbolAccessor)) throw new Error("key must be AST_SymbolAccessor");
|
||||
if (!(this.value instanceof AST_Accessor)) throw new Error("value must be AST_Accessor");
|
||||
},
|
||||
}, AST_ObjectProperty);
|
||||
|
||||
var AST_Symbol = DEFNODE("Symbol", "scope name thedef", {
|
||||
@@ -769,6 +1003,9 @@ var AST_Symbol = DEFNODE("Symbol", "scope name thedef", {
|
||||
thedef: "[SymbolDef/S] the definition of this symbol"
|
||||
},
|
||||
$documentation: "Base class for all symbols",
|
||||
_validate: function() {
|
||||
if (typeof this.name != "string") throw new Error("name must be string");
|
||||
},
|
||||
});
|
||||
|
||||
var AST_SymbolAccessor = DEFNODE("SymbolAccessor", null, {
|
||||
@@ -820,13 +1057,13 @@ var AST_LabelRef = DEFNODE("LabelRef", null, {
|
||||
|
||||
var AST_This = DEFNODE("This", null, {
|
||||
$documentation: "The `this` symbol",
|
||||
_validate: function() {
|
||||
if (this.name !== "this") throw new Error('name must be "this"');
|
||||
},
|
||||
}, AST_Symbol);
|
||||
|
||||
var AST_Constant = DEFNODE("Constant", null, {
|
||||
$documentation: "Base class for all constants",
|
||||
getValue: function() {
|
||||
return this.value;
|
||||
}
|
||||
});
|
||||
|
||||
var AST_String = DEFNODE("String", "value quote", {
|
||||
@@ -834,21 +1071,30 @@ var AST_String = DEFNODE("String", "value quote", {
|
||||
$propdoc: {
|
||||
value: "[string] the contents of this string",
|
||||
quote: "[string] the original quote character"
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (typeof this.value != "string") throw new Error("value must be string");
|
||||
},
|
||||
}, AST_Constant);
|
||||
|
||||
var AST_Number = DEFNODE("Number", "value", {
|
||||
$documentation: "A number literal",
|
||||
$propdoc: {
|
||||
value: "[number] the numeric value",
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (typeof this.value != "number") throw new Error("value must be number");
|
||||
},
|
||||
}, AST_Constant);
|
||||
|
||||
var AST_RegExp = DEFNODE("RegExp", "value", {
|
||||
$documentation: "A regexp literal",
|
||||
$propdoc: {
|
||||
value: "[RegExp] the actual regexp"
|
||||
}
|
||||
},
|
||||
_validate: function() {
|
||||
if (!(this.value instanceof RegExp)) throw new Error("value must be RegExp");
|
||||
},
|
||||
}, AST_Constant);
|
||||
|
||||
var AST_Atom = DEFNODE("Atom", null, {
|
||||
@@ -897,21 +1143,16 @@ var AST_True = DEFNODE("True", null, {
|
||||
/* -----[ TreeWalker ]----- */
|
||||
|
||||
function TreeWalker(callback) {
|
||||
this.visit = callback;
|
||||
this.stack = [];
|
||||
this.callback = callback;
|
||||
this.directives = Object.create(null);
|
||||
this.stack = [];
|
||||
}
|
||||
TreeWalker.prototype = {
|
||||
_visit: function(node, descend) {
|
||||
visit: function(node, descend) {
|
||||
this.push(node);
|
||||
var ret = this.visit(node, descend ? function() {
|
||||
descend.call(node);
|
||||
} : noop);
|
||||
if (!ret && descend) {
|
||||
descend.call(node);
|
||||
}
|
||||
var done = this.callback(node, descend || noop);
|
||||
if (!done && descend) descend();
|
||||
this.pop();
|
||||
return ret;
|
||||
},
|
||||
parent: function(n) {
|
||||
return this.stack[this.stack.length - 2 - (n || 0)];
|
||||
@@ -967,11 +1208,13 @@ TreeWalker.prototype = {
|
||||
in_boolean_context: function() {
|
||||
var self = this.self();
|
||||
for (var i = 0, p; p = this.parent(i); i++) {
|
||||
if (p instanceof AST_SimpleStatement
|
||||
|| p instanceof AST_Conditional && p.condition === self
|
||||
if (p instanceof AST_Conditional && p.condition === self
|
||||
|| p instanceof AST_DWLoop && p.condition === self
|
||||
|| p instanceof AST_For && p.condition === self
|
||||
|| p instanceof AST_If && p.condition === self
|
||||
|| p instanceof AST_Return && p.in_bool
|
||||
|| p instanceof AST_Sequence && p.tail_node() !== self
|
||||
|| p instanceof AST_SimpleStatement
|
||||
|| p instanceof AST_UnaryPrefix && p.operator == "!" && p.expression === self) {
|
||||
return true;
|
||||
}
|
||||
|
Reference in New Issue
Block a user