r91591 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r91590‎ | r91591 | r91592 >
Date:20:02, 6 July 2011
Author:brion
Status:resolved (Comments)
Tags:
Comment:
Followup r83885: add JSMin+ 1.3 to use its parser to verify output of JavaScriptMinifierTest unit test cases. Had to change some of the test cases because they were not valid JavaScript programs -- one of the quoting tests was incorrectly quoted in the PHP sources, and several tests around return, continue, and break keywords failed due to not using them in the contexts that they require.

http://crisp.tweakblogs.net/blog/1856/jsmin+-version-13.html
JSMin+ under MPL 1.1/ GPL 2.0 / LGPL 2.1 license.
Modified paths:
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/libs/jsminplus.php (added) (history)
  • /trunk/phase3/tests/phpunit/includes/libs/JavaScriptMinifierTest.php (modified) (history)

Diff [purge]

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

Follow-up revisions

RevisionCommit summaryAuthorDate
r91608* (bug 28626) Validate JavaScript files and pages loaded via ResourceLoader b...brion21:48, 6 July 2011
r92560Remove double ; and final ?> from jsminplus (imported in r91591)....platonides19:53, 19 July 2011
r92563Expand the defines from JSTokenizer::__construct() placing them at the top of...platonides20:03, 19 July 2011
r98281* (bug 31187) Fix for user JavaScript validation to allow identifiers with va...brion22:51, 27 September 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r83885(bug 27528) Incorporate Paul Copperman's minifiercatrope11:44, 14 March 2011

Comments

#Comment by Platonides (talk | contribs)   19:12, 19 July 2011

Is jsminplus.php complete?

Where are all these constants defined?

KEYWORD_BREAK
KEYWORD_CASE
KEYWORD_CATCH
KEYWORD_CONST
KEYWORD_CONTINUE
KEYWORD_DEBUGGER
KEYWORD_DEFAULT
KEYWORD_DELETE
KEYWORD_DO
KEYWORD_ELSE
KEYWORD_FALSE
KEYWORD_FINALLY
KEYWORD_FOR
KEYWORD_FUNCTION
KEYWORD_IF
KEYWORD_IN
KEYWORD_INSTANCEOF
KEYWORD_NEW
KEYWORD_NULL
KEYWORD_RETURN
KEYWORD_SWITCH
KEYWORD_THIS
KEYWORD_THROW
KEYWORD_TRUE
KEYWORD_TRY
KEYWORD_TYPEOF
KEYWORD_VAR
KEYWORD_VOID
KEYWORD_WHILE
KEYWORD_WITH
OP_AND
OP_ASSIGN
OP_BITWISE_AND
OP_BITWISE_NOT
OP_BITWISE_OR
OP_BITWISE_XOR
OP_COLON
OP_COMMA
OP_DECREMENT
OP_DIV
OP_DOT
OP_EQ
OP_GE
OP_GT
OP_HOOK
OP_INCREMENT
OP_LE
OP_LEFT_BRACKET
OP_LEFT_CURLY
OP_LEFT_PAREN
OP_LSH
OP_LT
OP_MINUS
OP_MOD
OP_MUL
OP_NE
OP_NOT
OP_OR
OP_PLUS
OP_RIGHT_BRACKET
OP_RIGHT_CURLY
OP_RIGHT_PAREN
OP_RSH
OP_SEMICOLON
OP_STRICT_EQ
OP_STRICT_NE
OP_UNARY_MINUS
OP_UNARY_PLUS
OP_URSH
#Comment by Reedy (talk | contribs)   19:13, 19 July 2011

^ I was looking at those before...

Googling found little...

#Comment by Platonides (talk | contribs)   19:15, 19 July 2011

Ok, I found it. They are dynamically defined at JSTokenizer::__construct.

Which means that if you constructo two JSTokenizer you get a bunch of PHP Notices...

#Comment by Reedy (talk | contribs)   19:50, 19 July 2011

Lovely....

#Comment by Platonides (talk | contribs)   20:18, 19 July 2011

I fixed it in r92563 (also modified it in r92560) and emailed upstream about them.

Status & tagging log