[345] | 1 | <?php |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * This file is part of Twig. |
---|
| 5 | * |
---|
| 6 | * (c) 2009 Fabien Potencier |
---|
| 7 | * (c) 2009 Armin Ronacher |
---|
| 8 | * |
---|
| 9 | * For the full copyright and license information, please view the LICENSE |
---|
| 10 | * file that was distributed with this source code. |
---|
| 11 | */ |
---|
| 12 | |
---|
| 13 | /** |
---|
| 14 | * Represents a token stream. |
---|
| 15 | * |
---|
| 16 | * @package twig |
---|
| 17 | * @author Fabien Potencier <fabien.potencier@symfony-project.com> |
---|
| 18 | */ |
---|
| 19 | class Twig_TokenStream |
---|
| 20 | { |
---|
| 21 | protected $tokens; |
---|
| 22 | protected $current; |
---|
| 23 | protected $filename; |
---|
| 24 | |
---|
| 25 | /** |
---|
| 26 | * Constructor. |
---|
| 27 | * |
---|
| 28 | * @param array $tokens An array of tokens |
---|
| 29 | * @param string $filename The name of the filename which tokens are associated with |
---|
| 30 | */ |
---|
| 31 | public function __construct(array $tokens, $filename = null) |
---|
| 32 | { |
---|
| 33 | $this->tokens = $tokens; |
---|
| 34 | $this->current = 0; |
---|
| 35 | $this->filename = $filename; |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | * Returns a string representation of the token stream. |
---|
| 40 | * |
---|
| 41 | * @return string |
---|
| 42 | */ |
---|
| 43 | public function __toString() |
---|
| 44 | { |
---|
| 45 | return implode("\n", $this->tokens); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | * Sets the pointer to the next token and returns the old one. |
---|
| 50 | * |
---|
| 51 | * @return Twig_Token |
---|
| 52 | */ |
---|
| 53 | public function next() |
---|
| 54 | { |
---|
| 55 | if (!isset($this->tokens[++$this->current])) { |
---|
| 56 | throw new Twig_Error_Syntax('Unexpected end of template'); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | return $this->tokens[$this->current - 1]; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | /** |
---|
| 63 | * Tests a token and returns it or throws a syntax error. |
---|
| 64 | * |
---|
| 65 | * @return Twig_Token |
---|
| 66 | */ |
---|
| 67 | public function expect($type, $value = null, $message = null) |
---|
| 68 | { |
---|
| 69 | $token = $this->tokens[$this->current]; |
---|
| 70 | if (!$token->test($type, $value)) { |
---|
| 71 | $line = $token->getLine(); |
---|
| 72 | throw new Twig_Error_Syntax(sprintf('%sUnexpected token "%s" of value "%s" ("%s" expected%s)', |
---|
| 73 | $message ? $message.'. ' : '', |
---|
| 74 | Twig_Token::typeToEnglish($token->getType(), $line), $token->getValue(), |
---|
| 75 | Twig_Token::typeToEnglish($type, $line), $value ? sprintf(' with value "%s"', $value) : ''), |
---|
| 76 | $line |
---|
| 77 | ); |
---|
| 78 | } |
---|
| 79 | $this->next(); |
---|
| 80 | |
---|
| 81 | return $token; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | /** |
---|
| 85 | * Looks at the next token. |
---|
| 86 | * |
---|
| 87 | * @param integer $number |
---|
| 88 | * |
---|
| 89 | * @return Twig_Token |
---|
| 90 | */ |
---|
| 91 | public function look($number = 1) |
---|
| 92 | { |
---|
| 93 | if (!isset($this->tokens[$this->current + $number])) { |
---|
| 94 | throw new Twig_Error_Syntax('Unexpected end of template'); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | return $this->tokens[$this->current + $number]; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | /** |
---|
| 101 | * Tests the current token |
---|
| 102 | * |
---|
| 103 | * @return bool |
---|
| 104 | */ |
---|
| 105 | public function test($primary, $secondary = null) |
---|
| 106 | { |
---|
| 107 | return $this->tokens[$this->current]->test($primary, $secondary); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | /** |
---|
| 111 | * Checks if end of stream was reached |
---|
| 112 | * |
---|
| 113 | * @return bool |
---|
| 114 | */ |
---|
| 115 | public function isEOF() |
---|
| 116 | { |
---|
| 117 | return $this->tokens[$this->current]->getType() === Twig_Token::EOF_TYPE; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | /** |
---|
| 121 | * Gets the current token |
---|
| 122 | * |
---|
| 123 | * @return Twig_Token |
---|
| 124 | */ |
---|
| 125 | public function getCurrent() |
---|
| 126 | { |
---|
| 127 | return $this->tokens[$this->current]; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /** |
---|
| 131 | * Gets the filename associated with this stream |
---|
| 132 | * |
---|
| 133 | * @return string |
---|
| 134 | */ |
---|
| 135 | public function getFilename() |
---|
| 136 | { |
---|
| 137 | return $this->filename; |
---|
| 138 | } |
---|
| 139 | } |
---|