public function addToken(Token $token) { if (null !== $this->reference) { throw new LogicException("Token buffer was not reset before adding tokens"); } $this->value .= $token->getValue(); $this->text .= $token->getText(); $this->length += $token->getLength(); return $this; }
/** */ public function testAddTokenAfterFlush() { $token = Token::factory(Token::TYPE_UNESCAPED, "x", 1, "x"); $buffer = ReferenceBuffer::factory()->addToken($token)->flush()->reset(); $token = Token::factory(Token::TYPE_UNESCAPED, "y", 1, "y"); $reference = $buffer->addToken($token)->flush()->getReference(); $this->assertEquals('y', $reference->getKey(), "Incorrect reference value built from tokens added after resetting flushed buffer"); }
/** * @param int $tokenAmount * @dataProvider providerTokenAmount */ public function testNotIsEndAfterAddingToken(int $tokenAmount) { $buffer = TokenBuffer::factory(); for ($i = 0; $i < $tokenAmount; $i++) { $token = Token::factory(Token::TYPE_SLASH, "/", 1); $buffer->addToken($token); } $this->assertFalse($buffer->isEnd(), "End condition after adding token to buffer"); }
/** * @param int $tokenAmount * @dataProvider providerTokenAmount */ public function testIsEndAfterReadingAllTokensFromBuffer(int $tokenAmount) { $buffer = TokenBuffer::factory(); for ($i = 0; $i < $tokenAmount; $i++) { $token = Token::factory(Token::TYPE_SLASH, "/", 1); $buffer->addToken($token); } for ($i = 0; $i < $tokenAmount; $i++) { $buffer->readToken(); } $this->assertTrue($buffer->isEnd(), "No end condition after reading"); }
/** * @expectedException \DomainException */ public function testSettingNegativePositionThrowsSplException() { Token::factory()->setPosition(-1); }
/** */ public function testNoErrorAfterSettingNonErrorType() { $token = Token::factory()->setType(Token::TYPE_SLASH); $this->assertFalse($token->isError(), "Error in token after setting non-error type"); }
public function testReadingSameTokenAfterUnreading() { $buffer = TokenBuffer::factory(); $token = Token::factory(Token::TYPE_SLASH, "/", 1); $buffer->addToken($token); $token = Token::factory(Token::TYPE_UNESCAPED, "a", 1, "a"); $buffer->addToken($token); $firstToken = $buffer->readToken(); $secondToken = $buffer->unreadToken()->readToken(); $this->assertSame($firstToken, $secondToken, "Reading after unreading returns different tokens"); }
/** * @expectedException \LengthException */ public function testSettingEmptyTextThrowsSplException() { Token::factory()->setText(''); }
public function addToken(Token $token) { $position = $this->getNextPosition(); $this->tokenList[] = $token->setPosition($position); return $this; }
/** * Checks if token is an error and processes it. * * @param Token $token * @return $this * @throws SyntaxException * @throws UnknownSyntaxException */ protected function assertErrorToken(Token $token) { if (!$token->isError()) { return $this; } switch ($token->getType()) { case Token::TYPE_ERROR_INVALID_ESCAPE: throw new SyntaxException("Invalid escape sequence at position #{$this->getNextPosition()}", $this->getNextPosition()); default: throw new UnknownSyntaxException("Unknown type of error token: {$token->getType()} at position #{$this->getNextPosition()}", $this->getNextPosition()); } }
/** * @param int[] $typeList * @return Token|null * @todo Split method in two. */ protected function matchNextToken(array $typeList) { $token = null; foreach ($typeList as $type) { $text = $this->matchTokenTextAtCursor($type); if (null !== $text) { $token = Token::factory($type, $text, $this->getTextLength($text)); switch ($type) { case Token::TYPE_ESCAPED: $token->setValue($this->unescape($text)); break; case Token::TYPE_UNESCAPED: $token->setValue($text); break; } $this->advanceCursor(strlen($text)); break; } } return $token; }