Skip to content

Commit 34cca1b

Browse files
committed
Node: added $start & $end positions (replaces $startLine & $endLine)
1 parent 06f7ec2 commit 34cca1b

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
@@ -15,14 +15,10 @@ final class Parser
1515
{
1616
private TokenStream $stream;
1717

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

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

2723
while ($this->stream->tryConsume(Token::Newline));
2824
$node = $this->parseBlock($this->stream->getIndentation());
@@ -240,22 +236,10 @@ private function checkArrayKey(Node $key, array &$arr): void
240236
private function injectPos(Node $node, ?int $start = null, ?int $end = null): Node
241237
{
242238
$node->startTokenPos = $start ?? $this->stream->getIndex();
243-
$node->startLine = $this->posToLine[$node->startTokenPos];
239+
$node->start = $this->stream->tokens[$node->startTokenPos]->position;
244240
$node->endTokenPos = $end ?? $node->startTokenPos;
245-
$node->endLine = $this->posToLine[$node->endTokenPos + 1] ?? end($this->posToLine);
241+
$token = $this->stream->tokens[$node->startTokenPos + 1] ?? $this->stream->tokens[$node->startTokenPos];
242+
$node->end = $token->position;
246243
return $node;
247244
}
248-
249-
250-
private function initLines(): void
251-
{
252-
$this->posToLine = [];
253-
$line = 1;
254-
foreach ($this->stream->tokens as $token) {
255-
$this->posToLine[] = $line;
256-
$line += substr_count($token->text, "\n");
257-
}
258-
259-
$this->posToLine[] = $line;
260-
}
261245
}

0 commit comments

Comments
 (0)