react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / handlebars / node_modules / uglify-js / lib / mozilla-ast.js
80713 views/***********************************************************************12A JavaScript tokenizer / parser / beautifier / compressor.3https://github.com/mishoo/UglifyJS245-------------------------------- (C) ---------------------------------67Author: Mihai Bazon8<[email protected]>9http://mihai.bazon.net/blog1011Distributed under the BSD license:1213Copyright 2012 (c) Mihai Bazon <[email protected]>1415Redistribution and use in source and binary forms, with or without16modification, are permitted provided that the following conditions17are met:1819* Redistributions of source code must retain the above20copyright notice, this list of conditions and the following21disclaimer.2223* Redistributions in binary form must reproduce the above24copyright notice, this list of conditions and the following25disclaimer in the documentation and/or other materials26provided with the distribution.2728THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY29EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE30IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR31PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE32LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,33OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,34PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR35PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY36THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR37TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF38THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF39SUCH DAMAGE.4041***********************************************************************/4243"use strict";4445(function(){4647var MOZ_TO_ME = {48TryStatement : function(M) {49return new AST_Try({50start : my_start_token(M),51end : my_end_token(M),52body : from_moz(M.block).body,53bcatch : from_moz(M.handlers[0]),54bfinally : M.finalizer ? new AST_Finally(from_moz(M.finalizer)) : null55});56},57CatchClause : function(M) {58return new AST_Catch({59start : my_start_token(M),60end : my_end_token(M),61argname : from_moz(M.param),62body : from_moz(M.body).body63});64},65ObjectExpression : function(M) {66return new AST_Object({67start : my_start_token(M),68end : my_end_token(M),69properties : M.properties.map(function(prop){70var key = prop.key;71var name = key.type == "Identifier" ? key.name : key.value;72var args = {73start : my_start_token(key),74end : my_end_token(prop.value),75key : name,76value : from_moz(prop.value)77};78switch (prop.kind) {79case "init":80return new AST_ObjectKeyVal(args);81case "set":82args.value.name = from_moz(key);83return new AST_ObjectSetter(args);84case "get":85args.value.name = from_moz(key);86return new AST_ObjectGetter(args);87}88})89});90},91SequenceExpression : function(M) {92return AST_Seq.from_array(M.expressions.map(from_moz));93},94MemberExpression : function(M) {95return new (M.computed ? AST_Sub : AST_Dot)({96start : my_start_token(M),97end : my_end_token(M),98property : M.computed ? from_moz(M.property) : M.property.name,99expression : from_moz(M.object)100});101},102SwitchCase : function(M) {103return new (M.test ? AST_Case : AST_Default)({104start : my_start_token(M),105end : my_end_token(M),106expression : from_moz(M.test),107body : M.consequent.map(from_moz)108});109},110Literal : function(M) {111var val = M.value, args = {112start : my_start_token(M),113end : my_end_token(M)114};115if (val === null) return new AST_Null(args);116switch (typeof val) {117case "string":118args.value = val;119return new AST_String(args);120case "number":121args.value = val;122return new AST_Number(args);123case "boolean":124return new (val ? AST_True : AST_False)(args);125default:126args.value = val;127return new AST_RegExp(args);128}129},130UnaryExpression: From_Moz_Unary,131UpdateExpression: From_Moz_Unary,132Identifier: function(M) {133var p = FROM_MOZ_STACK[FROM_MOZ_STACK.length - 2];134return new (M.name == "this" ? AST_This135: p.type == "LabeledStatement" ? AST_Label136: p.type == "VariableDeclarator" && p.id === M ? (p.kind == "const" ? AST_SymbolConst : AST_SymbolVar)137: p.type == "FunctionExpression" ? (p.id === M ? AST_SymbolLambda : AST_SymbolFunarg)138: p.type == "FunctionDeclaration" ? (p.id === M ? AST_SymbolDefun : AST_SymbolFunarg)139: p.type == "CatchClause" ? AST_SymbolCatch140: p.type == "BreakStatement" || p.type == "ContinueStatement" ? AST_LabelRef141: AST_SymbolRef)({142start : my_start_token(M),143end : my_end_token(M),144name : M.name145});146}147};148149function From_Moz_Unary(M) {150var prefix = "prefix" in M ? M.prefix151: M.type == "UnaryExpression" ? true : false;152return new (prefix ? AST_UnaryPrefix : AST_UnaryPostfix)({153start : my_start_token(M),154end : my_end_token(M),155operator : M.operator,156expression : from_moz(M.argument)157});158};159160var ME_TO_MOZ = {};161162map("Node", AST_Node);163map("Program", AST_Toplevel, "body@body");164map("Function", AST_Function, "id>name, params@argnames, body%body");165map("EmptyStatement", AST_EmptyStatement);166map("BlockStatement", AST_BlockStatement, "body@body");167map("ExpressionStatement", AST_SimpleStatement, "expression>body");168map("IfStatement", AST_If, "test>condition, consequent>body, alternate>alternative");169map("LabeledStatement", AST_LabeledStatement, "label>label, body>body");170map("BreakStatement", AST_Break, "label>label");171map("ContinueStatement", AST_Continue, "label>label");172map("WithStatement", AST_With, "object>expression, body>body");173map("SwitchStatement", AST_Switch, "discriminant>expression, cases@body");174map("ReturnStatement", AST_Return, "argument>value");175map("ThrowStatement", AST_Throw, "argument>value");176map("WhileStatement", AST_While, "test>condition, body>body");177map("DoWhileStatement", AST_Do, "test>condition, body>body");178map("ForStatement", AST_For, "init>init, test>condition, update>step, body>body");179map("ForInStatement", AST_ForIn, "left>init, right>object, body>body");180map("DebuggerStatement", AST_Debugger);181map("FunctionDeclaration", AST_Defun, "id>name, params@argnames, body%body");182map("VariableDeclaration", AST_Var, "declarations@definitions");183map("VariableDeclarator", AST_VarDef, "id>name, init>value");184185map("ThisExpression", AST_This);186map("ArrayExpression", AST_Array, "elements@elements");187map("FunctionExpression", AST_Function, "id>name, params@argnames, body%body");188map("BinaryExpression", AST_Binary, "operator=operator, left>left, right>right");189map("AssignmentExpression", AST_Assign, "operator=operator, left>left, right>right");190map("LogicalExpression", AST_Binary, "operator=operator, left>left, right>right");191map("ConditionalExpression", AST_Conditional, "test>condition, consequent>consequent, alternate>alternative");192map("NewExpression", AST_New, "callee>expression, arguments@args");193map("CallExpression", AST_Call, "callee>expression, arguments@args");194195/* -----[ tools ]----- */196197function my_start_token(moznode) {198return new AST_Token({199file : moznode.loc && moznode.loc.source,200line : moznode.loc && moznode.loc.start.line,201col : moznode.loc && moznode.loc.start.column,202pos : moznode.start,203endpos : moznode.start204});205};206207function my_end_token(moznode) {208return new AST_Token({209file : moznode.loc && moznode.loc.source,210line : moznode.loc && moznode.loc.end.line,211col : moznode.loc && moznode.loc.end.column,212pos : moznode.end,213endpos : moznode.end214});215};216217function map(moztype, mytype, propmap) {218var moz_to_me = "function From_Moz_" + moztype + "(M){\n";219moz_to_me += "return new mytype({\n" +220"start: my_start_token(M),\n" +221"end: my_end_token(M)";222223if (propmap) propmap.split(/\s*,\s*/).forEach(function(prop){224var m = /([a-z0-9$_]+)(=|@|>|%)([a-z0-9$_]+)/i.exec(prop);225if (!m) throw new Error("Can't understand property map: " + prop);226var moz = "M." + m[1], how = m[2], my = m[3];227moz_to_me += ",\n" + my + ": ";228if (how == "@") {229moz_to_me += moz + ".map(from_moz)";230} else if (how == ">") {231moz_to_me += "from_moz(" + moz + ")";232} else if (how == "=") {233moz_to_me += moz;234} else if (how == "%") {235moz_to_me += "from_moz(" + moz + ").body";236} else throw new Error("Can't understand operator in propmap: " + prop);237});238moz_to_me += "\n})}";239240// moz_to_me = parse(moz_to_me).print_to_string({ beautify: true });241// console.log(moz_to_me);242243moz_to_me = new Function("mytype", "my_start_token", "my_end_token", "from_moz", "return(" + moz_to_me + ")")(244mytype, my_start_token, my_end_token, from_moz245);246return MOZ_TO_ME[moztype] = moz_to_me;247};248249var FROM_MOZ_STACK = null;250251function from_moz(node) {252FROM_MOZ_STACK.push(node);253var ret = node != null ? MOZ_TO_ME[node.type](node) : null;254FROM_MOZ_STACK.pop();255return ret;256};257258AST_Node.from_mozilla_ast = function(node){259var save_stack = FROM_MOZ_STACK;260FROM_MOZ_STACK = [];261var ast = from_moz(node);262FROM_MOZ_STACK = save_stack;263return ast;264};265266})();267268269