Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / vendor / symfony / css-selector / Parser / Tokenizer / Tokenizer.php
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
12 namespace Symfony\Component\CssSelector\Parser\Tokenizer;
13
14 use Symfony\Component\CssSelector\Parser\Handler;
15 use Symfony\Component\CssSelector\Parser\Reader;
16 use Symfony\Component\CssSelector\Parser\Token;
17 use 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  */
29 class Tokenizer
30 {
31     /**
32      * @var Handler\HandlerInterface[]
33      */
34     private $handlers;
35
36     public function __construct()
37     {
38         $patterns = new TokenizerPatterns();
39         $escaping = new TokenizerEscaping($patterns);
40
41         $this->handlers = array(
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      * @return TokenStream
55      */
56     public function tokenize(Reader $reader)
57     {
58         $stream = new TokenStream();
59
60         while (!$reader->isEOF()) {
61             foreach ($this->handlers as $handler) {
62                 if ($handler->handle($reader, $stream)) {
63                     continue 2;
64                 }
65             }
66
67             $stream->push(new Token(Token::TYPE_DELIMITER, $reader->getSubstring(1), $reader->getPosition()));
68             $reader->moveForward(1);
69         }
70
71         return $stream
72             ->push(new Token(Token::TYPE_FILE_END, null, $reader->getPosition()))
73             ->freeze();
74     }
75 }