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