Merge "Chinese Conversion Table Update 2017-6"
[lhc/web/wiklou.git] / includes / libs / JavaScriptMinifier.php
1 <?php
2 // @codingStandardsIgnoreFile File external to MediaWiki. Ignore coding conventions checks.
3 /**
4 * JavaScript Minifier
5 *
6 * @file
7 * @author Paul Copperman <paul.copperman@gmail.com>
8 * @license Choose any of Apache, MIT, GPL, LGPL
9 */
10
11 /**
12 * This class is meant to safely minify javascript code, while leaving syntactically correct
13 * programs intact. Other libraries, such as JSMin require a certain coding style to work
14 * correctly. OTOH, libraries like jsminplus, that do parse the code correctly are rather
15 * slow, because they construct a complete parse tree before outputting the code minified.
16 * So this class is meant to allow arbitrary (but syntactically correct) input, while being
17 * fast enough to be used for on-the-fly minifying.
18 */
19 class JavaScriptMinifier {
20
21 /* Parsing states.
22 * The state machine is only necessary to decide whether to parse a slash as division
23 * operator or as regexp literal.
24 * States are named after the next expected item. We only distinguish states when the
25 * distinction is relevant for our purpose.
26 */
27 const STATEMENT = 0;
28 const CONDITION = 1;
29 const PROPERTY_ASSIGNMENT = 2;
30 const EXPRESSION = 3;
31 const EXPRESSION_NO_NL = 4; // only relevant for semicolon insertion
32 const EXPRESSION_OP = 5;
33 const EXPRESSION_FUNC = 6;
34 const EXPRESSION_TERNARY = 7; // used to determine the role of a colon
35 const EXPRESSION_TERNARY_OP = 8;
36 const EXPRESSION_TERNARY_FUNC = 9;
37 const PAREN_EXPRESSION = 10; // expression which is not on the top level
38 const PAREN_EXPRESSION_OP = 11;
39 const PAREN_EXPRESSION_FUNC = 12;
40 const PROPERTY_EXPRESSION = 13; // expression which is within an object literal
41 const PROPERTY_EXPRESSION_OP = 14;
42 const PROPERTY_EXPRESSION_FUNC = 15;
43
44 /* Token types */
45 const TYPE_UN_OP = 1; // unary operators
46 const TYPE_INCR_OP = 2; // ++ and --
47 const TYPE_BIN_OP = 3; // binary operators
48 const TYPE_ADD_OP = 4; // + and - which can be either unary or binary ops
49 const TYPE_HOOK = 5; // ?
50 const TYPE_COLON = 6; // :
51 const TYPE_COMMA = 7; // ,
52 const TYPE_SEMICOLON = 8; // ;
53 const TYPE_BRACE_OPEN = 9; // {
54 const TYPE_BRACE_CLOSE = 10; // }
55 const TYPE_PAREN_OPEN = 11; // ( and [
56 const TYPE_PAREN_CLOSE = 12; // ) and ]
57 const TYPE_RETURN = 13; // keywords: break, continue, return, throw
58 const TYPE_IF = 14; // keywords: catch, for, with, switch, while, if
59 const TYPE_DO = 15; // keywords: case, var, finally, else, do, try
60 const TYPE_FUNC = 16; // keywords: function
61 const TYPE_LITERAL = 17; // all literals, identifiers and unrecognised tokens
62
63 // Sanity limit to avoid excessive memory usage
64 const STACK_LIMIT = 1000;
65
66 /**
67 * Returns minified JavaScript code.
68 *
69 * NOTE: $maxLineLength isn't a strict maximum. Longer lines will be produced when
70 * literals (e.g. quoted strings) longer than $maxLineLength are encountered
71 * or when required to guard against semicolon insertion.
72 *
73 * @param string $s JavaScript code to minify
74 * @param int $maxLineLength Maximum length of a single line, or -1 for no maximum.
75 * @return String Minified code
76 */
77 public static function minify( $s, $maxLineLength = 1000 ) {
78 // First we declare a few tables that contain our parsing rules
79
80 // $opChars : characters, which can be combined without whitespace in between them
81 $opChars = array(
82 '!' => true,
83 '"' => true,
84 '%' => true,
85 '&' => true,
86 "'" => true,
87 '(' => true,
88 ')' => true,
89 '*' => true,
90 '+' => true,
91 ',' => true,
92 '-' => true,
93 '.' => true,
94 '/' => true,
95 ':' => true,
96 ';' => true,
97 '<' => true,
98 '=' => true,
99 '>' => true,
100 '?' => true,
101 '[' => true,
102 ']' => true,
103 '^' => true,
104 '{' => true,
105 '|' => true,
106 '}' => true,
107 '~' => true
108 );
109
110 // $tokenTypes : maps keywords and operators to their corresponding token type
111 $tokenTypes = array(
112 '!' => self::TYPE_UN_OP,
113 '~' => self::TYPE_UN_OP,
114 'delete' => self::TYPE_UN_OP,
115 'new' => self::TYPE_UN_OP,
116 'typeof' => self::TYPE_UN_OP,
117 'void' => self::TYPE_UN_OP,
118 '++' => self::TYPE_INCR_OP,
119 '--' => self::TYPE_INCR_OP,
120 '!=' => self::TYPE_BIN_OP,
121 '!==' => self::TYPE_BIN_OP,
122 '%' => self::TYPE_BIN_OP,
123 '%=' => self::TYPE_BIN_OP,
124 '&' => self::TYPE_BIN_OP,
125 '&&' => self::TYPE_BIN_OP,
126 '&=' => self::TYPE_BIN_OP,
127 '*' => self::TYPE_BIN_OP,
128 '*=' => self::TYPE_BIN_OP,
129 '+=' => self::TYPE_BIN_OP,
130 '-=' => self::TYPE_BIN_OP,
131 '.' => self::TYPE_BIN_OP,
132 '/' => self::TYPE_BIN_OP,
133 '/=' => self::TYPE_BIN_OP,
134 '<' => self::TYPE_BIN_OP,
135 '<<' => self::TYPE_BIN_OP,
136 '<<=' => self::TYPE_BIN_OP,
137 '<=' => self::TYPE_BIN_OP,
138 '=' => self::TYPE_BIN_OP,
139 '==' => self::TYPE_BIN_OP,
140 '===' => self::TYPE_BIN_OP,
141 '>' => self::TYPE_BIN_OP,
142 '>=' => self::TYPE_BIN_OP,
143 '>>' => self::TYPE_BIN_OP,
144 '>>=' => self::TYPE_BIN_OP,
145 '>>>' => self::TYPE_BIN_OP,
146 '>>>=' => self::TYPE_BIN_OP,
147 '^' => self::TYPE_BIN_OP,
148 '^=' => self::TYPE_BIN_OP,
149 '|' => self::TYPE_BIN_OP,
150 '|=' => self::TYPE_BIN_OP,
151 '||' => self::TYPE_BIN_OP,
152 'in' => self::TYPE_BIN_OP,
153 'instanceof' => self::TYPE_BIN_OP,
154 '+' => self::TYPE_ADD_OP,
155 '-' => self::TYPE_ADD_OP,
156 '?' => self::TYPE_HOOK,
157 ':' => self::TYPE_COLON,
158 ',' => self::TYPE_COMMA,
159 ';' => self::TYPE_SEMICOLON,
160 '{' => self::TYPE_BRACE_OPEN,
161 '}' => self::TYPE_BRACE_CLOSE,
162 '(' => self::TYPE_PAREN_OPEN,
163 '[' => self::TYPE_PAREN_OPEN,
164 ')' => self::TYPE_PAREN_CLOSE,
165 ']' => self::TYPE_PAREN_CLOSE,
166 'break' => self::TYPE_RETURN,
167 'continue' => self::TYPE_RETURN,
168 'return' => self::TYPE_RETURN,
169 'throw' => self::TYPE_RETURN,
170 'catch' => self::TYPE_IF,
171 'for' => self::TYPE_IF,
172 'if' => self::TYPE_IF,
173 'switch' => self::TYPE_IF,
174 'while' => self::TYPE_IF,
175 'with' => self::TYPE_IF,
176 'case' => self::TYPE_DO,
177 'do' => self::TYPE_DO,
178 'else' => self::TYPE_DO,
179 'finally' => self::TYPE_DO,
180 'try' => self::TYPE_DO,
181 'var' => self::TYPE_DO,
182 'function' => self::TYPE_FUNC
183 );
184
185 // $goto : This is the main table for our state machine. For every state/token pair
186 // the following state is defined. When no rule exists for a given pair,
187 // the state is left unchanged.
188 $goto = array(
189 self::STATEMENT => array(
190 self::TYPE_UN_OP => self::EXPRESSION,
191 self::TYPE_INCR_OP => self::EXPRESSION,
192 self::TYPE_ADD_OP => self::EXPRESSION,
193 self::TYPE_PAREN_OPEN => self::PAREN_EXPRESSION,
194 self::TYPE_RETURN => self::EXPRESSION_NO_NL,
195 self::TYPE_IF => self::CONDITION,
196 self::TYPE_FUNC => self::CONDITION,
197 self::TYPE_LITERAL => self::EXPRESSION_OP
198 ),
199 self::CONDITION => array(
200 self::TYPE_PAREN_OPEN => self::PAREN_EXPRESSION
201 ),
202 self::PROPERTY_ASSIGNMENT => array(
203 self::TYPE_COLON => self::PROPERTY_EXPRESSION,
204 self::TYPE_BRACE_OPEN => self::STATEMENT
205 ),
206 self::EXPRESSION => array(
207 self::TYPE_SEMICOLON => self::STATEMENT,
208 self::TYPE_BRACE_OPEN => self::PROPERTY_ASSIGNMENT,
209 self::TYPE_PAREN_OPEN => self::PAREN_EXPRESSION,
210 self::TYPE_FUNC => self::EXPRESSION_FUNC,
211 self::TYPE_LITERAL => self::EXPRESSION_OP
212 ),
213 self::EXPRESSION_NO_NL => array(
214 self::TYPE_SEMICOLON => self::STATEMENT,
215 self::TYPE_BRACE_OPEN => self::PROPERTY_ASSIGNMENT,
216 self::TYPE_PAREN_OPEN => self::PAREN_EXPRESSION,
217 self::TYPE_FUNC => self::EXPRESSION_FUNC,
218 self::TYPE_LITERAL => self::EXPRESSION_OP
219 ),
220 self::EXPRESSION_OP => array(
221 self::TYPE_BIN_OP => self::EXPRESSION,
222 self::TYPE_ADD_OP => self::EXPRESSION,
223 self::TYPE_HOOK => self::EXPRESSION_TERNARY,
224 self::TYPE_COLON => self::STATEMENT,
225 self::TYPE_COMMA => self::EXPRESSION,
226 self::TYPE_SEMICOLON => self::STATEMENT,
227 self::TYPE_PAREN_OPEN => self::PAREN_EXPRESSION
228 ),
229 self::EXPRESSION_FUNC => array(
230 self::TYPE_BRACE_OPEN => self::STATEMENT
231 ),
232 self::EXPRESSION_TERNARY => array(
233 self::TYPE_BRACE_OPEN => self::PROPERTY_ASSIGNMENT,
234 self::TYPE_PAREN_OPEN => self::PAREN_EXPRESSION,
235 self::TYPE_FUNC => self::EXPRESSION_TERNARY_FUNC,
236 self::TYPE_LITERAL => self::EXPRESSION_TERNARY_OP
237 ),
238 self::EXPRESSION_TERNARY_OP => array(
239 self::TYPE_BIN_OP => self::EXPRESSION_TERNARY,
240 self::TYPE_ADD_OP => self::EXPRESSION_TERNARY,
241 self::TYPE_HOOK => self::EXPRESSION_TERNARY,
242 self::TYPE_COMMA => self::EXPRESSION_TERNARY,
243 self::TYPE_PAREN_OPEN => self::PAREN_EXPRESSION
244 ),
245 self::EXPRESSION_TERNARY_FUNC => array(
246 self::TYPE_BRACE_OPEN => self::STATEMENT
247 ),
248 self::PAREN_EXPRESSION => array(
249 self::TYPE_BRACE_OPEN => self::PROPERTY_ASSIGNMENT,
250 self::TYPE_PAREN_OPEN => self::PAREN_EXPRESSION,
251 self::TYPE_FUNC => self::PAREN_EXPRESSION_FUNC,
252 self::TYPE_LITERAL => self::PAREN_EXPRESSION_OP
253 ),
254 self::PAREN_EXPRESSION_OP => array(
255 self::TYPE_BIN_OP => self::PAREN_EXPRESSION,
256 self::TYPE_ADD_OP => self::PAREN_EXPRESSION,
257 self::TYPE_HOOK => self::PAREN_EXPRESSION,
258 self::TYPE_COLON => self::PAREN_EXPRESSION,
259 self::TYPE_COMMA => self::PAREN_EXPRESSION,
260 self::TYPE_SEMICOLON => self::PAREN_EXPRESSION,
261 self::TYPE_PAREN_OPEN => self::PAREN_EXPRESSION
262 ),
263 self::PAREN_EXPRESSION_FUNC => array(
264 self::TYPE_BRACE_OPEN => self::STATEMENT
265 ),
266 self::PROPERTY_EXPRESSION => array(
267 self::TYPE_BRACE_OPEN => self::PROPERTY_ASSIGNMENT,
268 self::TYPE_PAREN_OPEN => self::PAREN_EXPRESSION,
269 self::TYPE_FUNC => self::PROPERTY_EXPRESSION_FUNC,
270 self::TYPE_LITERAL => self::PROPERTY_EXPRESSION_OP
271 ),
272 self::PROPERTY_EXPRESSION_OP => array(
273 self::TYPE_BIN_OP => self::PROPERTY_EXPRESSION,
274 self::TYPE_ADD_OP => self::PROPERTY_EXPRESSION,
275 self::TYPE_HOOK => self::PROPERTY_EXPRESSION,
276 self::TYPE_COMMA => self::PROPERTY_ASSIGNMENT,
277 self::TYPE_PAREN_OPEN => self::PAREN_EXPRESSION
278 ),
279 self::PROPERTY_EXPRESSION_FUNC => array(
280 self::TYPE_BRACE_OPEN => self::STATEMENT
281 )
282 );
283
284 // $push : This table contains the rules for when to push a state onto the stack.
285 // The pushed state is the state to return to when the corresponding
286 // closing token is found
287 $push = array(
288 self::STATEMENT => array(
289 self::TYPE_BRACE_OPEN => self::STATEMENT,
290 self::TYPE_PAREN_OPEN => self::EXPRESSION_OP
291 ),
292 self::CONDITION => array(
293 self::TYPE_PAREN_OPEN => self::STATEMENT
294 ),
295 self::PROPERTY_ASSIGNMENT => array(
296 self::TYPE_BRACE_OPEN => self::PROPERTY_ASSIGNMENT
297 ),
298 self::EXPRESSION => array(
299 self::TYPE_BRACE_OPEN => self::EXPRESSION_OP,
300 self::TYPE_PAREN_OPEN => self::EXPRESSION_OP
301 ),
302 self::EXPRESSION_NO_NL => array(
303 self::TYPE_BRACE_OPEN => self::EXPRESSION_OP,
304 self::TYPE_PAREN_OPEN => self::EXPRESSION_OP
305 ),
306 self::EXPRESSION_OP => array(
307 self::TYPE_HOOK => self::EXPRESSION,
308 self::TYPE_PAREN_OPEN => self::EXPRESSION_OP
309 ),
310 self::EXPRESSION_FUNC => array(
311 self::TYPE_BRACE_OPEN => self::EXPRESSION_OP
312 ),
313 self::EXPRESSION_TERNARY => array(
314 self::TYPE_BRACE_OPEN => self::EXPRESSION_TERNARY_OP,
315 self::TYPE_PAREN_OPEN => self::EXPRESSION_TERNARY_OP
316 ),
317 self::EXPRESSION_TERNARY_OP => array(
318 self::TYPE_HOOK => self::EXPRESSION_TERNARY,
319 self::TYPE_PAREN_OPEN => self::EXPRESSION_TERNARY_OP
320 ),
321 self::EXPRESSION_TERNARY_FUNC => array(
322 self::TYPE_BRACE_OPEN => self::EXPRESSION_TERNARY_OP
323 ),
324 self::PAREN_EXPRESSION => array(
325 self::TYPE_BRACE_OPEN => self::PAREN_EXPRESSION_OP,
326 self::TYPE_PAREN_OPEN => self::PAREN_EXPRESSION_OP
327 ),
328 self::PAREN_EXPRESSION_OP => array(
329 self::TYPE_PAREN_OPEN => self::PAREN_EXPRESSION_OP
330 ),
331 self::PAREN_EXPRESSION_FUNC => array(
332 self::TYPE_BRACE_OPEN => self::PAREN_EXPRESSION_OP
333 ),
334 self::PROPERTY_EXPRESSION => array(
335 self::TYPE_BRACE_OPEN => self::PROPERTY_EXPRESSION_OP,
336 self::TYPE_PAREN_OPEN => self::PROPERTY_EXPRESSION_OP
337 ),
338 self::PROPERTY_EXPRESSION_OP => array(
339 self::TYPE_PAREN_OPEN => self::PROPERTY_EXPRESSION_OP
340 ),
341 self::PROPERTY_EXPRESSION_FUNC => array(
342 self::TYPE_BRACE_OPEN => self::PROPERTY_EXPRESSION_OP
343 )
344 );
345
346 // $pop : Rules for when to pop a state from the stack
347 $pop = array(
348 self::STATEMENT => array( self::TYPE_BRACE_CLOSE => true ),
349 self::PROPERTY_ASSIGNMENT => array( self::TYPE_BRACE_CLOSE => true ),
350 self::EXPRESSION => array( self::TYPE_BRACE_CLOSE => true ),
351 self::EXPRESSION_NO_NL => array( self::TYPE_BRACE_CLOSE => true ),
352 self::EXPRESSION_OP => array( self::TYPE_BRACE_CLOSE => true ),
353 self::EXPRESSION_TERNARY_OP => array( self::TYPE_COLON => true ),
354 self::PAREN_EXPRESSION => array( self::TYPE_PAREN_CLOSE => true ),
355 self::PAREN_EXPRESSION_OP => array( self::TYPE_PAREN_CLOSE => true ),
356 self::PROPERTY_EXPRESSION => array( self::TYPE_BRACE_CLOSE => true ),
357 self::PROPERTY_EXPRESSION_OP => array( self::TYPE_BRACE_CLOSE => true )
358 );
359
360 // $semicolon : Rules for when a semicolon insertion is appropriate
361 $semicolon = array(
362 self::EXPRESSION_NO_NL => array(
363 self::TYPE_UN_OP => true,
364 self::TYPE_INCR_OP => true,
365 self::TYPE_ADD_OP => true,
366 self::TYPE_BRACE_OPEN => true,
367 self::TYPE_PAREN_OPEN => true,
368 self::TYPE_RETURN => true,
369 self::TYPE_IF => true,
370 self::TYPE_DO => true,
371 self::TYPE_FUNC => true,
372 self::TYPE_LITERAL => true
373 ),
374 self::EXPRESSION_OP => array(
375 self::TYPE_UN_OP => true,
376 self::TYPE_INCR_OP => true,
377 self::TYPE_BRACE_OPEN => true,
378 self::TYPE_RETURN => true,
379 self::TYPE_IF => true,
380 self::TYPE_DO => true,
381 self::TYPE_FUNC => true,
382 self::TYPE_LITERAL => true
383 )
384 );
385
386 // $divStates : Contains all states that can be followed by a division operator
387 $divStates = array(
388 self::EXPRESSION_OP => true,
389 self::EXPRESSION_TERNARY_OP => true,
390 self::PAREN_EXPRESSION_OP => true,
391 self::PROPERTY_EXPRESSION_OP => true
392 );
393
394 // Here's where the minifying takes place: Loop through the input, looking for tokens
395 // and output them to $out, taking actions to the above defined rules when appropriate.
396 $out = '';
397 $pos = 0;
398 $length = strlen( $s );
399 $lineLength = 0;
400 $newlineFound = true;
401 $state = self::STATEMENT;
402 $stack = array();
403 $last = ';'; // Pretend that we have seen a semicolon yet
404 while( $pos < $length ) {
405 // First, skip over any whitespace and multiline comments, recording whether we
406 // found any newline character
407 $skip = strspn( $s, " \t\n\r\xb\xc", $pos );
408 if( !$skip ) {
409 $ch = $s[$pos];
410 if( $ch === '/' && substr( $s, $pos, 2 ) === '/*' ) {
411 // Multiline comment. Search for the end token or EOT.
412 $end = strpos( $s, '*/', $pos + 2 );
413 $skip = $end === false ? $length - $pos : $end - $pos + 2;
414 }
415 }
416 if( $skip ) {
417 // The semicolon insertion mechanism needs to know whether there was a newline
418 // between two tokens, so record it now.
419 if( !$newlineFound && strcspn( $s, "\r\n", $pos, $skip ) !== $skip ) {
420 $newlineFound = true;
421 }
422 $pos += $skip;
423 continue;
424 }
425 // Handle C++-style comments and html comments, which are treated as single line
426 // comments by the browser, regardless of whether the end tag is on the same line.
427 // Handle --> the same way, but only if it's at the beginning of the line
428 if( ( $ch === '/' && substr( $s, $pos, 2 ) === '//' )
429 || ( $ch === '<' && substr( $s, $pos, 4 ) === '<!--' )
430 || ( $ch === '-' && $newlineFound && substr( $s, $pos, 3 ) === '-->' )
431 ) {
432 $pos += strcspn( $s, "\r\n", $pos );
433 continue;
434 }
435
436 // Find out which kind of token we're handling. $end will point past the end of it.
437 $end = $pos + 1;
438 // Handle string literals
439 if( $ch === "'" || $ch === '"' ) {
440 // Search to the end of the string literal, skipping over backslash escapes
441 $search = $ch . '\\';
442 do{
443 $end += strcspn( $s, $search, $end ) + 2;
444 } while( $end - 2 < $length && $s[$end - 2] === '\\' );
445 $end--;
446 // We have to distinguish between regexp literals and division operators
447 // A division operator is only possible in certain states
448 } elseif( $ch === '/' && !isset( $divStates[$state] ) ) {
449 // Regexp literal
450 for( ; ; ) {
451 do{
452 // Skip until we find "/" (end of regexp), "\" (backslash escapes),
453 // or "[" (start of character classes).
454 $end += strcspn( $s, '/[\\', $end ) + 2;
455 // If backslash escape, keep searching...
456 } while( $end - 2 < $length && $s[$end - 2] === '\\' );
457 $end--;
458 // If the end, stop here.
459 if( $end - 1 >= $length || $s[$end - 1] === '/' ) {
460 break;
461 }
462 // (Implicit else), we must've found the start of a char class,
463 // skip until we find "]" (end of char class), or "\" (backslash escape)
464 do{
465 $end += strcspn( $s, ']\\', $end ) + 2;
466 // If backslash escape, keep searching...
467 } while( $end - 2 < $length && $s[$end - 2] === '\\' );
468 $end--;
469 };
470 // Search past the regexp modifiers (gi)
471 while( $end < $length && ctype_alpha( $s[$end] ) ) {
472 $end++;
473 }
474 } elseif(
475 $ch === '0'
476 && ($pos + 1 < $length) && ($s[$pos + 1] === 'x' || $s[$pos + 1] === 'X' )
477 ) {
478 // Hex numeric literal
479 $end++; // x or X
480 $len = strspn( $s, '0123456789ABCDEFabcdef', $end );
481 if ( !$len ) {
482 return self::parseError($s, $pos, 'Expected a hexadecimal number but found ' . substr( $s, $pos, 5 ) . '...' );
483 }
484 $end += $len;
485 } elseif(
486 ctype_digit( $ch )
487 || ( $ch === '.' && $pos + 1 < $length && ctype_digit( $s[$pos + 1] ) )
488 ) {
489 $end += strspn( $s, '0123456789', $end );
490 $decimal = strspn( $s, '.', $end );
491 if ($decimal) {
492 if ( $decimal > 2 ) {
493 return self::parseError($s, $end, 'The number has too many decimal points' );
494 }
495 $end += strspn( $s, '0123456789', $end + 1 ) + $decimal;
496 }
497 $exponent = strspn( $s, 'eE', $end );
498 if( $exponent ) {
499 if ( $exponent > 1 ) {
500 return self::parseError($s, $end, 'Number with several E' );
501 }
502 $end++;
503
504 // + sign is optional; - sign is required.
505 $end += strspn( $s, '-+', $end );
506 $len = strspn( $s, '0123456789', $end );
507 if ( !$len ) {
508 return self::parseError($s, $pos, 'No decimal digits after e, how many zeroes should be added?' );
509 }
510 $end += $len;
511 }
512 } elseif( isset( $opChars[$ch] ) ) {
513 // Punctuation character. Search for the longest matching operator.
514 while(
515 $end < $length
516 && isset( $tokenTypes[substr( $s, $pos, $end - $pos + 1 )] )
517 ) {
518 $end++;
519 }
520 } else {
521 // Identifier or reserved word. Search for the end by excluding whitespace and
522 // punctuation.
523 $end += strcspn( $s, " \t\n.;,=<>+-{}()[]?:*/%'\"!&|^~\xb\xc\r", $end );
524 }
525
526 // Now get the token type from our type array
527 $token = substr( $s, $pos, $end - $pos ); // so $end - $pos == strlen( $token )
528 $type = isset( $tokenTypes[$token] ) ? $tokenTypes[$token] : self::TYPE_LITERAL;
529
530 if( $newlineFound && isset( $semicolon[$state][$type] ) ) {
531 // This token triggers the semicolon insertion mechanism of javascript. While we
532 // could add the ; token here ourselves, keeping the newline has a few advantages.
533 $out .= "\n";
534 $state = self::STATEMENT;
535 $lineLength = 0;
536 } elseif( $maxLineLength > 0 && $lineLength + $end - $pos > $maxLineLength &&
537 !isset( $semicolon[$state][$type] ) && $type !== self::TYPE_INCR_OP )
538 {
539 // This line would get too long if we added $token, so add a newline first.
540 // Only do this if it won't trigger semicolon insertion and if it won't
541 // put a postfix increment operator on its own line, which is illegal in js.
542 $out .= "\n";
543 $lineLength = 0;
544 // Check, whether we have to separate the token from the last one with whitespace
545 } elseif( !isset( $opChars[$last] ) && !isset( $opChars[$ch] ) ) {
546 $out .= ' ';
547 $lineLength++;
548 // Don't accidentally create ++, -- or // tokens
549 } elseif( $last === $ch && ( $ch === '+' || $ch === '-' || $ch === '/' ) ) {
550 $out .= ' ';
551 $lineLength++;
552 }
553 if (
554 $type === self::TYPE_LITERAL
555 && ( $token === 'true' || $token === 'false' )
556 && ( $state === self::EXPRESSION || $state === self::PROPERTY_EXPRESSION )
557 && $last !== '.'
558 ) {
559 $token = ( $token === 'true' ) ? '!0' : '!1';
560 }
561
562 $out .= $token;
563 $lineLength += $end - $pos; // += strlen( $token )
564 $last = $s[$end - 1];
565 $pos = $end;
566 $newlineFound = false;
567
568 // Now that we have output our token, transition into the new state.
569 if( isset( $push[$state][$type] ) && count( $stack ) < self::STACK_LIMIT ) {
570 $stack[] = $push[$state][$type];
571 }
572 if( $stack && isset( $pop[$state][$type] ) ) {
573 $state = array_pop( $stack );
574 } elseif( isset( $goto[$state][$type] ) ) {
575 $state = $goto[$state][$type];
576 }
577 }
578 return $out;
579 }
580
581 static function parseError($fullJavascript, $position, $errorMsg) {
582 // TODO: Handle the error: trigger_error, throw exception, return false...
583 return false;
584 }
585 }