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.
15
 *
16
 * @package twig
17
 * @author  Fabien Potencier <fabien@symfony.com>
18
 */
19
class Twig_Token
20
{
21
    protected $value;
22
    protected $type;
23
    protected $lineno;
24

    
25
    const EOF_TYPE                  = -1;
26
    const TEXT_TYPE                 = 0;
27
    const BLOCK_START_TYPE          = 1;
28
    const VAR_START_TYPE            = 2;
29
    const BLOCK_END_TYPE            = 3;
30
    const VAR_END_TYPE              = 4;
31
    const NAME_TYPE                 = 5;
32
    const NUMBER_TYPE               = 6;
33
    const STRING_TYPE               = 7;
34
    const OPERATOR_TYPE             = 8;
35
    const PUNCTUATION_TYPE          = 9;
36
    const INTERPOLATION_START_TYPE  = 10;
37
    const INTERPOLATION_END_TYPE    = 11;
38

    
39
    /**
40
     * Constructor.
41
     *
42
     * @param integer $type   The type of the token
43
     * @param string  $value  The token value
44
     * @param integer $lineno The line position in the source
45
     */
46
    public function __construct($type, $value, $lineno)
47
    {
48
        $this->type   = $type;
49
        $this->value  = $value;
50
        $this->lineno = $lineno;
51
    }
52

    
53
    /**
54
     * Returns a string representation of the token.
55
     *
56
     * @return string A string representation of the token
57
     */
58
    public function __toString()
59
    {
60
        return sprintf('%s(%s)', self::typeToString($this->type, true, $this->lineno), $this->value);
61
    }
62

    
63
    /**
64
     * Tests the current token for a type and/or a value.
65
     *
66
     * Parameters may be:
67
     * * just type
68
     * * type and value (or array of possible values)
69
     * * just value (or array of possible values) (NAME_TYPE is used as type)
70
     *
71
     * @param array|integer     $type   The type to test
72
     * @param array|string|null $values The token value
73
     *
74
     * @return Boolean
75
     */
76
    public function test($type, $values = null)
77
    {
78
        if (null === $values && !is_int($type)) {
79
            $values = $type;
80
            $type = self::NAME_TYPE;
81
        }
82

    
83
        return ($this->type === $type) && (
84
            null === $values ||
85
            (is_array($values) && in_array($this->value, $values)) ||
86
            $this->value == $values
87
        );
88
    }
89

    
90
    /**
91
     * Gets the line.
92
     *
93
     * @return integer The source line
94
     */
95
    public function getLine()
96
    {
97
        return $this->lineno;
98
    }
99

    
100
    /**
101
     * Gets the token type.
102
     *
103
     * @return integer The token type
104
     */
105
    public function getType()
106
    {
107
        return $this->type;
108
    }
109

    
110
    /**
111
     * Gets the token value.
112
     *
113
     * @return string The token value
114
     */
115
    public function getValue()
116
    {
117
        return $this->value;
118
    }
119

    
120
    /**
121
     * Returns the constant representation (internal) of a given type.
122
     *
123
     * @param integer $type  The type as an integer
124
     * @param Boolean $short Whether to return a short representation or not
125
     * @param integer $line  The code line
126
     *
127
     * @return string The string representation
128
     */
129
    static public function typeToString($type, $short = false, $line = -1)
130
    {
131
        switch ($type) {
132
            case self::EOF_TYPE:
133
                $name = 'EOF_TYPE';
134
                break;
135
            case self::TEXT_TYPE:
136
                $name = 'TEXT_TYPE';
137
                break;
138
            case self::BLOCK_START_TYPE:
139
                $name = 'BLOCK_START_TYPE';
140
                break;
141
            case self::VAR_START_TYPE:
142
                $name = 'VAR_START_TYPE';
143
                break;
144
            case self::BLOCK_END_TYPE:
145
                $name = 'BLOCK_END_TYPE';
146
                break;
147
            case self::VAR_END_TYPE:
148
                $name = 'VAR_END_TYPE';
149
                break;
150
            case self::NAME_TYPE:
151
                $name = 'NAME_TYPE';
152
                break;
153
            case self::NUMBER_TYPE:
154
                $name = 'NUMBER_TYPE';
155
                break;
156
            case self::STRING_TYPE:
157
                $name = 'STRING_TYPE';
158
                break;
159
            case self::OPERATOR_TYPE:
160
                $name = 'OPERATOR_TYPE';
161
                break;
162
            case self::PUNCTUATION_TYPE:
163
                $name = 'PUNCTUATION_TYPE';
164
                break;
165
            case self::INTERPOLATION_START_TYPE:
166
                $name = 'INTERPOLATION_START_TYPE';
167
                break;
168
            case self::INTERPOLATION_END_TYPE:
169
                $name = 'INTERPOLATION_END_TYPE';
170
                break;
171
            default:
172
                throw new Twig_Error_Syntax(sprintf('Token of type "%s" does not exist.', $type), $line);
173
        }
174

    
175
        return $short ? $name : 'Twig_Token::'.$name;
176
    }
177

    
178
    /**
179
     * Returns the english representation of a given type.
180
     *
181
     * @param integer $type The type as an integer
182
     * @param integer $line The code line
183
     *
184
     * @return string The string representation
185
     */
186
    static public function typeToEnglish($type, $line = -1)
187
    {
188
        switch ($type) {
189
            case self::EOF_TYPE:
190
                return 'end of template';
191
            case self::TEXT_TYPE:
192
                return 'text';
193
            case self::BLOCK_START_TYPE:
194
                return 'begin of statement block';
195
            case self::VAR_START_TYPE:
196
                return 'begin of print statement';
197
            case self::BLOCK_END_TYPE:
198
                return 'end of statement block';
199
            case self::VAR_END_TYPE:
200
                return 'end of print statement';
201
            case self::NAME_TYPE:
202
                return 'name';
203
            case self::NUMBER_TYPE:
204
                return 'number';
205
            case self::STRING_TYPE:
206
                return 'string';
207
            case self::OPERATOR_TYPE:
208
                return 'operator';
209
            case self::PUNCTUATION_TYPE:
210
                return 'punctuation';
211
            case self::INTERPOLATION_START_TYPE:
212
                return 'begin of string interpolation';
213
            case self::INTERPOLATION_END_TYPE:
214
                return 'end of string interpolation';
215
            default:
216
                throw new Twig_Error_Syntax(sprintf('Token of type "%s" does not exist.', $type), $line);
217
        }
218
    }
219
}
(28-28/33)