コード例 #1
0
ファイル: DefinitionVisitor.java プロジェクト: ElliotCP/701A2
 public void visit(ReturnStmt n, Object arg) {
   if (n.getExpr() != null) {
     n.getExpr().accept(this, arg);
   }
 }
コード例 #2
0
 /**
  * Highly recursive method that will parse a file into its semantical statements. Currently
  * identifies all often used Statements (if, for, while, try, etc.). Also every other non-flow
  * dependant statement like variable assignments, method calls, etc are considered Expression
  * Statements. As they are "primitive" in terms that cannot contain inner expressions, we just use
  * the default name of the class for them as the Token Type.
  *
  * <p>For more information and examples on how this parser actually works, please refer to the
  * documentation.
  *
  * @param stmt
  */
 private void parseStatement(Statement stmt) {
   // full list of all statements:
   // https://java.net/projects/jpjavaparser/sources/source-code-repository/show/src/japa/parser/ast/stmt
   if (stmt == null) {
     return;
   }
   // Block Statement is a List of statements
   if (stmt instanceof BlockStmt) {
     BlockStmt blockStmt = (BlockStmt) stmt;
     for (Statement st : blockStmt.getStmts()) {
       parseStatement(st);
     }
     // If Statement
   } else if (stmt instanceof IfStmt) {
     IfStmt ifStmt = (IfStmt) stmt;
     tokens.add(new Token(ifStmt.getCondition().toString(), getObjectType(ifStmt)));
     parseStatement(ifStmt.getThenStmt());
     parseStatement(ifStmt.getElseStmt());
     // For statement
   } else if (stmt instanceof ForStmt) {
     ForStmt forStmt = (ForStmt) stmt;
     if (forStmt.getInit() != null
         && forStmt.getCompare() != null
         && forStmt.getUpdate() != null) {
       tokens.add(
           new Token(
               forStmt.getInit().toString()
                   + ";"
                   + forStmt.getCompare().toString()
                   + ";"
                   + forStmt.getUpdate().toString(),
               getObjectType(forStmt)));
     } else {
       tokens.add(new Token("For", getObjectType(forStmt)));
     }
     parseStatement(forStmt.getBody());
     // Foreach Statement
   } else if (stmt instanceof ForeachStmt) {
     ForeachStmt foreachStmt = (ForeachStmt) stmt;
     tokens.add(
         new Token(
             foreachStmt.getVariable().toString() + ":" + foreachStmt.getIterable().toString(),
             getObjectType(foreachStmt)));
     parseStatement(foreachStmt.getBody());
     // While Statement
   } else if (stmt instanceof WhileStmt) {
     WhileStmt whileStmt = (WhileStmt) stmt;
     tokens.add(new Token(whileStmt.getCondition().toString(), getObjectType(whileStmt)));
     parseStatement(whileStmt.getBody());
     // Do Statement
   } else if (stmt instanceof DoStmt) {
     DoStmt doStmt = (DoStmt) stmt;
     tokens.add(new Token(doStmt.getCondition().toString(), getObjectType(doStmt)));
     parseStatement(doStmt.getBody());
     // Try Statement
   } else if (stmt instanceof TryStmt) {
     TryStmt tryStmt = (TryStmt) stmt;
     tokens.add(new Token("Try", getObjectType(tryStmt)));
     parseStatement(tryStmt.getTryBlock());
     // Return Statement
   } else if (stmt instanceof ReturnStmt) {
     ReturnStmt returnStmt = (ReturnStmt) stmt;
     if (returnStmt.getExpr() != null) {
       tokens.add(new Token(returnStmt.getExpr().toString(), getObjectType(returnStmt)));
     } else {
       tokens.add(new Token("Void", getObjectType(returnStmt)));
     }
     // Empty Statement
   } else if (stmt instanceof EmptyStmt) {
     tokens.add(new Token("", getObjectType(stmt)));
     // Continue Statement
   } else if (stmt instanceof ContinueStmt) {
     tokens.add(new Token("Continue", getObjectType(stmt)));
     // Break Statement
   } else if (stmt instanceof BreakStmt) {
     tokens.add(new Token("Break", getObjectType(stmt)));
     // Explicit Constructor Invocation Statement
   } else if (stmt instanceof ExplicitConstructorInvocationStmt) {
     ExplicitConstructorInvocationStmt eciStmt = (ExplicitConstructorInvocationStmt) stmt;
     tokens.add(new Token(eciStmt.getExpr().toString(), getObjectType(eciStmt)));
     // Expression Statement
   } else if (stmt instanceof ExpressionStmt) {
     ExpressionStmt exprStmt = (ExpressionStmt) stmt;
     tokens.add(new Token(exprStmt.toString(), getObjectType(exprStmt.getExpression())));
   } else {
     // this should never be called
     tokens.add(new Token(stmt.toString(), "OTHER"));
   }
 }