2012-05-27 11:09:01 +00:00
|
|
|
function DEFNODE(type, props, methods, base) {
|
|
|
|
|
if (arguments.length < 4) base = AST_Node;
|
|
|
|
|
if (!props) props = [];
|
|
|
|
|
else props = props.split(/\s+/);
|
|
|
|
|
if (base && base.PROPS)
|
|
|
|
|
props = props.concat(base.PROPS);
|
|
|
|
|
var code = "return function AST_" + type + "(props){ if (props) { ";
|
|
|
|
|
for (var i = props.length; --i >= 0;) {
|
|
|
|
|
code += "this." + props[i] + " = props." + props[i] + ";";
|
|
|
|
|
}
|
2012-08-15 10:32:37 +00:00
|
|
|
if (methods && methods.initialize)
|
|
|
|
|
code += "this.initialize();"
|
2012-05-27 11:09:01 +00:00
|
|
|
code += " } }";
|
|
|
|
|
var ctor = new Function(code)();
|
|
|
|
|
if (base) {
|
|
|
|
|
ctor.prototype = new base;
|
|
|
|
|
}
|
|
|
|
|
ctor.prototype.CTOR = ctor;
|
|
|
|
|
ctor.PROPS = props || null;
|
|
|
|
|
if (type) {
|
|
|
|
|
ctor.prototype.TYPE = ctor.TYPE = type;
|
|
|
|
|
}
|
2012-05-27 14:25:31 +00:00
|
|
|
if (methods) for (i in methods) if (HOP(methods, i)) {
|
2012-05-27 11:09:01 +00:00
|
|
|
ctor.prototype[i] = methods[i];
|
|
|
|
|
}
|
|
|
|
|
return ctor;
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-27 14:25:31 +00:00
|
|
|
var AST_Token = DEFNODE("Token", "type value line col pos endpos nlb comments_before", {
|
2012-05-27 11:09:01 +00:00
|
|
|
|
|
|
|
|
}, null);
|
|
|
|
|
|
|
|
|
|
var AST_Node = DEFNODE("Node", "start end", {
|
2012-08-15 10:32:37 +00:00
|
|
|
clone: function() {
|
|
|
|
|
return new this.CTOR(this);
|
2012-05-27 14:25:31 +00:00
|
|
|
},
|
2012-08-15 10:32:37 +00:00
|
|
|
// XXX: what was this for?
|
|
|
|
|
// renew: function(args) {
|
|
|
|
|
// var ctor = this.CTOR, props = ctor.props;
|
|
|
|
|
// for (var i in props) if (!HOP(args, i)) args[i] = this[i];
|
|
|
|
|
// return new ctor(args);
|
|
|
|
|
// },
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this);
|
|
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
}, null);
|
|
|
|
|
|
|
|
|
|
var AST_Directive = DEFNODE("Directive", "value", {
|
2012-05-27 14:25:31 +00:00
|
|
|
print: function(output) {
|
|
|
|
|
output.string(this.value);
|
|
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var AST_Debugger = DEFNODE("Debugger", null, {
|
2012-05-27 14:25:31 +00:00
|
|
|
print: function(output) {
|
|
|
|
|
output.print("debugger");
|
|
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var AST_Parenthesized = DEFNODE("Parenthesized", "expression", {
|
2012-05-27 14:25:31 +00:00
|
|
|
$documentation: "Represents an expression which is always parenthesized. Used for the \
|
|
|
|
|
conditions in IF/WHILE/DO and expression in SWITCH/WITH.",
|
|
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
this.expression.walk(w);
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var AST_Bracketed = DEFNODE("Bracketed", "body", {
|
2012-05-27 14:25:31 +00:00
|
|
|
$documentation: "Represents a block of statements that are always included in brackets. \
|
|
|
|
|
Used for bodies of FUNCTION/TRY/CATCH/THROW/SWITCH.",
|
|
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
this.body.forEach(function(stat){
|
|
|
|
|
stat.walk(w);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/* -----[ loops ]----- */
|
|
|
|
|
|
2012-08-15 10:32:37 +00:00
|
|
|
var AST_Statement = DEFNODE("Statement", "label body", {
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
if (this.label) this.label.walk(w);
|
|
|
|
|
if (this.body) {
|
2012-08-15 10:32:37 +00:00
|
|
|
if (this.body instanceof AST_Node)
|
2012-05-27 14:25:31 +00:00
|
|
|
this.body.walk(w);
|
2012-08-15 10:32:37 +00:00
|
|
|
else
|
|
|
|
|
this.walk_array(w);
|
2012-05-27 14:25:31 +00:00
|
|
|
}
|
|
|
|
|
});
|
2012-08-15 10:32:37 +00:00
|
|
|
},
|
|
|
|
|
walk_array: AST_Bracketed.prototype.walk
|
2012-05-27 11:09:01 +00:00
|
|
|
});
|
|
|
|
|
|
2012-08-15 10:32:37 +00:00
|
|
|
var AST_SimpleStatement = DEFNODE("SimpleStatement", null, {
|
|
|
|
|
|
|
|
|
|
}, AST_Statement);
|
2012-05-27 11:09:01 +00:00
|
|
|
|
2012-08-15 10:32:37 +00:00
|
|
|
var AST_BlockStatement = DEFNODE("BlockStatement", null, {
|
|
|
|
|
|
|
|
|
|
}, AST_Statement);
|
|
|
|
|
|
|
|
|
|
var AST_EmptyStatement = DEFNODE("EmptyStatement", null, {
|
|
|
|
|
|
|
|
|
|
}, AST_Statement);
|
2012-05-27 11:09:01 +00:00
|
|
|
|
|
|
|
|
var AST_Do = DEFNODE("Do", "condition", {
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
this.condition.walk(w);
|
2012-08-15 10:32:37 +00:00
|
|
|
AST_Statement.prototype.walk.call(this, w);
|
2012-05-27 14:25:31 +00:00
|
|
|
});
|
|
|
|
|
}
|
2012-08-15 10:32:37 +00:00
|
|
|
}, AST_Statement);
|
2012-05-27 11:09:01 +00:00
|
|
|
|
|
|
|
|
var AST_While = DEFNODE("While", "condition", {
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
this.condition.walk(w);
|
2012-08-15 10:32:37 +00:00
|
|
|
AST_Statement.prototype.walk.call(this, w);
|
2012-05-27 14:25:31 +00:00
|
|
|
});
|
|
|
|
|
}
|
2012-08-15 10:32:37 +00:00
|
|
|
}, AST_Statement);
|
2012-05-27 11:09:01 +00:00
|
|
|
|
|
|
|
|
var AST_For = DEFNODE("For", "init condition step", {
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
if (this.init) this.init.walk(w);
|
|
|
|
|
if (this.condition) this.condition.walk(w);
|
|
|
|
|
if (this.step) this.step.walk(w);
|
2012-08-15 10:32:37 +00:00
|
|
|
AST_Statement.prototype.walk.call(this, w);
|
2012-05-27 14:25:31 +00:00
|
|
|
});
|
|
|
|
|
}
|
2012-08-15 10:32:37 +00:00
|
|
|
}, AST_Statement);
|
2012-05-27 11:09:01 +00:00
|
|
|
|
|
|
|
|
var AST_ForIn = DEFNODE("ForIn", "init name object", {
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
if (this.init) this.init.walk(w);
|
|
|
|
|
this.object.walk(w);
|
2012-08-15 10:32:37 +00:00
|
|
|
AST_Statement.prototype.walk.call(this, w);
|
2012-05-27 14:25:31 +00:00
|
|
|
});
|
|
|
|
|
}
|
2012-08-15 10:32:37 +00:00
|
|
|
}, AST_Statement);
|
2012-05-27 11:09:01 +00:00
|
|
|
|
2012-08-15 10:32:37 +00:00
|
|
|
var AST_With = DEFNODE("With", "expression", {
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
this.expression.walk(w);
|
2012-08-15 10:32:37 +00:00
|
|
|
AST_Statement.prototype.walk.call(this, w);
|
2012-05-27 14:25:31 +00:00
|
|
|
});
|
|
|
|
|
}
|
2012-08-15 10:32:37 +00:00
|
|
|
}, AST_Statement);
|
2012-05-27 11:09:01 +00:00
|
|
|
|
|
|
|
|
/* -----[ functions ]----- */
|
|
|
|
|
|
2012-08-15 10:32:37 +00:00
|
|
|
var AST_Scope = DEFNODE("Scope", "identifiers", {
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
if (this.identifiers) this.identifiers.forEach(function(el){
|
|
|
|
|
el.walk(w);
|
|
|
|
|
});
|
2012-08-15 10:32:37 +00:00
|
|
|
AST_Statement.prototype.walk.call(this, w);
|
2012-05-27 14:25:31 +00:00
|
|
|
});
|
|
|
|
|
}
|
2012-08-15 10:32:37 +00:00
|
|
|
}, AST_Statement);
|
2012-05-27 11:09:01 +00:00
|
|
|
|
|
|
|
|
var AST_Toplevel = DEFNODE("Toplevel", null, {
|
|
|
|
|
|
|
|
|
|
}, AST_Scope);
|
|
|
|
|
|
|
|
|
|
var AST_Lambda = DEFNODE("Lambda", "name argnames", {
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
if (this.name) this.name.walk(w);
|
|
|
|
|
this.argnames.forEach(function(el){
|
|
|
|
|
el.walk(w);
|
|
|
|
|
});
|
|
|
|
|
AST_Scope.prototype.walk.call(this, w);
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
}, AST_Scope);
|
2012-05-27 14:25:31 +00:00
|
|
|
|
2012-05-27 11:09:01 +00:00
|
|
|
var AST_Function = DEFNODE("Function", null, {
|
|
|
|
|
|
|
|
|
|
}, AST_Lambda);
|
2012-05-27 14:25:31 +00:00
|
|
|
|
2012-05-27 11:09:01 +00:00
|
|
|
var AST_Defun = DEFNODE("Defun", null, {
|
|
|
|
|
|
|
|
|
|
}, AST_Function);
|
|
|
|
|
|
|
|
|
|
/* -----[ JUMPS ]----- */
|
|
|
|
|
|
2012-05-27 14:25:31 +00:00
|
|
|
var AST_Jump = DEFNODE("Jump", null, {
|
2012-05-27 11:09:01 +00:00
|
|
|
|
2012-05-27 14:25:31 +00:00
|
|
|
});
|
2012-05-27 11:09:01 +00:00
|
|
|
|
2012-05-27 14:25:31 +00:00
|
|
|
var AST_Exit = DEFNODE("Exit", "value", {
|
|
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
if (this.value) this.value.walk(w);
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
}, AST_Jump);
|
|
|
|
|
|
2012-05-27 14:25:31 +00:00
|
|
|
var AST_Return = DEFNODE("Return", null, {
|
|
|
|
|
|
|
|
|
|
}, AST_Exit);
|
|
|
|
|
|
2012-05-27 11:09:01 +00:00
|
|
|
var AST_Throw = DEFNODE("Throw", null, {
|
|
|
|
|
|
2012-05-27 14:25:31 +00:00
|
|
|
}, AST_Exit);
|
|
|
|
|
|
|
|
|
|
var AST_LoopControl = DEFNODE("LoopControl", "label", {
|
|
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
if (this.label) this.label.walk(w);
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
}, AST_Jump);
|
|
|
|
|
|
2012-05-27 14:25:31 +00:00
|
|
|
var AST_Break = DEFNODE("Break", null, {
|
|
|
|
|
|
|
|
|
|
}, AST_LoopControl);
|
|
|
|
|
|
|
|
|
|
var AST_Continue = DEFNODE("Continue", null, {
|
|
|
|
|
|
|
|
|
|
}, AST_LoopControl);
|
|
|
|
|
|
2012-05-27 11:09:01 +00:00
|
|
|
/* -----[ IF ]----- */
|
|
|
|
|
|
|
|
|
|
var AST_If = DEFNODE("If", "condition consequent alternative", {
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
this.condition.walk(w);
|
|
|
|
|
this.consequent.walk(w);
|
|
|
|
|
if (this.alternative) this.alternative.walk(w);
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/* -----[ SWITCH ]----- */
|
|
|
|
|
|
|
|
|
|
var AST_Switch = DEFNODE("Switch", "expression", {
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
this.expression.walk(w);
|
2012-08-15 10:32:37 +00:00
|
|
|
AST_Statement.prototype.walk.call(this, w);
|
2012-05-27 14:25:31 +00:00
|
|
|
});
|
|
|
|
|
}
|
2012-08-15 10:32:37 +00:00
|
|
|
}, AST_Statement);
|
2012-05-27 11:09:01 +00:00
|
|
|
|
|
|
|
|
var AST_SwitchBlock = DEFNODE("SwitchBlock", null, {
|
2012-08-15 10:32:37 +00:00
|
|
|
walk : AST_Statement.prototype.walk,
|
|
|
|
|
walk_array : AST_Bracketed.prototype.walk
|
2012-05-27 11:09:01 +00:00
|
|
|
}, AST_Bracketed);
|
|
|
|
|
|
|
|
|
|
var AST_SwitchBranch = DEFNODE("SwitchBranch", "body", {
|
2012-08-15 10:32:37 +00:00
|
|
|
walk : AST_Statement.prototype.walk,
|
|
|
|
|
walk_array : AST_Bracketed.prototype.walk
|
2012-05-27 11:09:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var AST_Default = DEFNODE("Default", null, {
|
2012-08-15 10:32:37 +00:00
|
|
|
|
2012-05-27 11:09:01 +00:00
|
|
|
}, AST_SwitchBranch);
|
|
|
|
|
|
|
|
|
|
var AST_Case = DEFNODE("Case", "expression", {
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
this.expression.walk(w);
|
|
|
|
|
AST_Statement.prototype.walk.call(this, w);
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
}, AST_SwitchBranch);
|
|
|
|
|
|
|
|
|
|
/* -----[ EXCEPTIONS ]----- */
|
|
|
|
|
|
|
|
|
|
var AST_Try = DEFNODE("Try", "btry bcatch bfinally", {
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
this.btry.walk(w);
|
|
|
|
|
if (this.bcatch) this.bcatch.walk(w);
|
|
|
|
|
if (this.bfinally) this.bfinally.walk(w);
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var AST_Catch = DEFNODE("Catch", "argname body", {
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
this.argname.walk(w);
|
|
|
|
|
this.body.walk(w);
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var AST_Finally = DEFNODE("Finally", null, {
|
|
|
|
|
|
|
|
|
|
}, AST_Bracketed);
|
|
|
|
|
|
|
|
|
|
/* -----[ VAR/CONST ]----- */
|
|
|
|
|
|
|
|
|
|
var AST_Definitions = DEFNODE("Definitions", "definitions", {
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
this.definitions.forEach(function(el){
|
|
|
|
|
el.walk(w);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var AST_Var = DEFNODE("Var", null, {
|
|
|
|
|
|
|
|
|
|
}, AST_Definitions);
|
|
|
|
|
|
|
|
|
|
var AST_Const = DEFNODE("Const", null, {
|
|
|
|
|
|
|
|
|
|
}, AST_Definitions);
|
|
|
|
|
|
|
|
|
|
var AST_VarDef = DEFNODE("VarDef", "name value", {
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
this.name.walk(w);
|
|
|
|
|
if (this.value) this.value.walk(w);
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/* -----[ OTHER ]----- */
|
|
|
|
|
|
|
|
|
|
var AST_Call = DEFNODE("Call", "expression args", {
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
this.expression.walk(w);
|
|
|
|
|
this.args.forEach(function(el){
|
|
|
|
|
el.walk(w);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var AST_New = DEFNODE("New", null, {
|
|
|
|
|
|
|
|
|
|
}, AST_Call);
|
|
|
|
|
|
|
|
|
|
var AST_Seq = DEFNODE("Seq", "first second", {
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
this.first.walk(w);
|
|
|
|
|
this.second.walk(w);
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var AST_PropAccess = DEFNODE("PropAccess", "expression property", {
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var AST_Dot = DEFNODE("Dot", null, {
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
this.expression.walk(w);
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
}, AST_PropAccess);
|
|
|
|
|
|
|
|
|
|
var AST_Sub = DEFNODE("Sub", null, {
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
this.expression.walk(w);
|
|
|
|
|
this.property.walk(w);
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
}, AST_PropAccess);
|
|
|
|
|
|
|
|
|
|
var AST_Unary = DEFNODE("Unary", "operator expression", {
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
this.expression.walk(w);
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var AST_UnaryPrefix = DEFNODE("UnaryPrefix", null, {
|
|
|
|
|
|
|
|
|
|
}, AST_Unary);
|
|
|
|
|
|
|
|
|
|
var AST_UnaryPostfix = DEFNODE("UnaryPostfix", null, {
|
|
|
|
|
|
|
|
|
|
}, AST_Unary);
|
|
|
|
|
|
|
|
|
|
var AST_Binary = DEFNODE("Binary", "left operator right", {
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
this.left.walk(w);
|
|
|
|
|
this.right.walk(w);
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var AST_Conditional = DEFNODE("Conditional", "condition consequent alternative", {
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
this.condition.walk(w);
|
|
|
|
|
this.consequent.walk(w);
|
|
|
|
|
this.alternative.walk(w);
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
});
|
|
|
|
|
|
2012-05-27 14:25:31 +00:00
|
|
|
var AST_Assign = DEFNODE("Assign", null, {
|
2012-05-27 11:09:01 +00:00
|
|
|
|
2012-05-27 14:25:31 +00:00
|
|
|
}, AST_Binary);
|
2012-05-27 11:09:01 +00:00
|
|
|
|
|
|
|
|
/* -----[ LITERALS ]----- */
|
|
|
|
|
|
|
|
|
|
var AST_Array = DEFNODE("Array", "elements", {
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
this.elements.forEach(function(el){
|
|
|
|
|
el.walk(w);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var AST_Object = DEFNODE("Object", "properties", {
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
this.properties.forEach(function(prop){
|
|
|
|
|
prop.walk(w);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var AST_ObjectProperty = DEFNODE("ObjectProperty");
|
|
|
|
|
|
|
|
|
|
var AST_ObjectKeyVal = DEFNODE("ObjectKeyval", "key value", {
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
this.value.walk(w);
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
}, AST_ObjectProperty);
|
|
|
|
|
|
|
|
|
|
var AST_ObjectSetter = DEFNODE("ObjectSetter", "name func", {
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
this.func.walk(w);
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
}, AST_ObjectProperty);
|
|
|
|
|
|
|
|
|
|
var AST_ObjectGetter = DEFNODE("ObjectGetter", "name func", {
|
2012-05-27 14:25:31 +00:00
|
|
|
walk: function(w) {
|
|
|
|
|
w._visit(this, function(){
|
|
|
|
|
this.func.walk(w);
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
}, AST_ObjectProperty);
|
|
|
|
|
|
|
|
|
|
var AST_Symbol = DEFNODE("Symbol", "name", {
|
2012-05-27 14:25:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var AST_This = DEFNODE("This", null, {
|
|
|
|
|
|
|
|
|
|
}, AST_Symbol);
|
|
|
|
|
|
|
|
|
|
var AST_SymbolRef = DEFNODE("SymbolRef", "scope symbol", {
|
|
|
|
|
|
|
|
|
|
}, AST_Symbol);
|
2012-05-27 11:09:01 +00:00
|
|
|
|
2012-05-27 14:25:31 +00:00
|
|
|
var AST_Label = DEFNODE("Label", null, {
|
|
|
|
|
|
|
|
|
|
}, AST_SymbolRef);
|
|
|
|
|
|
|
|
|
|
var AST_Constant = DEFNODE("Constant", null, {
|
|
|
|
|
getValue: function() {
|
|
|
|
|
return this.value;
|
|
|
|
|
}
|
2012-05-27 11:09:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var AST_String = DEFNODE("String", "value", {
|
|
|
|
|
|
2012-05-27 14:25:31 +00:00
|
|
|
}, AST_Constant);
|
2012-05-27 11:09:01 +00:00
|
|
|
|
|
|
|
|
var AST_Number = DEFNODE("Number", "value", {
|
|
|
|
|
|
2012-05-27 14:25:31 +00:00
|
|
|
}, AST_Constant);
|
2012-05-27 11:09:01 +00:00
|
|
|
|
2012-05-27 14:25:31 +00:00
|
|
|
var AST_RegExp = DEFNODE("Regexp", "pattern mods", {
|
2012-08-15 10:32:37 +00:00
|
|
|
initialize: function() {
|
|
|
|
|
this.value = new RegExp(this.pattern, this.mods);
|
2012-05-27 14:25:31 +00:00
|
|
|
}
|
|
|
|
|
}, AST_Constant);
|
2012-05-27 11:09:01 +00:00
|
|
|
|
|
|
|
|
var AST_Atom = DEFNODE("Atom", null, {
|
|
|
|
|
|
2012-05-27 14:25:31 +00:00
|
|
|
}, AST_Constant);
|
2012-05-27 11:09:01 +00:00
|
|
|
|
|
|
|
|
var AST_Null = DEFNODE("Null", null, {
|
2012-08-15 10:32:37 +00:00
|
|
|
value: null
|
2012-05-27 11:09:01 +00:00
|
|
|
}, AST_Atom);
|
|
|
|
|
|
|
|
|
|
var AST_Undefined = DEFNODE("Undefined", null, {
|
2012-08-15 10:32:37 +00:00
|
|
|
value: (function(){}())
|
2012-05-27 11:09:01 +00:00
|
|
|
}, AST_Atom);
|
|
|
|
|
|
|
|
|
|
var AST_False = DEFNODE("False", null, {
|
2012-08-15 10:32:37 +00:00
|
|
|
value: false
|
2012-05-27 11:09:01 +00:00
|
|
|
}, AST_Atom);
|
|
|
|
|
|
|
|
|
|
var AST_True = DEFNODE("True", null, {
|
2012-08-15 10:32:37 +00:00
|
|
|
value: true
|
2012-05-27 11:09:01 +00:00
|
|
|
}, AST_Atom);
|
2012-08-15 10:32:37 +00:00
|
|
|
|
|
|
|
|
/* -----[ Walker ]----- */
|
|
|
|
|
|
|
|
|
|
function TreeWalker(visitor) {
|
|
|
|
|
this.stack = [];
|
|
|
|
|
if (visitor) this.visit = visitor;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
TreeWalker.prototype = {
|
|
|
|
|
visit: function(node){},
|
|
|
|
|
parent: function(n) {
|
|
|
|
|
if (n == null) n = 1;
|
|
|
|
|
return this.stack[this.stack.length - n];
|
|
|
|
|
},
|
|
|
|
|
find_parent: function(type) {
|
|
|
|
|
for (var a = this.stack, i = a.length; --i >= 0;)
|
|
|
|
|
if (a[i] instanceof type) return a[i];
|
|
|
|
|
return null;
|
|
|
|
|
},
|
|
|
|
|
_visit: function(node, descend) {
|
|
|
|
|
this.visit(node);
|
|
|
|
|
if (descend) {
|
|
|
|
|
this.stack.push(node);
|
|
|
|
|
descend.call(node);
|
|
|
|
|
this.stack.pop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|