react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / umd / node_modules / uglify-js / lib / mozilla-ast.js
80743 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 = {48ExpressionStatement: function(M) {49var expr = M.expression;50if (expr.type === "Literal" && typeof expr.value === "string") {51return new AST_Directive({52start: my_start_token(M),53end: my_end_token(M),54value: expr.value55});56}57return new AST_SimpleStatement({58start: my_start_token(M),59end: my_end_token(M),60body: from_moz(expr)61});62},63TryStatement: function(M) {64var handlers = M.handlers || [M.handler];65if (handlers.length > 1 || M.guardedHandlers && M.guardedHandlers.length) {66throw new Error("Multiple catch clauses are not supported.");67}68return new AST_Try({69start : my_start_token(M),70end : my_end_token(M),71body : from_moz(M.block).body,72bcatch : from_moz(handlers[0]),73bfinally : M.finalizer ? new AST_Finally(from_moz(M.finalizer)) : null74});75},76Property: function(M) {77var key = M.key;78var name = key.type == "Identifier" ? key.name : key.value;79var args = {80start : my_start_token(key),81end : my_end_token(M.value),82key : name,83value : from_moz(M.value)84};85switch (M.kind) {86case "init":87return new AST_ObjectKeyVal(args);88case "set":89args.value.name = from_moz(key);90return new AST_ObjectSetter(args);91case "get":92args.value.name = from_moz(key);93return new AST_ObjectGetter(args);94}95},96ObjectExpression: function(M) {97return new AST_Object({98start : my_start_token(M),99end : my_end_token(M),100properties : M.properties.map(function(prop){101prop.type = "Property";102return from_moz(prop)103})104});105},106SequenceExpression: function(M) {107return AST_Seq.from_array(M.expressions.map(from_moz));108},109MemberExpression: function(M) {110return new (M.computed ? AST_Sub : AST_Dot)({111start : my_start_token(M),112end : my_end_token(M),113property : M.computed ? from_moz(M.property) : M.property.name,114expression : from_moz(M.object)115});116},117SwitchCase: function(M) {118return new (M.test ? AST_Case : AST_Default)({119start : my_start_token(M),120end : my_end_token(M),121expression : from_moz(M.test),122body : M.consequent.map(from_moz)123});124},125VariableDeclaration: function(M) {126return new (M.kind === "const" ? AST_Const : AST_Var)({127start : my_start_token(M),128end : my_end_token(M),129definitions : M.declarations.map(from_moz)130});131},132Literal: function(M) {133var val = M.value, args = {134start : my_start_token(M),135end : my_end_token(M)136};137if (val === null) return new AST_Null(args);138switch (typeof val) {139case "string":140args.value = val;141return new AST_String(args);142case "number":143args.value = val;144return new AST_Number(args);145case "boolean":146return new (val ? AST_True : AST_False)(args);147default:148args.value = val;149return new AST_RegExp(args);150}151},152Identifier: function(M) {153var p = FROM_MOZ_STACK[FROM_MOZ_STACK.length - 2];154return new ( p.type == "LabeledStatement" ? AST_Label155: p.type == "VariableDeclarator" && p.id === M ? (p.kind == "const" ? AST_SymbolConst : AST_SymbolVar)156: p.type == "FunctionExpression" ? (p.id === M ? AST_SymbolLambda : AST_SymbolFunarg)157: p.type == "FunctionDeclaration" ? (p.id === M ? AST_SymbolDefun : AST_SymbolFunarg)158: p.type == "CatchClause" ? AST_SymbolCatch159: p.type == "BreakStatement" || p.type == "ContinueStatement" ? AST_LabelRef160: AST_SymbolRef)({161start : my_start_token(M),162end : my_end_token(M),163name : M.name164});165}166};167168MOZ_TO_ME.UpdateExpression =169MOZ_TO_ME.UnaryExpression = function To_Moz_Unary(M) {170var prefix = "prefix" in M ? M.prefix171: M.type == "UnaryExpression" ? true : false;172return new (prefix ? AST_UnaryPrefix : AST_UnaryPostfix)({173start : my_start_token(M),174end : my_end_token(M),175operator : M.operator,176expression : from_moz(M.argument)177});178};179180map("Program", AST_Toplevel, "body@body");181map("EmptyStatement", AST_EmptyStatement);182map("BlockStatement", AST_BlockStatement, "body@body");183map("IfStatement", AST_If, "test>condition, consequent>body, alternate>alternative");184map("LabeledStatement", AST_LabeledStatement, "label>label, body>body");185map("BreakStatement", AST_Break, "label>label");186map("ContinueStatement", AST_Continue, "label>label");187map("WithStatement", AST_With, "object>expression, body>body");188map("SwitchStatement", AST_Switch, "discriminant>expression, cases@body");189map("ReturnStatement", AST_Return, "argument>value");190map("ThrowStatement", AST_Throw, "argument>value");191map("WhileStatement", AST_While, "test>condition, body>body");192map("DoWhileStatement", AST_Do, "test>condition, body>body");193map("ForStatement", AST_For, "init>init, test>condition, update>step, body>body");194map("ForInStatement", AST_ForIn, "left>init, right>object, body>body");195map("DebuggerStatement", AST_Debugger);196map("FunctionDeclaration", AST_Defun, "id>name, params@argnames, body%body");197map("VariableDeclarator", AST_VarDef, "id>name, init>value");198map("CatchClause", AST_Catch, "param>argname, body%body");199200map("ThisExpression", AST_This);201map("ArrayExpression", AST_Array, "elements@elements");202map("FunctionExpression", AST_Function, "id>name, params@argnames, body%body");203map("BinaryExpression", AST_Binary, "operator=operator, left>left, right>right");204map("LogicalExpression", AST_Binary, "operator=operator, left>left, right>right");205map("AssignmentExpression", AST_Assign, "operator=operator, left>left, right>right");206map("ConditionalExpression", AST_Conditional, "test>condition, consequent>consequent, alternate>alternative");207map("NewExpression", AST_New, "callee>expression, arguments@args");208map("CallExpression", AST_Call, "callee>expression, arguments@args");209210def_to_moz(AST_Directive, function To_Moz_Directive(M) {211return {212type: "ExpressionStatement",213expression: {214type: "Literal",215value: M.value216}217};218});219220def_to_moz(AST_SimpleStatement, function To_Moz_ExpressionStatement(M) {221return {222type: "ExpressionStatement",223expression: to_moz(M.body)224};225});226227def_to_moz(AST_SwitchBranch, function To_Moz_SwitchCase(M) {228return {229type: "SwitchCase",230test: to_moz(M.expression),231consequent: M.body.map(to_moz)232};233});234235def_to_moz(AST_Try, function To_Moz_TryStatement(M) {236return {237type: "TryStatement",238block: to_moz_block(M),239handler: to_moz(M.bcatch),240guardedHandlers: [],241finalizer: to_moz(M.bfinally)242};243});244245def_to_moz(AST_Catch, function To_Moz_CatchClause(M) {246return {247type: "CatchClause",248param: to_moz(M.argname),249guard: null,250body: to_moz_block(M)251};252});253254def_to_moz(AST_Definitions, function To_Moz_VariableDeclaration(M) {255return {256type: "VariableDeclaration",257kind: M instanceof AST_Const ? "const" : "var",258declarations: M.definitions.map(to_moz)259};260});261262def_to_moz(AST_Seq, function To_Moz_SequenceExpression(M) {263return {264type: "SequenceExpression",265expressions: M.to_array().map(to_moz)266};267});268269def_to_moz(AST_PropAccess, function To_Moz_MemberExpression(M) {270var isComputed = M instanceof AST_Sub;271return {272type: "MemberExpression",273object: to_moz(M.expression),274computed: isComputed,275property: isComputed ? to_moz(M.property) : {type: "Identifier", name: M.property}276};277});278279def_to_moz(AST_Unary, function To_Moz_Unary(M) {280return {281type: M.operator == "++" || M.operator == "--" ? "UpdateExpression" : "UnaryExpression",282operator: M.operator,283prefix: M instanceof AST_UnaryPrefix,284argument: to_moz(M.expression)285};286});287288def_to_moz(AST_Binary, function To_Moz_BinaryExpression(M) {289return {290type: M.operator == "&&" || M.operator == "||" ? "LogicalExpression" : "BinaryExpression",291left: to_moz(M.left),292operator: M.operator,293right: to_moz(M.right)294};295});296297def_to_moz(AST_Object, function To_Moz_ObjectExpression(M) {298return {299type: "ObjectExpression",300properties: M.properties.map(to_moz)301};302});303304def_to_moz(AST_ObjectProperty, function To_Moz_Property(M) {305var key = (306is_identifier(M.key)307? {type: "Identifier", name: M.key}308: {type: "Literal", value: M.key}309);310var kind;311if (M instanceof AST_ObjectKeyVal) {312kind = "init";313} else314if (M instanceof AST_ObjectGetter) {315kind = "get";316} else317if (M instanceof AST_ObjectSetter) {318kind = "set";319}320return {321type: "Property",322kind: kind,323key: key,324value: to_moz(M.value)325};326});327328def_to_moz(AST_Symbol, function To_Moz_Identifier(M) {329var def = M.definition();330return {331type: "Identifier",332name: def ? def.mangled_name || def.name : M.name333};334});335336def_to_moz(AST_Constant, function To_Moz_Literal(M) {337var value = M.value;338if (typeof value === 'number' && (value < 0 || (value === 0 && 1 / value < 0))) {339return {340type: "UnaryExpression",341operator: "-",342prefix: true,343argument: {344type: "Literal",345value: -value346}347};348}349return {350type: "Literal",351value: value352};353});354355def_to_moz(AST_Atom, function To_Moz_Atom(M) {356return {357type: "Identifier",358name: String(M.value)359};360});361362AST_Boolean.DEFMETHOD("to_mozilla_ast", AST_Constant.prototype.to_mozilla_ast);363AST_Null.DEFMETHOD("to_mozilla_ast", AST_Constant.prototype.to_mozilla_ast);364AST_Hole.DEFMETHOD("to_mozilla_ast", function To_Moz_ArrayHole() { return null });365366AST_Block.DEFMETHOD("to_mozilla_ast", AST_BlockStatement.prototype.to_mozilla_ast);367AST_Lambda.DEFMETHOD("to_mozilla_ast", AST_Function.prototype.to_mozilla_ast);368369/* -----[ tools ]----- */370371function my_start_token(moznode) {372var loc = moznode.loc, start = loc && loc.start;373var range = moznode.range;374return new AST_Token({375file : loc && loc.source,376line : start && start.line,377col : start && start.column,378pos : range ? range[0] : moznode.start,379endline : start && start.line,380endcol : start && start.column,381endpos : range ? range[0] : moznode.start382});383};384385function my_end_token(moznode) {386var loc = moznode.loc, end = loc && loc.end;387var range = moznode.range;388return new AST_Token({389file : loc && loc.source,390line : end && end.line,391col : end && end.column,392pos : range ? range[1] : moznode.end,393endline : end && end.line,394endcol : end && end.column,395endpos : range ? range[1] : moznode.end396});397};398399function map(moztype, mytype, propmap) {400var moz_to_me = "function From_Moz_" + moztype + "(M){\n";401moz_to_me += "return new " + mytype.name + "({\n" +402"start: my_start_token(M),\n" +403"end: my_end_token(M)";404405var me_to_moz = "function To_Moz_" + moztype + "(M){\n";406me_to_moz += "return {\n" +407"type: " + JSON.stringify(moztype);408409if (propmap) propmap.split(/\s*,\s*/).forEach(function(prop){410var m = /([a-z0-9$_]+)(=|@|>|%)([a-z0-9$_]+)/i.exec(prop);411if (!m) throw new Error("Can't understand property map: " + prop);412var moz = m[1], how = m[2], my = m[3];413moz_to_me += ",\n" + my + ": ";414me_to_moz += ",\n" + moz + ": ";415switch (how) {416case "@":417moz_to_me += "M." + moz + ".map(from_moz)";418me_to_moz += "M." + my + ".map(to_moz)";419break;420case ">":421moz_to_me += "from_moz(M." + moz + ")";422me_to_moz += "to_moz(M." + my + ")";423break;424case "=":425moz_to_me += "M." + moz;426me_to_moz += "M." + my;427break;428case "%":429moz_to_me += "from_moz(M." + moz + ").body";430me_to_moz += "to_moz_block(M)";431break;432default:433throw new Error("Can't understand operator in propmap: " + prop);434}435});436437moz_to_me += "\n})\n}";438me_to_moz += "\n}\n}";439440//moz_to_me = parse(moz_to_me).print_to_string({ beautify: true });441//me_to_moz = parse(me_to_moz).print_to_string({ beautify: true });442//console.log(moz_to_me);443444moz_to_me = new Function("my_start_token", "my_end_token", "from_moz", "return(" + moz_to_me + ")")(445my_start_token, my_end_token, from_moz446);447me_to_moz = new Function("to_moz", "to_moz_block", "return(" + me_to_moz + ")")(448to_moz, to_moz_block449);450MOZ_TO_ME[moztype] = moz_to_me;451def_to_moz(mytype, me_to_moz);452};453454var FROM_MOZ_STACK = null;455456function from_moz(node) {457FROM_MOZ_STACK.push(node);458var ret = node != null ? MOZ_TO_ME[node.type](node) : null;459FROM_MOZ_STACK.pop();460return ret;461};462463AST_Node.from_mozilla_ast = function(node){464var save_stack = FROM_MOZ_STACK;465FROM_MOZ_STACK = [];466var ast = from_moz(node);467FROM_MOZ_STACK = save_stack;468return ast;469};470471function set_moz_loc(mynode, moznode, myparent) {472var start = mynode.start;473var end = mynode.end;474if (start.pos != null && end.endpos != null) {475moznode.range = [start.pos, end.endpos];476}477if (start.line) {478moznode.loc = {479start: {line: start.line, column: start.col},480end: end.endline ? {line: end.endline, column: end.endcol} : null481};482if (start.file) {483moznode.loc.source = start.file;484}485}486return moznode;487};488489function def_to_moz(mytype, handler) {490mytype.DEFMETHOD("to_mozilla_ast", function() {491return set_moz_loc(this, handler(this));492});493};494495function to_moz(node) {496return node != null ? node.to_mozilla_ast() : null;497};498499function to_moz_block(node) {500return {501type: "BlockStatement",502body: node.body.map(to_moz)503};504};505506})();507508509