JSMinPlus.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872
  1. <?php
  2. /**
  3. * JSMinPlus version 1.1
  4. *
  5. * Minifies a javascript file using a javascript parser
  6. *
  7. * This implements a PHP port of Brendan Eich's Narcissus open source javascript engine (in javascript)
  8. * References: http://en.wikipedia.org/wiki/Narcissus_(JavaScript_engine)
  9. * Narcissus sourcecode: http://mxr.mozilla.org/mozilla/source/js/narcissus/
  10. * JSMinPlus weblog: http://crisp.tweakblogs.net/blog/cat/716
  11. *
  12. * Tino Zijdel <crisp@tweakers.net>
  13. *
  14. * Usage: $minified = JSMinPlus::minify($script [, $filename])
  15. *
  16. * Versionlog (see also changelog.txt):
  17. * 12-04-2009 - some small bugfixes and performance improvements
  18. * 09-04-2009 - initial open sourced version 1.0
  19. *
  20. * Latest version of this script: http://files.tweakers.net/jsminplus/jsminplus.zip
  21. *
  22. */
  23. /* ***** BEGIN LICENSE BLOCK *****
  24. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  25. *
  26. * The contents of this file are subject to the Mozilla Public License Version
  27. * 1.1 (the "License"); you may not use this file except in compliance with
  28. * the License. You may obtain a copy of the License at
  29. * http://www.mozilla.org/MPL/
  30. *
  31. * Software distributed under the License is distributed on an "AS IS" basis,
  32. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  33. * for the specific language governing rights and limitations under the
  34. * License.
  35. *
  36. * The Original Code is the Narcissus JavaScript engine.
  37. *
  38. * The Initial Developer of the Original Code is
  39. * Brendan Eich <brendan@mozilla.org>.
  40. * Portions created by the Initial Developer are Copyright (C) 2004
  41. * the Initial Developer. All Rights Reserved.
  42. *
  43. * Contributor(s): Tino Zijdel <crisp@tweakers.net>
  44. * PHP port, modifications and minifier routine are (C) 2009
  45. *
  46. * Alternatively, the contents of this file may be used under the terms of
  47. * either the GNU General Public License Version 2 or later (the "GPL"), or
  48. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  49. * in which case the provisions of the GPL or the LGPL are applicable instead
  50. * of those above. If you wish to allow use of your version of this file only
  51. * under the terms of either the GPL or the LGPL, and not to allow others to
  52. * use your version of this file under the terms of the MPL, indicate your
  53. * decision by deleting the provisions above and replace them with the notice
  54. * and other provisions required by the GPL or the LGPL. If you do not delete
  55. * the provisions above, a recipient may use your version of this file under
  56. * the terms of any one of the MPL, the GPL or the LGPL.
  57. *
  58. * ***** END LICENSE BLOCK ***** */
  59. define('TOKEN_END', 1);
  60. define('TOKEN_NUMBER', 2);
  61. define('TOKEN_IDENTIFIER', 3);
  62. define('TOKEN_STRING', 4);
  63. define('TOKEN_REGEXP', 5);
  64. define('TOKEN_NEWLINE', 6);
  65. define('TOKEN_CONDCOMMENT_MULTILINE', 7);
  66. define('JS_SCRIPT', 100);
  67. define('JS_BLOCK', 101);
  68. define('JS_LABEL', 102);
  69. define('JS_FOR_IN', 103);
  70. define('JS_CALL', 104);
  71. define('JS_NEW_WITH_ARGS', 105);
  72. define('JS_INDEX', 106);
  73. define('JS_ARRAY_INIT', 107);
  74. define('JS_OBJECT_INIT', 108);
  75. define('JS_PROPERTY_INIT', 109);
  76. define('JS_GETTER', 110);
  77. define('JS_SETTER', 111);
  78. define('JS_GROUP', 112);
  79. define('JS_LIST', 113);
  80. define('DECLARED_FORM', 0);
  81. define('EXPRESSED_FORM', 1);
  82. define('STATEMENT_FORM', 2);
  83. class JSMinPlus
  84. {
  85. private $parser;
  86. private $reserved = array(
  87. 'break', 'case', 'catch', 'continue', 'default', 'delete', 'do',
  88. 'else', 'finally', 'for', 'function', 'if', 'in', 'instanceof',
  89. 'new', 'return', 'switch', 'this', 'throw', 'try', 'typeof', 'var',
  90. 'void', 'while', 'with',
  91. // Words reserved for future use
  92. 'abstract', 'boolean', 'byte', 'char', 'class', 'const', 'debugger',
  93. 'double', 'enum', 'export', 'extends', 'final', 'float', 'goto',
  94. 'implements', 'import', 'int', 'interface', 'long', 'native',
  95. 'package', 'private', 'protected', 'public', 'short', 'static',
  96. 'super', 'synchronized', 'throws', 'transient', 'volatile',
  97. // These are not reserved, but should be taken into account
  98. // in isValidIdentifier (See jslint source code)
  99. 'arguments', 'eval', 'true', 'false', 'Infinity', 'NaN', 'null', 'undefined'
  100. );
  101. private function __construct()
  102. {
  103. $this->parser = new JSParser();
  104. }
  105. public static function minify($js, $filename='')
  106. {
  107. static $instance;
  108. // this is a singleton
  109. if(!$instance)
  110. $instance = new JSMinPlus();
  111. return $instance->min($js, $filename);
  112. }
  113. private function min($js, $filename)
  114. {
  115. try
  116. {
  117. $n = $this->parser->parse($js, $filename, 1);
  118. return $this->parseTree($n);
  119. }
  120. catch(Exception $e)
  121. {
  122. echo $e->getMessage() . "\n";
  123. }
  124. return false;
  125. }
  126. private function parseTree($n, $noBlockGrouping = false)
  127. {
  128. $s = '';
  129. switch ($n->type)
  130. {
  131. case KEYWORD_FUNCTION:
  132. $s .= 'function' . ($n->name ? ' ' . $n->name : '') . '(';
  133. $params = $n->params;
  134. for ($i = 0, $j = count($params); $i < $j; $i++)
  135. $s .= ($i ? ',' : '') . $params[$i];
  136. $s .= '){' . $this->parseTree($n->body, true) . '}';
  137. break;
  138. case JS_SCRIPT:
  139. // we do nothing with funDecls or varDecls
  140. $noBlockGrouping = true;
  141. // fall through
  142. case JS_BLOCK:
  143. $childs = $n->treeNodes;
  144. for ($c = 0, $i = 0, $j = count($childs); $i < $j; $i++)
  145. {
  146. $t = $this->parseTree($childs[$i]);
  147. if (strlen($t))
  148. {
  149. if ($c)
  150. {
  151. if ($childs[$i]->type == KEYWORD_FUNCTION && $childs[$i]->functionForm == DECLARED_FORM)
  152. $s .= "\n"; // put declared functions on a new line
  153. else
  154. $s .= ';';
  155. }
  156. $s .= $t;
  157. $c++;
  158. }
  159. }
  160. if ($c > 1 && !$noBlockGrouping)
  161. {
  162. $s = '{' . $s . '}';
  163. }
  164. break;
  165. case KEYWORD_IF:
  166. $s = 'if(' . $this->parseTree($n->condition) . ')';
  167. $thenPart = $this->parseTree($n->thenPart);
  168. $elsePart = $n->elsePart ? $this->parseTree($n->elsePart) : null;
  169. // quite a rancid hack to see if we should enclose the thenpart in brackets
  170. if ($thenPart[0] != '{')
  171. {
  172. if (strpos($thenPart, 'if(') !== false)
  173. $thenPart = '{' . $thenPart . '}';
  174. elseif ($elsePart)
  175. $thenPart .= ';';
  176. }
  177. $s .= $thenPart;
  178. if ($elsePart)
  179. {
  180. $s .= 'else';
  181. if ($elsePart[0] != '{')
  182. $s .= ' ';
  183. $s .= $elsePart;
  184. }
  185. break;
  186. case KEYWORD_SWITCH:
  187. $s = 'switch(' . $this->parseTree($n->discriminant) . '){';
  188. $cases = $n->cases;
  189. for ($i = 0, $j = count($cases); $i < $j; $i++)
  190. {
  191. $case = $cases[$i];
  192. if ($case->type == KEYWORD_CASE)
  193. $s .= 'case' . ($case->caseLabel->type != TOKEN_STRING ? ' ' : '') . $this->parseTree($case->caseLabel) . ':';
  194. else
  195. $s .= 'default:';
  196. $statement = $this->parseTree($case->statements);
  197. if ($statement)
  198. $s .= $statement . ';';
  199. }
  200. $s = rtrim($s, ';') . '}';
  201. break;
  202. case KEYWORD_FOR:
  203. $s = 'for(' . ($n->setup ? $this->parseTree($n->setup) : '')
  204. . ';' . ($n->condition ? $this->parseTree($n->condition) : '')
  205. . ';' . ($n->update ? $this->parseTree($n->update) : '') . ')'
  206. . $this->parseTree($n->body);
  207. break;
  208. case KEYWORD_WHILE:
  209. $s = 'while(' . $this->parseTree($n->condition) . ')' . $this->parseTree($n->body);
  210. break;
  211. case JS_FOR_IN:
  212. $s = 'for(' . ($n->varDecl ? $this->parseTree($n->varDecl) : $this->parseTree($n->iterator)) . ' in ' . $this->parseTree($n->object) . ')' . $this->parseTree($n->body);
  213. break;
  214. case KEYWORD_DO:
  215. $s = 'do{' . $this->parseTree($n->body, true) . '}while(' . $this->parseTree($n->condition) . ')';
  216. break;
  217. case KEYWORD_BREAK:
  218. case KEYWORD_CONTINUE:
  219. $s = $n->value . ($n->label ? ' ' . $n->label : '');
  220. break;
  221. case KEYWORD_TRY:
  222. $s = 'try{' . $this->parseTree($n->tryBlock, true) . '}';
  223. $catchClauses = $n->catchClauses;
  224. for ($i = 0, $j = count($catchClauses); $i < $j; $i++)
  225. {
  226. $t = $catchClauses[$i];
  227. $s .= 'catch(' . $t->varName . ($t->guard ? ' if ' . $this->parseTree($t->guard) : '') . '){' . $this->parseTree($t->block, true) . '}';
  228. }
  229. if ($n->finallyBlock)
  230. $s .= 'finally{' . $this->parseTree($n->finallyBlock, true) . '}';
  231. break;
  232. case KEYWORD_THROW:
  233. $s = 'throw ' . $this->parseTree($n->exception);
  234. break;
  235. case KEYWORD_RETURN:
  236. $s = 'return' . ($n->value ? ' ' . $this->parseTree($n->value) : '');
  237. break;
  238. case KEYWORD_WITH:
  239. $s = 'with(' . $this->parseTree($n->object) . ')' . $this->parseTree($n->body);
  240. break;
  241. case KEYWORD_VAR:
  242. case KEYWORD_CONST:
  243. $s = $n->value . ' ';
  244. $childs = $n->treeNodes;
  245. for ($i = 0, $j = count($childs); $i < $j; $i++)
  246. {
  247. $t = $childs[$i];
  248. $s .= ($i ? ',' : '') . $t->name;
  249. $u = $t->initializer;
  250. if ($u)
  251. $s .= '=' . $this->parseTree($u);
  252. }
  253. break;
  254. case KEYWORD_DEBUGGER:
  255. throw new Exception('NOT IMPLEMENTED: DEBUGGER');
  256. break;
  257. case TOKEN_CONDCOMMENT_MULTILINE:
  258. $s = $n->value . ' ';
  259. $childs = $n->treeNodes;
  260. for ($i = 0, $j = count($childs); $i < $j; $i++)
  261. $s .= $this->parseTree($childs[$i]);
  262. break;
  263. case OP_SEMICOLON:
  264. if ($expression = $n->expression)
  265. $s = $this->parseTree($expression);
  266. break;
  267. case JS_LABEL:
  268. $s = $n->label . ':' . $this->parseTree($n->statement);
  269. break;
  270. case OP_COMMA:
  271. $childs = $n->treeNodes;
  272. for ($i = 0, $j = count($childs); $i < $j; $i++)
  273. $s .= ($i ? ',' : '') . $this->parseTree($childs[$i]);
  274. break;
  275. case OP_ASSIGN:
  276. $s = $this->parseTree($n->treeNodes[0]) . $n->value . $this->parseTree($n->treeNodes[1]);
  277. break;
  278. case OP_HOOK:
  279. $s = $this->parseTree($n->treeNodes[0]) . '?' . $this->parseTree($n->treeNodes[1]) . ':' . $this->parseTree($n->treeNodes[2]);
  280. break;
  281. case OP_OR: case OP_AND:
  282. case OP_BITWISE_OR: case OP_BITWISE_XOR: case OP_BITWISE_AND:
  283. case OP_EQ: case OP_NE: case OP_STRICT_EQ: case OP_STRICT_NE:
  284. case OP_LT: case OP_LE: case OP_GE: case OP_GT:
  285. case OP_LSH: case OP_RSH: case OP_URSH:
  286. case OP_MUL: case OP_DIV: case OP_MOD:
  287. $s = $this->parseTree($n->treeNodes[0]) . $n->type . $this->parseTree($n->treeNodes[1]);
  288. break;
  289. case OP_PLUS:
  290. case OP_MINUS:
  291. $s = $this->parseTree($n->treeNodes[0]) . $n->type;
  292. $nextTokenType = $n->treeNodes[1]->type;
  293. if ( $nextTokenType == OP_PLUS || $nextTokenType == OP_MINUS ||
  294. $nextTokenType == OP_INCREMENT || $nextTokenType == OP_DECREMENT ||
  295. $nextTokenType == OP_UNARY_PLUS || $nextTokenType == OP_UNARY_MINUS
  296. )
  297. $s .= ' ';
  298. $s .= $this->parseTree($n->treeNodes[1]);
  299. break;
  300. case KEYWORD_IN:
  301. $s = $this->parseTree($n->treeNodes[0]) . ' in ' . $this->parseTree($n->treeNodes[1]);
  302. break;
  303. case KEYWORD_INSTANCEOF:
  304. $s = $this->parseTree($n->treeNodes[0]) . ' instanceof ' . $this->parseTree($n->treeNodes[1]);
  305. break;
  306. case KEYWORD_DELETE:
  307. $s = 'delete ' . $this->parseTree($n->treeNodes[0]);
  308. break;
  309. case KEYWORD_VOID:
  310. $s = 'void(' . $this->parseTree($n->treeNodes[0]) . ')';
  311. break;
  312. case KEYWORD_TYPEOF:
  313. $s = 'typeof ' . $this->parseTree($n->treeNodes[0]);
  314. break;
  315. case OP_NOT:
  316. case OP_BITWISE_NOT:
  317. case OP_UNARY_PLUS:
  318. case OP_UNARY_MINUS:
  319. $s = $n->value . $this->parseTree($n->treeNodes[0]);
  320. break;
  321. case OP_INCREMENT:
  322. case OP_DECREMENT:
  323. if ($n->postfix)
  324. $s = $this->parseTree($n->treeNodes[0]) . $n->value;
  325. else
  326. $s = $n->value . $this->parseTree($n->treeNodes[0]);
  327. break;
  328. case OP_DOT:
  329. $s = $this->parseTree($n->treeNodes[0]) . '.' . $this->parseTree($n->treeNodes[1]);
  330. break;
  331. case JS_INDEX:
  332. $s = $this->parseTree($n->treeNodes[0]);
  333. // See if we can replace named index with a dot saving 3 bytes
  334. if ( $n->treeNodes[0]->type == TOKEN_IDENTIFIER &&
  335. $n->treeNodes[1]->type == TOKEN_STRING &&
  336. $this->isValidIdentifier(substr($n->treeNodes[1]->value, 1, -1))
  337. )
  338. $s .= '.' . substr($n->treeNodes[1]->value, 1, -1);
  339. else
  340. $s .= '[' . $this->parseTree($n->treeNodes[1]) . ']';
  341. break;
  342. case JS_LIST:
  343. $childs = $n->treeNodes;
  344. for ($i = 0, $j = count($childs); $i < $j; $i++)
  345. $s .= ($i ? ',' : '') . $this->parseTree($childs[$i]);
  346. break;
  347. case JS_CALL:
  348. $s = $this->parseTree($n->treeNodes[0]) . '(' . $this->parseTree($n->treeNodes[1]) . ')';
  349. break;
  350. case KEYWORD_NEW:
  351. case JS_NEW_WITH_ARGS:
  352. $s = 'new ' . $this->parseTree($n->treeNodes[0]) . '(' . ($n->type == JS_NEW_WITH_ARGS ? $this->parseTree($n->treeNodes[1]) : '') . ')';
  353. break;
  354. case JS_ARRAY_INIT:
  355. $s = '[';
  356. $childs = $n->treeNodes;
  357. for ($i = 0, $j = count($childs); $i < $j; $i++)
  358. {
  359. $s .= ($i ? ',' : '') . $this->parseTree($childs[$i]);
  360. }
  361. $s .= ']';
  362. break;
  363. case JS_OBJECT_INIT:
  364. $s = '{';
  365. $childs = $n->treeNodes;
  366. for ($i = 0, $j = count($childs); $i < $j; $i++)
  367. {
  368. $t = $childs[$i];
  369. if ($i)
  370. $s .= ',';
  371. if ($t->type == JS_PROPERTY_INIT)
  372. {
  373. // Ditch the quotes when the index is a valid identifier
  374. if ( $t->treeNodes[0]->type == TOKEN_STRING &&
  375. $this->isValidIdentifier(substr($t->treeNodes[0]->value, 1, -1))
  376. )
  377. $s .= substr($t->treeNodes[0]->value, 1, -1);
  378. else
  379. $s .= $t->treeNodes[0]->value;
  380. $s .= ':' . $this->parseTree($t->treeNodes[1]);
  381. }
  382. else
  383. {
  384. $s .= $t->type == JS_GETTER ? 'get' : 'set';
  385. $s .= ' ' . $t->name . '(';
  386. $params = $t->params;
  387. for ($i = 0, $j = count($params); $i < $j; $i++)
  388. $s .= ($i ? ',' : '') . $params[$i];
  389. $s .= '){' . $this->parseTree($t->body, true) . '}';
  390. }
  391. }
  392. $s .= '}';
  393. break;
  394. case KEYWORD_NULL: case KEYWORD_THIS: case KEYWORD_TRUE: case KEYWORD_FALSE:
  395. case TOKEN_IDENTIFIER: case TOKEN_NUMBER: case TOKEN_STRING: case TOKEN_REGEXP:
  396. $s = $n->value;
  397. break;
  398. case JS_GROUP:
  399. $s = '(' . $this->parseTree($n->treeNodes[0]) . ')';
  400. break;
  401. default:
  402. throw new Exception('UNKNOWN TOKEN TYPE: ' . $n->type);
  403. }
  404. return $s;
  405. }
  406. private function isValidIdentifier($string)
  407. {
  408. return preg_match('/^[a-zA-Z_][a-zA-Z0-9_]*$/', $string) && !in_array($string, $this->reserved);
  409. }
  410. }
  411. class JSParser
  412. {
  413. private $t;
  414. private $opPrecedence = array(
  415. ';' => 0,
  416. ',' => 1,
  417. '=' => 2, '?' => 2, ':' => 2,
  418. // The above all have to have the same precedence, see bug 330975.
  419. '||' => 4,
  420. '&&' => 5,
  421. '|' => 6,
  422. '^' => 7,
  423. '&' => 8,
  424. '==' => 9, '!=' => 9, '===' => 9, '!==' => 9,
  425. '<' => 10, '<=' => 10, '>=' => 10, '>' => 10, 'in' => 10, 'instanceof' => 10,
  426. '<<' => 11, '>>' => 11, '>>>' => 11,
  427. '+' => 12, '-' => 12,
  428. '*' => 13, '/' => 13, '%' => 13,
  429. 'delete' => 14, 'void' => 14, 'typeof' => 14,
  430. '!' => 14, '~' => 14, 'U+' => 14, 'U-' => 14,
  431. '++' => 15, '--' => 15,
  432. 'new' => 16,
  433. '.' => 17,
  434. JS_NEW_WITH_ARGS => 0, JS_INDEX => 0, JS_CALL => 0,
  435. JS_ARRAY_INIT => 0, JS_OBJECT_INIT => 0, JS_GROUP => 0
  436. );
  437. private $opArity = array(
  438. ',' => -2,
  439. '=' => 2,
  440. '?' => 3,
  441. '||' => 2,
  442. '&&' => 2,
  443. '|' => 2,
  444. '^' => 2,
  445. '&' => 2,
  446. '==' => 2, '!=' => 2, '===' => 2, '!==' => 2,
  447. '<' => 2, '<=' => 2, '>=' => 2, '>' => 2, 'in' => 2, 'instanceof' => 2,
  448. '<<' => 2, '>>' => 2, '>>>' => 2,
  449. '+' => 2, '-' => 2,
  450. '*' => 2, '/' => 2, '%' => 2,
  451. 'delete' => 1, 'void' => 1, 'typeof' => 1,
  452. '!' => 1, '~' => 1, 'U+' => 1, 'U-' => 1,
  453. '++' => 1, '--' => 1,
  454. 'new' => 1,
  455. '.' => 2,
  456. JS_NEW_WITH_ARGS => 2, JS_INDEX => 2, JS_CALL => 2,
  457. JS_ARRAY_INIT => 1, JS_OBJECT_INIT => 1, JS_GROUP => 1,
  458. TOKEN_CONDCOMMENT_MULTILINE => 1
  459. );
  460. public function __construct()
  461. {
  462. $this->t = new JSTokenizer();
  463. }
  464. public function parse($s, $f, $l)
  465. {
  466. // initialize tokenizer
  467. $this->t->init($s, $f, $l);
  468. $x = new JSCompilerContext(false);
  469. $n = $this->Script($x);
  470. if (!$this->t->isDone())
  471. throw $this->t->newSyntaxError('Syntax error');
  472. return $n;
  473. }
  474. private function Script($x)
  475. {
  476. $n = $this->Statements($x);
  477. $n->type = JS_SCRIPT;
  478. $n->funDecls = $x->funDecls;
  479. $n->varDecls = $x->varDecls;
  480. return $n;
  481. }
  482. private function Statements($x)
  483. {
  484. $n = new JSNode($this->t, JS_BLOCK);
  485. array_push($x->stmtStack, $n);
  486. while (!$this->t->isDone() && $this->t->peek() != OP_RIGHT_CURLY)
  487. $n->addNode($this->Statement($x));
  488. array_pop($x->stmtStack);
  489. return $n;
  490. }
  491. private function Block($x)
  492. {
  493. $this->t->mustMatch(OP_LEFT_CURLY);
  494. $n = $this->Statements($x);
  495. $this->t->mustMatch(OP_RIGHT_CURLY);
  496. return $n;
  497. }
  498. private function Statement($x)
  499. {
  500. $tt = $this->t->get();
  501. $n2 = null;
  502. // Cases for statements ending in a right curly return early, avoiding the
  503. // common semicolon insertion magic after this switch.
  504. switch ($tt)
  505. {
  506. case KEYWORD_FUNCTION:
  507. return $this->FunctionDefinition(
  508. $x,
  509. true,
  510. count($x->stmtStack) > 1 ? STATEMENT_FORM : DECLARED_FORM
  511. );
  512. break;
  513. case OP_LEFT_CURLY:
  514. $n = $this->Statements($x);
  515. $this->t->mustMatch(OP_RIGHT_CURLY);
  516. return $n;
  517. case KEYWORD_IF:
  518. $n = new JSNode($this->t);
  519. $n->condition = $this->ParenExpression($x);
  520. array_push($x->stmtStack, $n);
  521. $n->thenPart = $this->Statement($x);
  522. $n->elsePart = $this->t->match(KEYWORD_ELSE) ? $this->Statement($x) : null;
  523. array_pop($x->stmtStack);
  524. return $n;
  525. case KEYWORD_SWITCH:
  526. $n = new JSNode($this->t);
  527. $this->t->mustMatch(OP_LEFT_PAREN);
  528. $n->discriminant = $this->Expression($x);
  529. $this->t->mustMatch(OP_RIGHT_PAREN);
  530. $n->cases = array();
  531. $n->defaultIndex = -1;
  532. array_push($x->stmtStack, $n);
  533. $this->t->mustMatch(OP_LEFT_CURLY);
  534. while (($tt = $this->t->get()) != OP_RIGHT_CURLY)
  535. {
  536. switch ($tt)
  537. {
  538. case KEYWORD_DEFAULT:
  539. if ($n->defaultIndex >= 0)
  540. throw $this->t->newSyntaxError('More than one switch default');
  541. // FALL THROUGH
  542. case KEYWORD_CASE:
  543. $n2 = new JSNode($this->t);
  544. if ($tt == KEYWORD_DEFAULT)
  545. $n->defaultIndex = count($n->cases);
  546. else
  547. $n2->caseLabel = $this->Expression($x, OP_COLON);
  548. break;
  549. default:
  550. throw $this->t->newSyntaxError('Invalid switch case');
  551. }
  552. $this->t->mustMatch(OP_COLON);
  553. $n2->statements = new JSNode($this->t, JS_BLOCK);
  554. while (($tt = $this->t->peek()) != KEYWORD_CASE && $tt != KEYWORD_DEFAULT && $tt != OP_RIGHT_CURLY)
  555. $n2->statements->addNode($this->Statement($x));
  556. array_push($n->cases, $n2);
  557. }
  558. array_pop($x->stmtStack);
  559. return $n;
  560. case KEYWORD_FOR:
  561. $n = new JSNode($this->t);
  562. $n->isLoop = true;
  563. $this->t->mustMatch(OP_LEFT_PAREN);
  564. if (($tt = $this->t->peek()) != OP_SEMICOLON)
  565. {
  566. $x->inForLoopInit = true;
  567. if ($tt == KEYWORD_VAR || $tt == KEYWORD_CONST)
  568. {
  569. $this->t->get();
  570. $n2 = $this->Variables($x);
  571. }
  572. else
  573. {
  574. $n2 = $this->Expression($x);
  575. }
  576. $x->inForLoopInit = false;
  577. }
  578. if ($n2 && $this->t->match(KEYWORD_IN))
  579. {
  580. $n->type = JS_FOR_IN;
  581. if ($n2->type == KEYWORD_VAR)
  582. {
  583. if (count($n2->treeNodes) != 1)
  584. {
  585. throw $this->t->SyntaxError(
  586. 'Invalid for..in left-hand side',
  587. $this->t->filename,
  588. $n2->lineno
  589. );
  590. }
  591. // NB: n2[0].type == IDENTIFIER and n2[0].value == n2[0].name.
  592. $n->iterator = $n2->treeNodes[0];
  593. $n->varDecl = $n2;
  594. }
  595. else
  596. {
  597. $n->iterator = $n2;
  598. $n->varDecl = null;
  599. }
  600. $n->object = $this->Expression($x);
  601. }
  602. else
  603. {
  604. $n->setup = $n2 ? $n2 : null;
  605. $this->t->mustMatch(OP_SEMICOLON);
  606. $n->condition = $this->t->peek() == OP_SEMICOLON ? null : $this->Expression($x);
  607. $this->t->mustMatch(OP_SEMICOLON);
  608. $n->update = $this->t->peek() == OP_RIGHT_PAREN ? null : $this->Expression($x);
  609. }
  610. $this->t->mustMatch(OP_RIGHT_PAREN);
  611. $n->body = $this->nest($x, $n);
  612. return $n;
  613. case KEYWORD_WHILE:
  614. $n = new JSNode($this->t);
  615. $n->isLoop = true;
  616. $n->condition = $this->ParenExpression($x);
  617. $n->body = $this->nest($x, $n);
  618. return $n;
  619. case KEYWORD_DO:
  620. $n = new JSNode($this->t);
  621. $n->isLoop = true;
  622. $n->body = $this->nest($x, $n, KEYWORD_WHILE);
  623. $n->condition = $this->ParenExpression($x);
  624. if (!$x->ecmaStrictMode)
  625. {
  626. // <script language="JavaScript"> (without version hints) may need
  627. // automatic semicolon insertion without a newline after do-while.
  628. // See http://bugzilla.mozilla.org/show_bug.cgi?id=238945.
  629. $this->t->match(OP_SEMICOLON);
  630. return $n;
  631. }
  632. break;
  633. case KEYWORD_BREAK:
  634. case KEYWORD_CONTINUE:
  635. $n = new JSNode($this->t);
  636. if ($this->t->peekOnSameLine() == TOKEN_IDENTIFIER)
  637. {
  638. $this->t->get();
  639. $n->label = $this->t->currentToken()->value;
  640. }
  641. $ss = $x->stmtStack;
  642. $i = count($ss);
  643. $label = $n->label;
  644. if ($label)
  645. {
  646. do
  647. {
  648. if (--$i < 0)
  649. throw $this->t->newSyntaxError('Label not found');
  650. }
  651. while ($ss[$i]->label != $label);
  652. }
  653. else
  654. {
  655. do
  656. {
  657. if (--$i < 0)
  658. throw $this->t->newSyntaxError('Invalid ' . $tt);
  659. }
  660. while (!$ss[$i]->isLoop && ($tt != KEYWORD_BREAK || $ss[$i]->type != KEYWORD_SWITCH));
  661. }
  662. $n->target = $ss[$i];
  663. break;
  664. case KEYWORD_TRY:
  665. $n = new JSNode($this->t);
  666. $n->tryBlock = $this->Block($x);
  667. $n->catchClauses = array();
  668. while ($this->t->match(KEYWORD_CATCH))
  669. {
  670. $n2 = new JSNode($this->t);
  671. $this->t->mustMatch(OP_LEFT_PAREN);
  672. $n2->varName = $this->t->mustMatch(TOKEN_IDENTIFIER)->value;
  673. if ($this->t->match(KEYWORD_IF))
  674. {
  675. if ($x->ecmaStrictMode)
  676. throw $this->t->newSyntaxError('Illegal catch guard');
  677. if (count($n->catchClauses) && !end($n->catchClauses)->guard)
  678. throw $this->t->newSyntaxError('Guarded catch after unguarded');
  679. $n2->guard = $this->Expression($x);
  680. }
  681. else
  682. {
  683. $n2->guard = null;
  684. }
  685. $this->t->mustMatch(OP_RIGHT_PAREN);
  686. $n2->block = $this->Block($x);
  687. array_push($n->catchClauses, $n2);
  688. }
  689. if ($this->t->match(KEYWORD_FINALLY))
  690. $n->finallyBlock = $this->Block($x);
  691. if (!count($n->catchClauses) && !$n->finallyBlock)
  692. throw $this->t->newSyntaxError('Invalid try statement');
  693. return $n;
  694. case KEYWORD_CATCH:
  695. case KEYWORD_FINALLY:
  696. throw $this->t->newSyntaxError($tt + ' without preceding try');
  697. case KEYWORD_THROW:
  698. $n = new JSNode($this->t);
  699. $n->exception = $this->Expression($x);
  700. break;
  701. case KEYWORD_RETURN:
  702. if (!$x->inFunction)
  703. throw $this->t->newSyntaxError('Invalid return');
  704. $n = new JSNode($this->t);
  705. $tt = $this->t->peekOnSameLine();
  706. if ($tt != TOKEN_END && $tt != TOKEN_NEWLINE && $tt != OP_SEMICOLON && $tt != OP_RIGHT_CURLY)
  707. $n->value = $this->Expression($x);
  708. else
  709. $n->value = null;
  710. break;
  711. case KEYWORD_WITH:
  712. $n = new JSNode($this->t);
  713. $n->object = $this->ParenExpression($x);
  714. $n->body = $this->nest($x, $n);
  715. return $n;
  716. case KEYWORD_VAR:
  717. case KEYWORD_CONST:
  718. $n = $this->Variables($x);
  719. break;
  720. case TOKEN_CONDCOMMENT_MULTILINE:
  721. $n = new JSNode($this->t);
  722. return $n;
  723. case KEYWORD_DEBUGGER:
  724. $n = new JSNode($this->t);
  725. break;
  726. case TOKEN_NEWLINE:
  727. case OP_SEMICOLON:
  728. $n = new JSNode($this->t, OP_SEMICOLON);
  729. $n->expression = null;
  730. return $n;
  731. default:
  732. if ($tt == TOKEN_IDENTIFIER)
  733. {
  734. $this->t->scanOperand = false;
  735. $tt = $this->t->peek();
  736. $this->t->scanOperand = true;
  737. if ($tt == OP_COLON)
  738. {
  739. $label = $this->t->currentToken()->value;
  740. $ss = $x->stmtStack;
  741. for ($i = count($ss) - 1; $i >= 0; --$i)
  742. {
  743. if ($ss[$i]->label == $label)
  744. throw $this->t->newSyntaxError('Duplicate label');
  745. }
  746. $this->t->get();
  747. $n = new JSNode($this->t, JS_LABEL);
  748. $n->label = $label;
  749. $n->statement = $this->nest($x, $n);
  750. return $n;
  751. }
  752. }
  753. $n = new JSNode($this->t, OP_SEMICOLON);
  754. $this->t->unget();
  755. $n->expression = $this->Expression($x);
  756. $n->end = $n->expression->end;
  757. break;
  758. }
  759. if ($this->t->lineno == $this->t->currentToken()->lineno)
  760. {
  761. $tt = $this->t->peekOnSameLine();
  762. if ($tt != TOKEN_END && $tt != TOKEN_NEWLINE && $tt != OP_SEMICOLON && $tt != OP_RIGHT_CURLY)
  763. throw $this->t->newSyntaxError('Missing ; before statement');
  764. }
  765. $this->t->match(OP_SEMICOLON);
  766. return $n;
  767. }
  768. private function FunctionDefinition($x, $requireName, $functionForm)
  769. {
  770. $f = new JSNode($this->t);
  771. if ($f->type != KEYWORD_FUNCTION)
  772. $f->type = ($f->value == 'get') ? JS_GETTER : JS_SETTER;
  773. if ($this->t->match(TOKEN_IDENTIFIER))
  774. $f->name = $this->t->currentToken()->value;
  775. elseif ($requireName)
  776. throw $this->t->newSyntaxError('Missing function identifier');
  777. $this->t->mustMatch(OP_LEFT_PAREN);
  778. $f->params = array();
  779. while (($tt = $this->t->get()) != OP_RIGHT_PAREN)
  780. {
  781. if ($tt != TOKEN_IDENTIFIER)
  782. throw $this->t->newSyntaxError('Missing formal parameter');
  783. array_push($f->params, $this->t->currentToken()->value);
  784. if ($this->t->peek() != OP_RIGHT_PAREN)
  785. $this->t->mustMatch(OP_COMMA);
  786. }
  787. $this->t->mustMatch(OP_LEFT_CURLY);
  788. $x2 = new JSCompilerContext(true);
  789. $f->body = $this->Script($x2);
  790. $this->t->mustMatch(OP_RIGHT_CURLY);
  791. $f->end = $this->t->currentToken()->end;
  792. $f->functionForm = $functionForm;
  793. if ($functionForm == DECLARED_FORM)
  794. array_push($x->funDecls, $f);
  795. return $f;
  796. }
  797. private function Variables($x)
  798. {
  799. $n = new JSNode($this->t);
  800. do
  801. {
  802. $this->t->mustMatch(TOKEN_IDENTIFIER);
  803. $n2 = new JSNode($this->t);
  804. $n2->name = $n2->value;
  805. if ($this->t->match(OP_ASSIGN))
  806. {
  807. if ($this->t->currentToken()->assignOp)
  808. throw $this->t->newSyntaxError('Invalid variable initialization');
  809. $n2->initializer = $this->Expression($x, OP_COMMA);
  810. }
  811. $n2->readOnly = $n->type == KEYWORD_CONST;
  812. $n->addNode($n2);
  813. array_push($x->varDecls, $n2);
  814. }
  815. while ($this->t->match(OP_COMMA));
  816. return $n;
  817. }
  818. private function Expression($x, $stop=false)
  819. {
  820. $operators = array();
  821. $operands = array();
  822. $n = false;
  823. $bl = $x->bracketLevel;
  824. $cl = $x->curlyLevel;
  825. $pl = $x->parenLevel;
  826. $hl = $x->hookLevel;
  827. while (($tt = $this->t->get()) != TOKEN_END)
  828. {
  829. if ($tt == $stop &&
  830. $x->bracketLevel == $bl &&
  831. $x->curlyLevel == $cl &&
  832. $x->parenLevel == $pl &&
  833. $x->hookLevel == $hl
  834. )
  835. {
  836. // Stop only if tt matches the optional stop parameter, and that
  837. // token is not quoted by some kind of bracket.
  838. break;
  839. }
  840. switch ($tt)
  841. {
  842. case OP_SEMICOLON:
  843. // NB: cannot be empty, Statement handled that.
  844. break 2;
  845. case OP_ASSIGN:
  846. case OP_HOOK:
  847. case OP_COLON:
  848. if ($this->t->scanOperand)
  849. break 2;
  850. // Use >, not >=, for right-associative ASSIGN and HOOK/COLON.
  851. while ( !empty($operators) &&
  852. ( $this->opPrecedence[end($operators)->type] > $this->opPrecedence[$tt] ||
  853. ($tt == OP_COLON && end($operators)->type == OP_ASSIGN)
  854. )
  855. )
  856. $this->reduce($operators, $operands);
  857. if ($tt == OP_COLON)
  858. {
  859. $n = end($operators);
  860. if ($n->type != OP_HOOK)
  861. throw $this->t->newSyntaxError('Invalid label');
  862. --$x->hookLevel;
  863. }
  864. else
  865. {
  866. array_push($operators, new JSNode($this->t));
  867. if ($tt == OP_ASSIGN)
  868. end($operands)->assignOp = $this->t->currentToken()->assignOp;
  869. else
  870. ++$x->hookLevel;
  871. }
  872. $this->t->scanOperand = true;
  873. break;
  874. case KEYWORD_IN:
  875. // An in operator should not be parsed if we're parsing the head of
  876. // a for (...) loop, unless it is in the then part of a conditional
  877. // expression, or parenthesized somehow.
  878. if ($x->inForLoopInit && !$x->hookLevel &&
  879. !$x->bracketLevel && !$x->curlyLevel &&
  880. !$x->parenLevel
  881. )
  882. {
  883. break 2;
  884. }
  885. // FALL THROUGH
  886. case OP_COMMA:
  887. // Treat comma as left-associative so reduce can fold left-heavy
  888. // COMMA trees into a single array.
  889. // FALL THROUGH
  890. case OP_OR:
  891. case OP_AND:
  892. case OP_BITWISE_OR:
  893. case OP_BITWISE_XOR:
  894. case OP_BITWISE_AND:
  895. case OP_EQ: case OP_NE: case OP_STRICT_EQ: case OP_STRICT_NE:
  896. case OP_LT: case OP_LE: case OP_GE: case OP_GT:
  897. case KEYWORD_INSTANCEOF:
  898. case OP_LSH: case OP_RSH: case OP_URSH:
  899. case OP_PLUS: case OP_MINUS:
  900. case OP_MUL: case OP_DIV: case OP_MOD:
  901. case OP_DOT:
  902. if ($this->t->scanOperand)
  903. break 2;
  904. while ( !empty($operators) &&
  905. $this->opPrecedence[end($operators)->type] >= $this->opPrecedence[$tt]
  906. )
  907. $this->reduce($operators, $operands);
  908. if ($tt == OP_DOT)
  909. {
  910. $this->t->mustMatch(TOKEN_IDENTIFIER);
  911. array_push($operands, new JSNode($this->t, OP_DOT, array_pop($operands), new JSNode($this->t)));
  912. }
  913. else
  914. {
  915. array_push($operators, new JSNode($this->t));
  916. $this->t->scanOperand = true;
  917. }
  918. break;
  919. case KEYWORD_DELETE: case KEYWORD_VOID: case KEYWORD_TYPEOF:
  920. case OP_NOT: case OP_BITWISE_NOT: case OP_UNARY_PLUS: case OP_UNARY_MINUS:
  921. case KEYWORD_NEW:
  922. if (!$this->t->scanOperand)
  923. break 2;
  924. array_push($operators, new JSNode($this->t));
  925. break;
  926. case OP_INCREMENT: case OP_DECREMENT:
  927. if ($this->t->scanOperand)
  928. {
  929. array_push($operators, new JSNode($this->t)); // prefix increment or decrement
  930. }
  931. else
  932. {
  933. // Don't cross a line boundary for postfix {in,de}crement.
  934. $t = $this->t->tokens[($this->t->tokenIndex + $this->t->lookahead - 1) & 3];
  935. if ($t && $t->lineno != $this->t->lineno)
  936. break 2;
  937. if (!empty($operators))
  938. {
  939. // Use >, not >=, so postfix has higher precedence than prefix.
  940. while ($this->opPrecedence[end($operators)->type] > $this->opPrecedence[$tt])
  941. $this->reduce($operators, $operands);
  942. }
  943. $n = new JSNode($this->t, $tt, array_pop($operands));
  944. $n->postfix = true;
  945. array_push($operands, $n);
  946. }
  947. break;
  948. case KEYWORD_FUNCTION:
  949. if (!$this->t->scanOperand)
  950. break 2;
  951. array_push($operands, $this->FunctionDefinition($x, false, EXPRESSED_FORM));
  952. $this->t->scanOperand = false;
  953. break;
  954. case KEYWORD_NULL: case KEYWORD_THIS: case KEYWORD_TRUE: case KEYWORD_FALSE:
  955. case TOKEN_IDENTIFIER: case TOKEN_NUMBER: case TOKEN_STRING: case TOKEN_REGEXP:
  956. if (!$this->t->scanOperand)
  957. break 2;
  958. array_push($operands, new JSNode($this->t));
  959. $this->t->scanOperand = false;
  960. break;
  961. case TOKEN_CONDCOMMENT_MULTILINE:
  962. if ($this->t->scanOperand)
  963. array_push($operators, new JSNode($this->t));
  964. else
  965. array_push($operands, new JSNode($this->t));
  966. break;
  967. case OP_LEFT_BRACKET:
  968. if ($this->t->scanOperand)
  969. {
  970. // Array initialiser. Parse using recursive descent, as the
  971. // sub-grammar here is not an operator grammar.
  972. $n = new JSNode($this->t, JS_ARRAY_INIT);
  973. while (($tt = $this->t->peek()) != OP_RIGHT_BRACKET)
  974. {
  975. if ($tt == OP_COMMA)
  976. {
  977. $this->t->get();
  978. $n->addNode(null);
  979. continue;
  980. }
  981. $n->addNode($this->Expression($x, OP_COMMA));
  982. if (!$this->t->match(OP_COMMA))
  983. break;
  984. }
  985. $this->t->mustMatch(OP_RIGHT_BRACKET);
  986. array_push($operands, $n);
  987. $this->t->scanOperand = false;
  988. }
  989. else
  990. {
  991. // Property indexing operator.
  992. array_push($operators, new JSNode($this->t, JS_INDEX));
  993. $this->t->scanOperand = true;
  994. ++$x->bracketLevel;
  995. }
  996. break;
  997. case OP_RIGHT_BRACKET:
  998. if ($this->t->scanOperand || $x->bracketLevel == $bl)
  999. break 2;
  1000. while ($this->reduce($operators, $operands)->type != JS_INDEX)
  1001. continue;
  1002. --$x->bracketLevel;
  1003. break;
  1004. case OP_LEFT_CURLY:
  1005. if (!$this->t->scanOperand)
  1006. break 2;
  1007. // Object initialiser. As for array initialisers (see above),
  1008. // parse using recursive descent.
  1009. ++$x->curlyLevel;
  1010. $n = new JSNode($this->t, JS_OBJECT_INIT);
  1011. while (!$this->t->match(OP_RIGHT_CURLY))
  1012. {
  1013. do
  1014. {
  1015. $tt = $this->t->get();
  1016. $tv = $this->t->currentToken()->value;
  1017. if (($tv == 'get' || $tv == 'set') && $this->t->peek() == TOKEN_IDENTIFIER)
  1018. {
  1019. if ($x->ecmaStrictMode)
  1020. throw $this->t->newSyntaxError('Illegal property accessor');
  1021. $n->addNode($this->FunctionDefinition($x, true, EXPRESSED_FORM));
  1022. }
  1023. else
  1024. {
  1025. switch ($tt)
  1026. {
  1027. case TOKEN_IDENTIFIER:
  1028. case TOKEN_NUMBER:
  1029. case TOKEN_STRING:
  1030. $id = new JSNode($this->t);
  1031. break;
  1032. case OP_RIGHT_CURLY:
  1033. if ($x->ecmaStrictMode)
  1034. throw $this->t->newSyntaxError('Illegal trailing ,');
  1035. break 3;
  1036. default:
  1037. throw $this->t->newSyntaxError('Invalid property name');
  1038. }
  1039. $this->t->mustMatch(OP_COLON);
  1040. $n->addNode(new JSNode($this->t, JS_PROPERTY_INIT, $id, $this->Expression($x, OP_COMMA)));
  1041. }
  1042. }
  1043. while ($this->t->match(OP_COMMA));
  1044. $this->t->mustMatch(OP_RIGHT_CURLY);
  1045. break;
  1046. }
  1047. array_push($operands, $n);
  1048. $this->t->scanOperand = false;
  1049. --$x->curlyLevel;
  1050. break;
  1051. case OP_RIGHT_CURLY:
  1052. if (!$this->t->scanOperand && $x->curlyLevel != $cl)
  1053. throw new Exception('PANIC: right curly botch');
  1054. break 2;
  1055. case OP_LEFT_PAREN:
  1056. if ($this->t->scanOperand)
  1057. {
  1058. array_push($operators, new JSNode($this->t, JS_GROUP));
  1059. }
  1060. else
  1061. {
  1062. while ( !empty($operators) &&
  1063. $this->opPrecedence[end($operators)->type] > $this->opPrecedence[KEYWORD_NEW]
  1064. )
  1065. $this->reduce($operators, $operands);
  1066. // Handle () now, to regularize the n-ary case for n > 0.
  1067. // We must set scanOperand in case there are arguments and
  1068. // the first one is a regexp or unary+/-.
  1069. $n = end($operators);
  1070. $this->t->scanOperand = true;
  1071. if ($this->t->match(OP_RIGHT_PAREN))
  1072. {
  1073. if ($n && $n->type == KEYWORD_NEW)
  1074. {
  1075. array_pop($operators);
  1076. $n->addNode(array_pop($operands));
  1077. }
  1078. else
  1079. {
  1080. $n = new JSNode($this->t, JS_CALL, array_pop($operands), new JSNode($this->t, JS_LIST));
  1081. }
  1082. array_push($operands, $n);
  1083. $this->t->scanOperand = false;
  1084. break;
  1085. }
  1086. if ($n && $n->type == KEYWORD_NEW)
  1087. $n->type = JS_NEW_WITH_ARGS;
  1088. else
  1089. array_push($operators, new JSNode($this->t, JS_CALL));
  1090. }
  1091. ++$x->parenLevel;
  1092. break;
  1093. case OP_RIGHT_PAREN:
  1094. if ($this->t->scanOperand || $x->parenLevel == $pl)
  1095. break 2;
  1096. while (($tt = $this->reduce($operators, $operands)->type) != JS_GROUP &&
  1097. $tt != JS_CALL && $tt != JS_NEW_WITH_ARGS
  1098. )
  1099. {
  1100. continue;
  1101. }
  1102. if ($tt != JS_GROUP)
  1103. {
  1104. $n = end($operands);
  1105. if ($n->treeNodes[1]->type != OP_COMMA)
  1106. $n->treeNodes[1] = new JSNode($this->t, JS_LIST, $n->treeNodes[1]);
  1107. else
  1108. $n->treeNodes[1]->type = JS_LIST;
  1109. }
  1110. --$x->parenLevel;
  1111. break;
  1112. // Automatic semicolon insertion means we may scan across a newline
  1113. // and into the beginning of another statement. If so, break out of
  1114. // the while loop and let the t.scanOperand logic handle errors.
  1115. default:
  1116. break 2;
  1117. }
  1118. }
  1119. if ($x->hookLevel != $hl)
  1120. throw $this->t->newSyntaxError('Missing : after ?');
  1121. if ($x->parenLevel != $pl)
  1122. throw $this->t->newSyntaxError('Missing ) in parenthetical');
  1123. if ($x->bracketLevel != $bl)
  1124. throw $this->t->newSyntaxError('Missing ] in index expression');
  1125. if ($this->t->scanOperand)
  1126. throw $this->t->newSyntaxError('Missing operand');
  1127. // Resume default mode, scanning for operands, not operators.
  1128. $this->t->scanOperand = true;
  1129. $this->t->unget();
  1130. while (count($operators))
  1131. $this->reduce($operators, $operands);
  1132. return array_pop($operands);
  1133. }
  1134. private function ParenExpression($x)
  1135. {
  1136. $this->t->mustMatch(OP_LEFT_PAREN);
  1137. $n = $this->Expression($x);
  1138. $this->t->mustMatch(OP_RIGHT_PAREN);
  1139. return $n;
  1140. }
  1141. // Statement stack and nested statement handler.
  1142. private function nest($x, $node, $end = false)
  1143. {
  1144. array_push($x->stmtStack, $node);
  1145. $n = $this->statement($x);
  1146. array_pop($x->stmtStack);
  1147. if ($end)
  1148. $this->t->mustMatch($end);
  1149. return $n;
  1150. }
  1151. private function reduce(&$operators, &$operands)
  1152. {
  1153. $n = array_pop($operators);
  1154. $op = $n->type;
  1155. $arity = $this->opArity[$op];
  1156. $c = count($operands);
  1157. if ($arity == -2)
  1158. {
  1159. // Flatten left-associative trees
  1160. if ($c >= 2)
  1161. {
  1162. $left = $operands[$c - 2];
  1163. if ($left->type == $op)
  1164. {
  1165. $right = array_pop($operands);
  1166. $left->addNode($right);
  1167. return $left;
  1168. }
  1169. }
  1170. $arity = 2;
  1171. }
  1172. // Always use push to add operands to n, to update start and end
  1173. $a = array_splice($operands, $c - $arity);
  1174. for ($i = 0; $i < $arity; $i++)
  1175. $n->addNode($a[$i]);
  1176. // Include closing bracket or postfix operator in [start,end]
  1177. $te = $this->t->currentToken()->end;
  1178. if ($n->end < $te)
  1179. $n->end = $te;
  1180. array_push($operands, $n);
  1181. return $n;
  1182. }
  1183. }
  1184. class JSCompilerContext
  1185. {
  1186. public $inFunction = false;
  1187. public $inForLoopInit = false;
  1188. public $ecmaStrictMode = false;
  1189. public $bracketLevel = 0;
  1190. public $curlyLevel = 0;
  1191. public $parenLevel = 0;
  1192. public $hookLevel = 0;
  1193. public $stmtStack = array();
  1194. public $funDecls = array();
  1195. public $varDecls = array();
  1196. public function __construct($inFunction)
  1197. {
  1198. $this->inFunction = $inFunction;
  1199. }
  1200. }
  1201. class JSNode
  1202. {
  1203. private $type;
  1204. private $value;
  1205. private $lineno;
  1206. private $start;
  1207. private $end;
  1208. public $treeNodes = array();
  1209. public $funDecls = array();
  1210. public $varDecls = array();
  1211. public function __construct($t, $type=0)
  1212. {
  1213. if ($token = $t->currentToken())
  1214. {
  1215. $this->type = $type ? $type : $token->type;
  1216. $this->value = $token->value;
  1217. $this->lineno = $token->lineno;
  1218. $this->start = $token->start;
  1219. $this->end = $token->end;
  1220. }
  1221. else
  1222. {
  1223. $this->type = $type;
  1224. $this->lineno = $t->lineno;
  1225. }
  1226. if (($numargs = func_num_args()) > 2)
  1227. {
  1228. $args = func_get_args();;
  1229. for ($i = 2; $i < $numargs; $i++)
  1230. $this->addNode($args[$i]);
  1231. }
  1232. }
  1233. // we don't want to bloat our object with all kind of specific properties, so we use overloading
  1234. public function __set($name, $value)
  1235. {
  1236. $this->$name = $value;
  1237. }
  1238. public function __get($name)
  1239. {
  1240. if (isset($this->$name))
  1241. return $this->$name;
  1242. return null;
  1243. }
  1244. public function addNode($node)
  1245. {
  1246. $this->treeNodes[] = $node;
  1247. }
  1248. }
  1249. class JSTokenizer
  1250. {
  1251. private $cursor = 0;
  1252. private $source;
  1253. public $tokens = array();
  1254. public $tokenIndex = 0;
  1255. public $lookahead = 0;
  1256. public $scanNewlines = false;
  1257. public $scanOperand = true;
  1258. public $filename;
  1259. public $lineno;
  1260. private $keywords = array(
  1261. 'break',
  1262. 'case', 'catch', 'const', 'continue',
  1263. 'debugger', 'default', 'delete', 'do',
  1264. 'else', 'enum',
  1265. 'false', 'finally', 'for', 'function',
  1266. 'if', 'in', 'instanceof',
  1267. 'new', 'null',
  1268. 'return',
  1269. 'switch',
  1270. 'this', 'throw', 'true', 'try', 'typeof',
  1271. 'var', 'void',
  1272. 'while', 'with'
  1273. );
  1274. private $opTypeNames = array(
  1275. ';' => 'SEMICOLON',
  1276. ',' => 'COMMA',
  1277. '?' => 'HOOK',
  1278. ':' => 'COLON',
  1279. '||' => 'OR',
  1280. '&&' => 'AND',
  1281. '|' => 'BITWISE_OR',
  1282. '^' => 'BITWISE_XOR',
  1283. '&' => 'BITWISE_AND',
  1284. '===' => 'STRICT_EQ',
  1285. '==' => 'EQ',
  1286. '=' => 'ASSIGN',
  1287. '!==' => 'STRICT_NE',
  1288. '!=' => 'NE',
  1289. '<<' => 'LSH',
  1290. '<=' => 'LE',
  1291. '<' => 'LT',
  1292. '>>>' => 'URSH',
  1293. '>>' => 'RSH',
  1294. '>=' => 'GE',
  1295. '>' => 'GT',
  1296. '++' => 'INCREMENT',
  1297. '--' => 'DECREMENT',
  1298. '+' => 'PLUS',
  1299. '-' => 'MINUS',
  1300. '*' => 'MUL',
  1301. '/' => 'DIV',
  1302. '%' => 'MOD',
  1303. '!' => 'NOT',
  1304. '~' => 'BITWISE_NOT',
  1305. '.' => 'DOT',
  1306. '[' => 'LEFT_BRACKET',
  1307. ']' => 'RIGHT_BRACKET',
  1308. '{' => 'LEFT_CURLY',
  1309. '}' => 'RIGHT_CURLY',
  1310. '(' => 'LEFT_PAREN',
  1311. ')' => 'RIGHT_PAREN',
  1312. '@*/' => 'CONDCOMMENT_END'
  1313. );
  1314. private $assignOps = array('|', '^', '&', '<<', '>>', '>>>', '+', '-', '*', '/', '%');
  1315. private $opRegExp;
  1316. public function __construct()
  1317. {
  1318. $this->opRegExp = '#^(' . implode('|', array_map('preg_quote', array_keys($this->opTypeNames))) . ')#';
  1319. // this is quite a hidden yet convenient place to create the defines for operators and keywords
  1320. foreach ($this->opTypeNames as $operand => $name)
  1321. define('OP_' . $name, $operand);
  1322. define('OP_UNARY_PLUS', 'U+');
  1323. define('OP_UNARY_MINUS', 'U-');
  1324. foreach ($this->keywords as $keyword)
  1325. define('KEYWORD_' . strtoupper($keyword), $keyword);
  1326. }
  1327. public function init($source, $filename = '', $lineno = 1)
  1328. {
  1329. $this->source = $source;
  1330. $this->filename = $filename ? $filename : '[inline]';
  1331. $this->lineno = $lineno;
  1332. $this->cursor = 0;
  1333. $this->tokens = array();
  1334. $this->tokenIndex = 0;
  1335. $this->lookahead = 0;
  1336. $this->scanNewlines = false;
  1337. $this->scanOperand = true;
  1338. }
  1339. public function getInput($chunksize)
  1340. {
  1341. if ($chunksize)
  1342. return substr($this->source, $this->cursor, $chunksize);
  1343. return substr($this->source, $this->cursor);
  1344. }
  1345. public function isDone()
  1346. {
  1347. return $this->peek() == TOKEN_END;
  1348. }
  1349. public function match($tt)
  1350. {
  1351. return $this->get() == $tt || $this->unget();
  1352. }
  1353. public function mustMatch($tt)
  1354. {
  1355. if (!$this->match($tt))
  1356. throw $this->newSyntaxError('Unexpected token; token ' . $tt . ' expected');
  1357. return $this->currentToken();
  1358. }
  1359. public function peek()
  1360. {
  1361. if ($this->lookahead)
  1362. {
  1363. $next = $this->tokens[($this->tokenIndex + $this->lookahead) & 3];
  1364. if ($this->scanNewlines && $next->lineno != $this->lineno)
  1365. $tt = TOKEN_NEWLINE;
  1366. else
  1367. $tt = $next->type;
  1368. }
  1369. else
  1370. {
  1371. $tt = $this->get();
  1372. $this->unget();
  1373. }
  1374. return $tt;
  1375. }
  1376. public function peekOnSameLine()
  1377. {
  1378. $this->scanNewlines = true;
  1379. $tt = $this->peek();
  1380. $this->scanNewlines = false;
  1381. return $tt;
  1382. }
  1383. public function currentToken()
  1384. {
  1385. if (!empty($this->tokens))
  1386. return $this->tokens[$this->tokenIndex];
  1387. }
  1388. public function get($chunksize = 1000)
  1389. {
  1390. while($this->lookahead)
  1391. {
  1392. $this->lookahead--;
  1393. $this->tokenIndex = ($this->tokenIndex + 1) & 3;
  1394. $token = $this->tokens[$this->tokenIndex];
  1395. if ($token->type != TOKEN_NEWLINE || $this->scanNewlines)
  1396. return $token->type;
  1397. }
  1398. $conditional_comment = false;
  1399. // strip whitespace and comments
  1400. while(true)
  1401. {
  1402. $input = $this->getInput($chunksize);
  1403. // whitespace handling; gobble up \r as well (effectively we don't have support for MAC newlines!)
  1404. $re = $this->scanNewlines ? '/^[ \r\t]+/' : '/^\s+/';
  1405. if (preg_match($re, $input, $match))
  1406. {
  1407. $spaces = $match[0];
  1408. $spacelen = strlen($spaces);
  1409. $this->cursor += $spacelen;
  1410. if (!$this->scanNewlines)
  1411. $this->lineno += substr_count($spaces, "\n");
  1412. if ($spacelen == $chunksize)
  1413. continue; // complete chunk contained whitespace
  1414. $input = $this->getInput($chunksize);
  1415. if ($input == '' || $input[0] != '/')
  1416. break;
  1417. }
  1418. // Comments
  1419. if (!preg_match('/^\/(?:\*(@(?:cc_on|if|elif|else|end))?(?:.|\n)*?\*\/|\/.*)/', $input, $match))
  1420. {
  1421. if (!$chunksize)
  1422. break;
  1423. // retry with a full chunk fetch; this also prevents breakage of long regular expressions (which will never match a comment)
  1424. $chunksize = null;
  1425. continue;
  1426. }
  1427. // check if this is a conditional (JScript) comment
  1428. if (!empty($match[1]))
  1429. {
  1430. //$match[0] = '/*' . $match[1];
  1431. $conditional_comment = true;
  1432. break;
  1433. }
  1434. else
  1435. {
  1436. $this->cursor += strlen($match[0]);
  1437. $this->lineno += substr_count($match[0], "\n");
  1438. }
  1439. }
  1440. if ($input == '')
  1441. {
  1442. $tt = TOKEN_END;
  1443. $match = array('');
  1444. }
  1445. elseif ($conditional_comment)
  1446. {
  1447. $tt = TOKEN_CONDCOMMENT_MULTILINE;
  1448. }
  1449. else
  1450. {
  1451. switch ($input[0])
  1452. {
  1453. case '0': case '1': case '2': case '3': case '4':
  1454. case '5': case '6': case '7': case '8': case '9':
  1455. if (preg_match('/^\d+\.\d*(?:[eE][-+]?\d+)?|^\d+(?:\.\d*)?[eE][-+]?\d+/', $input, $match))
  1456. {
  1457. $tt = TOKEN_NUMBER;
  1458. }
  1459. elseif (preg_match('/^0[xX][\da-fA-F]+|^0[0-7]*|^\d+/', $input, $match))
  1460. {
  1461. // this should always match because of \d+
  1462. $tt = TOKEN_NUMBER;
  1463. }
  1464. break;
  1465. case '"':
  1466. case "'":
  1467. if (preg_match('/^"(?:\\\\(?:.|\r?\n)|[^\\\\"\r\n])*"|^\'(?:\\\\(?:.|\r?\n)|[^\\\\\'\r\n])*\'/', $input, $match))
  1468. {
  1469. $tt = TOKEN_STRING;
  1470. }
  1471. else
  1472. {
  1473. if ($chunksize)
  1474. return $this->get(null); // retry with a full chunk fetch
  1475. throw $this->newSyntaxError('Unterminated string literal');
  1476. }
  1477. break;
  1478. case '/':
  1479. if ($this->scanOperand && preg_match('/^\/((?:\\\\.|\[(?:\\\\.|[^\]])*\]|[^\/])+)\/([gimy]*)/', $input, $match))
  1480. {
  1481. $tt = TOKEN_REGEXP;
  1482. break;
  1483. }
  1484. // fall through
  1485. case '|':
  1486. case '^':
  1487. case '&':
  1488. case '<':
  1489. case '>':
  1490. case '+':
  1491. case '-':
  1492. case '*':
  1493. case '%':
  1494. case '=':
  1495. case '!':
  1496. // should always match
  1497. preg_match($this->opRegExp, $input, $match);
  1498. $op = $match[0];
  1499. if (in_array($op, $this->assignOps) && $input[strlen($op)] == '=')
  1500. {
  1501. $tt = OP_ASSIGN;
  1502. $match[0] .= '=';
  1503. }
  1504. else
  1505. {
  1506. $tt = $op;
  1507. if ($this->scanOperand)
  1508. {
  1509. if ($op == OP_PLUS)
  1510. $tt = OP_UNARY_PLUS;
  1511. elseif ($op == OP_MINUS)
  1512. $tt = OP_UNARY_MINUS;
  1513. }
  1514. $op = null;
  1515. }
  1516. break;
  1517. case '.':
  1518. if (preg_match('/^\.\d+(?:[eE][-+]?\d+)?/', $input, $match))
  1519. {
  1520. $tt = TOKEN_NUMBER;
  1521. break;
  1522. }
  1523. // fall through
  1524. case ';':
  1525. case ',':
  1526. case '?':
  1527. case ':':
  1528. case '~':
  1529. case '[':
  1530. case ']':
  1531. case '{':
  1532. case '}':
  1533. case '(':
  1534. case ')':
  1535. // these are all single
  1536. $match = array($input[0]);
  1537. $tt = $input[0];
  1538. break;
  1539. case '@':
  1540. throw $this->newSyntaxError('Illegal token');
  1541. break;
  1542. case "\n":
  1543. if ($this->scanNewlines)
  1544. {
  1545. $match = array("\n");
  1546. $tt = TOKEN_NEWLINE;
  1547. }
  1548. else
  1549. throw $this->newSyntaxError('Illegal token');
  1550. break;
  1551. default:
  1552. // FIXME: add support for unicode and unicode escape sequence \uHHHH
  1553. if (preg_match('/^[$\w]+/', $input, $match))
  1554. {
  1555. $tt = in_array($match[0], $this->keywords) ? $match[0] : TOKEN_IDENTIFIER;
  1556. }
  1557. else
  1558. throw $this->newSyntaxError('Illegal token');
  1559. }
  1560. }
  1561. $this->tokenIndex = ($this->tokenIndex + 1) & 3;
  1562. if (!isset($this->tokens[$this->tokenIndex]))
  1563. $this->tokens[$this->tokenIndex] = new JSToken();
  1564. $token = $this->tokens[$this->tokenIndex];
  1565. $token->type = $tt;
  1566. if ($tt == OP_ASSIGN)
  1567. $token->assignOp = $op;
  1568. $token->start = $this->cursor;
  1569. $token->value = $match[0];
  1570. $this->cursor += strlen($match[0]);
  1571. $token->end = $this->cursor;
  1572. $token->lineno = $this->lineno;
  1573. return $tt;
  1574. }
  1575. public function unget()
  1576. {
  1577. if (++$this->lookahead == 4)
  1578. throw $this->newSyntaxError('PANIC: too much lookahead!');
  1579. $this->tokenIndex = ($this->tokenIndex - 1) & 3;
  1580. }
  1581. public function newSyntaxError($m)
  1582. {
  1583. return new Exception('Parse error: ' . $m . ' in file \'' . $this->filename . '\' on line ' . $this->lineno);
  1584. }
  1585. }
  1586. class JSToken
  1587. {
  1588. public $type;
  1589. public $value;
  1590. public $start;
  1591. public $end;
  1592. public $lineno;
  1593. public $assignOp;
  1594. }
  1595. ?>