|
0
|
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 |
|
|
|
37 |
/** |
|
|
38 |
* Constructor. |
|
|
39 |
* |
|
|
40 |
* @param integer $type The type of the token |
|
|
41 |
* @param string $value The token value |
|
|
42 |
* @param integer $lineno The line position in the source |
|
|
43 |
*/ |
|
|
44 |
public function __construct($type, $value, $lineno) |
|
|
45 |
{ |
|
|
46 |
$this->type = $type; |
|
|
47 |
$this->value = $value; |
|
|
48 |
$this->lineno = $lineno; |
|
|
49 |
} |
|
|
50 |
|
|
|
51 |
/** |
|
|
52 |
* Returns a string representation of the token. |
|
|
53 |
* |
|
|
54 |
* @return string A string representation of the token |
|
|
55 |
*/ |
|
|
56 |
public function __toString() |
|
|
57 |
{ |
|
|
58 |
return sprintf('%s(%s)', self::typeToString($this->type, true, $this->lineno), $this->value); |
|
|
59 |
} |
|
|
60 |
|
|
|
61 |
/** |
|
|
62 |
* Tests the current token for a type and/or a value. |
|
|
63 |
* |
|
|
64 |
* Parameters may be: |
|
|
65 |
* * just type |
|
|
66 |
* * type and value (or array of possible values) |
|
|
67 |
* * just value (or array of possible values) (NAME_TYPE is used as type) |
|
|
68 |
* |
|
|
69 |
* @param array|integer $type The type to test |
|
|
70 |
* @param array|string|null $values The token value |
|
|
71 |
* |
|
|
72 |
* @return Boolean |
|
|
73 |
*/ |
|
|
74 |
public function test($type, $values = null) |
|
|
75 |
{ |
|
|
76 |
if (null === $values && !is_int($type)) { |
|
|
77 |
$values = $type; |
|
|
78 |
$type = self::NAME_TYPE; |
|
|
79 |
} |
|
|
80 |
|
|
|
81 |
return ($this->type === $type) && ( |
|
|
82 |
null === $values || |
|
|
83 |
(is_array($values) && in_array($this->value, $values)) || |
|
|
84 |
$this->value == $values |
|
|
85 |
); |
|
|
86 |
} |
|
|
87 |
|
|
|
88 |
/** |
|
|
89 |
* Gets the line. |
|
|
90 |
* |
|
|
91 |
* @return integer The source line |
|
|
92 |
*/ |
|
|
93 |
public function getLine() |
|
|
94 |
{ |
|
|
95 |
return $this->lineno; |
|
|
96 |
} |
|
|
97 |
|
|
|
98 |
/** |
|
|
99 |
* Gets the token type. |
|
|
100 |
* |
|
|
101 |
* @return integer The token type |
|
|
102 |
*/ |
|
|
103 |
public function getType() |
|
|
104 |
{ |
|
|
105 |
return $this->type; |
|
|
106 |
} |
|
|
107 |
|
|
|
108 |
/** |
|
|
109 |
* Gets the token value. |
|
|
110 |
* |
|
|
111 |
* @return string The token value |
|
|
112 |
*/ |
|
|
113 |
public function getValue() |
|
|
114 |
{ |
|
|
115 |
return $this->value; |
|
|
116 |
} |
|
|
117 |
|
|
|
118 |
/** |
|
|
119 |
* Returns the constant representation (internal) of a given type. |
|
|
120 |
* |
|
|
121 |
* @param integer $type The type as an integer |
|
|
122 |
* @param Boolean $short Whether to return a short representation or not |
|
|
123 |
* |
|
|
124 |
* @return string The string representation |
|
|
125 |
*/ |
|
|
126 |
static public function typeToString($type, $short = false, $line = -1) |
|
|
127 |
{ |
|
|
128 |
switch ($type) { |
|
|
129 |
case self::EOF_TYPE: |
|
|
130 |
$name = 'EOF_TYPE'; |
|
|
131 |
break; |
|
|
132 |
case self::TEXT_TYPE: |
|
|
133 |
$name = 'TEXT_TYPE'; |
|
|
134 |
break; |
|
|
135 |
case self::BLOCK_START_TYPE: |
|
|
136 |
$name = 'BLOCK_START_TYPE'; |
|
|
137 |
break; |
|
|
138 |
case self::VAR_START_TYPE: |
|
|
139 |
$name = 'VAR_START_TYPE'; |
|
|
140 |
break; |
|
|
141 |
case self::BLOCK_END_TYPE: |
|
|
142 |
$name = 'BLOCK_END_TYPE'; |
|
|
143 |
break; |
|
|
144 |
case self::VAR_END_TYPE: |
|
|
145 |
$name = 'VAR_END_TYPE'; |
|
|
146 |
break; |
|
|
147 |
case self::NAME_TYPE: |
|
|
148 |
$name = 'NAME_TYPE'; |
|
|
149 |
break; |
|
|
150 |
case self::NUMBER_TYPE: |
|
|
151 |
$name = 'NUMBER_TYPE'; |
|
|
152 |
break; |
|
|
153 |
case self::STRING_TYPE: |
|
|
154 |
$name = 'STRING_TYPE'; |
|
|
155 |
break; |
|
|
156 |
case self::OPERATOR_TYPE: |
|
|
157 |
$name = 'OPERATOR_TYPE'; |
|
|
158 |
break; |
|
|
159 |
case self::PUNCTUATION_TYPE: |
|
|
160 |
$name = 'PUNCTUATION_TYPE'; |
|
|
161 |
break; |
|
|
162 |
default: |
|
|
163 |
throw new Twig_Error_Syntax(sprintf('Token of type "%s" does not exist.', $type), $line); |
|
|
164 |
} |
|
|
165 |
|
|
|
166 |
return $short ? $name : 'Twig_Token::'.$name; |
|
|
167 |
} |
|
|
168 |
|
|
|
169 |
/** |
|
|
170 |
* Returns the english representation of a given type. |
|
|
171 |
* |
|
|
172 |
* @param integer $type The type as an integer |
|
|
173 |
* @param Boolean $short Whether to return a short representation or not |
|
|
174 |
* |
|
|
175 |
* @return string The string representation |
|
|
176 |
*/ |
|
|
177 |
static public function typeToEnglish($type, $line = -1) |
|
|
178 |
{ |
|
|
179 |
switch ($type) { |
|
|
180 |
case self::EOF_TYPE: |
|
|
181 |
return 'end of template'; |
|
|
182 |
case self::TEXT_TYPE: |
|
|
183 |
return 'text'; |
|
|
184 |
case self::BLOCK_START_TYPE: |
|
|
185 |
return 'begin of statement block'; |
|
|
186 |
case self::VAR_START_TYPE: |
|
|
187 |
return 'begin of print statement'; |
|
|
188 |
case self::BLOCK_END_TYPE: |
|
|
189 |
return 'end of statement block'; |
|
|
190 |
case self::VAR_END_TYPE: |
|
|
191 |
return 'end of print statement'; |
|
|
192 |
case self::NAME_TYPE: |
|
|
193 |
return 'name'; |
|
|
194 |
case self::NUMBER_TYPE: |
|
|
195 |
return 'number'; |
|
|
196 |
case self::STRING_TYPE: |
|
|
197 |
return 'string'; |
|
|
198 |
case self::OPERATOR_TYPE: |
|
|
199 |
return 'operator'; |
|
|
200 |
case self::PUNCTUATION_TYPE: |
|
|
201 |
return 'punctuation'; |
|
|
202 |
default: |
|
|
203 |
throw new Twig_Error_Syntax(sprintf('Token of type "%s" does not exist.', $type), $line); |
|
|
204 |
} |
|
|
205 |
} |
|
|
206 |
} |