Skip to content

Commit

Permalink
Node: startPos & endPos renamed to startTokenPos & endTokenPos
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 10, 2022
1 parent d61c30d commit 51fca28
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 214 deletions.
4 changes: 2 additions & 2 deletions src/Neon/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
abstract class Node
{
/** @var ?int */
public $startPos;
public $startTokenPos;

/** @var ?int */
public $endPos;
public $endTokenPos;


/** @return mixed */
Expand Down
24 changes: 12 additions & 12 deletions src/Neon/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ private function parseBlock(string $indent, bool $onlyBullets = false): Node
$item->value->indentation = substr($item->value->indentation, strlen($indent));
}

$this->injectPos($res, $res->startPos, $item->value->endPos);
$this->injectPos($item, $item->startPos, $item->value->endPos);
$this->injectPos($res, $res->startTokenPos, $item->value->endTokenPos);
$this->injectPos($item, $item->startTokenPos, $item->value->endTokenPos);

while ($this->tokens->consume(Token::Newline));
if (!$this->tokens->isNext()) {
Expand Down Expand Up @@ -151,23 +151,23 @@ private function parseEntity(Node $node): Node
}

$attributes = $this->parseBraces();
$entities[] = $this->injectPos(new Node\EntityNode($node, $attributes->items), $node->startPos, $attributes->endPos);
$entities[] = $this->injectPos(new Node\EntityNode($node, $attributes->items), $node->startTokenPos, $attributes->endTokenPos);

while ($token = $this->tokens->consume(Token::Literal)) {
$valueNode = new Node\LiteralNode(Node\LiteralNode::parse($token->value));
$this->injectPos($valueNode, $this->tokens->getPos() - 1);
if ($this->tokens->isNext('(')) {
$attributes = $this->parseBraces();
$entities[] = $this->injectPos(new Node\EntityNode($valueNode, $attributes->items), $valueNode->startPos, $attributes->endPos);
$entities[] = $this->injectPos(new Node\EntityNode($valueNode, $attributes->items), $valueNode->startTokenPos, $attributes->endTokenPos);
} else {
$entities[] = $this->injectPos(new Node\EntityNode($valueNode), $valueNode->startPos);
$entities[] = $this->injectPos(new Node\EntityNode($valueNode), $valueNode->startTokenPos);
break;
}
}

return count($entities) === 1
? $entities[0]
: $this->injectPos(new Node\EntityChainNode($entities), $node->startPos, end($entities)->endPos);
: $this->injectPos(new Node\EntityChainNode($entities), $node->startTokenPos, end($entities)->endTokenPos);
}


Expand All @@ -182,7 +182,7 @@ private function parseBraces(): Node\InlineArrayNode
loop:
while ($this->tokens->consume(Token::Newline));
if ($this->tokens->consume($endBrace)) {
$this->injectPos($res, $res->startPos, $this->tokens->getPos() - 1);
$this->injectPos($res, $res->startTokenPos, $this->tokens->getPos() - 1);
return $res;
}

Expand All @@ -200,7 +200,7 @@ private function parseBraces(): Node\InlineArrayNode
$item->value = $value;
}

$this->injectPos($item, $item->startPos, $item->value->endPos);
$this->injectPos($item, $item->startTokenPos, $item->value->endTokenPos);

if ($this->tokens->consume(',', Token::Newline)) {
goto loop;
Expand All @@ -218,12 +218,12 @@ private function parseBraces(): Node\InlineArrayNode
private function checkArrayKey(Node $key, array &$arr): void
{
if ((!$key instanceof Node\StringNode && !$key instanceof Node\LiteralNode) || !is_scalar($key->value)) {
$this->tokens->error('Unacceptable key', $key->startPos);
$this->tokens->error('Unacceptable key', $key->startTokenPos);
}

$k = (string) $key->value;
if (array_key_exists($k, $arr)) {
$this->tokens->error("Duplicated key '$k'", $key->startPos);
$this->tokens->error("Duplicated key '$k'", $key->startTokenPos);
}

$arr[$k] = true;
Expand All @@ -232,8 +232,8 @@ private function checkArrayKey(Node $key, array &$arr): void

private function injectPos(Node $node, int $start = null, int $end = null): Node
{
$node->startPos = $start ?? $this->tokens->getPos();
$node->endPos = $end ?? $node->startPos;
$node->startTokenPos = $start ?? $this->tokens->getPos();
$node->endTokenPos = $end ?? $node->startTokenPos;
return $node;
}
}
4 changes: 2 additions & 2 deletions tests/Neon/Parser.nodes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ Assert::matchFile(
$traverser = new Traverser;
$traverser->traverse($node, function (Node $node) use ($stream) {
$node->code = '';
foreach (array_slice($stream->getTokens(), $node->startPos, $node->endPos - $node->startPos + 1) as $token) {
foreach (array_slice($stream->getTokens(), $node->startTokenPos, $node->endTokenPos - $node->startTokenPos + 1) as $token) {
$node->code .= $token->value;
}

unset($node->startPos, $node->endPos);
unset($node->startTokenPos, $node->endTokenPos);
});

Assert::matchFile(
Expand Down
Loading

0 comments on commit 51fca28

Please sign in to comment.