Skip to content

Commit aa449b1

Browse files
committed
Node: added $start & $end positions (replaces $startLine & $endLine)
1 parent dc4fe08 commit aa449b1

File tree

4 files changed

+711
-283
lines changed

4 files changed

+711
-283
lines changed

src/Neon/Node.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ abstract class Node implements \IteratorAggregate
1717
{
1818
public ?int $startTokenPos = null;
1919
public ?int $endTokenPos = null;
20-
public ?int $startLine = null;
21-
public ?int $endLine = null;
20+
public ?Position $start = null;
21+
public ?Position $end = null;
2222

2323

2424
abstract public function toValue(): mixed;

src/Neon/Parser.php

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

20-
/** @var int[] */
21-
private $posToLine = [];
22-
2320

2421
public function parse(TokenStream $stream): Node
2522
{
2623
$this->stream = $stream;
27-
$this->initLines();
2824

2925
while ($this->stream->tryConsume(Token::Newline));
3026
$node = $this->parseBlock($this->stream->getIndentation());
@@ -242,22 +238,10 @@ private function checkArrayKey(Node $key, array &$arr): void
242238
private function injectPos(Node $node, ?int $start = null, ?int $end = null): Node
243239
{
244240
$node->startTokenPos = $start ?? $this->stream->getIndex();
245-
$node->startLine = $this->posToLine[$node->startTokenPos];
241+
$node->start = $this->stream->tokens[$node->startTokenPos]->position;
246242
$node->endTokenPos = $end ?? $node->startTokenPos;
247-
$node->endLine = $this->posToLine[$node->endTokenPos + 1] ?? end($this->posToLine);
243+
$token = $this->stream->tokens[$node->startTokenPos + 1] ?? $this->stream->tokens[$node->startTokenPos];
244+
$node->end = $token->position;
248245
return $node;
249246
}
250-
251-
252-
private function initLines(): void
253-
{
254-
$this->posToLine = [];
255-
$line = 1;
256-
foreach ($this->stream->tokens as $token) {
257-
$this->posToLine[] = $line;
258-
$line += substr_count($token->text, "\n");
259-
}
260-
261-
$this->posToLine[] = $line;
262-
}
263247
}

0 commit comments

Comments
 (0)