Ejemplo n.º 1
0
 /**
  * @param bool|false $namedArguments
  * @param bool|false $definition
  * @return Node
  * @throws SyntaxException
  */
 public function parseArguments($namedArguments = false, $definition = false)
 {
     $args = [];
     $openParan = $this->stream->expect(TokenTypes::T_OPEN_PARAN);
     $line = $openParan->getLine();
     $allowNonNamedArgs = true;
     while (!$this->stream->test(TokenTypes::T_CLOSE_PARAN)) {
         if (!empty($args)) {
             $this->stream->expect(TokenTypes::T_COMMA, ',', 'Arguments must be separated by comma.');
         }
         $name = null;
         $value = null;
         if ($definition) {
             $token = $this->stream->expect(TokenTypes::T_NAME, null, 'Arguments must have name.');
             if (!$this->isSimpleName($token)) {
                 throw new SyntaxException('Invalid argument name , "%s"', $token->getValue());
             }
             $name = $token->getValue();
             if ($this->stream->test(TokenTypes::T_SET)) {
                 $this->stream->expect(TokenTypes::T_SET);
                 $value = $this->parseExpression();
             }
         } else {
             if ($namedArguments && $this->stream->look()->is(TokenTypes::T_SET)) {
                 $nameToken = $this->stream->expect(TokenTypes::T_NAME, null, 'Missing argument name.');
                 if (!$this->isSimpleName($nameToken)) {
                     throw new SyntaxException('Invalid argument name , "%s"', $nameToken->getValue());
                 }
                 $name = $nameToken->getValue();
                 $this->stream->expect(TokenTypes::T_SET);
                 $allowNonNamedArgs = false;
             } elseif (!$allowNonNamedArgs) {
                 throw new SyntaxException('Non-named args must come before named args.');
             }
             $value = $this->parseExpression();
         }
         if ($name) {
             $args[$name] = $value;
         } else {
             $args[] = $value;
         }
     }
     $this->stream->expect(TokenTypes::T_CLOSE_PARAN, ')', 'A list of arguments must be closed by parenthesis');
     return new Node\Expression\Arguments($args, $definition, $line);
 }