/**
   * Appends comment nodes to existing AST. It traverses each node in AST, looks for hidden comment
   * tokens and appends found comment tokens as nodes in AST.
   *
   * @param root root of AST.
   * @return root of AST with comment nodes.
   */
  private static DetailAST appendHiddenCommentNodes(DetailAST root) {
    DetailAST result = root;
    DetailAST curNode = root;
    DetailAST lastNode = root;

    while (curNode != null) {
      if (isPositionGreater(curNode, lastNode)) {
        lastNode = curNode;
      }

      CommonHiddenStreamToken tokenBefore = curNode.getHiddenBefore();
      DetailAST currentSibling = curNode;
      while (tokenBefore != null) {
        final DetailAST newCommentNode = createCommentAstFromToken(tokenBefore);

        currentSibling.addPreviousSibling(newCommentNode);

        if (currentSibling == result) {
          result = newCommentNode;
        }

        currentSibling = newCommentNode;
        tokenBefore = tokenBefore.getHiddenBefore();
      }

      DetailAST toVisit = curNode.getFirstChild();
      while (curNode != null && toVisit == null) {
        toVisit = curNode.getNextSibling();
        if (toVisit == null) {
          curNode = curNode.getParent();
        }
      }
      curNode = toVisit;
    }
    if (lastNode != null) {
      CommonHiddenStreamToken tokenAfter = lastNode.getHiddenAfter();
      DetailAST currentSibling = lastNode;
      while (tokenAfter != null) {
        final DetailAST newCommentNode = createCommentAstFromToken(tokenAfter);

        currentSibling.addNextSibling(newCommentNode);

        currentSibling = newCommentNode;
        tokenAfter = tokenAfter.getHiddenAfter();
      }
    }
    return result;
  }
  private String debugHiddenBefore(AST ast) {
    if (!(ast instanceof antlr.CommonASTWithHiddenTokens)) {
      return "";
    }
    antlr.CommonHiddenStreamToken parent =
        ((antlr.CommonASTWithHiddenTokens) ast).getHiddenBefore();

    if (parent == null) {
      return "";
    }

    antlr.CommonHiddenStreamToken child = null;
    do {
      child = parent;
      parent = child.getHiddenBefore();
    } while (parent != null);

    return debugHiddenTokens(child);
  }