|
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
|
|
|
/** |
|
42
|
|
|
* @param mixed $value |
|
43
|
|
|
* @param int $offset |
|
44
|
|
|
* @param Stack $stack |
|
45
|
|
|
*/ |
|
46
|
228 |
|
public function __construct($value, int $offset = 0, Stack $stack = null) |
|
47
|
|
|
{ |
|
48
|
228 |
|
$this->value = $value; |
|
49
|
228 |
|
$this->offset = $offset; |
|
50
|
228 |
|
$this->stack = $stack; |
|
51
|
228 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
abstract public function getGroup() : int; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return mixed |
|
57
|
|
|
*/ |
|
58
|
210 |
|
public function getValue() |
|
59
|
|
|
{ |
|
60
|
210 |
|
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
|
|
|
public function setOffset(int $offset = 0) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->offset = $offset; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
106 |
|
public function getStack() : Stack |
|
86
|
|
|
{ |
|
87
|
106 |
|
return $this->stack; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
90 |
|
public function setStack(Stack $stack) |
|
91
|
|
|
{ |
|
92
|
90 |
|
$this->stack = $stack; |
|
93
|
90 |
|
} |
|
94
|
|
|
|
|
95
|
18 |
|
public function supportsMethodCalls() : bool |
|
96
|
|
|
{ |
|
97
|
18 |
|
return false; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
50 |
|
public function getPosition() : int |
|
101
|
|
|
{ |
|
102
|
50 |
|
if (!isset($this->position)) { |
|
103
|
48 |
|
$this->getLineAndPosition(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
50 |
|
return $this->position; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
50 |
|
public function getLine() : int |
|
110
|
|
|
{ |
|
111
|
50 |
|
if (!isset($this->line)) { |
|
112
|
2 |
|
$this->getLineAndPosition(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
50 |
|
return $this->line; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
50 |
|
private function getLineAndPosition() |
|
119
|
|
|
{ |
|
120
|
50 |
|
$this->line = 1; |
|
121
|
50 |
|
$this->position = 0; |
|
122
|
|
|
|
|
123
|
50 |
|
foreach ($this->stack as $token) { |
|
124
|
50 |
|
$sumPosition = true; |
|
125
|
|
|
|
|
126
|
50 |
|
if ($token === $this) { |
|
127
|
48 |
|
break; |
|
128
|
|
|
} elseif ($token instanceof TokenNewline) { |
|
129
|
4 |
|
$this->line += 1; |
|
130
|
4 |
|
$this->position = 0; |
|
131
|
4 |
|
$sumPosition = false; |
|
132
|
|
|
} elseif ($token instanceof TokenComment) { |
|
133
|
4 |
|
$this->line += substr_count($token->getValue(), "\n"); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
40 |
|
if ($sumPosition) { |
|
137
|
40 |
|
$this->position += strlen($token->getOriginalValue()); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
50 |
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|