Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.56 KB | None | 0 0
  1. import org.eclipse.cdt.core.dom.ast.*;
  2. import org.eclipse.cdt.internal.core.dom.parser.ASTAmbiguousNode;
  3. import org.eclipse.cdt.internal.core.dom.parser.c.CASTBinaryExpression;
  4. import org.eclipse.cdt.internal.core.dom.parser.c.CASTDeclarator;
  5. import org.eclipse.cdt.internal.core.dom.parser.c.CASTExpressionStatement;
  6.  
  7. import java.lang.reflect.Array;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10.  
  11. public class MyVisitor extends ASTGenericVisitor {
  12.     public MyVisitor(boolean visitNodes) {
  13.         super(visitNodes);
  14.     }
  15.  
  16.     @Override
  17.     public int visit(IASTDeclaration declaration) {
  18.         return super.visit(declaration);
  19.     }
  20.  
  21.     private void printRaw(IASTNode node) {
  22.         System.out.println("::::\n" + node.getRawSignature() + " <<< " + node.getClass().toString());
  23.         for (IASTNode node1:node.getChildren()) {
  24.             printRaw(node1);
  25.         }
  26.     }
  27.  
  28. //    @Override
  29. //    public int visit(IASTStatement statement) {
  30. //        //System.out.println("::::\n"+statement.getRawSignature());
  31. //        return super.visit(statement);
  32. //    }
  33.  
  34.     @Override
  35.     public int visit(IASTName name) {
  36.         //System.out.println("::::\n"+name.getRawSignature());
  37.         return super.visit(name);
  38.     }
  39.  
  40.     /*@Override
  41.     public int visit(IASTToken token) {
  42.         System.out.println("::::\n"+token.getRawSignature());
  43.         return super.visit(token);
  44.     }*/
  45.  
  46.     @Override
  47.     public int visit(IASTTranslationUnit tu) {
  48.         //System.out.println("::::\n"+tu.getRawSignature());
  49.         return super.visit(tu);
  50.     }
  51.  
  52.     @Override
  53.     public int visit(IASTStatement statement) {
  54.         HashMap<String, ArrayList<String>> nodeMap = new HashMap<String, ArrayList<String>>();
  55.         for (IASTNode iastNode:statement.getChildren()) {
  56.             if (iastNode instanceof CASTBinaryExpression) {
  57.                 ArrayList<String> nodeList = nodeMap.get(iastNode.getParent().getRawSignature());
  58.                 if (nodeList == null) {
  59.                     ArrayList<String> list = new ArrayList<String>();
  60.                     list.add(iastNode.getRawSignature());
  61.                     nodeMap.put(iastNode.getParent().getRawSignature(), list);
  62.                 } else {
  63.                     nodeList.add(iastNode.getRawSignature());
  64.                 }
  65.             }
  66. //            System.out.println("::::\n"+iastNode.getRawSignature() + " <<< " + iastNode.getClass().toString());
  67.         }
  68.         int i = 0;
  69.         for (String parent : nodeMap.keySet()) {
  70.             System.out.println("In position i = "+i+" we have the nodes: ");
  71.             for (String s : nodeMap.get(parent)) {
  72.                 System.out.println("-> "+s);
  73.             }
  74.             i++;
  75.         }
  76.         return super.visit(statement);
  77.     }
  78.  
  79.     @Override
  80.     protected int genericVisit(IASTNode node) {
  81.         for (IASTNode n:node.getChildren()) {
  82.             if (n instanceof  IASTWhileStatement) {
  83.                 for (IASTNode whileNode:n.getChildren()) {
  84.                     if (whileNode instanceof  IASTBinaryExpression) {
  85. //                        System.out.println(whileNode.getRawSignature());
  86.                     }
  87.                 }
  88.             }
  89.             if (n instanceof  IASTBinaryExpression) {
  90.                 if (n.getParent() instanceof IASTWhileStatement) {
  91. //                    System.out.println(n.getRawSignature());
  92.                 }
  93.             }
  94. //            System.out.println("::::\n"+node.getRawSignature() + " <<< " + node.getClass().toString());
  95.         }
  96.         return super.genericVisit(node);
  97.     }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement