DocLexer.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. protected $noCase = array(
  51. '@' => self::T_AT,
  52. ',' => self::T_COMMA,
  53. '(' => self::T_OPEN_PARENTHESIS,
  54. ')' => self::T_CLOSE_PARENTHESIS,
  55. '{' => self::T_OPEN_CURLY_BRACES,
  56. '}' => self::T_CLOSE_CURLY_BRACES,
  57. '=' => self::T_EQUALS,
  58. ':' => self::T_COLON,
  59. '\\' => self::T_NAMESPACE_SEPARATOR
  60. );
  61. protected $withCase = array(
  62. 'true' => self::T_TRUE,
  63. 'false' => self::T_FALSE,
  64. 'null' => self::T_NULL
  65. );
  66. /**
  67. * {@inheritdoc}
  68. */
  69. protected function getCatchablePatterns()
  70. {
  71. return array(
  72. '[a-z_\\\][a-z0-9_\:\\\]*[a-z]{1}',
  73. '(?:[+-]?[0-9]+(?:[\.][0-9]+)*)(?:[eE][+-]?[0-9]+)?',
  74. '"(?:[^"]|"")*"',
  75. );
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. protected function getNonCatchablePatterns()
  81. {
  82. return array('\s+', '\*+', '(.)');
  83. }
  84. /**
  85. * {@inheritdoc}
  86. *
  87. * @param string $value
  88. *
  89. * @return int
  90. */
  91. protected function getType(&$value)
  92. {
  93. $type = self::T_NONE;
  94. if ($value[0] === '"') {
  95. $value = str_replace('""', '"', substr($value, 1, strlen($value) - 2));
  96. return self::T_STRING;
  97. }
  98. if (isset($this->noCase[$value])) {
  99. return $this->noCase[$value];
  100. }
  101. if ($value[0] === '_' || $value[0] === '\\' || ctype_alpha($value[0])) {
  102. return self::T_IDENTIFIER;
  103. }
  104. $lowerValue = strtolower($value);
  105. if (isset($this->withCase[$lowerValue])) {
  106. return $this->withCase[$lowerValue];
  107. }
  108. // Checking numeric value
  109. if (is_numeric($value)) {
  110. return (strpos($value, '.') !== false || stripos($value, 'e') !== false)
  111. ? self::T_FLOAT : self::T_INTEGER;
  112. }
  113. return $type;
  114. }
  115. }