Skip to content

Commit

Permalink
Node: added $startLine & $endLine
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 16, 2021
1 parent 2900782 commit 5aa0b42
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/Neon/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ abstract class Node
/** @var ?int */
public $endPos;

/** @var ?int */
public $startLine;

/** @var ?int */
public $endLine;


/** @return mixed */
abstract public function toValue();
Expand Down
21 changes: 20 additions & 1 deletion src/Neon/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ final class Parser
/** @var TokenStream */
private $tokens;

/** @var int[] */
private $posToLine = [];


public function parse(TokenStream $tokens): Node
{
$this->tokens = $tokens;
$this->initLines();

while ($this->tokens->consume(Token::NEWLINE));
$node = $this->parseBlock($this->tokens->getIndentation());

Expand Down Expand Up @@ -234,13 +239,27 @@ private function checkArrayKey(Node $key, array &$arr): void
private function setPos(Node $node, int $startPos = null, int $endPos = null): Node
{
$node->startPos = $startPos ?? $this->tokens->getPos();
$node->endPos = $endPos ?? $node->startPos;
$node->startLine = $this->posToLine[$node->startPos];
$this->setEndPos($node, $endPos ?? $node->startPos);
return $node;
}


private function setEndPos(Node $node, int $endPos): void
{
$node->endPos = $endPos;
$node->endLine = $this->posToLine[$node->endPos + 1] ?? end($this->posToLine);
}


private function initLines(): void
{
$this->posToLine = [];
$line = 1;
foreach ($this->tokens->getTokens() as $token) {
$this->posToLine[] = $line;
$line += substr_count($token->value, "\n");
}
$this->posToLine[] = $line;
}
}
106 changes: 106 additions & 0 deletions tests/Neon/fixtures/Encoder.nodes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Nette\Neon\Node\InlineArrayNode
| | | value: 'map'
| | | startPos: null
| | | endPos: null
| | | startLine: null
| | | endLine: null
| | value: Nette\Neon\Node\InlineArrayNode
| | | bracket: '{'
| | | items: array (2)
Expand All @@ -14,32 +16,50 @@ Nette\Neon\Node\InlineArrayNode
| | | | | | value: 'a'
| | | | | | startPos: null
| | | | | | endPos: null
| | | | | | startLine: null
| | | | | | endLine: null
| | | | | value: Nette\Neon\Node\LiteralNode
| | | | | | value: 'b'
| | | | | | startPos: null
| | | | | | endPos: null
| | | | | | startLine: null
| | | | | | endLine: null
| | | | | startPos: null
| | | | | endPos: null
| | | | | startLine: null
| | | | | endLine: null
| | | | 1 => Nette\Neon\Node\ArrayItemNode
| | | | | key: Nette\Neon\Node\LiteralNode
| | | | | | value: 'c'
| | | | | | startPos: null
| | | | | | endPos: null
| | | | | | startLine: null
| | | | | | endLine: null
| | | | | value: Nette\Neon\Node\LiteralNode
| | | | | | value: 'd'
| | | | | | startPos: null
| | | | | | endPos: null
| | | | | | startLine: null
| | | | | | endLine: null
| | | | | startPos: null
| | | | | endPos: null
| | | | | startLine: null
| | | | | endLine: null
| | | startPos: null
| | | endPos: null
| | | startLine: null
| | | endLine: null
| | startPos: null
| | endPos: null
| | startLine: null
| | endLine: null
| 1 => Nette\Neon\Node\ArrayItemNode
| | key: Nette\Neon\Node\LiteralNode
| | | value: 'index'
| | | startPos: null
| | | endPos: null
| | | startLine: null
| | | endLine: null
| | value: Nette\Neon\Node\InlineArrayNode
| | | bracket: '['
| | | items: array (3)
Expand All @@ -49,33 +69,51 @@ Nette\Neon\Node\InlineArrayNode
| | | | | | value: 'a'
| | | | | | startPos: null
| | | | | | endPos: null
| | | | | | startLine: null
| | | | | | endLine: null
| | | | | startPos: null
| | | | | endPos: null
| | | | | startLine: null
| | | | | endLine: null
| | | | 1 => Nette\Neon\Node\ArrayItemNode
| | | | | key: null
| | | | | value: Nette\Neon\Node\LiteralNode
| | | | | | value: 'b'
| | | | | | startPos: null
| | | | | | endPos: null
| | | | | | startLine: null
| | | | | | endLine: null
| | | | | startPos: null
| | | | | endPos: null
| | | | | startLine: null
| | | | | endLine: null
| | | | 2 => Nette\Neon\Node\ArrayItemNode
| | | | | key: null
| | | | | value: Nette\Neon\Node\LiteralNode
| | | | | | value: 'c'
| | | | | | startPos: null
| | | | | | endPos: null
| | | | | | startLine: null
| | | | | | endLine: null
| | | | | startPos: null
| | | | | endPos: null
| | | | | startLine: null
| | | | | endLine: null
| | | startPos: null
| | | endPos: null
| | | startLine: null
| | | endLine: null
| | startPos: null
| | endPos: null
| | startLine: null
| | endLine: null
| 2 => Nette\Neon\Node\ArrayItemNode
| | key: Nette\Neon\Node\LiteralNode
| | | value: 'mixed'
| | | startPos: null
| | | endPos: null
| | | startLine: null
| | | endLine: null
| | value: Nette\Neon\Node\InlineArrayNode
| | | bracket: '{'
| | | items: array (4)
Expand All @@ -85,128 +123,196 @@ Nette\Neon\Node\InlineArrayNode
| | | | | | value: 'a'
| | | | | | startPos: null
| | | | | | endPos: null
| | | | | | startLine: null
| | | | | | endLine: null
| | | | | startPos: null
| | | | | endPos: null
| | | | | startLine: null
| | | | | endLine: null
| | | | 1 => Nette\Neon\Node\ArrayItemNode
| | | | | key: null
| | | | | value: Nette\Neon\Node\LiteralNode
| | | | | | value: 'b'
| | | | | | startPos: null
| | | | | | endPos: null
| | | | | | startLine: null
| | | | | | endLine: null
| | | | | startPos: null
| | | | | endPos: null
| | | | | startLine: null
| | | | | endLine: null
| | | | 2 => Nette\Neon\Node\ArrayItemNode
| | | | | key: Nette\Neon\Node\LiteralNode
| | | | | | value: 4
| | | | | | startPos: null
| | | | | | endPos: null
| | | | | | startLine: null
| | | | | | endLine: null
| | | | | value: Nette\Neon\Node\LiteralNode
| | | | | | value: 'c'
| | | | | | startPos: null
| | | | | | endPos: null
| | | | | | startLine: null
| | | | | | endLine: null
| | | | | startPos: null
| | | | | endPos: null
| | | | | startLine: null
| | | | | endLine: null
| | | | 3 => Nette\Neon\Node\ArrayItemNode
| | | | | key: Nette\Neon\Node\LiteralNode
| | | | | | value: 5
| | | | | | startPos: null
| | | | | | endPos: null
| | | | | | startLine: null
| | | | | | endLine: null
| | | | | value: Nette\Neon\Node\LiteralNode
| | | | | | value: 'd'
| | | | | | startPos: null
| | | | | | endPos: null
| | | | | | startLine: null
| | | | | | endLine: null
| | | | | startPos: null
| | | | | endPos: null
| | | | | startLine: null
| | | | | endLine: null
| | | startPos: null
| | | endPos: null
| | | startLine: null
| | | endLine: null
| | startPos: null
| | endPos: null
| | startLine: null
| | endLine: null
| 3 => Nette\Neon\Node\ArrayItemNode
| | key: Nette\Neon\Node\LiteralNode
| | | value: 'entity'
| | | startPos: null
| | | endPos: null
| | | startLine: null
| | | endLine: null
| | value: Nette\Neon\Node\EntityNode
| | | value: Nette\Neon\Node\LiteralNode
| | | | value: 'ent'
| | | | startPos: null
| | | | endPos: null
| | | | startLine: null
| | | | endLine: null
| | | attributes: array (2)
| | | | 0 => Nette\Neon\Node\ArrayItemNode
| | | | | key: null
| | | | | value: Nette\Neon\Node\LiteralNode
| | | | | | value: 'a'
| | | | | | startPos: null
| | | | | | endPos: null
| | | | | | startLine: null
| | | | | | endLine: null
| | | | | startPos: null
| | | | | endPos: null
| | | | | startLine: null
| | | | | endLine: null
| | | | 1 => Nette\Neon\Node\ArrayItemNode
| | | | | key: null
| | | | | value: Nette\Neon\Node\LiteralNode
| | | | | | value: 'b'
| | | | | | startPos: null
| | | | | | endPos: null
| | | | | | startLine: null
| | | | | | endLine: null
| | | | | startPos: null
| | | | | endPos: null
| | | | | startLine: null
| | | | | endLine: null
| | | startPos: null
| | | endPos: null
| | | startLine: null
| | | endLine: null
| | startPos: null
| | endPos: null
| | startLine: null
| | endLine: null
| 4 => Nette\Neon\Node\ArrayItemNode
| | key: Nette\Neon\Node\LiteralNode
| | | value: 'chain'
| | | startPos: null
| | | endPos: null
| | | startLine: null
| | | endLine: null
| | value: Nette\Neon\Node\EntityChainNode
| | | chain: array (2)
| | | | 0 => Nette\Neon\Node\EntityNode
| | | | | value: Nette\Neon\Node\LiteralNode
| | | | | | value: 'first'
| | | | | | startPos: null
| | | | | | endPos: null
| | | | | | startLine: null
| | | | | | endLine: null
| | | | | attributes: array (2)
| | | | | | 0 => Nette\Neon\Node\ArrayItemNode ...
| | | | | | 1 => Nette\Neon\Node\ArrayItemNode ...
| | | | | startPos: null
| | | | | endPos: null
| | | | | startLine: null
| | | | | endLine: null
| | | | 1 => Nette\Neon\Node\EntityNode
| | | | | value: Nette\Neon\Node\LiteralNode
| | | | | | value: 'second'
| | | | | | startPos: null
| | | | | | endPos: null
| | | | | | startLine: null
| | | | | | endLine: null
| | | | | attributes: array (0)
| | | | | startPos: null
| | | | | endPos: null
| | | | | startLine: null
| | | | | endLine: null
| | | startPos: null
| | | endPos: null
| | | startLine: null
| | | endLine: null
| | startPos: null
| | endPos: null
| | startLine: null
| | endLine: null
| 5 => Nette\Neon\Node\ArrayItemNode
| | key: Nette\Neon\Node\LiteralNode
| | | value: 'multiline'
| | | startPos: null
| | | endPos: null
| | | startLine: null
| | | endLine: null
| | value: Nette\Neon\Node\StringNode
| | | value: string
| | | | 'hello\n
| | | | world'
| | | startPos: null
| | | endPos: null
| | | startLine: null
| | | endLine: null
| | startPos: null
| | endPos: null
| | startLine: null
| | endLine: null
| 6 => Nette\Neon\Node\ArrayItemNode
| | key: Nette\Neon\Node\LiteralNode
| | | value: 'date'
| | | startPos: null
| | | endPos: null
| | | startLine: null
| | | endLine: null
| | value: Nette\Neon\Node\LiteralNode
| | | value: DateTime
| | | | date: '2016-06-03 19:00:00.000000'
| | | | timezone_type: 1
| | | | timezone: '+02:00'
| | | startPos: null
| | | endPos: null
| | | startLine: null
| | | endLine: null
| | startPos: null
| | endPos: null
| | startLine: null
| | endLine: null
startPos: null
endPos: null
startLine: null
endLine: null
Loading

0 comments on commit 5aa0b42

Please sign in to comment.