Пример #1
0
  /**
   * Gets whether or not {@code a} is an ancestor of or equal to {@code b}.
   *
   * @param a The first tree.
   * @param b The second tree.
   * @return {@code true} if {@code a} is an ancestor of or is equal to {@code b}, otherwise {@code
   *     false}.
   */
  public static boolean isAncestorOf(@NonNull ParseTree a, @NonNull ParseTree b) {
    for (ParseTree current = b; current != null; current = current.getParent()) {
      if (current.equals(a)) {
        return true;
      }
    }

    return false;
  }
  /**
   * Gets next sibling of ParseTree node.
   *
   * @param node ParseTree node
   * @return next sibling of ParseTree node.
   */
  private static ParseTree getNextSibling(ParseTree node) {
    ParseTree nextSibling = null;

    if (node.getParent() != null) {
      final ParseTree parent = node.getParent();
      final int childCount = parent.getChildCount();

      int index = 0;
      while (true) {
        final ParseTree currentNode = parent.getChild(index);
        if (currentNode.equals(node)) {
          if (index != childCount - 1) {
            nextSibling = parent.getChild(index + 1);
          }
          break;
        }
        index++;
      }
    }
    return nextSibling;
  }