for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace Remorhaz\UniLex\AST;
use Remorhaz\UniLex\Exception;
class Tree
{
private $nextNodeId = 1;
/**
* @var Node[]
*/
private $nodeMap = [];
private $rootNodeId;
public function createNode(string $name, int $id = null): Node
$node = new Node($id ?? $this->getNextNodeId(), $name);
$this->nodeMap[$node->getId()] = $node;
return $node;
}
* @param int $id
* @return Node
* @throws Exception
public function getNode(int $id): Node
if (!isset($this->nodeMap[$id])) {
throw new Exception("Node {$id} is not defined in syntax tree");
return $this->nodeMap[$id];
* @param Node $node
public function setRootNode(Node $node): void
if (isset($this->rootNodeId)) {
throw new Exception("Root node of syntax tree is already set");
$this->rootNodeId = $node->getId();
public function getRootNode(): Node
if (!isset($this->rootNodeId)) {
throw new Exception("Root node of syntax tree is undefined");
return $this->getNode($this->rootNodeId);
private function getNextNodeId(): int
return $this->nextNodeId++;