Tokenizer.php 2.1 KB

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