friendship ended with social-app. php is my new best friend
1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\CssSelector\Parser\Tokenizer;
13
14use Symfony\Component\CssSelector\Parser\Handler;
15use Symfony\Component\CssSelector\Parser\Reader;
16use Symfony\Component\CssSelector\Parser\Token;
17use Symfony\Component\CssSelector\Parser\TokenStream;
18
19/**
20 * CSS selector tokenizer.
21 *
22 * This component is a port of the Python cssselect library,
23 * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
24 *
25 * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
26 *
27 * @internal
28 */
29class Tokenizer
30{
31 /**
32 * @var Handler\HandlerInterface[]
33 */
34 private array $handlers;
35
36 public function __construct()
37 {
38 $patterns = new TokenizerPatterns();
39 $escaping = new TokenizerEscaping($patterns);
40
41 $this->handlers = [
42 new Handler\WhitespaceHandler(),
43 new Handler\IdentifierHandler($patterns, $escaping),
44 new Handler\HashHandler($patterns, $escaping),
45 new Handler\StringHandler($patterns, $escaping),
46 new Handler\NumberHandler($patterns),
47 new Handler\CommentHandler(),
48 ];
49 }
50
51 /**
52 * Tokenize selector source code.
53 */
54 public function tokenize(Reader $reader): TokenStream
55 {
56 $stream = new TokenStream();
57
58 while (!$reader->isEOF()) {
59 foreach ($this->handlers as $handler) {
60 if ($handler->handle($reader, $stream)) {
61 continue 2;
62 }
63 }
64
65 $stream->push(new Token(Token::TYPE_DELIMITER, $reader->getSubstring(1), $reader->getPosition()));
66 $reader->moveForward(1);
67 }
68
69 return $stream
70 ->push(new Token(Token::TYPE_FILE_END, null, $reader->getPosition()))
71 ->freeze();
72 }
73}