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.
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>.
20 namespace Doctrine\Common;
23 * Base class for writing simple lexers, i.e. for creating small DSLs.
26 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
27 * @author Jonathan Wage <jonwage@gmail.com>
28 * @author Roman Borschel <roman@code-factory.org>
29 * @todo Rename: AbstractLexer
34 * @var array Array of scanned tokens
36 private $tokens = array();
39 * @var integer Current lexer position in input string
41 private $position = 0;
44 * @var integer Current peek of current lexer position
49 * @var array The next token in the input.
54 * @var array The last matched/seen token.
59 * Sets the input data to be tokenized.
61 * The Lexer is immediately reset and the new input tokenized.
62 * Any unprocessed tokens from any previous input are lost.
64 * @param string $input The input to be tokenized.
66 public function setInput($input)
68 $this->tokens = array();
76 public function reset()
78 $this->lookahead = null;
85 * Resets the peek pointer to 0.
87 public function resetPeek()
93 * Resets the lexer position on the input to the given position.
95 * @param integer $position Position to place the lexical scanner
97 public function resetPosition($position = 0)
99 $this->position = $position;
103 * Checks whether a given token matches the current lookahead.
105 * @param integer|string $token
108 public function isNextToken($token)
110 return null !== $this->lookahead && $this->lookahead['type'] === $token;
114 * Checks whether any of the given tokens matches the current lookahead
116 * @param array $tokens
119 public function isNextTokenAny(array $tokens)
121 return null !== $this->lookahead && in_array($this->lookahead['type'], $tokens, true);
125 * Moves to the next token in the input string.
127 * A token is an associative array containing three items:
128 * - 'value' : the string value of the token in the input string
129 * - 'type' : the type of the token (identifier, numeric, string, input
131 * - 'position' : the position of the token in the input string
133 * @return array|null the next token; null if there is no more tokens left
135 public function moveNext()
138 $this->token = $this->lookahead;
139 $this->lookahead = (isset($this->tokens[$this->position]))
140 ? $this->tokens[$this->position++] : null;
142 return $this->lookahead !== null;
146 * Tells the lexer to skip input tokens until it sees a token with the given value.
148 * @param string $type The token type to skip until.
150 public function skipUntil($type)
152 while ($this->lookahead !== null && $this->lookahead['type'] !== $type) {
158 * Checks if given value is identical to the given token
160 * @param mixed $value
161 * @param integer $token
164 public function isA($value, $token)
166 return $this->getType($value) === $token;
170 * Moves the lookahead token forward.
172 * @return array | null The next token or NULL if there are no more tokens ahead.
174 public function peek()
176 if (isset($this->tokens[$this->position + $this->peek])) {
177 return $this->tokens[$this->position + $this->peek++];
184 * Peeks at the next token, returns it and immediately resets the peek.
186 * @return array|null The next token or NULL if there are no more tokens ahead.
188 public function glimpse()
190 $peek = $this->peek();
196 * Scans the input string for tokens.
198 * @param string $input a query string
200 protected function scan($input)
204 if ( ! isset($regex)) {
205 $regex = '/(' . implode(')|(', $this->getCatchablePatterns()) . ')|'
206 . implode('|', $this->getNonCatchablePatterns()) . '/i';
209 $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE;
210 $matches = preg_split($regex, $input, -1, $flags);
212 foreach ($matches as $match) {
213 // Must remain before 'value' assignment since it can change content
214 $type = $this->getType($match[0]);
216 $this->tokens[] = array(
217 'value' => $match[0],
219 'position' => $match[1],
225 * Gets the literal for a given token.
227 * @param integer $token
230 public function getLiteral($token)
232 $className = get_class($this);
233 $reflClass = new \ReflectionClass($className);
234 $constants = $reflClass->getConstants();
236 foreach ($constants as $name => $value) {
237 if ($value === $token) {
238 return $className . '::' . $name;
246 * Lexical catchable patterns.
250 abstract protected function getCatchablePatterns();
253 * Lexical non-catchable patterns.
257 abstract protected function getNonCatchablePatterns();
260 * Retrieve token type. Also processes the token value if necessary.
262 * @param string $value
265 abstract protected function getType(&$value);