return new this.CTOR(this);
},
$documentation: "Base class of all AST nodes",
+ $propdoc: {
+ $self: "[AST_Node] Reference to $self. Not modified by clone(). XXX: this could be removed.",
+ 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);
},
var AST_Directive = DEFNODE("Directive", "value scope", {
$documentation: "Represents a directive, like \"use strict\";",
+ $propdoc: {
+ value: "[string] The value of this directive as a plain string (it's not an AST_String!)",
+ scope: "[AST_Scope/S] The scope that this directive affects"
+ },
}, 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);
var AST_Block = DEFNODE("Block", "body", {
$documentation: "A body of statements (usually bracketed)",
+ $propdoc: {
+ body: "[AST_Statement*] an array of statements"
+ },
_walk: function(visitor) {
return visitor._visit(this, function(){
walk_body(this, visitor);
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"
+ },
_walk: function(visitor) {
return visitor._visit(this, function(){
this.body._walk(visitor);
var AST_LabeledStatement = DEFNODE("LabeledStatement", "label", {
$documentation: "Statement with a label",
+ $propdoc: {
+ label: "[AST_Label] a label definition"
+ },
_walk: function(visitor) {
return visitor._visit(this, function(){
this.label._walk(visitor);
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"
+ },
_walk: function(visitor) {
return visitor._visit(this, function(){
this.condition._walk(visitor);
}, AST_StatementWithBody);
var AST_Do = DEFNODE("Do", null, {
- $documentation: "A `do` statement"
+ $documentation: "A `do` statement",
}, AST_DWLoop);
var AST_While = DEFNODE("While", null, {
- $documentation: "A `while` statement"
+ $documentation: "A `while` statement",
}, AST_DWLoop);
var AST_For = DEFNODE("For", "init condition step", {
$documentation: "A `for` statement",
+ $propdoc: {
+ init: "[AST_Node?] the `for` initialization code, or null if empty",
+ 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);
var AST_ForIn = DEFNODE("ForIn", "init name object", {
$documentation: "A `for ... in` statement",
+ $propdoc: {
+ init: "[AST_Node] the `for/in` initialization code",
+ name: "[AST_SymbolRef?] the loop variable, only if `init` is AST_Var",
+ object: "[AST_Node] the object that we're looping through"
+ },
_walk: function(visitor) {
return visitor._visit(this, function(){
this.init._walk(visitor);
var AST_With = DEFNODE("With", "expression", {
$documentation: "A `with` statement",
+ $propdoc: {
+ expression: "[AST_Node] the `with` expression"
+ },
_walk: function(visitor) {
return visitor._visit(this, function(){
this.expression._walk(visitor);
var AST_Scope = DEFNODE("Scope", "directives variables functions uses_with uses_eval parent_scope enclosed cname", {
$documentation: "Base class for all statements introducing a lexical scope",
+ $propdoc: {
+ directives: "[string*/S] an array of directives declared in this scope",
+ variables: "[Object/S] a map of name -> SymbolDef for all variables/functions defined in this scope",
+ functions: "[Object/S] like `variables`, but only lists function declarations",
+ uses_with: "[boolean/S] tells whether this scope uses the `with` statement",
+ uses_eval: "[boolean/S] tells whether this scope contains a direct call to the global `eval`",
+ parent_scope: "[AST_Scope?/S] link to the parent scope",
+ enclosed: "[SymbolDef*/S] a list of all symbol definitions that are accessed from this scope or any subscopes",
+ cname: "[integer/S] current index for mangling variables (used internally by the mangler)",
+ },
}, AST_Block);
var AST_Toplevel = DEFNODE("Toplevel", "globals", {
$documentation: "The toplevel scope",
+ $propdoc: {
+ globals: "[Object/S] a map of name -> SymbolDef for all undeclared names",
+ },
wrap_commonjs: function(name, export_all) {
var self = this;
if (export_all) {
var AST_Lambda = DEFNODE("Lambda", "name argnames uses_arguments", {
$documentation: "Base class for functions",
+ $propdoc: {
+ name: "[AST_SymbolDeclaration?] the name of this function",
+ 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);
var AST_Exit = DEFNODE("Exit", "value", {
$documentation: "Base class for “exits” (`return` and `throw`)",
+ $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);
$documentation: "A `throw` statement"
}, AST_Exit);
-var AST_LoopControl = DEFNODE("LoopControl", "label loopcontrol_target", {
+var AST_LoopControl = DEFNODE("LoopControl", "label", {
$documentation: "Base class for loop control statements (`break` and `continue`)",
+ $propdoc: {
+ label: "[AST_LabelRef?] the label, or null if none",
+ },
_walk: function(visitor) {
return visitor._visit(this, this.label && function(){
this.label._walk(visitor);
var AST_If = DEFNODE("If", "condition alternative", {
$documentation: "A `if` statement",
+ $propdoc: {
+ 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);
/* -----[ SWITCH ]----- */
-var AST_Switch = DEFNODE("Switch", "body expression", {
+var AST_Switch = DEFNODE("Switch", "expression", {
$documentation: "A `switch` statement",
+ $propdoc: {
+ expression: "[AST_Node] the `switch` “discriminant”"
+ },
_walk: function(visitor) {
return visitor._visit(this, function(){
this.expression._walk(visitor);
walk_body(this, visitor);
});
}
-}, AST_Statement);
+}, AST_Block);
var AST_SwitchBranch = DEFNODE("SwitchBranch", null, {
$documentation: "Base class for `switch` branches",
var AST_Case = DEFNODE("Case", "expression", {
$documentation: "A `case` switch branch",
+ $propdoc: {
+ expression: "[AST_Node] the `case` expression"
+ },
_walk: function(visitor) {
return visitor._visit(this, function(){
this.expression._walk(visitor);
var AST_Try = DEFNODE("Try", "bcatch bfinally", {
$documentation: "A `try` statement",
+ $propdoc: {
+ 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);
// AST_Scope.
var AST_Catch = DEFNODE("Catch", "argname", {
$documentation: "A `catch` node; only makes sense as part of a `try` statement",
+ $propdoc: {
+ argname: "[AST_SymbolCatch] symbol for the exception"
+ },
_walk: function(visitor) {
return visitor._visit(this, function(){
this.argname._walk(visitor);
var AST_Definitions = DEFNODE("Definitions", "definitions", {
$documentation: "Base class for `var` or `const` nodes (variable declarations/initializations)",
+ $propdoc: {
+ definitions: "[AST_VarDef*] array of variable definitions"
+ },
_walk: function(visitor) {
return visitor._visit(this, function(){
this.definitions.forEach(function(def){
var AST_VarDef = DEFNODE("VarDef", "name value", {
$documentation: "A variable declaration; only appears in a AST_Definitions node",
+ $propdoc: {
+ name: "[AST_SymbolVar|AST_SymbolConst] 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);
var AST_Call = DEFNODE("Call", "expression args", {
$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);
var AST_Seq = DEFNODE("Seq", "car cdr", {
$documentation: "A sequence expression (two comma-separated expressions)",
+ $propdoc: {
+ car: "[AST_Node] first element in sequence",
+ cdr: "[AST_Node] second element in sequence"
+ },
$cons: function(x, y) {
var seq = new AST_Seq(x);
seq.car = x;
});
var AST_PropAccess = DEFNODE("PropAccess", "expression property", {
- $documentation: "Base class for property access expressions, i.e. `a.foo` or `a[\"foo\"]`"
+ $documentation: "Base class for property access expressions, i.e. `a.foo` or `a[\"foo\"]`",
+ $propdoc: {
+ expression: "[AST_Node] the “container” expression",
+ property: "[AST_Node|string] the property to access. For AST_Dot this is always a plain string, while for AST_Sub it's an arbitrary AST_Node"
+ }
});
var AST_Dot = DEFNODE("Dot", null, {
var AST_Unary = DEFNODE("Unary", "operator expression", {
$documentation: "Base class for unary expressions",
+ $propdoc: {
+ 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);
var AST_Binary = DEFNODE("Binary", "left operator right", {
$documentation: "Binary expression, i.e. `a + b`",
+ $propdoc: {
+ left: "[AST_Node] left-hand side expression",
+ operator: "[string] the operator",
+ right: "[AST_Node] right-hand side expression"
+ },
_walk: function(visitor) {
return visitor._visit(this, function(){
this.left._walk(visitor);
var AST_Conditional = DEFNODE("Conditional", "condition consequent alternative", {
$documentation: "Conditional expression using the ternary operator, i.e. `a ? b : c`",
+ $propdoc: {
+ condition: "[AST_Node]",
+ consequent: "[AST_Node]",
+ alternative: "[AST_Node]"
+ },
_walk: function(visitor) {
return visitor._visit(this, function(){
this.condition._walk(visitor);
}
});
-var AST_Assign = DEFNODE("Assign", "left operator right", {
+var AST_Assign = DEFNODE("Assign", null, {
$documentation: "An assignment expression — `a = b + 5`",
}, AST_Binary);
var AST_Array = DEFNODE("Array", "elements", {
$documentation: "An array literal",
+ $propdoc: {
+ elements: "[AST_Node*] array of elements"
+ },
_walk: function(visitor) {
return visitor._visit(this, function(){
this.elements.forEach(function(el){
var AST_Object = DEFNODE("Object", "properties", {
$documentation: "An object literal",
+ $propdoc: {
+ properties: "[AST_ObjectProperty*] array of properties"
+ },
_walk: function(visitor) {
return visitor._visit(this, function(){
this.properties.forEach(function(prop){
var AST_ObjectProperty = DEFNODE("ObjectProperty", "key value", {
$documentation: "Base class for literal object properties",
+ $propdoc: {
+ key: "[string] the property name; it's always a plain string in our AST, no matter if it was a string, number or identifier in original code",
+ value: "[AST_Node] property value. For setters and getters this is an AST_Function."
+ },
_walk: function(visitor) {
return visitor._visit(this, function(){
this.value._walk(visitor);
}, AST_ObjectProperty);
var AST_Symbol = DEFNODE("Symbol", "scope name thedef", {
+ $propdoc: {
+ name: "[string] name of this symbol",
+ scope: "[AST_Scope/S] the current scope (not necessarily the definition scope)",
+ thedef: "[SymbolDef/S] the definition of this symbol"
+ },
$documentation: "Base class for all symbols",
});
var AST_SymbolDeclaration = DEFNODE("SymbolDeclaration", "init", {
$documentation: "A declaration symbol (symbol in var/const, function name or argument, symbol in catch)",
+ $propdoc: {
+ init: "[AST_Node*/S] array of initializers for this declaration."
+ }
}, AST_Symbol);
var AST_SymbolVar = DEFNODE("SymbolVar", null, {
$documentation: "Symbol naming the exception in catch",
}, AST_SymbolDeclaration);
-var AST_Label = DEFNODE("Label", "references label_target", {
+var AST_Label = DEFNODE("Label", "references", {
$documentation: "Symbol naming a label (declaration)",
+ $propdoc: {
+ references: "[AST_LabelRef*] a list of nodes referring to this label"
+ }
}, AST_SymbolDeclaration);
var AST_SymbolRef = DEFNODE("SymbolRef", null, {
var AST_String = DEFNODE("String", "value", {
$documentation: "A string literal",
+ $propdoc: {
+ value: "[string] the contents of this string"
+ }
}, AST_Constant);
var AST_Number = DEFNODE("Number", "value", {
$documentation: "A number literal",
+ $propdoc: {
+ value: "[number] the numeric value"
+ }
}, AST_Constant);
-var AST_RegExp = DEFNODE("Regexp", "pattern mods", {
+var AST_RegExp = DEFNODE("RegExp", "value", {
$documentation: "A regexp literal",
- initialize: function() {
- this.value = new RegExp(this.pattern, this.mods);
+ $propdoc: {
+ value: "[RegExp] the actual regexp"
}
}, AST_Constant);
self = p;
}
},
+ loopcontrol_target: function(label) {
+ var stack = this.stack;
+ if (label) {
+ for (var i = stack.length; --i >= 0;) {
+ var x = stack[i];
+ if (x instanceof AST_LabeledStatement && x.label.name == label.name) {
+ return x.body.$self;
+ }
+ }
+ } else {
+ for (var i = stack.length; --i >= 0;) {
+ var x = stack[i];
+ if (x instanceof AST_Switch) return x.$self;
+ if (x instanceof AST_For || x instanceof AST_ForIn || x instanceof AST_DWLoop) {
+ return (x.body instanceof AST_BlockStatement ? x.body : x).$self;
+ }
+ }
+ }
+ }
};