aboutsummaryrefslogtreecommitdiff
path: root/lib/mozilla-ast.js
diff options
context:
space:
mode:
authorMihai Bazon <mihai@bazon.net>2012-10-03 21:39:47 +0300
committerMihai Bazon <mihai@bazon.net>2012-10-03 21:39:47 +0300
commitf20c25188297d8a18e81c01af10986b23d90671c (patch)
tree49654f5199316a308de783d7666bfad62bcc1cbf /lib/mozilla-ast.js
parent2bd8a118c273853848e4231c98f833757dda18b0 (diff)
downloadtracifyjs-f20c25188297d8a18e81c01af10986b23d90671c.tar.gz
tracifyjs-f20c25188297d8a18e81c01af10986b23d90671c.zip
moving code around
Diffstat (limited to 'lib/mozilla-ast.js')
-rw-r--r--lib/mozilla-ast.js267
1 files changed, 132 insertions, 135 deletions
diff --git a/lib/mozilla-ast.js b/lib/mozilla-ast.js
index 34f3fa5f..6bc33953 100644
--- a/lib/mozilla-ast.js
+++ b/lib/mozilla-ast.js
@@ -45,9 +45,140 @@
(function(){
- var MOZ_TO_ME = {};
+ var MOZ_TO_ME = {
+ TryStatement : function(M) {
+ return new AST_Try({
+ start : my_start_token(M),
+ end : my_end_token(M),
+ body : from_moz(M.block).body,
+ bcatch : from_moz(M.handlers[0]),
+ bfinally : M.finalizer ? new AST_Finally(from_moz(M.finalizer)) : null
+ });
+ },
+ CatchClause : function(M) {
+ return new AST_Catch({
+ start : my_start_token(M),
+ end : my_start_token(M),
+ argname : from_moz(M.param),
+ body : from_moz(M.body).body
+ });
+ },
+ ObjectExpression : function(M) {
+ return new AST_Object({
+ start : my_start_token(M),
+ end : my_end_token(M),
+ properties : M.properties.map(function(prop){
+ var key = prop.key;
+ var args = {
+ start : my_start_token(key),
+ end : my_end_token(prop.value),
+ key : key.type == "Identifier" ? key.name : key.value,
+ value : from_moz(prop.value)
+ };
+ switch (prop.kind) {
+ case "init":
+ return new AST_ObjectKeyVal(args);
+ case "set":
+ return new AST_ObjectSetter(args);
+ case "get":
+ return new AST_ObjectGetter(args);
+ }
+ })
+ });
+ },
+ SequenceExpression : function(M) {
+ return AST_Seq.from_array(M.expressions.map(from_moz));
+ },
+ MemberExpression : function(M) {
+ return new (M.computed ? AST_Sub : AST_Dot)({
+ start : my_start_token(M),
+ end : my_start_token(M),
+ property : M.computed ? from_moz(M.property) : M.property.name,
+ expression : from_moz(M.object)
+ });
+ },
+ SwitchCase : function(M) {
+ return new (M.test ? AST_Case : AST_Default)({
+ start : my_start_token(M),
+ end : my_start_token(M),
+ expression : from_moz(M.test),
+ body : M.consequent.map(from_moz)
+ });
+ },
+ Literal : function(M) {
+ var val = M.value, args = {
+ start : my_start_token(M),
+ end : my_end_token(M)
+ };
+ if (val === null) return new AST_Null(args);
+ switch (typeof val) {
+ case "string":
+ args.value = val;
+ return new AST_String(args);
+ case "number":
+ args.value = val;
+ return new AST_Number(args);
+ case "boolean":
+ return new (val ? AST_True : AST_False)(args);
+ default:
+ args.value = val;
+ var m = /\/(.*)\/(.*)/.exec(val+"");
+ args.pattern = m[1];
+ args.mods = m[2];
+ return new AST_RegExp(args);
+ }
+ },
+ UnaryExpression: From_Moz_Unary,
+ UpdateExpression: From_Moz_Unary
+ };
+
+ function From_Moz_Unary(M) {
+ return new (M.prefix ? AST_UnaryPrefix : AST_UnaryPostfix)({
+ start : my_start_token(M),
+ end : my_end_token(M),
+ operator : M.operator,
+ expression : from_moz(M.argument)
+ })
+ };
+
var ME_TO_MOZ = {};
+ map("Node", AST_Node);
+ map("Program", AST_Toplevel, "body@body");
+ map("Function", AST_Function, "id>name, params@argnames, body%body");
+ map("EmptyStatement", AST_EmptyStatement);
+ map("BlockStatement", AST_BlockStatement, "body@body");
+ map("ExpressionStatement", AST_SimpleStatement, "expression>body");
+ map("IfStatement", AST_If, "test>condition, consequent>body, alternate>alternative");
+ map("LabeledStatement", AST_LabeledStatement, "label>label, body>body");
+ map("BreakStatement", AST_Break, "label>label");
+ map("ContinueStatement", AST_Continue, "label>label");
+ map("WithStatement", AST_With, "object>expression, body>body");
+ map("SwitchStatement", AST_Switch, "discriminant>expression, cases@body");
+ map("ReturnStatement", AST_Return, "argument>value");
+ map("ThrowStatement", AST_Throw, "argument>value");
+ map("WhileStatement", AST_While, "test>condition, body>body");
+ map("DoWhileStatement", AST_Do, "test>condition, body>body");
+ map("ForStatement", AST_For, "init>init, test>condition, update>step, body>body");
+ map("ForInStatement", AST_ForIn, "left>init, right>object, body>body");
+ map("DebuggerStatement", AST_Debugger);
+ map("FunctionDeclaration", AST_Defun, "id>name, params@argnames, body%body");
+ map("VariableDeclaration", AST_Var, "declarations@definitions");
+ map("VariableDeclarator", AST_VarDef, "id>name, init>value");
+
+ map("ThisExpression", AST_This);
+ map("ArrayExpression", AST_Array, "elements@elements");
+ map("FunctionExpression", AST_Function, "id>name, params@argnames, body%body");
+ map("BinaryExpression", AST_Binary, "operator=operator, left>left, right>right");
+ map("AssignmentExpression", AST_Assign, "operator=operator, left>left, right>right");
+ map("LogicalExpression", AST_Binary, "operator=operator, left>left, right>right");
+ map("ConditionalExpression", AST_Conditional, "test>condition, consequent>consequent, alternate>alternative");
+ map("NewExpression", AST_New, "callee>expression, arguments@args");
+ map("CallExpression", AST_Call, "callee>expression, arguments@args");
+ map("Identifier", AST_Symbol, "name=name");
+
+ /* -----[ tools ]----- */
+
function my_start_token(moznode) {
return new AST_Token({
file : moznode.loc.start.source,
@@ -102,140 +233,6 @@
return MOZ_TO_ME[moztype] = moz_to_me;
};
- map("Node", AST_Node);
- map("Program", AST_Toplevel, "body@body");
- map("Function", AST_Function, "id>name, params@argnames, body%body");
- map("EmptyStatement", AST_EmptyStatement);
- map("BlockStatement", AST_BlockStatement, "body@body");
- map("ExpressionStatement", AST_SimpleStatement, "expression>body");
- map("IfStatement", AST_If, "test>condition, consequent>body, alternate>alternative");
- map("LabeledStatement", AST_LabeledStatement, "label>label, body>body");
- map("BreakStatement", AST_Break, "label>label");
- map("ContinueStatement", AST_Continue, "label>label");
- map("WithStatement", AST_With, "object>expression, body>body");
- map("SwitchStatement", AST_Switch, "discriminant>expression, cases@body");
- map("ReturnStatement", AST_Return, "argument>value");
- map("ThrowStatement", AST_Throw, "argument>value");
- map("WhileStatement", AST_While, "test>condition, body>body");
- map("DoWhileStatement", AST_Do, "test>condition, body>body");
- map("ForStatement", AST_For, "init>init, test>condition, update>step, body>body");
- map("ForInStatement", AST_ForIn, "left>init, right>object, body>body");
- map("DebuggerStatement", AST_Debugger);
- map("FunctionDeclaration", AST_Defun, "id>name, params@argnames, body%body");
- map("VariableDeclaration", AST_Var, "declarations@definitions");
- map("VariableDeclarator", AST_VarDef, "id>name, init>value");
-
- map("ThisExpression", AST_This);
- map("ArrayExpression", AST_Array, "elements@elements");
- map("FunctionExpression", AST_Function, "id>name, params@argnames, body%body");
- map("BinaryExpression", AST_Binary, "operator=operator, left>left, right>right");
- map("AssignmentExpression", AST_Assign, "operator=operator, left>left, right>right");
- map("LogicalExpression", AST_Binary, "operator=operator, left>left, right>right");
- map("ConditionalExpression", AST_Conditional, "test>condition, consequent>consequent, alternate>alternative");
- map("NewExpression", AST_New, "callee>expression, arguments@args");
- map("CallExpression", AST_Call, "callee>expression, arguments@args");
- map("Identifier", AST_Symbol, "name=name");
-
- /* -----[ stuff our little macro cannot handle ]----- */
-
- MOZ_TO_ME.TryStatement = function(M) {
- return new AST_Try({
- start : my_start_token(M),
- end : my_end_token(M),
- body : from_moz(M.block).body,
- bcatch : from_moz(M.handlers[0]),
- bfinally : M.finalizer ? new AST_Finally(from_moz(M.finalizer)) : null
- });
- };
-
- MOZ_TO_ME.CatchClause = function(M) {
- return new AST_Catch({
- start : my_start_token(M),
- end : my_start_token(M),
- argname : from_moz(M.param),
- body : from_moz(M.body).body
- });
- };
-
- MOZ_TO_ME.ObjectExpression = function(M) {
- return new AST_Object({
- start : my_start_token(M),
- end : my_end_token(M),
- properties : M.properties.map(function(prop){
- var key = prop.key;
- var args = {
- start : my_start_token(key),
- end : my_end_token(prop.value),
- key : key.type == "Identifier" ? key.name : key.value,
- value : from_moz(prop.value)
- };
- switch (prop.kind) {
- case "init":
- return new AST_ObjectKeyVal(args);
- case "set":
- return new AST_ObjectSetter(args);
- case "get":
- return new AST_ObjectGetter(args);
- }
- })
- });
- };
-
- MOZ_TO_ME.SequenceExpression = function(M) {
- return AST_Seq.from_array(M.expressions.map(from_moz));
- };
-
- MOZ_TO_ME.UnaryExpression = MOZ_TO_ME.UpdateExpression = function(M) {
- return new (M.prefix ? AST_UnaryPrefix : AST_UnaryPostfix)({
- start : my_start_token(M),
- end : my_end_token(M),
- operator : M.operator,
- expression : from_moz(M.argument)
- })
- };
-
- MOZ_TO_ME.MemberExpression = function(M) {
- return new (M.computed ? AST_Sub : AST_Dot)({
- start : my_start_token(M),
- end : my_start_token(M),
- property : M.computed ? from_moz(M.property) : M.property.name,
- expression : from_moz(M.object)
- });
- };
-
- MOZ_TO_ME.SwitchCase = function(M) {
- return new (M.test ? AST_Case : AST_Default)({
- start : my_start_token(M),
- end : my_start_token(M),
- expression : from_moz(M.test),
- body : M.consequent.map(from_moz)
- });
- };
-
- MOZ_TO_ME.Literal = function(M) {
- var val = M.value, args = {
- start : my_start_token(M),
- end : my_end_token(M)
- };
- if (val === null) return new AST_Null(args);
- switch (typeof val) {
- case "string":
- args.value = val;
- return new AST_String(args);
- case "number":
- args.value = val;
- return new AST_Number(args);
- case "boolean":
- return new (val ? AST_True : AST_False)(args);
- default:
- args.value = val;
- var m = /\/(.*)\/(.*)/.exec(val+"");
- args.pattern = m[1];
- args.mods = m[2];
- return new AST_RegExp(args);
- }
- };
-
function from_moz(node) {
return node != null ? MOZ_TO_ME[node.type](node) : null;
};