Skip to content

Commit 2490635

Browse files
committed
Node: added $start & $end positions (replaces $startLine & $endLine)
1 parent 6dcda2b commit 2490635

5 files changed

Lines changed: 711 additions & 289 deletions

File tree

phpstan-baseline.neon

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,6 @@ parameters:
4242
count: 1
4343
path: src/Neon/Parser.php
4444

45-
-
46-
message: '#^Property Nette\\Neon\\Node\:\:\$endLine \(int\|null\) does not accept int\|false\.$#'
47-
identifier: assign.propertyType
48-
count: 1
49-
path: src/Neon/Parser.php
50-
5145
-
5246
message: '#^Unreachable statement \- code above always terminates\.$#'
5347
identifier: deadCode.unreachable

src/Neon/Node.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ abstract class Node implements \IteratorAggregate
1515
{
1616
public ?int $startTokenPos = null;
1717
public ?int $endTokenPos = null;
18-
public ?int $startLine = null;
19-
public ?int $endLine = null;
18+
public ?Position $start = null;
19+
public ?Position $end = null;
2020

2121

2222
abstract public function toValue(): mixed;

src/Neon/Parser.php

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,10 @@ final class Parser
1515
{
1616
private TokenStream $stream;
1717

18-
/** @var list<int> */
19-
private array $posToLine = [];
20-
2118

2219
public function parse(TokenStream $tokens): Node
2320
{
2421
$this->stream = $tokens;
25-
$this->initLines();
2622

2723
while ($this->stream->tryConsume(Token::Newline));
2824
$node = $this->parseBlock($this->stream->getIndentation());
@@ -244,22 +240,10 @@ private function checkArrayKey(Node $key, array &$arr): void
244240
private function injectPos(Node $node, ?int $start = null, ?int $end = null): Node
245241
{
246242
$node->startTokenPos = $start ?? $this->stream->getIndex();
247-
$node->startLine = $this->posToLine[$node->startTokenPos];
243+
$node->start = $this->stream->tokens[$node->startTokenPos]->position;
248244
$node->endTokenPos = $end ?? $node->startTokenPos;
249-
$node->endLine = $this->posToLine[$node->endTokenPos + 1] ?? end($this->posToLine);
245+
$token = $this->stream->tokens[$node->startTokenPos + 1] ?? $this->stream->tokens[$node->startTokenPos];
246+
$node->end = $token->position;
250247
return $node;
251248
}
252-
253-
254-
private function initLines(): void
255-
{
256-
$this->posToLine = [];
257-
$line = 1;
258-
foreach ($this->stream->tokens as $token) {
259-
$this->posToLine[] = $line;
260-
$line += substr_count($token->text, "\n");
261-
}
262-
263-
$this->posToLine[] = $line;
264-
}
265249
}

0 commit comments

Comments
 (0)