annotate vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/TokenParser.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 7a779792577d
children
rev   line source
Chris@0 1 <?php
Chris@0 2 /*
Chris@0 3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Chris@0 4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Chris@0 5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Chris@0 6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Chris@0 7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Chris@0 8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Chris@0 9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Chris@0 10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Chris@0 11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Chris@0 12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Chris@0 13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Chris@0 14 *
Chris@0 15 * This software consists of voluntary contributions made by many individuals
Chris@0 16 * and is licensed under the MIT license. For more information, see
Chris@0 17 * <http://www.doctrine-project.org>.
Chris@0 18 */
Chris@0 19
Chris@0 20 namespace Doctrine\Common\Annotations;
Chris@0 21
Chris@0 22 /**
Chris@0 23 * Parses a file for namespaces/use/class declarations.
Chris@0 24 *
Chris@0 25 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 26 * @author Christian Kaps <christian.kaps@mohiva.com>
Chris@0 27 */
Chris@0 28 class TokenParser
Chris@0 29 {
Chris@0 30 /**
Chris@0 31 * The token list.
Chris@0 32 *
Chris@0 33 * @var array
Chris@0 34 */
Chris@0 35 private $tokens;
Chris@0 36
Chris@0 37 /**
Chris@0 38 * The number of tokens.
Chris@0 39 *
Chris@0 40 * @var int
Chris@0 41 */
Chris@0 42 private $numTokens;
Chris@0 43
Chris@0 44 /**
Chris@0 45 * The current array pointer.
Chris@0 46 *
Chris@0 47 * @var int
Chris@0 48 */
Chris@0 49 private $pointer = 0;
Chris@0 50
Chris@0 51 /**
Chris@0 52 * @param string $contents
Chris@0 53 */
Chris@0 54 public function __construct($contents)
Chris@0 55 {
Chris@0 56 $this->tokens = token_get_all($contents);
Chris@0 57
Chris@0 58 // The PHP parser sets internal compiler globals for certain things. Annoyingly, the last docblock comment it
Chris@0 59 // saw gets stored in doc_comment. When it comes to compile the next thing to be include()d this stored
Chris@0 60 // doc_comment becomes owned by the first thing the compiler sees in the file that it considers might have a
Chris@0 61 // docblock. If the first thing in the file is a class without a doc block this would cause calls to
Chris@0 62 // getDocBlock() on said class to return our long lost doc_comment. Argh.
Chris@0 63 // To workaround, cause the parser to parse an empty docblock. Sure getDocBlock() will return this, but at least
Chris@0 64 // it's harmless to us.
Chris@0 65 token_get_all("<?php\n/**\n *\n */");
Chris@0 66
Chris@0 67 $this->numTokens = count($this->tokens);
Chris@0 68 }
Chris@0 69
Chris@0 70 /**
Chris@0 71 * Gets the next non whitespace and non comment token.
Chris@0 72 *
Chris@0 73 * @param boolean $docCommentIsComment If TRUE then a doc comment is considered a comment and skipped.
Chris@0 74 * If FALSE then only whitespace and normal comments are skipped.
Chris@0 75 *
Chris@0 76 * @return array|null The token if exists, null otherwise.
Chris@0 77 */
Chris@0 78 public function next($docCommentIsComment = TRUE)
Chris@0 79 {
Chris@0 80 for ($i = $this->pointer; $i < $this->numTokens; $i++) {
Chris@0 81 $this->pointer++;
Chris@0 82 if ($this->tokens[$i][0] === T_WHITESPACE ||
Chris@0 83 $this->tokens[$i][0] === T_COMMENT ||
Chris@0 84 ($docCommentIsComment && $this->tokens[$i][0] === T_DOC_COMMENT)) {
Chris@0 85
Chris@0 86 continue;
Chris@0 87 }
Chris@0 88
Chris@0 89 return $this->tokens[$i];
Chris@0 90 }
Chris@0 91
Chris@0 92 return null;
Chris@0 93 }
Chris@0 94
Chris@0 95 /**
Chris@0 96 * Parses a single use statement.
Chris@0 97 *
Chris@0 98 * @return array A list with all found class names for a use statement.
Chris@0 99 */
Chris@0 100 public function parseUseStatement()
Chris@0 101 {
Chris@12 102
Chris@12 103 $groupRoot = '';
Chris@0 104 $class = '';
Chris@0 105 $alias = '';
Chris@0 106 $statements = array();
Chris@0 107 $explicitAlias = false;
Chris@0 108 while (($token = $this->next())) {
Chris@0 109 $isNameToken = $token[0] === T_STRING || $token[0] === T_NS_SEPARATOR;
Chris@0 110 if (!$explicitAlias && $isNameToken) {
Chris@0 111 $class .= $token[1];
Chris@0 112 $alias = $token[1];
Chris@0 113 } else if ($explicitAlias && $isNameToken) {
Chris@0 114 $alias .= $token[1];
Chris@0 115 } else if ($token[0] === T_AS) {
Chris@0 116 $explicitAlias = true;
Chris@0 117 $alias = '';
Chris@0 118 } else if ($token === ',') {
Chris@12 119 $statements[strtolower($alias)] = $groupRoot . $class;
Chris@0 120 $class = '';
Chris@0 121 $alias = '';
Chris@0 122 $explicitAlias = false;
Chris@0 123 } else if ($token === ';') {
Chris@12 124 $statements[strtolower($alias)] = $groupRoot . $class;
Chris@0 125 break;
Chris@12 126 } else if ($token === '{' ) {
Chris@12 127 $groupRoot = $class;
Chris@12 128 $class = '';
Chris@12 129 } else if ($token === '}' ) {
Chris@12 130 continue;
Chris@0 131 } else {
Chris@0 132 break;
Chris@0 133 }
Chris@0 134 }
Chris@0 135
Chris@0 136 return $statements;
Chris@0 137 }
Chris@0 138
Chris@0 139 /**
Chris@0 140 * Gets all use statements.
Chris@0 141 *
Chris@0 142 * @param string $namespaceName The namespace name of the reflected class.
Chris@0 143 *
Chris@0 144 * @return array A list with all found use statements.
Chris@0 145 */
Chris@0 146 public function parseUseStatements($namespaceName)
Chris@0 147 {
Chris@0 148 $statements = array();
Chris@0 149 while (($token = $this->next())) {
Chris@0 150 if ($token[0] === T_USE) {
Chris@0 151 $statements = array_merge($statements, $this->parseUseStatement());
Chris@0 152 continue;
Chris@0 153 }
Chris@0 154 if ($token[0] !== T_NAMESPACE || $this->parseNamespace() != $namespaceName) {
Chris@0 155 continue;
Chris@0 156 }
Chris@0 157
Chris@0 158 // Get fresh array for new namespace. This is to prevent the parser to collect the use statements
Chris@0 159 // for a previous namespace with the same name. This is the case if a namespace is defined twice
Chris@0 160 // or if a namespace with the same name is commented out.
Chris@0 161 $statements = array();
Chris@0 162 }
Chris@0 163
Chris@0 164 return $statements;
Chris@0 165 }
Chris@0 166
Chris@0 167 /**
Chris@0 168 * Gets the namespace.
Chris@0 169 *
Chris@0 170 * @return string The found namespace.
Chris@0 171 */
Chris@0 172 public function parseNamespace()
Chris@0 173 {
Chris@0 174 $name = '';
Chris@0 175 while (($token = $this->next()) && ($token[0] === T_STRING || $token[0] === T_NS_SEPARATOR)) {
Chris@0 176 $name .= $token[1];
Chris@0 177 }
Chris@0 178
Chris@0 179 return $name;
Chris@0 180 }
Chris@0 181
Chris@0 182 /**
Chris@0 183 * Gets the class name.
Chris@0 184 *
Chris@0 185 * @return string The found class name.
Chris@0 186 */
Chris@0 187 public function parseClass()
Chris@0 188 {
Chris@0 189 // Namespaces and class names are tokenized the same: T_STRINGs
Chris@0 190 // separated by T_NS_SEPARATOR so we can use one function to provide
Chris@0 191 // both.
Chris@0 192 return $this->parseNamespace();
Chris@0 193 }
Chris@0 194 }