DocLexer.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace Doctrine\Common\Annotations;
  3. use Doctrine\Common\Lexer\AbstractLexer;
  4. use function ctype_alpha;
  5. use function is_numeric;
  6. use function str_replace;
  7. use function stripos;
  8. use function strlen;
  9. use function strpos;
  10. use function strtolower;
  11. use function substr;
  12. /**
  13. * Simple lexer for docblock annotations.
  14. *
  15. * @template-extends AbstractLexer<DocLexer::T_*, string>
  16. */
  17. final class DocLexer extends AbstractLexer
  18. {
  19. public const T_NONE = 1;
  20. public const T_INTEGER = 2;
  21. public const T_STRING = 3;
  22. public const T_FLOAT = 4;
  23. // All tokens that are also identifiers should be >= 100
  24. public const T_IDENTIFIER = 100;
  25. public const T_AT = 101;
  26. public const T_CLOSE_CURLY_BRACES = 102;
  27. public const T_CLOSE_PARENTHESIS = 103;
  28. public const T_COMMA = 104;
  29. public const T_EQUALS = 105;
  30. public const T_FALSE = 106;
  31. public const T_NAMESPACE_SEPARATOR = 107;
  32. public const T_OPEN_CURLY_BRACES = 108;
  33. public const T_OPEN_PARENTHESIS = 109;
  34. public const T_TRUE = 110;
  35. public const T_NULL = 111;
  36. public const T_COLON = 112;
  37. public const T_MINUS = 113;
  38. /** @var array<string, self::T*> */
  39. protected $noCase = [
  40. '@' => self::T_AT,
  41. ',' => self::T_COMMA,
  42. '(' => self::T_OPEN_PARENTHESIS,
  43. ')' => self::T_CLOSE_PARENTHESIS,
  44. '{' => self::T_OPEN_CURLY_BRACES,
  45. '}' => self::T_CLOSE_CURLY_BRACES,
  46. '=' => self::T_EQUALS,
  47. ':' => self::T_COLON,
  48. '-' => self::T_MINUS,
  49. '\\' => self::T_NAMESPACE_SEPARATOR,
  50. ];
  51. /** @var array<string, self::T*> */
  52. protected $withCase = [
  53. 'true' => self::T_TRUE,
  54. 'false' => self::T_FALSE,
  55. 'null' => self::T_NULL,
  56. ];
  57. /**
  58. * Whether the next token starts immediately, or if there were
  59. * non-captured symbols before that
  60. */
  61. public function nextTokenIsAdjacent(): bool
  62. {
  63. return $this->token === null
  64. || ($this->lookahead !== null
  65. && ($this->lookahead['position'] - $this->token['position']) === strlen($this->token['value']));
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. protected function getCatchablePatterns()
  71. {
  72. return [
  73. '[a-z_\\\][a-z0-9_\:\\\]*[a-z_][a-z0-9_]*',
  74. '(?:[+-]?[0-9]+(?:[\.][0-9]+)*)(?:[eE][+-]?[0-9]+)?',
  75. '"(?:""|[^"])*+"',
  76. ];
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. protected function getNonCatchablePatterns()
  82. {
  83. return ['\s+', '\*+', '(.)'];
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. protected function getType(&$value)
  89. {
  90. $type = self::T_NONE;
  91. if ($value[0] === '"') {
  92. $value = str_replace('""', '"', substr($value, 1, strlen($value) - 2));
  93. return self::T_STRING;
  94. }
  95. if (isset($this->noCase[$value])) {
  96. return $this->noCase[$value];
  97. }
  98. if ($value[0] === '_' || $value[0] === '\\' || ctype_alpha($value[0])) {
  99. return self::T_IDENTIFIER;
  100. }
  101. $lowerValue = strtolower($value);
  102. if (isset($this->withCase[$lowerValue])) {
  103. return $this->withCase[$lowerValue];
  104. }
  105. // Checking numeric value
  106. if (is_numeric($value)) {
  107. return strpos($value, '.') !== false || stripos($value, 'e') !== false
  108. ? self::T_FLOAT : self::T_INTEGER;
  109. }
  110. return $type;
  111. }
  112. /** @return array{value: int|string, type:self::T_*|null, position:int} */
  113. public function peek(): ?array
  114. {
  115. $token = parent::peek();
  116. if ($token === null) {
  117. return null;
  118. }
  119. return (array) $token;
  120. }
  121. }