/** * @param Item[] $items * @param string[] $input * @param Rule $rule * @return Tree */ private function buildTree(&$items, &$input, Rule $rule) { $result = new Tree($rule); $children = []; $entries = array_reverse($rule->getEntries()); foreach ($entries as $entry) { $class = get_class($entry); switch ($class) { case Reference::class: $item = $this->consumeItemMatchingRule($items, $entry->getRuleName()); $children[] = $this->buildTree($items, $input, $item->getRule()); break; case TokenReference::class: $children[] = new Leaf(array_pop($input)); break; default: echo 'BLERGH!!!!' . "\n"; break; } } $children = array_reverse($children); foreach ($children as $child) { $result->addChild($child); } return $result; }
public function __toString() { $children = $this->children; $output = sprintf("%s ->\n", $this->rule->getName()); foreach ($children as $child) { $childOutput = explode("\n", $child); $childLines = array_map(function ($line) { return sprintf("| %s\n", $line); }, $childOutput); $output .= implode('', $childLines); } return $output; }
/** * @return bool */ public function isComplete() { return $this->entryPosition === count($this->rule->getEntries()); }