No Description

Tokenizer.php 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. }