JSMinPlus.php 51 KB

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