db backup prior to drupal security update
[yaffs-website] / vendor / symfony / expression-language / TokenStream.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\ExpressionLanguage;
13
14 /**
15  * Represents a token stream.
16  *
17  * @author Fabien Potencier <fabien@symfony.com>
18  */
19 class TokenStream
20 {
21     public $current;
22
23     private $tokens;
24     private $position = 0;
25     private $expression;
26
27     /**
28      * Constructor.
29      *
30      * @param array  $tokens     An array of tokens
31      * @param string $expression
32      */
33     public function __construct(array $tokens, $expression = '')
34     {
35         $this->tokens = $tokens;
36         $this->current = $tokens[0];
37         $this->expression = $expression;
38     }
39
40     /**
41      * Returns a string representation of the token stream.
42      *
43      * @return string
44      */
45     public function __toString()
46     {
47         return implode("\n", $this->tokens);
48     }
49
50     /**
51      * Sets the pointer to the next token and returns the old one.
52      */
53     public function next()
54     {
55         if (!isset($this->tokens[$this->position])) {
56             throw new SyntaxError('Unexpected end of expression', $this->current->cursor, $this->expression);
57         }
58
59         ++$this->position;
60
61         $this->current = $this->tokens[$this->position];
62     }
63
64     /**
65      * Tests a token.
66      *
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
70      */
71     public function expect($type, $value = null, $message = null)
72     {
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);
76         }
77         $this->next();
78     }
79
80     /**
81      * Checks if end of stream was reached.
82      *
83      * @return bool
84      */
85     public function isEOF()
86     {
87         return $this->current->type === Token::EOF_TYPE;
88     }
89
90     /**
91      * @internal
92      *
93      * @return string
94      */
95     public function getExpression()
96     {
97         return $this->expression;
98     }
99 }