aboutsummaryrefslogtreecommitdiff
path: root/lib/ast.js
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2021-02-07 22:44:20 +0000
committerGitHub <noreply@github.com>2021-02-08 06:44:20 +0800
commitfd4caf7a9cbfaa482d8cc77d2bc8cd00e8bc256a (patch)
tree75e5f2eedf3dbf809d76e419ab03d4dc62363a79 /lib/ast.js
parentc44b6399c30b043f9bf687ce9b44458abc211d80 (diff)
downloadtracifyjs-fd4caf7a9cbfaa482d8cc77d2bc8cd00e8bc256a.tar.gz
tracifyjs-fd4caf7a9cbfaa482d8cc77d2bc8cd00e8bc256a.zip
support generator functions (#4620)
Diffstat (limited to 'lib/ast.js')
-rw-r--r--lib/ast.js182
1 files changed, 146 insertions, 36 deletions
diff --git a/lib/ast.js b/lib/ast.js
index ead2c7d5..ea823b28 100644
--- a/lib/ast.js
+++ b/lib/ast.js
@@ -113,7 +113,9 @@ var AST_Node = DEFNODE("Node", "start end", {
walk: function(visitor) {
visitor.visit(this);
},
- _validate: noop,
+ _validate: function() {
+ if (this.TYPE == "Node") throw new Error("should not instantiate AST_Node");
+ },
validate: function() {
var ctor = this.CTOR;
do {
@@ -187,6 +189,9 @@ AST_Node.disable_validation = function() {
var AST_Statement = DEFNODE("Statement", null, {
$documentation: "Base class of all statements",
+ _validate: function() {
+ if (this.TYPE == "Statement") throw new Error("should not instantiate AST_Statement");
+ },
});
var AST_Debugger = DEFNODE("Debugger", null, {
@@ -215,7 +220,7 @@ function validate_expression(value, prop, multiple, allow_spread, allow_hole) {
if (value instanceof AST_Destructured) throw new Error(prop + " cannot " + multiple + " AST_Destructured");
if (value instanceof AST_Hole && !allow_hole) throw new Error(prop + " cannot " + multiple + " AST_Hole");
if (value instanceof AST_Spread && !allow_spread) throw new Error(prop + " cannot " + multiple + " AST_Spread");
- if (value instanceof AST_Statement && !is_function(value)) {
+ if (value instanceof AST_Statement && !(value instanceof AST_LambdaExpression)) {
throw new Error(prop + " cannot " + multiple + " AST_Statement");
}
if (value instanceof AST_SymbolDeclaration) {
@@ -265,6 +270,7 @@ var AST_BlockScope = DEFNODE("BlockScope", "enclosed functions make_def parent_s
return this.parent_scope.resolve();
},
_validate: function() {
+ if (this.TYPE == "BlockScope") throw new Error("should not instantiate AST_BlockScope");
if (this.parent_scope == null) return;
if (!(this.parent_scope instanceof AST_BlockScope)) throw new Error("parent_scope must be AST_BlockScope");
if (!(this.resolve() instanceof AST_Scope)) throw new Error("must be contained within AST_Scope");
@@ -289,9 +295,10 @@ var AST_Block = DEFNODE("Block", "body", {
});
},
_validate: function() {
+ if (this.TYPE == "Block") throw new Error("should not instantiate AST_Block");
this.body.forEach(function(node) {
if (!(node instanceof AST_Statement)) throw new Error("body must be AST_Statement[]");
- if (is_function(node)) throw new Error("body cannot contain AST_Function");
+ if (node instanceof AST_LambdaExpression) throw new Error("body cannot contain AST_LambdaExpression");
});
},
}, AST_BlockScope);
@@ -306,8 +313,9 @@ var AST_StatementWithBody = DEFNODE("StatementWithBody", "body", {
body: "[AST_Statement] the body; this should always be present, even if it's an AST_EmptyStatement"
},
_validate: function() {
+ if (this.TYPE == "StatementWithBody") throw new Error("should not instantiate AST_StatementWithBody");
if (!(this.body instanceof AST_Statement)) throw new Error("body must be AST_Statement");
- if (is_function(this.body)) throw new Error("body cannot be AST_Function");
+ if (this.body instanceof AST_LambdaExpression) throw new Error("body cannot be AST_LambdaExpression");
},
}, AST_BlockScope);
@@ -346,7 +354,10 @@ var AST_LabeledStatement = DEFNODE("LabeledStatement", "label", {
}, AST_StatementWithBody);
var AST_IterationStatement = DEFNODE("IterationStatement", null, {
- $documentation: "Internal class. All loops inherit from it."
+ $documentation: "Internal class. All loops inherit from it.",
+ _validate: function() {
+ if (this.TYPE == "IterationStatement") throw new Error("should not instantiate AST_IterationStatement");
+ },
}, AST_StatementWithBody);
var AST_DWLoop = DEFNODE("DWLoop", "condition", {
@@ -355,6 +366,7 @@ var AST_DWLoop = DEFNODE("DWLoop", "condition", {
condition: "[AST_Node] the loop condition. Should not be instanceof AST_Statement"
},
_validate: function() {
+ if (this.TYPE == "DWLoop") throw new Error("should not instantiate AST_DWLoop");
must_be_expression(this, "condition");
},
}, AST_IterationStatement);
@@ -401,7 +413,7 @@ var AST_For = DEFNODE("For", "init condition step", {
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 || is_function(this.init))) {
+ && !(this.init instanceof AST_Definitions || this.init instanceof AST_LambdaExpression)) {
throw new Error("init cannot be AST_Statement");
}
}
@@ -467,6 +479,9 @@ var AST_Scope = DEFNODE("Scope", "uses_eval uses_with", {
return this.uses_eval || this.uses_with;
},
resolve: return_this,
+ _validate: function() {
+ if (this.TYPE == "Scope") throw new Error("should not instantiate AST_Scope");
+ },
}, AST_Block);
var AST_Toplevel = DEFNODE("Toplevel", "globals", {
@@ -517,8 +532,9 @@ var AST_Lambda = DEFNODE("Lambda", "argnames length_read rest uses_arguments", {
$documentation: "Base class for functions",
$propdoc: {
argnames: "[(AST_DefaultValue|AST_Destructured|AST_SymbolFunarg)*] array of function arguments and/or destructured literals",
+ length_read: "[boolean/S] whether length property of this function is accessed",
rest: "[(AST_Destructured|AST_SymbolFunarg)?] rest parameter, or null if absent",
- uses_arguments: "[boolean/S] tells whether this function accesses the arguments array",
+ uses_arguments: "[boolean/S] whether this function accesses the arguments array",
},
each_argname: function(visit) {
var tw = new TreeWalker(function(node) {
@@ -549,6 +565,7 @@ var AST_Lambda = DEFNODE("Lambda", "argnames length_read rest uses_arguments", {
});
},
_validate: function() {
+ if (this.TYPE == "Lambda") throw new Error("should not instantiate AST_Lambda");
this.argnames.forEach(function(node) {
validate_destructured(node, function(node) {
if (!(node instanceof AST_SymbolFunarg)) throw new Error("argnames must be AST_SymbolFunarg[]");
@@ -567,12 +584,33 @@ var AST_Accessor = DEFNODE("Accessor", null, {
},
}, AST_Lambda);
+var AST_LambdaExpression = DEFNODE("LambdaExpression", "inlined", {
+ $documentation: "Base class for function expressions",
+ $propdoc: {
+ inlined: "[boolean/S] whether this function has been inlined",
+ },
+ _validate: function() {
+ if (this.TYPE == "LambdaExpression") throw new Error("should not instantiate AST_LambdaExpression");
+ },
+}, AST_Lambda);
+
function is_arrow(node) {
- return node instanceof AST_AsyncArrow || node instanceof AST_Arrow;
+ return node instanceof AST_Arrow || node instanceof AST_AsyncArrow;
+}
+
+function is_async(node) {
+ return node instanceof AST_AsyncArrow
+ || node instanceof AST_AsyncDefun
+ || node instanceof AST_AsyncFunction
+ || node instanceof AST_AsyncGeneratorDefun
+ || node instanceof AST_AsyncGeneratorFunction;
}
-function is_function(node) {
- return is_arrow(node) || node instanceof AST_AsyncFunction || node instanceof AST_Function;
+function is_generator(node) {
+ return node instanceof AST_AsyncGeneratorDefun
+ || node instanceof AST_AsyncGeneratorFunction
+ || node instanceof AST_GeneratorDefun
+ || node instanceof AST_GeneratorFunction;
}
function walk_lambda(node, tw) {
@@ -583,7 +621,7 @@ function walk_lambda(node, tw) {
}
}
-var AST_Arrow = DEFNODE("Arrow", "inlined value", {
+var AST_Arrow = DEFNODE("Arrow", "value", {
$documentation: "An arrow function expression",
$propdoc: {
value: "[AST_Node?] simple return expression, or null if using function body.",
@@ -610,13 +648,9 @@ var AST_Arrow = DEFNODE("Arrow", "inlined value", {
if (this.body.length) throw new Error("body must be empty if value exists");
}
},
-}, AST_Lambda);
+}, AST_LambdaExpression);
-function is_async(node) {
- return node instanceof AST_AsyncArrow || node instanceof AST_AsyncDefun || node instanceof AST_AsyncFunction;
-}
-
-var AST_AsyncArrow = DEFNODE("AsyncArrow", "inlined value", {
+var AST_AsyncArrow = DEFNODE("AsyncArrow", "value", {
$documentation: "An asynchronous arrow function expression",
$propdoc: {
value: "[AST_Node?] simple return expression, or null if using function body.",
@@ -643,9 +677,9 @@ var AST_AsyncArrow = DEFNODE("AsyncArrow", "inlined value", {
if (this.body.length) throw new Error("body must be empty if value exists");
}
},
-}, AST_Lambda);
+}, AST_LambdaExpression);
-var AST_AsyncFunction = DEFNODE("AsyncFunction", "inlined name", {
+var AST_AsyncFunction = DEFNODE("AsyncFunction", "name", {
$documentation: "An asynchronous function expression",
$propdoc: {
name: "[AST_SymbolLambda?] the name of this function",
@@ -655,10 +689,10 @@ var AST_AsyncFunction = DEFNODE("AsyncFunction", "inlined name", {
if (!(this.name instanceof AST_SymbolLambda)) throw new Error("name must be AST_SymbolLambda");
}
},
-}, AST_Lambda);
+}, AST_LambdaExpression);
-var AST_Function = DEFNODE("Function", "inlined name", {
- $documentation: "A function expression",
+var AST_AsyncGeneratorFunction = DEFNODE("AsyncGeneratorFunction", "name", {
+ $documentation: "An asynchronous generator function expression",
$propdoc: {
name: "[AST_SymbolLambda?] the name of this function",
},
@@ -667,36 +701,67 @@ var AST_Function = DEFNODE("Function", "inlined name", {
if (!(this.name instanceof AST_SymbolLambda)) throw new Error("name must be AST_SymbolLambda");
}
},
-}, AST_Lambda);
+}, AST_LambdaExpression);
-function is_defun(node) {
- return node instanceof AST_AsyncDefun || node instanceof AST_Defun;
-}
+var AST_Function = DEFNODE("Function", "name", {
+ $documentation: "A function expression",
+ $propdoc: {
+ name: "[AST_SymbolLambda?] the name of this function",
+ },
+ _validate: function() {
+ if (this.name != null) {
+ if (!(this.name instanceof AST_SymbolLambda)) throw new Error("name must be AST_SymbolLambda");
+ }
+ },
+}, AST_LambdaExpression);
-var AST_AsyncDefun = DEFNODE("AsyncDefun", "inlined name", {
- $documentation: "An asynchronous function definition",
+var AST_GeneratorFunction = DEFNODE("GeneratorFunction", "name", {
+ $documentation: "A generator function expression",
$propdoc: {
- name: "[AST_SymbolDefun] the name of this function",
+ name: "[AST_SymbolLambda?] the name of this function",
},
_validate: function() {
- if (!(this.name instanceof AST_SymbolDefun)) throw new Error("name must be AST_SymbolDefun");
+ if (this.name != null) {
+ if (!(this.name instanceof AST_SymbolLambda)) throw new Error("name must be AST_SymbolLambda");
+ }
},
-}, AST_Lambda);
+}, AST_LambdaExpression);
-var AST_Defun = DEFNODE("Defun", "inlined name", {
- $documentation: "A function definition",
+var AST_LambdaDefinition = DEFNODE("LambdaDefinition", "inlined name", {
+ $documentation: "Base class for function definitions",
$propdoc: {
+ inlined: "[boolean/S] whether this function has been inlined",
name: "[AST_SymbolDefun] the name of this function",
},
_validate: function() {
+ if (this.TYPE == "LambdaDefinition") throw new Error("should not instantiate AST_LambdaDefinition");
if (!(this.name instanceof AST_SymbolDefun)) throw new Error("name must be AST_SymbolDefun");
},
}, AST_Lambda);
+var AST_AsyncDefun = DEFNODE("AsyncDefun", null, {
+ $documentation: "An asynchronous function definition",
+}, AST_LambdaDefinition);
+
+var AST_AsyncGeneratorDefun = DEFNODE("AsyncGeneratorDefun", null, {
+ $documentation: "An asynchronous generator function definition",
+}, AST_LambdaDefinition);
+
+var AST_Defun = DEFNODE("Defun", null, {
+ $documentation: "A function definition",
+}, AST_LambdaDefinition);
+
+var AST_GeneratorDefun = DEFNODE("GeneratorDefun", null, {
+ $documentation: "A generator function definition",
+}, AST_LambdaDefinition);
+
/* -----[ JUMPS ]----- */
var AST_Jump = DEFNODE("Jump", null, {
- $documentation: "Base class for “jumps” (for now that's `return`, `throw`, `break` and `continue`)"
+ $documentation: "Base class for “jumps” (for now that's `return`, `throw`, `break` and `continue`)",
+ _validate: function() {
+ if (this.TYPE == "Jump") throw new Error("should not instantiate AST_Jump");
+ },
}, AST_Statement);
var AST_Exit = DEFNODE("Exit", "value", {
@@ -709,7 +774,10 @@ var AST_Exit = DEFNODE("Exit", "value", {
visitor.visit(node, function() {
if (node.value) node.value.walk(visitor);
});
- }
+ },
+ _validate: function() {
+ if (this.TYPE == "Exit") throw new Error("should not instantiate AST_Exit");
+ },
}, AST_Jump);
var AST_Return = DEFNODE("Return", null, {
@@ -738,6 +806,7 @@ var AST_LoopControl = DEFNODE("LoopControl", "label", {
});
},
_validate: function() {
+ if (this.TYPE == "LoopControl") throw new Error("should not instantiate AST_LoopControl");
if (this.label != null) {
if (!(this.label instanceof AST_LabelRef)) throw new Error("label must be AST_LabelRef");
}
@@ -772,7 +841,7 @@ var AST_If = DEFNODE("If", "condition alternative", {
must_be_expression(this, "condition");
if (this.alternative != null) {
if (!(this.alternative instanceof AST_Statement)) throw new Error("alternative must be AST_Statement");
- if (is_function(this.alternative)) throw new error("alternative cannot be AST_Function");
+ if (this.alternative instanceof AST_LambdaExpression) throw new error("alternative cannot be AST_LambdaExpression");
}
},
}, AST_StatementWithBody);
@@ -801,6 +870,9 @@ var AST_Switch = DEFNODE("Switch", "expression", {
var AST_SwitchBranch = DEFNODE("SwitchBranch", null, {
$documentation: "Base class for `switch` branches",
+ _validate: function() {
+ if (this.TYPE == "SwitchBranch") throw new Error("should not instantiate AST_SwitchBranch");
+ },
}, AST_Block);
var AST_Default = DEFNODE("Default", null, {
@@ -889,6 +961,7 @@ var AST_Definitions = DEFNODE("Definitions", "definitions", {
});
},
_validate: function() {
+ if (this.TYPE == "Definitions") throw new Error("should not instantiate AST_Definitions");
if (this.definitions.length < 1) throw new Error("must have at least one definition");
},
}, AST_Statement);
@@ -1036,6 +1109,7 @@ var AST_PropAccess = DEFNODE("PropAccess", "expression property", {
return p;
},
_validate: function() {
+ if (this.TYPE == "PropAccess") throw new Error("should not instantiate AST_PropAccess");
must_be_expression(this, "expression");
},
});
@@ -1096,6 +1170,7 @@ var AST_Unary = DEFNODE("Unary", "operator expression", {
});
},
_validate: function() {
+ if (this.TYPE == "Unary") throw new Error("should not instantiate AST_Unary");
if (typeof this.operator != "string") throw new Error("operator must be string");
must_be_expression(this, "expression");
},
@@ -1183,6 +1258,27 @@ var AST_Await = DEFNODE("Await", "expression", {
},
});
+var AST_Yield = DEFNODE("Yield", "expression nested", {
+ $documentation: "A yield expression",
+ $propdoc: {
+ expression: "[AST_Node?] return value for iterator, or null if undefined",
+ nested: "[boolean] whether to iterate over expression as generator",
+ },
+ walk: function(visitor) {
+ var node = this;
+ visitor.visit(node, function() {
+ if (node.expression) node.expression.walk(visitor);
+ });
+ },
+ _validate: function() {
+ if (this.expression != null) {
+ must_be_expression(this, "expression");
+ } else if (this.nested) {
+ throw new Error("yield* must contain expression");
+ }
+ },
+});
+
/* -----[ LITERALS ]----- */
var AST_Array = DEFNODE("Array", "elements", {
@@ -1208,6 +1304,9 @@ var AST_Destructured = DEFNODE("Destructured", "rest", {
$propdoc: {
rest: "[(AST_Destructured|AST_SymbolDeclaration|AST_SymbolRef)?] rest parameter, or null if absent",
},
+ _validate: function() {
+ if (this.TYPE == "Destructured") throw new Error("should not instantiate AST_Destructured");
+ },
});
function validate_destructured(node, check, allow_default) {
@@ -1319,6 +1418,7 @@ var AST_ObjectProperty = DEFNODE("ObjectProperty", "key value", {
});
},
_validate: function() {
+ if (this.TYPE == "ObjectProperty") throw new Error("should not instantiate AST_ObjectProperty");
if (typeof this.key != "string") {
if (!(this.key instanceof AST_Node)) throw new Error("key must be string or AST_Node");
must_be_expression(this, "key");
@@ -1356,6 +1456,7 @@ var AST_Symbol = DEFNODE("Symbol", "scope name thedef", {
thedef: "[SymbolDef/S] the definition of this symbol"
},
_validate: function() {
+ if (this.TYPE == "Symbol") throw new Error("should not instantiate AST_Symbol");
if (typeof this.name != "string") throw new Error("name must be string");
},
});
@@ -1448,6 +1549,9 @@ var AST_Template = DEFNODE("Template", "expressions strings tag", {
var AST_Constant = DEFNODE("Constant", null, {
$documentation: "Base class for all constants",
+ _validate: function() {
+ if (this.TYPE == "Constant") throw new Error("should not instantiate AST_Constant");
+ },
});
var AST_String = DEFNODE("String", "value quote", {
@@ -1496,6 +1600,9 @@ var AST_RegExp = DEFNODE("RegExp", "value", {
var AST_Atom = DEFNODE("Atom", null, {
$documentation: "Base class for atoms",
+ _validate: function() {
+ if (this.TYPE == "Atom") throw new Error("should not instantiate AST_Atom");
+ },
}, AST_Constant);
var AST_Null = DEFNODE("Null", null, {
@@ -1525,6 +1632,9 @@ var AST_Infinity = DEFNODE("Infinity", null, {
var AST_Boolean = DEFNODE("Boolean", null, {
$documentation: "Base class for booleans",
+ _validate: function() {
+ if (this.TYPE == "Boolean") throw new Error("should not instantiate AST_Boolean");
+ },
}, AST_Atom);
var AST_False = DEFNODE("False", null, {