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