Project

General

Profile

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
    public function injectTokens(array $tokens)
49
    {
50
        $this->tokens = array_merge(array_slice($this->tokens, 0, $this->current), $tokens, array_slice($this->tokens, $this->current));
51
    }
52

    
53
    /**
54
     * Sets the pointer to the next token and returns the old one.
55
     *
56
     * @return Twig_Token
57
     */
58
    public function next()
59
    {
60
        if (!isset($this->tokens[++$this->current])) {
61
            throw new Twig_Error_Syntax('Unexpected end of template', $this->token[$this->current - 1]->getLine(), $this->filename);
62
        }
63

    
64
        return $this->tokens[$this->current - 1];
65
    }
66

    
67
    /**
68
     * Tests a token and returns it or throws a syntax error.
69
     *
70
     * @return Twig_Token
71
     */
72
    public function expect($type, $value = null, $message = null)
73
    {
74
        $token = $this->tokens[$this->current];
75
        if (!$token->test($type, $value)) {
76
            $line = $token->getLine();
77
            throw new Twig_Error_Syntax(sprintf('%sUnexpected token "%s" of value "%s" ("%s" expected%s)',
78
                $message ? $message.'. ' : '',
79
                Twig_Token::typeToEnglish($token->getType(), $line), $token->getValue(),
80
                Twig_Token::typeToEnglish($type, $line), $value ? sprintf(' with value "%s"', $value) : ''),
81
                $line,
82
                $this->filename
83
            );
84
        }
85
        $this->next();
86

    
87
        return $token;
88
    }
89

    
90
    /**
91
     * Looks at the next token.
92
     *
93
     * @param integer $number
94
     *
95
     * @return Twig_Token
96
     */
97
    public function look($number = 1)
98
    {
99
        if (!isset($this->tokens[$this->current + $number])) {
100
            throw new Twig_Error_Syntax('Unexpected end of template', $this->token[$this->current + $number - 1]->getLine(), $this->filename);
101
        }
102

    
103
        return $this->tokens[$this->current + $number];
104
    }
105

    
106
    /**
107
     * Tests the current token
108
     *
109
     * @return bool
110
     */
111
    public function test($primary, $secondary = null)
112
    {
113
        return $this->tokens[$this->current]->test($primary, $secondary);
114
    }
115

    
116
    /**
117
     * Checks if end of stream was reached
118
     *
119
     * @return bool
120
     */
121
    public function isEOF()
122
    {
123
        return $this->tokens[$this->current]->getType() === Twig_Token::EOF_TYPE;
124
    }
125

    
126
    /**
127
     * Gets the current token
128
     *
129
     * @return Twig_Token
130
     */
131
    public function getCurrent()
132
    {
133
        return $this->tokens[$this->current];
134
    }
135

    
136
    /**
137
     * Gets the filename associated with this stream
138
     *
139
     * @return string
140
     */
141
    public function getFilename()
142
    {
143
        return $this->filename;
144
    }
145
}
(34-34/34)