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()); }
protected function parseArray() { $token = $this->stream->getCurrent(); if ($token->is(TokenTypes::T_OPEN_BRACKET)) { $openToken = TokenTypes::T_OPEN_BRACKET; $closeToken = TokenTypes::T_CLOSE_BRACKET; } else { $openToken = TokenTypes::T_OPEN_BRACE; $closeToken = TokenTypes::T_CLOSE_BRACE; } $arrayType = $token->is(TokenTypes::T_OPEN_BRACE) ? 'BRACE' : 'BRACKET'; $allowKey = $arrayType == 'BRACE'; $elements = []; // $stack = []; $first = true; $this->stream->expect($openToken); while (!$this->stream->test($closeToken)) { if (!$first) { $this->stream->expect(TokenTypes::T_COMMA, null, 'Array elements must be separate by comma.'); } $key = $value = null; $value = $this->parseExpression(); if ($this->stream->test(TokenTypes::T_COLON)) { if (!$allowKey) { throw new SyntaxException(sprintf('Vector array not allowed')); } $this->stream->expect(TokenTypes::T_COLON); $key = $value; $value = $this->parseExpression(); if (!$key instanceof Node\Expression\Name && !$key instanceof Node\Expression\Constant) { throw new SyntaxException(sprintf('Invalid array key definition')); } } $elements[] = [$key, $value]; $first = false; } $this->stream->expect($closeToken); return new Node\Expression\ArrayNode($elements, $token->getLine()); }