菜谱项目

TokenParser.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. /**
  21. * Parses a file for namespaces/use/class declarations.
  22. *
  23. * @author Fabien Potencier <fabien@symfony.com>
  24. * @author Christian Kaps <christian.kaps@mohiva.com>
  25. */
  26. class TokenParser
  27. {
  28. /**
  29. * The token list.
  30. *
  31. * @var array
  32. */
  33. private $tokens;
  34. /**
  35. * The number of tokens.
  36. *
  37. * @var int
  38. */
  39. private $numTokens;
  40. /**
  41. * The current array pointer.
  42. *
  43. * @var int
  44. */
  45. private $pointer = 0;
  46. /**
  47. * @param string $contents
  48. */
  49. public function __construct($contents)
  50. {
  51. $this->tokens = token_get_all($contents);
  52. // The PHP parser sets internal compiler globals for certain things. Annoyingly, the last docblock comment it
  53. // saw gets stored in doc_comment. When it comes to compile the next thing to be include()d this stored
  54. // doc_comment becomes owned by the first thing the compiler sees in the file that it considers might have a
  55. // docblock. If the first thing in the file is a class without a doc block this would cause calls to
  56. // getDocBlock() on said class to return our long lost doc_comment. Argh.
  57. // To workaround, cause the parser to parse an empty docblock. Sure getDocBlock() will return this, but at least
  58. // it's harmless to us.
  59. token_get_all("<?php\n/**\n *\n */");
  60. $this->numTokens = count($this->tokens);
  61. }
  62. /**
  63. * Gets the next non whitespace and non comment token.
  64. *
  65. * @param boolean $docCommentIsComment If TRUE then a doc comment is considered a comment and skipped.
  66. * If FALSE then only whitespace and normal comments are skipped.
  67. *
  68. * @return array|null The token if exists, null otherwise.
  69. */
  70. public function next($docCommentIsComment = TRUE)
  71. {
  72. for ($i = $this->pointer; $i < $this->numTokens; $i++) {
  73. $this->pointer++;
  74. if ($this->tokens[$i][0] === T_WHITESPACE ||
  75. $this->tokens[$i][0] === T_COMMENT ||
  76. ($docCommentIsComment && $this->tokens[$i][0] === T_DOC_COMMENT)) {
  77. continue;
  78. }
  79. return $this->tokens[$i];
  80. }
  81. return null;
  82. }
  83. /**
  84. * Parses a single use statement.
  85. *
  86. * @return array A list with all found class names for a use statement.
  87. */
  88. public function parseUseStatement()
  89. {
  90. $groupRoot = '';
  91. $class = '';
  92. $alias = '';
  93. $statements = array();
  94. $explicitAlias = false;
  95. while (($token = $this->next())) {
  96. $isNameToken = $token[0] === T_STRING || $token[0] === T_NS_SEPARATOR;
  97. if (!$explicitAlias && $isNameToken) {
  98. $class .= $token[1];
  99. $alias = $token[1];
  100. } else if ($explicitAlias && $isNameToken) {
  101. $alias .= $token[1];
  102. } else if ($token[0] === T_AS) {
  103. $explicitAlias = true;
  104. $alias = '';
  105. } else if ($token === ',') {
  106. $statements[strtolower($alias)] = $groupRoot . $class;
  107. $class = '';
  108. $alias = '';
  109. $explicitAlias = false;
  110. } else if ($token === ';') {
  111. $statements[strtolower($alias)] = $groupRoot . $class;
  112. break;
  113. } else if ($token === '{' ) {
  114. $groupRoot = $class;
  115. $class = '';
  116. } else if ($token === '}' ) {
  117. continue;
  118. } else {
  119. break;
  120. }
  121. }
  122. return $statements;
  123. }
  124. /**
  125. * Gets all use statements.
  126. *
  127. * @param string $namespaceName The namespace name of the reflected class.
  128. *
  129. * @return array A list with all found use statements.
  130. */
  131. public function parseUseStatements($namespaceName)
  132. {
  133. $statements = array();
  134. while (($token = $this->next())) {
  135. if ($token[0] === T_USE) {
  136. $statements = array_merge($statements, $this->parseUseStatement());
  137. continue;
  138. }
  139. if ($token[0] !== T_NAMESPACE || $this->parseNamespace() != $namespaceName) {
  140. continue;
  141. }
  142. // Get fresh array for new namespace. This is to prevent the parser to collect the use statements
  143. // for a previous namespace with the same name. This is the case if a namespace is defined twice
  144. // or if a namespace with the same name is commented out.
  145. $statements = array();
  146. }
  147. return $statements;
  148. }
  149. /**
  150. * Gets the namespace.
  151. *
  152. * @return string The found namespace.
  153. */
  154. public function parseNamespace()
  155. {
  156. $name = '';
  157. while (($token = $this->next()) && ($token[0] === T_STRING || $token[0] === T_NS_SEPARATOR)) {
  158. $name .= $token[1];
  159. }
  160. return $name;
  161. }
  162. /**
  163. * Gets the class name.
  164. *
  165. * @return string The found class name.
  166. */
  167. public function parseClass()
  168. {
  169. // Namespaces and class names are tokenized the same: T_STRINGs
  170. // separated by T_NS_SEPARATOR so we can use one function to provide
  171. // both.
  172. return $this->parseNamespace();
  173. }
  174. }