1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Hoa |
7
|
|
|
* |
8
|
|
|
* |
9
|
|
|
* |
10
|
|
|
* |
11
|
|
|
* BSD 3-Clause License |
12
|
|
|
* |
13
|
|
|
* Copyright © 2007-2017, Hoa community. All rights reserved. |
14
|
|
|
* |
15
|
|
|
* Redistribution and use in source and binary forms, with or without |
16
|
|
|
* modification, are permitted provided that the following conditions are met: |
17
|
|
|
* |
18
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this |
19
|
|
|
* list of conditions and the following disclaimer. |
20
|
|
|
* |
21
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice, |
22
|
|
|
* this list of conditions and the following disclaimer in the documentation |
23
|
|
|
* and/or other materials provided with the distribution. |
24
|
|
|
* |
25
|
|
|
* 3. Neither the name of the copyright holder nor the names of its |
26
|
|
|
* contributors may be used to endorse or promote products derived from |
27
|
|
|
* this software without specific prior written permission. |
28
|
|
|
* |
29
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
30
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
31
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
32
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
33
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
34
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
35
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
36
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
37
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
38
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
39
|
|
|
*/ |
40
|
|
|
|
41
|
|
|
namespace JMS\Serializer\Type\Compiler\Llk\Rule; |
42
|
|
|
|
43
|
|
|
use JMS\Serializer\Type\Compiler\Llk\Parser; |
44
|
|
|
use JMS\Serializer\Type\Compiler\Llk\Rule; |
45
|
|
|
use JMS\Serializer\Type\Compiler\Llk\TreeNode; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Class \JMS\Serializer\Type\Compiler\Llk\Rule\Token. |
49
|
|
|
* |
50
|
|
|
* The token rule. |
51
|
|
|
*/ |
52
|
|
|
final class Token extends Rule |
53
|
|
|
{ |
54
|
|
|
/** |
55
|
|
|
* LL(k) compiler of hoa://Library/Regex/Grammar.pp. |
56
|
|
|
* |
57
|
|
|
* @var Parser|null |
58
|
|
|
*/ |
59
|
|
|
protected static $_regexCompiler = null; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Token name. |
63
|
|
|
* |
64
|
|
|
* @var string|null |
65
|
|
|
*/ |
66
|
|
|
protected $_tokenName = null; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Namespace. |
70
|
|
|
* |
71
|
|
|
* @var string|null |
72
|
|
|
*/ |
73
|
|
|
protected $_namespace = null; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Token representation. |
77
|
|
|
* |
78
|
|
|
* @var string|null |
79
|
|
|
*/ |
80
|
|
|
protected $_regex = null; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* AST of the regex. |
84
|
|
|
* |
85
|
|
|
* @var TreeNode |
86
|
|
|
*/ |
87
|
|
|
protected $_ast; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Token value. |
91
|
|
|
* |
92
|
|
|
* @var string|null |
93
|
|
|
*/ |
94
|
|
|
protected $_value = null; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Whether the token is kept or not in the AST. |
98
|
|
|
* |
99
|
|
|
* @var bool |
100
|
|
|
*/ |
101
|
|
|
protected $_kept = false; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Unification index. |
105
|
|
|
* |
106
|
|
|
* @var int |
107
|
|
|
*/ |
108
|
|
|
protected $_unification = -1; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Token offset. |
112
|
|
|
* |
113
|
|
|
* @var int |
114
|
|
|
*/ |
115
|
|
|
protected $_offset = 0; |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string|int $name Name. |
119
|
|
|
* @param string $tokenName Token name. |
120
|
|
|
* @param string|null $nodeId Node ID. |
121
|
|
|
* @param int $unification Unification index. |
122
|
|
|
* @param bool $kept Whether the token is kept or not in the AST. |
123
|
|
|
*/ |
124
|
|
|
public function __construct( |
125
|
|
|
$name, |
126
|
|
|
$tokenName, |
127
|
|
|
$nodeId, |
128
|
|
|
$unification, |
129
|
|
|
$kept = false |
130
|
|
|
) { |
131
|
|
|
parent::__construct($name, null, $nodeId); |
132
|
|
|
|
133
|
|
|
$this->_tokenName = $tokenName; |
134
|
|
|
$this->_unification = $unification; |
135
|
|
|
$this->setKept($kept); |
136
|
|
|
|
137
|
|
|
return; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get token name. |
142
|
|
|
*/ |
143
|
|
|
public function getTokenName(): ?string |
144
|
|
|
{ |
145
|
|
|
return $this->_tokenName; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Set token namespace. |
150
|
|
|
* |
151
|
|
|
* @param string $namespace Namespace. |
152
|
|
|
*/ |
153
|
|
|
public function setNamespace(string $namespace): ?string |
154
|
|
|
{ |
155
|
|
|
$old = $this->_namespace; |
156
|
|
|
$this->_namespace = $namespace; |
157
|
|
|
|
158
|
|
|
return $old; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get token namespace. |
163
|
|
|
*/ |
164
|
|
|
public function getNamespace(): ?string |
165
|
|
|
{ |
166
|
|
|
return $this->_namespace; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Set representation. |
171
|
|
|
* |
172
|
|
|
* @param string $regex Representation. |
173
|
|
|
*/ |
174
|
|
|
public function setRepresentation(string $regex): ?string |
175
|
|
|
{ |
176
|
|
|
$old = $this->_regex; |
177
|
|
|
$this->_regex = $regex; |
178
|
|
|
|
179
|
|
|
return $old; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get token representation. |
184
|
|
|
*/ |
185
|
|
|
public function getRepresentation(): ?string |
186
|
|
|
{ |
187
|
|
|
return $this->_regex; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Set token value. |
192
|
|
|
* |
193
|
|
|
* @param ?string $value Value. |
194
|
|
|
*/ |
195
|
|
|
public function setValue(string $value): ?string |
196
|
|
|
{ |
197
|
|
|
$old = $this->_value; |
198
|
|
|
$this->_value = $value; |
199
|
|
|
|
200
|
|
|
return $old; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get token value. |
205
|
|
|
*/ |
206
|
|
|
public function getValue(): string |
207
|
|
|
{ |
208
|
|
|
return $this->_value; |
|
|
|
|
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Set token offset. |
213
|
|
|
* |
214
|
|
|
* @param int $offset Offset. |
215
|
|
|
*/ |
216
|
|
|
public function setOffset(int $offset): int |
217
|
|
|
{ |
218
|
|
|
$old = $this->_offset; |
219
|
|
|
$this->_offset = $offset; |
220
|
|
|
|
221
|
|
|
return $old; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Get token offset. |
226
|
|
|
*/ |
227
|
|
|
public function getOffset(): int |
228
|
|
|
{ |
229
|
|
|
return $this->_offset; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Set whether the token is kept or not in the AST. |
234
|
|
|
* |
235
|
|
|
* @param bool $kept Kept. |
236
|
|
|
*/ |
237
|
|
|
public function setKept(bool $kept): bool |
238
|
|
|
{ |
239
|
|
|
$old = $this->_kept; |
240
|
|
|
$this->_kept = $kept; |
241
|
|
|
|
242
|
|
|
return $old; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Check whether the token is kept in the AST or not. |
247
|
|
|
*/ |
248
|
|
|
public function isKept(): bool |
249
|
|
|
{ |
250
|
|
|
return $this->_kept; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Get unification index. |
255
|
|
|
*/ |
256
|
|
|
public function getUnificationIndex(): int |
257
|
|
|
{ |
258
|
|
|
return $this->_unification; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|