예제 #1
0
  @CheckForNull
  public static TerminalNode findTerminalNode(@NonNull ParseTree node, Token symbol) {
    if (symbol == null) {
      return null;
    }

    if (node instanceof TerminalNode) {
      TerminalNode terminalNode = (TerminalNode) node;
      if (Utils.equals(terminalNode.getSymbol(), symbol)) {
        return terminalNode;
      }

      return null;
    }

    for (int i = 0; i < node.getChildCount(); i++) {
      ParseTree child = node.getChild(i);
      TerminalNode stopNode = ParseTrees.getStopNode(child);
      if (stopNode == null) {
        continue;
      }

      Token stopSymbol = stopNode.getSymbol();
      if (stopSymbol.getStopIndex() < symbol.getStartIndex()) {
        continue;
      }

      TerminalNode startNode = ParseTrees.getStartNode(child);
      assert startNode != null;

      stopSymbol = startNode.getSymbol();
      if (stopSymbol == null || stopSymbol.getStartIndex() > symbol.getStopIndex()) {
        break;
      }

      if (stopSymbol.equals(symbol)) {
        return startNode;
      }

      TerminalNode terminalNode = findTerminalNode(child, symbol);
      if (terminalNode != null) {
        return terminalNode;
      }
    }

    return null;
  }
예제 #2
0
  /**
   * Gets whether or not the first symbol of {@code tree} is the first non-whitespace symbol on a
   * line.
   *
   * @param tree The parse tree to test.
   * @return {@code true} if the only characters appearing before the first token of {@code tree} on
   *     the line where {@code tree} starts are whitespace characters according to {@link
   *     Character#isWhitespace}.
   */
  public static boolean elementStartsLine(ParseTree tree) {
    TerminalNode symbol = ParseTrees.getStartNode(tree);
    if (symbol == null) {
      throw new NotImplementedException();
    }

    return elementStartsLine(symbol.getSymbol());
  }