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()); }
/** * @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); }
public function decideIfBraceEnd(TokenStream $stream) { return $stream->test([TokenTypes::T_SECTION_CLOSE], '}'); }
public function testEndBrace(TokenStream $stream) { return $stream->test(TokenTypes::T_SECTION_CLOSE, '}'); }