Example #1
0
 /**
  * @param mixed $value
  * @return BaseToken
  * @throws ParserException
  */
 public static function createFromPHPType($value)
 {
     switch ($type = gettype($value)) {
         case 'string':
             $current = new TokenString($value);
             break;
         case 'integer':
             $current = new TokenInteger($value);
             break;
         case 'boolean':
             $current = new TokenBool($value);
             break;
         case 'NULL':
             $current = new TokenNull($value);
             break;
         case 'double':
             $current = new TokenFloat($value);
             break;
         case 'array':
             $params = new TokenCollection();
             foreach ($value as $item) {
                 $params->attach(static::createFromPHPType($item));
             }
             $current = new TokenArray($params);
             break;
         default:
             throw new ParserException('Unsupported PHP type: "%s"', $type);
     }
     return $current;
 }
Example #2
0
 /**
  * @since 0.3.4
  * @param string $stopAt
  * @return AST\TokenCollection
  * @throws ParserException
  */
 protected function getCommaSeparatedValues($stopAt = ')')
 {
     $commaExpected = \false;
     $items = new AST\TokenCollection();
     do {
         $this->ast->next();
         if (!($current = $this->ast->current())) {
             throw new ParserException(sprintf('Unexpected end of string. Expected "%s"', $stopAt));
         }
         if ($current->getGroup() === Constants::GROUP_VALUE) {
             if ($commaExpected) {
                 throw new ParserException(sprintf('Unexpected value at position %d on line %d', $current->getPosition(), $current->getLine()));
             }
             $commaExpected = \true;
             $items->attach($current);
         } elseif ($current instanceof Tokens\TokenComma) {
             if (!$commaExpected) {
                 throw new ParserException(sprintf('Unexpected token "," at position %d on line %d', $current->getPosition(), $current->getLine()));
             }
             $commaExpected = \false;
         } elseif ($current->getValue() === $stopAt) {
             break;
         } elseif (!$this->isIgnoredToken($current)) {
             throw new ParserException(sprintf('Unexpected token "%s" at position %d on line %d', $current->getOriginalValue(), $current->getPosition(), $current->getLine()));
         }
     } while ($this->ast->valid());
     if (!$commaExpected && $items->count() > 0) {
         throw new ParserException(sprintf('Unexpected token "," at position %d on line %d', $current->getPosition(), $current->getLine()));
     }
     $items->rewind();
     return $items;
 }