mozilla-ast.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /***********************************************************************
  2. A JavaScript tokenizer / parser / beautifier / compressor.
  3. https://github.com/mishoo/UglifyJS2
  4. -------------------------------- (C) ---------------------------------
  5. Author: Mihai Bazon
  6. <mihai.bazon@gmail.com>
  7. http://mihai.bazon.net/blog
  8. Distributed under the BSD license:
  9. Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. * Redistributions of source code must retain the above
  14. copyright notice, this list of conditions and the following
  15. disclaimer.
  16. * Redistributions in binary form must reproduce the above
  17. copyright notice, this list of conditions and the following
  18. disclaimer in the documentation and/or other materials
  19. provided with the distribution.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
  21. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
  24. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  25. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  29. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. SUCH DAMAGE.
  32. ***********************************************************************/
  33. "use strict";
  34. (function(){
  35. var MOZ_TO_ME = {
  36. TryStatement : function(M) {
  37. return new AST_Try({
  38. start : my_start_token(M),
  39. end : my_end_token(M),
  40. body : from_moz(M.block).body,
  41. bcatch : from_moz(M.handlers[0]),
  42. bfinally : M.finalizer ? new AST_Finally(from_moz(M.finalizer)) : null
  43. });
  44. },
  45. CatchClause : function(M) {
  46. return new AST_Catch({
  47. start : my_start_token(M),
  48. end : my_end_token(M),
  49. argname : from_moz(M.param),
  50. body : from_moz(M.body).body
  51. });
  52. },
  53. ObjectExpression : function(M) {
  54. return new AST_Object({
  55. start : my_start_token(M),
  56. end : my_end_token(M),
  57. properties : M.properties.map(function(prop){
  58. var key = prop.key;
  59. var name = key.type == "Identifier" ? key.name : key.value;
  60. var args = {
  61. start : my_start_token(key),
  62. end : my_end_token(prop.value),
  63. key : name,
  64. value : from_moz(prop.value)
  65. };
  66. switch (prop.kind) {
  67. case "init":
  68. return new AST_ObjectKeyVal(args);
  69. case "set":
  70. args.value.name = from_moz(key);
  71. return new AST_ObjectSetter(args);
  72. case "get":
  73. args.value.name = from_moz(key);
  74. return new AST_ObjectGetter(args);
  75. }
  76. })
  77. });
  78. },
  79. SequenceExpression : function(M) {
  80. return AST_Seq.from_array(M.expressions.map(from_moz));
  81. },
  82. MemberExpression : function(M) {
  83. return new (M.computed ? AST_Sub : AST_Dot)({
  84. start : my_start_token(M),
  85. end : my_end_token(M),
  86. property : M.computed ? from_moz(M.property) : M.property.name,
  87. expression : from_moz(M.object)
  88. });
  89. },
  90. SwitchCase : function(M) {
  91. return new (M.test ? AST_Case : AST_Default)({
  92. start : my_start_token(M),
  93. end : my_end_token(M),
  94. expression : from_moz(M.test),
  95. body : M.consequent.map(from_moz)
  96. });
  97. },
  98. Literal : function(M) {
  99. var val = M.value, args = {
  100. start : my_start_token(M),
  101. end : my_end_token(M)
  102. };
  103. if (val === null) return new AST_Null(args);
  104. switch (typeof val) {
  105. case "string":
  106. args.value = val;
  107. return new AST_String(args);
  108. case "number":
  109. args.value = val;
  110. return new AST_Number(args);
  111. case "boolean":
  112. return new (val ? AST_True : AST_False)(args);
  113. default:
  114. args.value = val;
  115. return new AST_RegExp(args);
  116. }
  117. },
  118. UnaryExpression: From_Moz_Unary,
  119. UpdateExpression: From_Moz_Unary,
  120. Identifier: function(M) {
  121. var p = FROM_MOZ_STACK[FROM_MOZ_STACK.length - 2];
  122. return new (M.name == "this" ? AST_This
  123. : p.type == "LabeledStatement" ? AST_Label
  124. : p.type == "VariableDeclarator" && p.id === M ? (p.kind == "const" ? AST_SymbolConst : AST_SymbolVar)
  125. : p.type == "FunctionExpression" ? (p.id === M ? AST_SymbolLambda : AST_SymbolFunarg)
  126. : p.type == "FunctionDeclaration" ? (p.id === M ? AST_SymbolDefun : AST_SymbolFunarg)
  127. : p.type == "CatchClause" ? AST_SymbolCatch
  128. : p.type == "BreakStatement" || p.type == "ContinueStatement" ? AST_LabelRef
  129. : AST_SymbolRef)({
  130. start : my_start_token(M),
  131. end : my_end_token(M),
  132. name : M.name
  133. });
  134. }
  135. };
  136. function From_Moz_Unary(M) {
  137. var prefix = "prefix" in M ? M.prefix
  138. : M.type == "UnaryExpression" ? true : false;
  139. return new (prefix ? AST_UnaryPrefix : AST_UnaryPostfix)({
  140. start : my_start_token(M),
  141. end : my_end_token(M),
  142. operator : M.operator,
  143. expression : from_moz(M.argument)
  144. });
  145. };
  146. var ME_TO_MOZ = {};
  147. map("Node", AST_Node);
  148. map("Program", AST_Toplevel, "body@body");
  149. map("Function", AST_Function, "id>name, params@argnames, body%body");
  150. map("EmptyStatement", AST_EmptyStatement);
  151. map("BlockStatement", AST_BlockStatement, "body@body");
  152. map("ExpressionStatement", AST_SimpleStatement, "expression>body");
  153. map("IfStatement", AST_If, "test>condition, consequent>body, alternate>alternative");
  154. map("LabeledStatement", AST_LabeledStatement, "label>label, body>body");
  155. map("BreakStatement", AST_Break, "label>label");
  156. map("ContinueStatement", AST_Continue, "label>label");
  157. map("WithStatement", AST_With, "object>expression, body>body");
  158. map("SwitchStatement", AST_Switch, "discriminant>expression, cases@body");
  159. map("ReturnStatement", AST_Return, "argument>value");
  160. map("ThrowStatement", AST_Throw, "argument>value");
  161. map("WhileStatement", AST_While, "test>condition, body>body");
  162. map("DoWhileStatement", AST_Do, "test>condition, body>body");
  163. map("ForStatement", AST_For, "init>init, test>condition, update>step, body>body");
  164. map("ForInStatement", AST_ForIn, "left>init, right>object, body>body");
  165. map("DebuggerStatement", AST_Debugger);
  166. map("FunctionDeclaration", AST_Defun, "id>name, params@argnames, body%body");
  167. map("VariableDeclaration", AST_Var, "declarations@definitions");
  168. map("VariableDeclarator", AST_VarDef, "id>name, init>value");
  169. map("ThisExpression", AST_This);
  170. map("ArrayExpression", AST_Array, "elements@elements");
  171. map("FunctionExpression", AST_Function, "id>name, params@argnames, body%body");
  172. map("BinaryExpression", AST_Binary, "operator=operator, left>left, right>right");
  173. map("AssignmentExpression", AST_Assign, "operator=operator, left>left, right>right");
  174. map("LogicalExpression", AST_Binary, "operator=operator, left>left, right>right");
  175. map("ConditionalExpression", AST_Conditional, "test>condition, consequent>consequent, alternate>alternative");
  176. map("NewExpression", AST_New, "callee>expression, arguments@args");
  177. map("CallExpression", AST_Call, "callee>expression, arguments@args");
  178. /* -----[ tools ]----- */
  179. function my_start_token(moznode) {
  180. return new AST_Token({
  181. file : moznode.loc && moznode.loc.source,
  182. line : moznode.loc && moznode.loc.start.line,
  183. col : moznode.loc && moznode.loc.start.column,
  184. pos : moznode.start,
  185. endpos : moznode.start
  186. });
  187. };
  188. function my_end_token(moznode) {
  189. return new AST_Token({
  190. file : moznode.loc && moznode.loc.source,
  191. line : moznode.loc && moznode.loc.end.line,
  192. col : moznode.loc && moznode.loc.end.column,
  193. pos : moznode.end,
  194. endpos : moznode.end
  195. });
  196. };
  197. function map(moztype, mytype, propmap) {
  198. var moz_to_me = "function From_Moz_" + moztype + "(M){\n";
  199. moz_to_me += "return new mytype({\n" +
  200. "start: my_start_token(M),\n" +
  201. "end: my_end_token(M)";
  202. if (propmap) propmap.split(/\s*,\s*/).forEach(function(prop){
  203. var m = /([a-z0-9$_]+)(=|@|>|%)([a-z0-9$_]+)/i.exec(prop);
  204. if (!m) throw new Error("Can't understand property map: " + prop);
  205. var moz = "M." + m[1], how = m[2], my = m[3];
  206. moz_to_me += ",\n" + my + ": ";
  207. if (how == "@") {
  208. moz_to_me += moz + ".map(from_moz)";
  209. } else if (how == ">") {
  210. moz_to_me += "from_moz(" + moz + ")";
  211. } else if (how == "=") {
  212. moz_to_me += moz;
  213. } else if (how == "%") {
  214. moz_to_me += "from_moz(" + moz + ").body";
  215. } else throw new Error("Can't understand operator in propmap: " + prop);
  216. });
  217. moz_to_me += "\n})}";
  218. // moz_to_me = parse(moz_to_me).print_to_string({ beautify: true });
  219. // console.log(moz_to_me);
  220. moz_to_me = new Function("mytype", "my_start_token", "my_end_token", "from_moz", "return(" + moz_to_me + ")")(
  221. mytype, my_start_token, my_end_token, from_moz
  222. );
  223. return MOZ_TO_ME[moztype] = moz_to_me;
  224. };
  225. var FROM_MOZ_STACK = null;
  226. function from_moz(node) {
  227. FROM_MOZ_STACK.push(node);
  228. var ret = node != null ? MOZ_TO_ME[node.type](node) : null;
  229. FROM_MOZ_STACK.pop();
  230. return ret;
  231. };
  232. AST_Node.from_mozilla_ast = function(node){
  233. var save_stack = FROM_MOZ_STACK;
  234. FROM_MOZ_STACK = [];
  235. var ast = from_moz(node);
  236. FROM_MOZ_STACK = save_stack;
  237. return ast;
  238. };
  239. })();