protected String getErrorMessage(RecognitionException re) {
    String message = "";

    Parser recognizer = (Parser) re.getRecognizer();
    TokenStream tokens = recognizer.getInputStream();

    if (re instanceof NoViableAltException) {
      NoViableAltException e = (NoViableAltException) re;
      Token startToken = e.getStartToken();
      String input =
          (startToken.getType() == Token.EOF)
              ? "end of text"
              : quote(tokens.getText(startToken, e.getOffendingToken()));

      message = "no viable date format found at " + input;
    } else if (re instanceof InputMismatchException) {
      InputMismatchException e = (InputMismatchException) re;
      message =
          "did not expect "
              + getTokenDisplayString(e.getOffendingToken())
              + " while looking for "
              + e.getExpectedTokens().toString(recognizer.getTokenNames());
    } else if (re instanceof FailedPredicateException) {
      FailedPredicateException e = (FailedPredicateException) re;
      String ruleName = recognizer.getRuleNames()[recognizer.getContext().getRuleIndex()];

      message = "failed predicate " + ruleName + ": " + e.getMessage();
    }

    return message;
  }
  @Test
  public void testCompleteBufferAfterConsuming() throws Exception {
    LexerGrammar g =
        new LexerGrammar(
            "lexer grammar t;\n"
                + "ID : 'a'..'z'+;\n"
                + "INT : '0'..'9'+;\n"
                + "SEMI : ';';\n"
                + "ASSIGN : '=';\n"
                + "PLUS : '+';\n"
                + "MULT : '*';\n"
                + "WS : ' '+;\n");
    // Tokens: 012345678901234567
    // Input:  x = 3 * 0 + 2 * 0;
    CharStream input = new ANTLRInputStream("x = 3 * 0 + 2 * 0;");
    LexerInterpreter lexEngine = g.createLexerInterpreter(input);
    TokenStream tokens = createTokenStream(lexEngine);

    Token t = tokens.LT(1);
    while (t.getType() != Token.EOF) {
      tokens.consume();
      t = tokens.LT(1);
    }

    String result = tokens.getText();
    String expecting = "x = 3 * 0 + 2 * 0;";
    assertEquals(expecting, result);
  }
  public String filterMethod(String compilationUnit, String className, String methodNameAndParams) {

    final String methodName = extractMethodName(methodNameAndParams);
    final String[] params = extractParams(methodNameAndParams);

    ANTLRInputStream in = new ANTLRInputStream(compilationUnit);
    TokenStream tokenStream = new CommonTokenStream(new Java8Lexer(in));
    Java8Parser parser = new Java8Parser(tokenStream);

    MethodFilterListener filterListener = new MethodFilterListener(className, methodName, params);

    parser.addParseListener(filterListener);
    parser.setBuildParseTree(true);
    parser.compilationUnit();

    if (filterListener.getInterval() == null) {
      throw new IllegalArgumentException("Method '" + methodName + "' not found!");
    }

    final Token startToken = tokenStream.get(filterListener.getInterval().a);
    final Token endToken = tokenStream.get(filterListener.getInterval().b);

    final int startLine = startToken.getLine();
    final int endLine = endToken.getLine();

    return getLines(compilationUnit, startLine, endLine);
  }
 /** Listen to matches of methodDeclaration */
 @Override
 public void enterMethodDeclaration(JavaParser.MethodDeclarationContext ctx) {
   // need parser to get tokens
   TokenStream tokens = parser.getTokenStream();
   String type = "void";
   if (ctx.type() != null) {
     type = tokens.getText(ctx.type());
   }
   String args = tokens.getText(ctx.formalParameters());
   System.out.println("\t" + type + " " + ctx.Identifier() + args + ";");
 }
  @Test
  public void testLookback() throws Exception {
    LexerGrammar g =
        new LexerGrammar(
            "lexer grammar t;\n"
                + "ID : 'a'..'z'+;\n"
                + "INT : '0'..'9'+;\n"
                + "SEMI : ';';\n"
                + "ASSIGN : '=';\n"
                + "PLUS : '+';\n"
                + "MULT : '*';\n"
                + "WS : ' '+;\n");
    // Tokens: 012345678901234567
    // Input:  x = 3 * 0 + 2 * 0;
    CharStream input = new ANTLRInputStream("x = 3 * 0 + 2 * 0;");
    LexerInterpreter lexEngine = g.createLexerInterpreter(input);
    TokenStream tokens = createTokenStream(lexEngine);

    tokens.consume(); // get x into buffer
    Token t = tokens.LT(-1);
    assertEquals("x", t.getText());

    tokens.consume();
    tokens.consume(); // consume '='
    t = tokens.LT(-3);
    assertEquals("x", t.getText());
    t = tokens.LT(-2);
    assertEquals(" ", t.getText());
    t = tokens.LT(-1);
    assertEquals("=", t.getText());
  }
  @Test
  public void test2ndToken() throws Exception {
    LexerGrammar g =
        new LexerGrammar(
            "lexer grammar t;\n"
                + "ID : 'a'..'z'+;\n"
                + "INT : '0'..'9'+;\n"
                + "SEMI : ';';\n"
                + "ASSIGN : '=';\n"
                + "PLUS : '+';\n"
                + "MULT : '*';\n"
                + "WS : ' '+;\n");
    // Tokens: 012345678901234567
    // Input:  x = 3 * 0 + 2 * 0;
    CharStream input = new ANTLRInputStream("x = 3 * 0 + 2 * 0;");
    LexerInterpreter lexEngine = g.createLexerInterpreter(input);
    TokenStream tokens = createTokenStream(lexEngine);

    String result = tokens.LT(2).getText();
    String expecting = " ";
    assertEquals(expecting, result);
  }
Esempio n. 7
0
  @Override
  public void exitSql_stmt(Sql_stmtContext ctx) {

    String sql = stream.getText(ctx.getSourceInterval());
    super.exitSql_stmt(ctx);
    if (ctx.insert_stmt() != null) {
      out.println("###-----------Insert statement---------------");
      printtext(ctx.insert_stmt());
      out.println("###-----------Dependency---------------");
      printSubQuery(ctx.insert_stmt().stmt);
      out.println("###-----------Graph---------------");
      os.addFlow(ctx.insert_stmt().stmt);
      out.println("###-----------End-------");
    }

    if (ctx.create_table_stmt() != null) {
      // out.println("SOT---------------");
      // printtext(ctx.create_table_stmt());
      // printSubQuery(ctx.create_table_stmt().stmt);
      // if (ctx.create_table_stmt().insert != null) {
      // out.println("IT+++++++++++++");
      // printSubQuery(ctx.create_table_stmt().insert);
      // } else {
      // out.println("ET-------------");
      // }
      // out.println("EOT---------------");
      if (ctx.create_table_stmt().insert != null) {
        out.println("###-----------create table as xxx statement---------------");
        printtext(ctx.create_table_stmt());
        out.println("###-----------Dependency---------------");
        printSubQuery(ctx.create_table_stmt().insert);
        out.println("###-----------Graph---------------");
        os.addFlow(ctx.create_table_stmt().insert);
        out.println("###-----------End-------");
      }
    }

    if (ctx.create_view_stmt() != null) {
      // out.println("SOV---------------");
      // printtext(ctx.create_view_stmt());
      // printSubQuery(ctx.create_view_stmt().stmt);
      // out.println("IT+++++++++++++");
      // printSubQuery(ctx.create_view_stmt().insert);
      // out.println("EOV---------------");
      out.println("###-----------create view statement---------------");
      printtext(ctx.create_view_stmt());
      out.println("###-----------Dependency---------------");
      printSubQuery(ctx.create_view_stmt().insert);

      out.println("###-----------Graph---------------");
      os.addFlow(ctx.create_view_stmt().insert);
      out.println("###-----------End-------");
    }

    if (ctx.update_stmt() != null) {
      out.println("###-----------update statement---------------");
      printtext(ctx.update_stmt());
      out.println("###-----------Dependency---------------");
      printSubQuery(ctx.update_stmt().q);

      out.println("###-----------Graph---------------");
      os.addFlow(ctx.update_stmt().q);
      out.println("###-----------End-------");
    }
  }
Esempio n. 8
0
 private void printtext(RuleContext ctx) {
   out.println("\"\"\"");
   out.print(stream.getText(ctx.getSourceInterval()));
   out.println(";");
   out.println("\"\"\"");
 }