2 // phpcs:ignoreFile -- File external to MediaWiki. Ignore coding conventions checks.
7 * @author Paul Copperman <paul.copperman@gmail.com>
8 * @license Choose any of Apache, MIT, GPL, LGPL
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.
19 class JavaScriptMinifier
{
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.
29 const PROPERTY_ASSIGNMENT
= 2;
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;
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
63 // Sanity limit to avoid excessive memory usage
64 const STACK_LIMIT
= 1000;
67 * Returns minified JavaScript code.
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.
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
77 public static function minify( $s, $maxLineLength = 1000 ) {
78 // First we declare a few tables that contain our parsing rules
80 // $opChars : characters, which can be combined without whitespace in between them
110 // $tokenTypes : maps keywords and operators to their corresponding token type
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
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.
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
199 self
::CONDITION
=> array(
200 self
::TYPE_PAREN_OPEN
=> self
::PAREN_EXPRESSION
202 self
::PROPERTY_ASSIGNMENT
=> array(
203 self
::TYPE_COLON
=> self
::PROPERTY_EXPRESSION
,
204 self
::TYPE_BRACE_OPEN
=> self
::STATEMENT
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
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
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
229 self
::EXPRESSION_FUNC
=> array(
230 self
::TYPE_BRACE_OPEN
=> self
::STATEMENT
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
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
245 self
::EXPRESSION_TERNARY_FUNC
=> array(
246 self
::TYPE_BRACE_OPEN
=> self
::STATEMENT
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
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
263 self
::PAREN_EXPRESSION_FUNC
=> array(
264 self
::TYPE_BRACE_OPEN
=> self
::STATEMENT
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
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
279 self
::PROPERTY_EXPRESSION_FUNC
=> array(
280 self
::TYPE_BRACE_OPEN
=> self
::STATEMENT
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
288 self
::STATEMENT
=> array(
289 self
::TYPE_BRACE_OPEN
=> self
::STATEMENT
,
290 self
::TYPE_PAREN_OPEN
=> self
::EXPRESSION_OP
292 self
::CONDITION
=> array(
293 self
::TYPE_PAREN_OPEN
=> self
::STATEMENT
295 self
::PROPERTY_ASSIGNMENT
=> array(
296 self
::TYPE_BRACE_OPEN
=> self
::PROPERTY_ASSIGNMENT
298 self
::EXPRESSION
=> array(
299 self
::TYPE_BRACE_OPEN
=> self
::EXPRESSION_OP
,
300 self
::TYPE_PAREN_OPEN
=> self
::EXPRESSION_OP
302 self
::EXPRESSION_NO_NL
=> array(
303 self
::TYPE_BRACE_OPEN
=> self
::EXPRESSION_OP
,
304 self
::TYPE_PAREN_OPEN
=> self
::EXPRESSION_OP
306 self
::EXPRESSION_OP
=> array(
307 self
::TYPE_HOOK
=> self
::EXPRESSION
,
308 self
::TYPE_PAREN_OPEN
=> self
::EXPRESSION_OP
310 self
::EXPRESSION_FUNC
=> array(
311 self
::TYPE_BRACE_OPEN
=> self
::EXPRESSION_OP
313 self
::EXPRESSION_TERNARY
=> array(
314 self
::TYPE_BRACE_OPEN
=> self
::EXPRESSION_TERNARY_OP
,
315 self
::TYPE_PAREN_OPEN
=> self
::EXPRESSION_TERNARY_OP
317 self
::EXPRESSION_TERNARY_OP
=> array(
318 self
::TYPE_HOOK
=> self
::EXPRESSION_TERNARY
,
319 self
::TYPE_PAREN_OPEN
=> self
::EXPRESSION_TERNARY_OP
321 self
::EXPRESSION_TERNARY_FUNC
=> array(
322 self
::TYPE_BRACE_OPEN
=> self
::EXPRESSION_TERNARY_OP
324 self
::PAREN_EXPRESSION
=> array(
325 self
::TYPE_BRACE_OPEN
=> self
::PAREN_EXPRESSION_OP
,
326 self
::TYPE_PAREN_OPEN
=> self
::PAREN_EXPRESSION_OP
328 self
::PAREN_EXPRESSION_OP
=> array(
329 self
::TYPE_PAREN_OPEN
=> self
::PAREN_EXPRESSION_OP
331 self
::PAREN_EXPRESSION_FUNC
=> array(
332 self
::TYPE_BRACE_OPEN
=> self
::PAREN_EXPRESSION_OP
334 self
::PROPERTY_EXPRESSION
=> array(
335 self
::TYPE_BRACE_OPEN
=> self
::PROPERTY_EXPRESSION_OP
,
336 self
::TYPE_PAREN_OPEN
=> self
::PROPERTY_EXPRESSION_OP
338 self
::PROPERTY_EXPRESSION_OP
=> array(
339 self
::TYPE_PAREN_OPEN
=> self
::PROPERTY_EXPRESSION_OP
341 self
::PROPERTY_EXPRESSION_FUNC
=> array(
342 self
::TYPE_BRACE_OPEN
=> self
::PROPERTY_EXPRESSION_OP
346 // $pop : Rules for when to pop a state from the stack
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 )
360 // $semicolon : Rules for when a semicolon insertion is appropriate
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
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
386 // $divStates : Contains all states that can be followed by a division operator
388 self
::EXPRESSION_OP
=> true,
389 self
::EXPRESSION_TERNARY_OP
=> true,
390 self
::PAREN_EXPRESSION_OP
=> true,
391 self
::PROPERTY_EXPRESSION_OP
=> true
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.
398 $length = strlen( $s );
400 $newlineFound = true;
401 $state = self
::STATEMENT
;
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 );
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;
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;
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 ) === '-->' )
432 $pos +
= strcspn( $s, "\r\n", $pos );
436 // Find out which kind of token we're handling.
437 // Note: $end must point past the end of the current token
438 // so that `substr($s, $pos, $end - $pos)` would be the entire token.
439 // In order words, $end will be the offset of the last relevant character
440 // in the stream + 1, or simply put: The offset of the first character
441 // of any next token in the stream.
443 // Handle string literals
444 if( $ch === "'" ||
$ch === '"' ) {
445 // Search to the end of the string literal, skipping over backslash escapes
446 $search = $ch . '\\';
448 // Speculatively add 2 to the end so that if we see a backslash,
449 // the next iteration will start 2 characters further (one for the
450 // backslash, one for the escaped character).
451 // We'll correct this outside the loop.
452 $end +
= strcspn( $s, $search, $end ) +
2;
453 // If the last character in our search for a quote or a backlash
454 // matched a backslash and we haven't reached the end, keep searching..
455 } while( $end - 2 < $length && $s[$end - 2] === '\\' );
456 // Correction (1): Undo speculative add, keep only one (end of string literal)
458 if ( $end > $length ) {
459 // Correction (2): Loop wrongly assumed an end quote ended the search,
460 // but search ended because we've reached the end. Correct $end.
461 // TODO: This is invalid and should throw.
464 // We have to distinguish between regexp literals and division operators
465 // A division operator is only possible in certain states
466 } elseif( $ch === '/' && !isset( $divStates[$state] ) ) {
469 // Search until we find "/" (end of regexp), "\" (backslash escapes),
470 // or "[" (start of character classes).
472 // Speculatively add 2 to ensure next iteration skips
473 // over backslash and escaped character.
474 // We'll correct this outside the loop.
475 $end +
= strcspn( $s, '/[\\', $end ) +
2;
476 // If backslash escape, keep searching...
477 } while( $end - 2 < $length && $s[$end - 2] === '\\' );
478 // Correction (1): Undo speculative add, keep only one (end of regexp)
480 if ( $end > $length ) {
481 // Correction (2): Loop wrongly assumed end slash was seen
482 // String ended without end of regexp. Correct $end.
483 // TODO: This is invalid and should throw.
487 if ( $s[$end - 1] === '/' ) {
490 // (Implicit else), we must've found the start of a char class,
491 // skip until we find "]" (end of char class), or "\" (backslash escape)
493 // Speculatively add 2 for backslash escape.
494 // We'll substract one outside the loop.
495 $end +
= strcspn( $s, ']\\', $end ) +
2;
496 // If backslash escape, keep searching...
497 } while( $end - 2 < $length && $s[$end - 2] === '\\' );
498 // Correction (1): Undo speculative add, keep only one (end of regexp)
501 // Search past the regexp modifiers (gi)
502 while( $end < $length && ctype_alpha( $s[$end] ) ) {
507 && ($pos +
1 < $length) && ($s[$pos +
1] === 'x' ||
$s[$pos +
1] === 'X' )
509 // Hex numeric literal
511 $len = strspn( $s, '0123456789ABCDEFabcdef', $end );
513 return self
::parseError($s, $pos, 'Expected a hexadecimal number but found ' . substr( $s, $pos, 5 ) . '...' );
518 ||
( $ch === '.' && $pos +
1 < $length && ctype_digit( $s[$pos +
1] ) )
520 $end +
= strspn( $s, '0123456789', $end );
521 $decimal = strspn( $s, '.', $end );
523 if ( $decimal > 2 ) {
524 return self
::parseError($s, $end, 'The number has too many decimal points' );
526 $end +
= strspn( $s, '0123456789', $end +
1 ) +
$decimal;
528 $exponent = strspn( $s, 'eE', $end );
530 if ( $exponent > 1 ) {
531 return self
::parseError($s, $end, 'Number with several E' );
535 // + sign is optional; - sign is required.
536 $end +
= strspn( $s, '-+', $end );
537 $len = strspn( $s, '0123456789', $end );
539 return self
::parseError($s, $pos, 'No decimal digits after e, how many zeroes should be added?' );
543 } elseif( isset( $opChars[$ch] ) ) {
544 // Punctuation character. Search for the longest matching operator.
547 && isset( $tokenTypes[substr( $s, $pos, $end - $pos +
1 )] )
552 // Identifier or reserved word. Search for the end by excluding whitespace and
554 $end +
= strcspn( $s, " \t\n.;,=<>+-{}()[]?:*/%'\"!&|^~\xb\xc\r", $end );
557 // Now get the token type from our type array
558 $token = substr( $s, $pos, $end - $pos ); // so $end - $pos == strlen( $token )
559 $type = isset( $tokenTypes[$token] ) ?
$tokenTypes[$token] : self
::TYPE_LITERAL
;
561 if( $newlineFound && isset( $semicolon[$state][$type] ) ) {
562 // This token triggers the semicolon insertion mechanism of javascript. While we
563 // could add the ; token here ourselves, keeping the newline has a few advantages.
565 $state = self
::STATEMENT
;
567 } elseif( $maxLineLength > 0 && $lineLength +
$end - $pos > $maxLineLength &&
568 !isset( $semicolon[$state][$type] ) && $type !== self
::TYPE_INCR_OP
)
570 // This line would get too long if we added $token, so add a newline first.
571 // Only do this if it won't trigger semicolon insertion and if it won't
572 // put a postfix increment operator on its own line, which is illegal in js.
575 // Check, whether we have to separate the token from the last one with whitespace
576 } elseif( !isset( $opChars[$last] ) && !isset( $opChars[$ch] ) ) {
579 // Don't accidentally create ++, -- or // tokens
580 } elseif( $last === $ch && ( $ch === '+' ||
$ch === '-' ||
$ch === '/' ) ) {
585 $type === self
::TYPE_LITERAL
586 && ( $token === 'true' ||
$token === 'false' )
587 && ( $state === self
::EXPRESSION ||
$state === self
::PROPERTY_EXPRESSION
)
590 $token = ( $token === 'true' ) ?
'!0' : '!1';
594 $lineLength +
= $end - $pos; // += strlen( $token )
595 $last = $s[$end - 1];
597 $newlineFound = false;
599 // Now that we have output our token, transition into the new state.
600 if( isset( $push[$state][$type] ) && count( $stack ) < self
::STACK_LIMIT
) {
601 $stack[] = $push[$state][$type];
603 if( $stack && isset( $pop[$state][$type] ) ) {
604 $state = array_pop( $stack );
605 } elseif( isset( $goto[$state][$type] ) ) {
606 $state = $goto[$state][$type];
612 static function parseError($fullJavascript, $position, $errorMsg) {
613 // TODO: Handle the error: trigger_error, throw exception, return false...