1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT |
7
|
|
|
* @link https://github.com/nicoSWD |
8
|
|
|
* @author Nicolas Oelgart <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
namespace nicoSWD\Rule\TokenStream\Token; |
11
|
|
|
|
12
|
|
|
use nicoSWD\Rule\Tokenizer\TokenStack; |
13
|
|
|
use nicoSWD\Rule\TokenStream\TokenStream; |
14
|
|
|
|
15
|
|
|
abstract class BaseToken |
16
|
|
|
{ |
17
|
|
|
/** @var mixed */ |
18
|
|
|
protected $value; |
19
|
|
|
/** @var int */ |
20
|
|
|
protected $offset = 0; |
21
|
|
|
/** @var TokenStack */ |
22
|
|
|
protected $stack; |
23
|
|
|
/** @var int */ |
24
|
|
|
protected $position = null; |
25
|
|
|
/** @var int */ |
26
|
|
|
protected $line = null; |
27
|
|
|
|
28
|
|
|
abstract public function getType(): int; |
29
|
|
|
|
30
|
230 |
|
public function __construct($value, int $offset = 0, TokenStack $stack = null) |
31
|
|
|
{ |
32
|
230 |
|
$this->value = $value; |
33
|
230 |
|
$this->offset = $offset; |
34
|
230 |
|
$this->stack = $stack; |
35
|
230 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
204 |
|
public function getValue() |
41
|
|
|
{ |
42
|
204 |
|
return $this->value; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Some tokens can be represented by different operators, so the original value is used for error reporting, |
47
|
|
|
* while the other one is used internally. |
48
|
|
|
* |
49
|
|
|
* @return mixed |
50
|
|
|
*/ |
51
|
124 |
|
final public function getOriginalValue() |
52
|
|
|
{ |
53
|
124 |
|
return $this->value; |
54
|
|
|
} |
55
|
|
|
|
56
|
134 |
|
public function getOffset(): int |
57
|
|
|
{ |
58
|
134 |
|
return $this->offset; |
59
|
|
|
} |
60
|
|
|
|
61
|
90 |
|
public function getStack(): TokenStack |
62
|
|
|
{ |
63
|
90 |
|
return $this->stack; |
64
|
|
|
} |
65
|
|
|
|
66
|
86 |
|
public function setStack(TokenStack $stack) |
67
|
|
|
{ |
68
|
86 |
|
$this->stack = $stack; |
69
|
86 |
|
} |
70
|
|
|
|
71
|
188 |
|
public function isWhitespace(): bool |
72
|
|
|
{ |
73
|
188 |
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
192 |
|
public function isOfType(int $type): bool |
77
|
|
|
{ |
78
|
192 |
|
return $this->getType() === $type; |
79
|
|
|
} |
80
|
|
|
|
81
|
208 |
|
public function createNode(TokenStream $tokenStream): self |
82
|
|
|
{ |
83
|
208 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
50 |
|
public function getPosition(): int |
87
|
|
|
{ |
88
|
50 |
|
if (!isset($this->position)) { |
89
|
48 |
|
$this->getLineAndPosition(); |
90
|
|
|
} |
91
|
|
|
|
92
|
50 |
|
return $this->position; |
93
|
|
|
} |
94
|
|
|
|
95
|
50 |
|
public function getLine(): int |
96
|
|
|
{ |
97
|
50 |
|
if (!isset($this->line)) { |
98
|
2 |
|
$this->getLineAndPosition(); |
99
|
|
|
} |
100
|
|
|
|
101
|
50 |
|
return $this->line; |
102
|
|
|
} |
103
|
|
|
|
104
|
50 |
|
private function getLineAndPosition() |
105
|
|
|
{ |
106
|
50 |
|
$this->line = 1; |
107
|
50 |
|
$this->position = 0; |
108
|
|
|
|
109
|
50 |
|
foreach ($this->stack as $token) { |
110
|
50 |
|
$sumPosition = true; |
111
|
|
|
|
112
|
50 |
|
if ($token === $this) { |
113
|
50 |
|
break; |
114
|
40 |
|
} elseif ($token instanceof TokenNewline) { |
115
|
4 |
|
$this->line += 1; |
116
|
4 |
|
$this->position = 0; |
117
|
4 |
|
$sumPosition = false; |
118
|
40 |
|
} elseif ($token instanceof TokenComment) { |
119
|
4 |
|
$this->line += substr_count($token->getValue(), "\n"); |
120
|
|
|
} |
121
|
|
|
|
122
|
40 |
|
if ($sumPosition) { |
123
|
40 |
|
$this->position += strlen($token->getOriginalValue()); |
124
|
|
|
} |
125
|
|
|
} |
126
|
50 |
|
} |
127
|
|
|
} |
128
|
|
|
|