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@symfony.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', -1, $this->filename);
|
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
|
$this->filename
|
78
|
);
|
79
|
}
|
80
|
$this->next();
|
81
|
|
82
|
return $token;
|
83
|
}
|
84
|
|
85
|
/**
|
86
|
* Looks at the next token.
|
87
|
*
|
88
|
* @param integer $number
|
89
|
*
|
90
|
* @return Twig_Token
|
91
|
*/
|
92
|
public function look($number = 1)
|
93
|
{
|
94
|
if (!isset($this->tokens[$this->current + $number])) {
|
95
|
throw new Twig_Error_Syntax('Unexpected end of template', -1, $this->filename);
|
96
|
}
|
97
|
|
98
|
return $this->tokens[$this->current + $number];
|
99
|
}
|
100
|
|
101
|
/**
|
102
|
* Tests the current token
|
103
|
*
|
104
|
* @return bool
|
105
|
*/
|
106
|
public function test($primary, $secondary = null)
|
107
|
{
|
108
|
return $this->tokens[$this->current]->test($primary, $secondary);
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* Checks if end of stream was reached
|
113
|
*
|
114
|
* @return bool
|
115
|
*/
|
116
|
public function isEOF()
|
117
|
{
|
118
|
return $this->tokens[$this->current]->getType() === Twig_Token::EOF_TYPE;
|
119
|
}
|
120
|
|
121
|
/**
|
122
|
* Gets the current token
|
123
|
*
|
124
|
* @return Twig_Token
|
125
|
*/
|
126
|
public function getCurrent()
|
127
|
{
|
128
|
return $this->tokens[$this->current];
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* Gets the filename associated with this stream
|
133
|
*
|
134
|
* @return string
|
135
|
*/
|
136
|
public function getFilename()
|
137
|
{
|
138
|
return $this->filename;
|
139
|
}
|
140
|
}
|