-
Notifications
You must be signed in to change notification settings - Fork 14
/
Token.php
198 lines (171 loc) · 3.05 KB
/
Token.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
/*
* This file is part of Spoon Library.
*
* (c) Davy Hellemans <[email protected]>
*
* For the full copyright and license information, please view the license
* file that was distributed with this source code.
*/
namespace Spoon\Template;
/**
* This class represents a single token with some extra information such as its line number and
* type.
*
* @author Davy Hellemans <[email protected]>
*/
class Token
{
/**
* List of token types.
*
* @var int
*/
const EOF = -1;
const TEXT = 0;
const BLOCK_START = 1;
const BLOCK_END = 2;
const VAR_START = 3;
const VAR_END = 4;
const NAME = 5;
const NUMBER = 6;
const STRING = 7;
const OPERATOR = 8;
const PUNCTUATION = 9;
/**
* Line number this token starts on.
*
* @var int
*/
protected $line;
/**
* @var int
*/
protected $type;
/**
* Contains the actual value.
*
* @var mixed
*/
protected $value;
/**
* @param int $type
* @param mixed $value
* @param int $line
*/
public function __construct($type, $value, $line)
{
$this->type = (int) $type;
$this->value = $value;
$this->line = (int) $line;
}
/**
* Returns a more readable token representation.
*
* @return string
*/
public function __toString()
{
$string = '[line]: ' . $this->line . "\n";
$string .= '[type]: ' . $this->typeToString($this->type) . "\n";
$string .= '[value]: ' . $this->value . "\n";
return $string;
}
/**
* Returns the line number.
*
* @return int
*/
public function getLine()
{
return $this->line;
}
/**
* Returns the type.
*
* @return int
*/
public function getType()
{
return $this->type;
}
/**
* Returns the value.
*
* @return mixed This method can return either a string or an integer.
*/
public function getValue()
{
return $this->value;
}
/**
* Test some assertions about this token.
*
* @param string $type
* @param array[optional] $values
* @return bool
*/
public function test($type, $values = null)
{
if($this->type === $type)
{
if(is_array($values))
{
return in_array($this->value, $values);
}
elseif($values !== null)
{
return ($this->value === $values);
}
return true;
}
return false;
}
/**
* Returns the type as a string representation.
*
* @param int $type
* @return string
*/
public static function typeToString($type)
{
switch($type)
{
case self::EOF:
return 'EOF';
break;
case self::TEXT:
return 'TEXT';
break;
case self::BLOCK_START:
return 'BLOCK_START';
break;
case self::BLOCK_END:
return 'BLOCK_END';
break;
case self::VAR_START:
return 'VAR_START';
break;
case self::VAR_END:
return 'VAR_END';
break;
case self::NAME:
return 'NAME';
break;
case self::NUMBER:
return 'NUMBER';
break;
case self::STRING:
return 'STRING';
break;
case self::OPERATOR:
return 'OPERATOR';
break;
case self::PUNCTUATION:
return 'PUNCTUATION';
break;
default:
throw new Exception(sprintf('There is no token type "%s"', $type));
}
}
}