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\Lexer;
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>
30 abstract class AbstractLexer
33 * Lexer original input string.
40 * Array of scanned tokens.
42 * Each token is an associative array containing three items:
43 * - 'value' : the string value of the token in the input string
44 * - 'type' : the type of the token (identifier, numeric, string, input
46 * - 'position' : the position of the token in the input string
50 private $tokens = array();
53 * Current lexer position in input string.
57 private $position = 0;
60 * Current peek of current lexer position.
67 * The next token in the input.
74 * The last matched/seen token.
81 * Sets the input data to be tokenized.
83 * The Lexer is immediately reset and the new input tokenized.
84 * Any unprocessed tokens from any previous input are lost.
86 * @param string $input The input to be tokenized.
90 public function setInput($input)
92 $this->input = $input;
93 $this->tokens = array();
104 public function reset()
106 $this->lookahead = null;
113 * Resets the peek pointer to 0.
117 public function resetPeek()
123 * Resets the lexer position on the input to the given position.
125 * @param integer $position Position to place the lexical scanner.
129 public function resetPosition($position = 0)
131 $this->position = $position;
135 * Retrieve the original lexer's input until a given position.
137 * @param integer $position
141 public function getInputUntilPosition($position)
143 return substr($this->input, 0, $position);
147 * Checks whether a given token matches the current lookahead.
149 * @param integer|string $token
153 public function isNextToken($token)
155 return null !== $this->lookahead && $this->lookahead['type'] === $token;
159 * Checks whether any of the given tokens matches the current lookahead.
161 * @param array $tokens
165 public function isNextTokenAny(array $tokens)
167 return null !== $this->lookahead && in_array($this->lookahead['type'], $tokens, true);
171 * Moves to the next token in the input string.
175 public function moveNext()
178 $this->token = $this->lookahead;
179 $this->lookahead = (isset($this->tokens[$this->position]))
180 ? $this->tokens[$this->position++] : null;
182 return $this->lookahead !== null;
186 * Tells the lexer to skip input tokens until it sees a token with the given value.
188 * @param string $type The token type to skip until.
192 public function skipUntil($type)
194 while ($this->lookahead !== null && $this->lookahead['type'] !== $type) {
200 * Checks if given value is identical to the given token.
202 * @param mixed $value
203 * @param integer $token
207 public function isA($value, $token)
209 return $this->getType($value) === $token;
213 * Moves the lookahead token forward.
215 * @return array|null The next token or NULL if there are no more tokens ahead.
217 public function peek()
219 if (isset($this->tokens[$this->position + $this->peek])) {
220 return $this->tokens[$this->position + $this->peek++];
227 * Peeks at the next token, returns it and immediately resets the peek.
229 * @return array|null The next token or NULL if there are no more tokens ahead.
231 public function glimpse()
233 $peek = $this->peek();
239 * Scans the input string for tokens.
241 * @param string $input A query string.
245 protected function scan($input)
249 if ( ! isset($regex)) {
252 implode(')|(', $this->getCatchablePatterns()),
253 implode('|', $this->getNonCatchablePatterns()),
254 $this->getModifiers()
258 $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE;
259 $matches = preg_split($regex, $input, -1, $flags);
261 foreach ($matches as $match) {
262 // Must remain before 'value' assignment since it can change content
263 $type = $this->getType($match[0]);
265 $this->tokens[] = array(
266 'value' => $match[0],
268 'position' => $match[1],
274 * Gets the literal for a given token.
276 * @param integer $token
280 public function getLiteral($token)
282 $className = get_class($this);
283 $reflClass = new \ReflectionClass($className);
284 $constants = $reflClass->getConstants();
286 foreach ($constants as $name => $value) {
287 if ($value === $token) {
288 return $className . '::' . $name;
300 protected function getModifiers()
306 * Lexical catchable patterns.
310 abstract protected function getCatchablePatterns();
313 * Lexical non-catchable patterns.
317 abstract protected function getNonCatchablePatterns();
320 * Retrieve token type. Also processes the token value if necessary.
322 * @param string $value
326 abstract protected function getType(&$value);