菜谱项目

DocLexer.php 4.0KB

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