Token.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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\ExpressionLanguage;
  11. /**
  12. * Represents a Token.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class Token
  17. {
  18. public $value;
  19. public $type;
  20. public $cursor;
  21. const EOF_TYPE = 'end of expression';
  22. const NAME_TYPE = 'name';
  23. const NUMBER_TYPE = 'number';
  24. const STRING_TYPE = 'string';
  25. const OPERATOR_TYPE = 'operator';
  26. const PUNCTUATION_TYPE = 'punctuation';
  27. /**
  28. * @param string $type The type of the token (self::*_TYPE)
  29. * @param string|int|float|null $value The token value
  30. * @param int $cursor The cursor position in the source
  31. */
  32. public function __construct($type, $value, $cursor)
  33. {
  34. $this->type = $type;
  35. $this->value = $value;
  36. $this->cursor = $cursor;
  37. }
  38. /**
  39. * Returns a string representation of the token.
  40. *
  41. * @return string A string representation of the token
  42. */
  43. public function __toString()
  44. {
  45. return sprintf('%3d %-11s %s', $this->cursor, strtoupper($this->type), $this->value);
  46. }
  47. /**
  48. * Tests the current token for a type and/or a value.
  49. *
  50. * @param array|int $type The type to test
  51. * @param string|null $value The token value
  52. *
  53. * @return bool
  54. */
  55. public function test($type, $value = null)
  56. {
  57. return $this->type === $type && (null === $value || $this->value == $value);
  58. }
  59. }