Exemple #1
0
 protected function parseCode($code)
 {
     $this->length = strlen($code);
     for ($i = 0; $i < $this->length; $i++) {
         switch ($code[$i]) {
             case '{':
                 //handle sub-scopes
                 $this->segments[] = '{';
                 $subScope = new JsScope(substr($code, $i + 1));
                 $this->segments[] = $subScope;
                 $i += $subScope->getLength() + 1;
                 $this->segments[] = '}';
                 break;
             case '}':
                 $this->length = $i;
                 $this->removeLastSemicolon();
                 $this->removeLastSpace();
                 break;
             case '(':
                 $this->segments[] = '(';
                 $subScope = new JsScope(substr($code, $i + 1));
                 $this->segments[] = $subScope;
                 $i += $subScope->getLength() + 1;
                 $this->segments[] = ')';
                 break;
             case ')':
                 $this->length = $i;
                 $this->removeLastSpace();
                 break;
             case "'":
                 //handle strings
                 $string = new JsString(substr($code, $i), "'");
                 $this->segments[] = $string;
                 $i += $string->getLength() - 1;
                 break;
             case '"':
                 $string = new JsString(substr($code, $i), '"');
                 $this->segments[] = $string;
                 $i += $string->getLength() - 1;
                 break;
             case '/':
                 //handle comments
                 $comment = new JsComment(substr($code, $i));
                 if ($comment->isComment()) {
                     $i += $comment->getLength() - 1;
                     break;
                 } else {
                     $lastChar = end($this->segments);
                     if (ctype_space($lastChar) || in_array($lastChar, [',', ';', '='])) {
                         $regex = new JsRegex(substr($code, $i));
                         $this->segments[] = $regex;
                         $i += $regex->getLength() - 1;
                         break;
                     }
                 }
                 // no break
             // no break
             case 'f':
                 if (substr($code, $i, 3) == 'for' && (ctype_space($code[3]) || $code[3] == '(')) {
                     $conditionStart = strpos($code, '(', $i + 3);
                     $i = $conditionStart - 1;
                     $this->segments[] = 'for';
                     break;
                 } elseif (substr($code, $i, 8) == 'function' && (ctype_space($code[8]) || $code[8] == '(')) {
                     $function = new JsFunction(substr($code, $i));
                     $this->segments[] = $function;
                     $i += $function->getLength() - 1;
                     break;
                 }
                 // no break
             // no break
             case 'i':
                 if (substr($code, $i, 2) == 'if' && (ctype_space($code[2]) || $code[2] == '(')) {
                     $conditionStart = strpos($code, '(', $i + 2);
                     $i = $conditionStart - 1;
                     $this->segments[] = 'if';
                     break;
                 }
                 // no break
             // no break
             case 'w':
                 if (substr($code, $i, 5) == 'while' && (ctype_space($code[5]) || $code[5] == '(')) {
                     $conditionStart = strpos($code, '(', $i + 5);
                     $i = $conditionStart - 1;
                     $this->segments[] = 'while';
                     break;
                 }
                 // no break
             // no break
             default:
                 if (ctype_space($code[$i])) {
                     //avoid pointless whitespaces
                     $lastChar = end($this->segments);
                     if (is_string($lastChar) && !ctype_space($lastChar) && !in_array($lastChar, self::$jsNoSpaceBehindChars, true)) {
                         $this->segments[] = ' ';
                     }
                 } else {
                     $lastChar = end($this->segments);
                     if (!$lastChar || ctype_space($lastChar) || in_array($lastChar, self::$jsNoSpaceBehindChars, true) && !in_array($code[$i], JsVariable::$jsVariableDelimiters, true)) {
                         $variable = new JsVariable(substr($code, $i));
                         if ($variable->isKeyword()) {
                             $this->segments[] = $variable->__toString();
                             $i += $variable->getLength() - 1;
                         } elseif ($variable->isFunctionCall()) {
                             $this->segments[] = $variable;
                             $i += $variable->getLength() - 1;
                         } else {
                             $this->segments[] = $variable;
                             $i += $variable->getLength() - 1;
                         }
                     } else {
                         $this->segments[] = $code[$i];
                     }
                 }
                 break;
         }
     }
 }