/**
  * A utility method to output consistent error messages.
  *
  * @param ctx The ANTLR node the error occurred in.
  * @return The error message with tacked on line number and character position.
  */
 static String error(final ParserRuleContext ctx) {
   return "Analyzer Error ["
       + ctx.getStart().getLine()
       + ":"
       + ctx.getStart().getCharPositionInLine()
       + "]: ";
 }
  @Override
  public void parse(StatementSkip node, ParserRuleContext context) {
    // Get Symbol for line
    TerminalNodeImpl firstTerminal = (TerminalNodeImpl) context.getChild(0);
    Label label =
        new LabelLine(
            node.getClass(), firstTerminal.getSymbol().getText(), context.getStart().getLine());

    node.setL(label);
  }
  @Override
  public void parse(DeclarationArrayInteger node, ParserRuleContext context) {
    // Get Symbol for line
    TerminalNodeImpl firstTerminal = (TerminalNodeImpl) context.getChild(1);
    Label label =
        new LabelLine(
            node.getClass(), firstTerminal.getSymbol().getText(), context.getStart().getLine());

    node.setL(label);
  }
  @Override
  public void enterStatement(JavaParser.StatementContext ctx) {
    ctx.getChildCount();
    String text = ctx.getStart().getText();
    if (!text.equals("if") && !text.equals("while") && !text.equals("for") && !text.equals("do")) {
      return;
    }

    int depth = 1;
    ParserRuleContext tempContext = ctx.getParent();
    while (!(tempContext instanceof JavaParser.MethodDeclarationContext)) {
      if (tempContext instanceof JavaParser.StatementContext) {
        text = tempContext.getStart().getText();
        if (text.equals("if") || text.equals("while") || text.equals("for") || text.equals("do")) {
          depth++;
        }
      }
      tempContext = tempContext.getParent();
    }
    report.setDepthOfConditionNesting(depth);
  }
示例#5
0
  public static Interval getSourceInterval(@NonNull ParserRuleContext context) {
    Parameters.notNull("context", context);
    int startIndex = context.start.getStartIndex();
    Token stopSymbol = getStopSymbol(context);
    if (stopSymbol == null) {
      return new Interval(startIndex, startIndex - 1);
    }

    int stopIndex;
    if (stopSymbol.getType() != Token.EOF) {
      stopIndex = stopSymbol.getStopIndex();
    } else {
      TokenSource tokenSource = context.getStart().getTokenSource();
      CharStream inputStream = tokenSource != null ? tokenSource.getInputStream() : null;
      if (inputStream != null) {
        stopIndex = inputStream.size() - 1;
      } else {
        stopIndex = context.start.getStartIndex() - 1;
      }
    }

    stopIndex = Math.max(stopIndex, startIndex - 1);
    return new Interval(startIndex, stopIndex);
  }