DocLexer.php 3.4 KB

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