DocLexer.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Common\Annotations;
  20. use Doctrine\Common\Lexer;
  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 Lexer
  31. {
  32. const T_NONE = 1;
  33. const T_IDENTIFIER = 2;
  34. const T_INTEGER = 3;
  35. const T_STRING = 4;
  36. const T_FLOAT = 5;
  37. const T_AT = 101;
  38. const T_CLOSE_CURLY_BRACES = 102;
  39. const T_CLOSE_PARENTHESIS = 103;
  40. const T_COMMA = 104;
  41. const T_EQUALS = 105;
  42. const T_FALSE = 106;
  43. const T_NAMESPACE_SEPARATOR = 107;
  44. const T_OPEN_CURLY_BRACES = 108;
  45. const T_OPEN_PARENTHESIS = 109;
  46. const T_TRUE = 110;
  47. const T_NULL = 111;
  48. const T_COLON = 112;
  49. /**
  50. * @inheritdoc
  51. */
  52. protected function getCatchablePatterns()
  53. {
  54. return array(
  55. '[a-z_][a-z0-9_:]*',
  56. '(?:[+-]?[0-9]+(?:[\.][0-9]+)*)(?:[eE][+-]?[0-9]+)?',
  57. '"(?:[^"]|"")*"',
  58. );
  59. }
  60. /**
  61. * @inheritdoc
  62. */
  63. protected function getNonCatchablePatterns()
  64. {
  65. return array('\s+', '\*+', '(.)');
  66. }
  67. /**
  68. * @inheritdoc
  69. */
  70. protected function getType(&$value)
  71. {
  72. $type = self::T_NONE;
  73. // Checking numeric value
  74. if (is_numeric($value)) {
  75. return (strpos($value, '.') !== false || stripos($value, 'e') !== false)
  76. ? self::T_FLOAT : self::T_INTEGER;
  77. }
  78. if ($value[0] === '"') {
  79. $value = str_replace('""', '"', substr($value, 1, strlen($value) - 2));
  80. return self::T_STRING;
  81. } else {
  82. switch (strtolower($value)) {
  83. case '@':
  84. return self::T_AT;
  85. case ',':
  86. return self::T_COMMA;
  87. case '(':
  88. return self::T_OPEN_PARENTHESIS;
  89. case ')':
  90. return self::T_CLOSE_PARENTHESIS;
  91. case '{':
  92. return self::T_OPEN_CURLY_BRACES;
  93. case '}':
  94. return self::T_CLOSE_CURLY_BRACES;
  95. case '=':
  96. return self::T_EQUALS;
  97. case '\\':
  98. return self::T_NAMESPACE_SEPARATOR;
  99. case 'true':
  100. return self::T_TRUE;
  101. case 'false':
  102. return self::T_FALSE;
  103. case 'null':
  104. return self::T_NULL;
  105. case ':':
  106. return self::T_COLON;
  107. default:
  108. if (ctype_alpha($value[0]) || $value[0] === '_') {
  109. return self::T_IDENTIFIER;
  110. }
  111. break;
  112. }
  113. }
  114. return $type;
  115. }
  116. }