comparison vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/TokenParser.php @ 0:4c8ae668cc8c

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