|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Railt package. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Railt\SDL\Frontend\Ast; |
|
13
|
|
|
|
|
14
|
|
|
use Phplrt\Position\Position; |
|
15
|
|
|
use Phplrt\Position\PositionInterface; |
|
16
|
|
|
use Phplrt\Contracts\Source\FileInterface; |
|
17
|
|
|
use Phplrt\Contracts\Source\ReadableInterface; |
|
18
|
|
|
use Phplrt\Source\Exception\NotAccessibleException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Contains a range of byte offsets that identify the region of the source |
|
22
|
|
|
* from which the AST derived. |
|
23
|
|
|
*/ |
|
24
|
|
|
final class Location implements \JsonSerializable |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var ReadableInterface|FileInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
public ReadableInterface $source; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var int |
|
33
|
|
|
*/ |
|
34
|
|
|
public int $start; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var int |
|
38
|
|
|
*/ |
|
39
|
|
|
public int $end; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var PositionInterface|null |
|
43
|
|
|
*/ |
|
44
|
|
|
private ?PositionInterface $startPosition = null; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var PositionInterface|null |
|
48
|
|
|
*/ |
|
49
|
|
|
private ?PositionInterface $endPosition = null; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Location constructor. |
|
53
|
|
|
* |
|
54
|
|
|
* @param ReadableInterface $source |
|
55
|
|
|
* @param int $start |
|
56
|
|
|
* @param int $end |
|
57
|
|
|
*/ |
|
58
|
|
|
public function __construct(ReadableInterface $source, int $start, int $end) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->source = $source; |
|
61
|
|
|
$this->start = $start; |
|
62
|
|
|
$this->end = $end; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return array |
|
67
|
|
|
* @throws NotAccessibleException |
|
68
|
|
|
* @throws \RuntimeException |
|
69
|
|
|
*/ |
|
70
|
|
|
public function jsonSerialize(): array |
|
71
|
|
|
{ |
|
72
|
|
|
$source = $this->source instanceof FileInterface |
|
73
|
|
|
? $this->source->getPathname() |
|
|
|
|
|
|
74
|
|
|
: '{ ... }'; |
|
75
|
|
|
|
|
76
|
|
|
return [ |
|
77
|
|
|
'source' => $source, |
|
78
|
|
|
'start' => ['line' => $this->getStartLine(), 'column' => $this->getStartColumn()], |
|
79
|
|
|
'end' => ['line' => $this->getEndLine(), 'column' => $this->getEndColumn()], |
|
80
|
|
|
]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return int |
|
85
|
|
|
* @throws NotAccessibleException |
|
86
|
|
|
* @throws \RuntimeException |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getStartLine(): int |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->getStartPosition()->getLine(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return PositionInterface |
|
95
|
|
|
* @throws NotAccessibleException |
|
96
|
|
|
* @throws \RuntimeException |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getStartPosition(): PositionInterface |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->startPosition ?? $this->startPosition = Position::fromOffset($this->source, $this->start); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return int |
|
105
|
|
|
* @throws NotAccessibleException |
|
106
|
|
|
* @throws \RuntimeException |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getStartColumn(): int |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->getStartPosition()->getColumn(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return int |
|
115
|
|
|
* @throws NotAccessibleException |
|
116
|
|
|
* @throws \RuntimeException |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getEndLine(): int |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->getEndPosition()->getLine(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return PositionInterface |
|
125
|
|
|
* @throws NotAccessibleException |
|
126
|
|
|
* @throws \RuntimeException |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getEndPosition(): PositionInterface |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->endPosition ?? $this->endPosition = Position::fromOffset($this->source, $this->end); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @return int |
|
135
|
|
|
* @throws NotAccessibleException |
|
136
|
|
|
* @throws \RuntimeException |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getEndColumn(): int |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->getEndPosition()->getColumn(); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|