4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\ExpressionLanguage;
15 * Represents a token stream.
17 * @author Fabien Potencier <fabien@symfony.com>
24 private $position = 0;
30 * @param array $tokens An array of tokens
31 * @param string $expression
33 public function __construct(array $tokens, $expression = '')
35 $this->tokens = $tokens;
36 $this->current = $tokens[0];
37 $this->expression = $expression;
41 * Returns a string representation of the token stream.
45 public function __toString()
47 return implode("\n", $this->tokens);
51 * Sets the pointer to the next token and returns the old one.
53 public function next()
55 if (!isset($this->tokens[$this->position])) {
56 throw new SyntaxError('Unexpected end of expression', $this->current->cursor, $this->expression);
61 $this->current = $this->tokens[$this->position];
67 * @param array|int $type The type to test
68 * @param string|null $value The token value
69 * @param string|null $message The syntax error message
71 public function expect($type, $value = null, $message = null)
73 $token = $this->current;
74 if (!$token->test($type, $value)) {
75 throw new SyntaxError(sprintf('%sUnexpected token "%s" of value "%s" ("%s" expected%s)', $message ? $message.'. ' : '', $token->type, $token->value, $type, $value ? sprintf(' with value "%s"', $value) : ''), $token->cursor, $this->expression);
81 * Checks if end of stream was reached.
85 public function isEOF()
87 return $this->current->type === Token::EOF_TYPE;
95 public function getExpression()
97 return $this->expression;