Example #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;
  }
Example #2
0
  public String toString(boolean rendered) {
    if (!rendered) {
      return Utils.join(all.iterator(), "\n");
    }

    if (tool == null) {
      throw new IllegalStateException(
          String.format("No %s instance is available.", Tool.class.getName()));
    }

    StringBuilder buf = new StringBuilder();
    for (ANTLRMessage m : all) {
      ST st = tool.errMgr.getMessageTemplate(m);
      buf.append(st.render());
      buf.append("\n");
    }

    return buf.toString();
  }
Example #3
0
 @Override
 public String toString() {
   return Utils.join(Arrays.asList(opnds).iterator(), "||");
 }