diff options
author | Anthony Van de Gejuchte <anthonyvdgent@gmail.com> | 2016-06-15 12:18:18 +0200 |
---|---|---|
committer | Anthony Van de Gejuchte <anthonyvdgent@gmail.com> | 2016-07-03 12:36:57 +0200 |
commit | 698705a820708ab2203869e1523b041030ad77cb (patch) | |
tree | 3385f130a1bf77919b16b4e4f290aed06c905d44 /lib/mozilla-ast.js | |
parent | debc525fa117438d4971df3790f9f476fed65858 (diff) | |
download | tracifyjs-698705a820708ab2203869e1523b041030ad77cb.tar.gz tracifyjs-698705a820708ab2203869e1523b041030ad77cb.zip |
Don't convert all strings to directives from moz-ast
Diffstat (limited to 'lib/mozilla-ast.js')
-rw-r--r-- | lib/mozilla-ast.js | 81 |
1 files changed, 69 insertions, 12 deletions
diff --git a/lib/mozilla-ast.js b/lib/mozilla-ast.js index 34332215..12b55dc5 100644 --- a/lib/mozilla-ast.js +++ b/lib/mozilla-ast.js @@ -45,20 +45,55 @@ (function(){ - var MOZ_TO_ME = { - ExpressionStatement: function(M) { - var expr = M.expression; - if (expr.type === "Literal" && typeof expr.value === "string") { - return new AST_Directive({ - start: my_start_token(M), - end: my_end_token(M), - value: expr.value + var normalize_directives = function(body) { + var in_directive = true; + + for (var i = 0; i < body.length; i++) { + if (in_directive && body[i] instanceof AST_Statement && body[i].body instanceof AST_String) { + body[i] = new AST_Directive({ + start: body[i].start, + end: body[i].end, + value: body[i].body.value }); + } else if (in_directive && !(body[i] instanceof AST_Statement && body[i].body instanceof AST_String)) { + in_directive = false; } + } + + return body; + }; + + var MOZ_TO_ME = { + Program: function(M) { + return new AST_Toplevel({ + start: my_start_token(M), + end: my_end_token(M), + body: normalize_directives(M.body.map(from_moz)) + }); + }, + FunctionDeclaration: function(M) { + return new AST_Defun({ + start: my_start_token(M), + end: my_end_token(M), + name: from_moz(M.id), + argnames: M.params.map(from_moz), + body: normalize_directives(from_moz(M.body).body) + }); + }, + FunctionExpression: function(M) { + return new AST_Function({ + start: my_start_token(M), + end: my_end_token(M), + name: from_moz(M.id), + argnames: M.params.map(from_moz), + body: normalize_directives(from_moz(M.body).body) + }); + }, + ExpressionStatement: function(M) { return new AST_SimpleStatement({ start: my_start_token(M), end: my_end_token(M), - body: from_moz(expr) + body: from_moz(M.expression) }); }, TryStatement: function(M) { @@ -194,7 +229,6 @@ }); }; - map("Program", AST_Toplevel, "body@body"); map("EmptyStatement", AST_EmptyStatement); map("BlockStatement", AST_BlockStatement, "body@body"); map("IfStatement", AST_If, "test>condition, consequent>body, alternate>alternative"); @@ -210,12 +244,10 @@ 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("VariableDeclarator", AST_VarDef, "id>name, init>value"); map("CatchClause", AST_Catch, "param>argname, body%body"); map("ThisExpression", AST_This); - map("FunctionExpression", AST_Function, "id>name, params@argnames, body%body"); map("BinaryExpression", AST_Binary, "operator=operator, left>left, right>right"); map("LogicalExpression", AST_Binary, "operator=operator, left>left, right>right"); map("AssignmentExpression", AST_Assign, "operator=operator, left>left, right>right"); @@ -223,6 +255,31 @@ map("NewExpression", AST_New, "callee>expression, arguments@args"); map("CallExpression", AST_Call, "callee>expression, arguments@args"); + def_to_moz(AST_Toplevel, function To_Moz_Program(M) { + return { + type: "Program", + body: M.body.map(to_moz) + }; + }); + + def_to_moz(AST_Defun, function To_Moz_FunctionDeclaration(M) { + return { + type: "FunctionDeclaration", + id: to_moz(M.name), + params: M.argnames.map(to_moz), + body: to_moz_block(M) + } + }); + + def_to_moz(AST_Function, function To_Moz_FunctionExpression(M) { + return { + type: "FunctionExpression", + id: to_moz(M.name), + params: M.argnames.map(to_moz), + body: to_moz_block(M) + } + }); + def_to_moz(AST_Directive, function To_Moz_Directive(M) { return { type: "ExpressionStatement", |