DocLexer.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Common\Annotations;
  20. use Doctrine\Common\Lexer\AbstractLexer;
  21. /**
  22. * Simple lexer for docblock annotations.
  23. *
  24. * @author Benjamin Eberlei <kontakt@beberlei.de>
  25. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  26. * @author Jonathan Wage <jonwage@gmail.com>
  27. * @author Roman Borschel <roman@code-factory.org>
  28. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  29. */
  30. final class DocLexer extends AbstractLexer
  31. {
  32. const T_NONE = 1;
  33. const T_INTEGER = 2;
  34. const T_STRING = 3;
  35. const T_FLOAT = 4;
  36. // All tokens that are also identifiers should be >= 100
  37. const T_IDENTIFIER = 100;
  38. const T_AT = 101;
  39. const T_CLOSE_CURLY_BRACES = 102;
  40. const T_CLOSE_PARENTHESIS = 103;
  41. const T_COMMA = 104;
  42. const T_EQUALS = 105;
  43. const T_FALSE = 106;
  44. const T_NAMESPACE_SEPARATOR = 107;
  45. const T_OPEN_CURLY_BRACES = 108;
  46. const T_OPEN_PARENTHESIS = 109;
  47. const T_TRUE = 110;
  48. const T_NULL = 111;
  49. const T_COLON = 112;
  50. const T_MINUS = 113;
  51. /**
  52. * @var array
  53. */
  54. protected $noCase = array(
  55. '@' => self::T_AT,
  56. ',' => self::T_COMMA,
  57. '(' => self::T_OPEN_PARENTHESIS,
  58. ')' => self::T_CLOSE_PARENTHESIS,
  59. '{' => self::T_OPEN_CURLY_BRACES,
  60. '}' => self::T_CLOSE_CURLY_BRACES,
  61. '=' => self::T_EQUALS,
  62. ':' => self::T_COLON,
  63. '-' => self::T_MINUS,
  64. '\\' => self::T_NAMESPACE_SEPARATOR
  65. );
  66. /**
  67. * @var array
  68. */
  69. protected $withCase = array(
  70. 'true' => self::T_TRUE,
  71. 'false' => self::T_FALSE,
  72. 'null' => self::T_NULL
  73. );
  74. /**
  75. * Whether the next token starts immediately, or if there were
  76. * non-captured symbols before that
  77. */
  78. public function nextTokenIsAdjacent() : bool
  79. {
  80. return $this->token === null
  81. || ($this->lookahead !== null
  82. && ($this->lookahead['position'] - $this->token['position']) === strlen($this->token['value']));
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. protected function getCatchablePatterns()
  88. {
  89. return array(
  90. '[a-z_\\\][a-z0-9_\:\\\]*[a-z_][a-z0-9_]*',
  91. '(?:[+-]?[0-9]+(?:[\.][0-9]+)*)(?:[eE][+-]?[0-9]+)?',
  92. '"(?:""|[^"])*+"',
  93. );
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. protected function getNonCatchablePatterns()
  99. {
  100. return array('\s+', '\*+', '(.)');
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. protected function getType(&$value)
  106. {
  107. $type = self::T_NONE;
  108. if ($value[0] === '"') {
  109. $value = str_replace('""', '"', substr($value, 1, strlen($value) - 2));
  110. return self::T_STRING;
  111. }
  112. if (isset($this->noCase[$value])) {
  113. return $this->noCase[$value];
  114. }
  115. if ($value[0] === '_' || $value[0] === '\\' || ctype_alpha($value[0])) {
  116. return self::T_IDENTIFIER;
  117. }
  118. $lowerValue = strtolower($value);
  119. if (isset($this->withCase[$lowerValue])) {
  120. return $this->withCase[$lowerValue];
  121. }
  122. // Checking numeric value
  123. if (is_numeric($value)) {
  124. return (strpos($value, '.') !== false || stripos($value, 'e') !== false)
  125. ? self::T_FLOAT : self::T_INTEGER;
  126. }
  127. return $type;
  128. }
  129. }