DocLexer.php 3.9KB

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