},
walk: function(visitor) {
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) {
}
})();
+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, {
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: {
visitor.visit(node, function() {
node.body.walk(visitor);
});
- }
+ },
+ _validate: function() {
+ must_be_expression(this, "body");
+ },
}, AST_Statement);
function walk_body(node, visitor) {
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, {
$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", {
}));
}
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, {
$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, {
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", {
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", {
node.expression.walk(visitor);
node.body.walk(visitor);
});
- }
+ },
+ _validate: function() {
+ must_be_expression(this, "expression");
+ },
}, AST_StatementWithBody);
/* -----[ scope and functions ]----- */
},
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", {
});
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 ]----- */
}, 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", {
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, {
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 ]----- */
node.expression.walk(visitor);
walk_body(node, visitor);
});
- }
+ },
+ _validate: function() {
+ must_be_expression(this, "expression");
+ },
}, AST_Block);
var AST_SwitchBranch = DEFNODE("SwitchBranch", null, {
node.expression.walk(visitor);
walk_body(node, visitor);
});
- }
+ },
+ _validate: function() {
+ must_be_expression(this, "expression");
+ },
}, AST_SwitchBranch);
/* -----[ EXCEPTIONS ]----- */
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", {
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, {
}, 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", {
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 ]----- */
+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", {
$documentation: "A function call expression",
$propdoc: {
arg.walk(visitor);
});
});
- }
+ },
+ _validate: function() {
+ must_be_expression(this, "expression");
+ must_be_expressions(this, "args");
+ },
});
var AST_New = DEFNODE("New", null, {
expr.walk(visitor);
});
});
- }
+ },
+ _validate: function() {
+ must_be_expressions(this, "expressions");
+ },
});
var AST_PropAccess = DEFNODE("PropAccess", "expression property", {
return;
}
return p;
- }
+ },
+ _validate: function() {
+ must_be_expression(this, "expression");
+ },
});
var AST_Dot = DEFNODE("Dot", null, {
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, {
node.expression.walk(visitor);
node.property.walk(visitor);
});
- }
+ },
+ _validate: function() {
+ must_be_expression(this, "property");
+ },
}, AST_PropAccess);
var AST_Unary = DEFNODE("Unary", "operator expression", {
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, {
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", {
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 ]----- */
element.walk(visitor);
});
});
- }
+ },
+ _validate: function() {
+ must_be_expressions(this, "elements");
+ },
});
var AST_Object = DEFNODE("Object", "properties", {
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", {
$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", {
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, {
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, {
$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, {