From a9b6f9909a331740cae4a37f1fc6bb40801674fe Mon Sep 17 00:00:00 2001 From: Mihai Bazon Date: Thu, 4 Oct 2012 17:28:35 +0300 Subject: use the appropriate constructor for symbols --- lib/mozilla-ast.js | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/lib/mozilla-ast.js b/lib/mozilla-ast.js index 6bc33953..41cf13ce 100644 --- a/lib/mozilla-ast.js +++ b/lib/mozilla-ast.js @@ -69,18 +69,21 @@ end : my_end_token(M), properties : M.properties.map(function(prop){ var key = prop.key; + var name = key.type == "Identifier" ? key.name : key.value; var args = { start : my_start_token(key), end : my_end_token(prop.value), - key : key.type == "Identifier" ? key.name : key.value, + key : name, value : from_moz(prop.value) }; switch (prop.kind) { case "init": return new AST_ObjectKeyVal(args); case "set": + args.value.name = from_moz(key); return new AST_ObjectSetter(args); case "get": + args.value.name = from_moz(key); return new AST_ObjectGetter(args); } }) @@ -129,7 +132,22 @@ } }, UnaryExpression: From_Moz_Unary, - UpdateExpression: From_Moz_Unary + UpdateExpression: From_Moz_Unary, + Identifier: function(M) { + var p = FROM_MOZ_STACK[FROM_MOZ_STACK.length - 2]; + return new (M.name == "this" ? AST_This + : p.type == "LabeledStatement" ? AST_Label + : p.type == "VariableDeclarator" && p.id === M ? (p.kind == "const" ? AST_SymbolConst : AST_SymbolVar) + : p.type == "FunctionExpression" ? (p.id === M ? AST_SymbolLambda : AST_SymbolFunarg) + : p.type == "FunctionDeclaration" ? (p.id === M ? AST_SymbolDefun : AST_SymbolFunarg) + : p.type == "CatchClause" ? AST_SymbolCatch + : p.type == "BreakStatement" || p.type == "ContinueStatement" ? AST_LabelRef + : AST_SymbolRef)({ + start : my_start_token(M), + end : my_end_token(M), + name : M.name + }); + } }; function From_Moz_Unary(M) { @@ -175,7 +193,6 @@ 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 ]----- */ @@ -233,10 +250,21 @@ return MOZ_TO_ME[moztype] = moz_to_me; }; + var FROM_MOZ_STACK = null; + function from_moz(node) { - return node != null ? MOZ_TO_ME[node.type](node) : null; + FROM_MOZ_STACK.push(node); + var ret = node != null ? MOZ_TO_ME[node.type](node) : null; + FROM_MOZ_STACK.pop(); + return ret; }; - AST_Node.from_mozilla_ast = from_moz; + AST_Node.from_mozilla_ast = function(node){ + var save_stack = FROM_MOZ_STACK; + FROM_MOZ_STACK = []; + var ast = from_moz(node); + FROM_MOZ_STACK = save_stack; + return ast; + }; })(); -- cgit v1.2.3