!|"); $max_len = sizeof($text); $num_newlines = 0; do { if ($pos >= $max_len) { return array('', TK_EOF); } $c = $text[$pos]; $pos += 1; if ($c == "\r") { $num_newlines += 1; } } while (in_array($c, $whitespace)); if ($num_newlines > 1) { // theoretically it should be js_beautify job to print something for ($i = 1 ; $i < $num_newlines; $i++) nl(false); } if (in_array($c, $wordchar)) { if ($pos < $max_len) { while (in_array($text[$pos], $wordchar)) { $c .= $text[$pos]; $pos += 1; if ($pos == $max_len) break; } } return array($c, TK_WORD); } if ($c == '(' || $c == '[') { return array($c, TK_START_EXPR); } if ($c == ')' || $c == ']') { return array($c, TK_END_EXPR); } if ($c == '{') { return array($c, TK_START_BLOCK); } if ($c == '}') { return array($c, TK_END_BLOCK); } if ($c == ';') { return array($c, TK_END_COMMAND); } if ($c == '/') { // peek for comment /* ... */ if ($text[$pos] == '*') { $comment = ''; $pos += 1; if ($pos < $max_len){ while (!($text[$pos] == '*' && isset($text[$pos + 1]) && $text[$pos + 1] == '/') && $pos < $max_len) { $comment .= $text[$pos]; $pos += 1; if ($pos >= $max_len) break; } } $pos +=2; return array("/*$comment*/", TK_BLOCK_COMMENT); } // peek for comment // ... if ($text[$pos] == '/') { $comment = $c; while ($text[$pos] != "\x0d" && $text[$pos] != "\x0a") { $comment .= $text[$pos]; $pos += 1; if ($pos >= $max_len) break; } $pos += 1; return array($comment, TK_COMMENT); } } if ($c == "'" || // string $c == '"' || // string ($c == '/' && ($lasttok == TK_START_EXPR || $lasttok == TK_PUNCT || $lasttok == TK_EOF || $lasttok == TK_END_COMMAND))) { // regexp $sep = $c; $c = ''; $esc = false; if ($pos < $max_len) { while ($esc || $text[$pos] != $sep) { $c .= $text[$pos]; if (!$esc) { $esc = $text[$pos] == '\\'; } else { $esc = false; } $pos += 1; if ($pos >= $max_len) break; } } $pos += 1; if ($lasttok == TK_END_COMMAND) { nl(); } return array($sep . $c . $sep, TK_STRING); } if (in_array($c, $punct)) { if ($pos < $max_len) { while (in_array($text[$pos], $punct)) { $c .= $text[$pos]; $pos += 1; if ($pos >= $max_len) break; } } return array($c, TK_PUNCT); } if ($c == '/') { return array($c, TK_PUNCT); } return array($c, TK_UNKNOWN); } if (false and function_exists('mb_convert_case')) { // transform UTF-8 string to array of characters function array_from_string($str) { $arr = array(); for ($i = 0 ; $i < mb_strlen($str, 'UTF-8'); $i++) { $arr[] = mb_substr($str, $i, 1, 'UTF-8'); } return $arr; } } else { // mb_functions not supported -- fallback function array_from_string($str) { $arr = array(); for ($i = 0 ; $i < strlen($str); $i++) { $arr[] = $str[$i]; } return $arr; } } ?>