Ejemplo n.º 1
0
 public function testTokenStream()
 {
     $stream = new TokenStream([new Token(null, 'Hello', TokenTypes::T_NAME, 0, 0), new Token(null, '"World"', TokenTypes::T_STR, 0, 0), new Token(null, '', TokenTypes::T_EOF, 0, 0)]);
     $this->assertTrue($stream->test(TokenTypes::T_NAME, 'Hello'));
     $this->assertEquals(new Token(null, '"World"', TokenTypes::T_STR, 0, 0), $stream->nextIf(TokenTypes::T_STR));
     $this->assertEquals(new Token(null, '"World"', TokenTypes::T_STR, 0, 0), $stream->getCurrent());
     $this->assertEquals(new Token(null, '"World"', TokenTypes::T_STR, 0, 0), $stream->expect(TokenTypes::T_STR));
     $this->assertTrue($stream->isEOF());
 }
Ejemplo n.º 2
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);
 }
Ejemplo n.º 3
0
 public function decideIfBraceEnd(TokenStream $stream)
 {
     return $stream->test([TokenTypes::T_SECTION_CLOSE], '}');
 }
Ejemplo n.º 4
0
 public function testEndBrace(TokenStream $stream)
 {
     return $stream->test(TokenTypes::T_SECTION_CLOSE, '}');
 }