jsminplus.php 55 KB

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