TokenStream example

use Symfony\Component\CssSelector\Parser\Handler\CommentHandler;
use Symfony\Component\CssSelector\Parser\Reader;
use Symfony\Component\CssSelector\Parser\Token;
use Symfony\Component\CssSelector\Parser\TokenStream;

class CommentHandlerTest extends AbstractHandlerTestCase
{
    /** @dataProvider getHandleValueTestData */
    public function testHandleValue($value, Token $unusedArgument$remainingContent)
    {
        $reader = new Reader($value);
        $stream = new TokenStream();

        $this->assertTrue($this->generateHandler()->handle($reader$stream));
        // comments are ignored (not pushed as token in stream)         $this->assertStreamEmpty($stream);
        $this->assertRemainingContent($reader$remainingContent);
    }

    public static function getHandleValueTestData()
    {
        return [
            // 2nd argument only exists for inherited method compatibility
protected function setUp(): void
    {
        $this->lexer = new Lexer();
    }

    /** * @dataProvider getTokenizeData */
    public function testTokenize($tokens$expression)
    {
        $tokens[] = new Token('end of expression', null, \strlen($expression) + 1);
        $this->assertEquals(new TokenStream($tokens$expression)$this->lexer->tokenize($expression));
    }

    public function testTokenizeThrowsErrorWithMessage()
    {
        $this->expectException(SyntaxError::class);
        $this->expectExceptionMessage('Unexpected character "\'" around position 33 for expression `service(faulty.expression.example\').dummyMethod()`.');
        $expression = "service(faulty.expression.example').dummyMethod()";
        $this->lexer->tokenize($expression);
    }

    public function testTokenizeThrowsErrorOnUnclosedBrace()
    {
use Symfony\Component\CssSelector\Parser\TokenStream;

/** * @author Jean-François Simon <contact@jfsimon.fr> */
abstract class AbstractHandlerTestCase extends TestCase
{
    /** @dataProvider getHandleValueTestData */
    public function testHandleValue($value, Token $expectedToken$remainingContent)
    {
        $reader = new Reader($value);
        $stream = new TokenStream();

        $this->assertTrue($this->generateHandler()->handle($reader$stream));
        $this->assertEquals($expectedToken$stream->getNext());
        $this->assertRemainingContent($reader$remainingContent);
    }

    /** @dataProvider getDontHandleValueTestData */
    public function testDontHandleValue($value)
    {
        $reader = new Reader($value);
        $stream = new TokenStream();

        
                throw new SyntaxError(sprintf('Unexpected character "%s".', $expression[$cursor])$cursor$expression);
            }
        }

        $tokens[] = new Token(Token::EOF_TYPE, null, $cursor + 1);

        if ($brackets) {
            [$expect$cur] = array_pop($brackets);
            throw new SyntaxError(sprintf('Unclosed "%s".', $expect)$cur$expression);
        }

        return new TokenStream($tokens$expression);
    }
}
break;
            }
        }

        $this->pushToken(/* Token::EOF_TYPE */ -1);

        if (!empty($this->brackets)) {
            list($expect$lineno) = array_pop($this->brackets);
            throw new SyntaxError(sprintf('Unclosed "%s".', $expect)$lineno$this->source);
        }

        return new TokenStream($this->tokens, $this->source);
    }

    private function lexData(): void
    {
        // if no matches are left we return the rest of the template as simple text token         if ($this->position == \count($this->positions[0]) - 1) {
            $this->pushToken(/* Token::TEXT_TYPE */ 0, substr($this->code, $this->cursor));
            $this->cursor = $this->end;

            return;
        }

        
namespace Symfony\Component\CssSelector\Tests\Parser;

use PHPUnit\Framework\TestCase;
use Symfony\Component\CssSelector\Exception\SyntaxErrorException;
use Symfony\Component\CssSelector\Parser\Token;
use Symfony\Component\CssSelector\Parser\TokenStream;

class TokenStreamTest extends TestCase
{
    public function testGetNext()
    {
        $stream = new TokenStream();
        $stream->push($t1 = new Token(Token::TYPE_IDENTIFIER, 'h1', 0));
        $stream->push($t2 = new Token(Token::TYPE_DELIMITER, '.', 2));
        $stream->push($t3 = new Token(Token::TYPE_IDENTIFIER, 'title', 3));

        $this->assertSame($t1$stream->getNext());
        $this->assertSame($t2$stream->getNext());
        $this->assertSame($t3$stream->getNext());
    }

    public function testGetPeek()
    {
        
new Handler\StringHandler($patterns$escaping),
            new Handler\NumberHandler($patterns),
            new Handler\CommentHandler(),
        ];
    }

    /** * Tokenize selector source code. */
    public function tokenize(Reader $reader): TokenStream
    {
        $stream = new TokenStream();

        while (!$reader->isEOF()) {
            foreach ($this->handlers as $handler) {
                if ($handler->handle($reader$stream)) {
                    continue 2;
                }
            }

            $stream->push(new Token(Token::TYPE_DELIMITER, $reader->getSubstring(1)$reader->getPosition()));
            $reader->moveForward(1);
        }

        
Home | Imprint | This part of the site doesn't use cookies.