public function resolveGrouping(NodeContract $node)
    {
        if ($node->countChildren() === 0) {
            return;
        }

        foreach ($node->getChildren() as $child) {
            $this->resolveGrouping($child);
        }

        $p = 0;
        $last_opening = null;

        foreach ($node->getChildren() as $n => $child) {
            if ($child instanceof Nodes\GroupOpeningNode) {
                ++$p;
                $last_opening = $n;
            }

            if ($child instanceof Nodes\GroupClosingNode) {
                --$p;
                // A group has found. Close and re-resolve

                if ($last_opening === null) {
                    throw new Exceptions\QuerySyntaxException('Unexpected closing bracket: '.$node->getRoot()->getValue());
                }

                $new_node = new $this->node();

                $children = $node->getChildrenBetweenIndexes((int) $last_opening, $n);
                $node->removeChildren($children);
                $new_node->addChildren($children);

                $new_node->removeChildByIndex(0);
                $new_node->removeChildByIndex($new_node->countChildren() - 1);
                $node->addChildBeforeNodeByIndex($new_node, (int) $last_opening);

                if ($node->getParent() !== null) {
                    $this->resolveGrouping($node->getParent());
                }

                return;
            }
        }

        if ($p > 0) {
            throw new Exceptions\QuerySyntaxException('Expected closing bracket: '.$node->getRoot()->getValue());
        }
    }