1

我想遍历 ast 然后输出方法、变量、用户定义类型等。有人可以给我一些示例代码或建议吗?顺便说一句,我只想在遍历叶子节点(没有子节点)时输出,我该怎么办?这是我的代码,希望得到一些建议~^-^

    public class test1 {
/**
 * @param args
 */
public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub
    IParserLogService log = new DefaultLogService();
    String code = FileToString.readFile("C:/Users/badboy/Desktop/程序/A.cpp");
    System.out.println(code);
    CodeReader reader = new CodeReader(code.toCharArray());
    Map definedSymbols = new HashMap();
    String[] includePaths = new String[0];
    IScannerInfo info = new ScannerInfo(definedSymbols, includePaths);
    ICodeReaderFactory readerFactory = FileCodeReaderFactory.getInstance();
    IASTTranslationUnit translationUnit = GPPLanguage.getDefault()
            .getASTTranslationUnit(reader, info, readerFactory, null, log);
    ASTVisitor visitor = new ASTVisitor() {
        public int visit(IASTName name) {
            String prtName = name.toString();
            if (prtName.length() == 0)
                prtName = name.getRawSignature(); // use pre pre-processor
            // value
            System.out.println("Visiting name: " + prtName);
            return PROCESS_CONTINUE;
        }

        public int visit(IASTStatement stmt) { // lots more
            String sig = stmt.getRawSignature();
            if (sig.length() > 0)
                System.out.println("Visiting stmt: "
                        + stmt.getRawSignature());
            else if (stmt instanceof IASTCompoundStatement) {
                IASTCompoundStatement cstmt = (IASTCompoundStatement) stmt;
                IASTStatement[] stmts = cstmt.getStatements();
                System.out.println("Visiting compound stmt with stmts: "
                        + stmts.length);
                for (IASTStatement st : stmts) {
                    String rawSig = st.getRawSignature();

                    if (rawSig.length() == 0) {
                        System.out.println("   ->" + st);
                        if (st instanceof CASTExpressionStatement) {
                            CASTExpressionStatement es = (CASTExpressionStatement) st;
                            IASTExpression exp = es.getExpression();
                            if (exp instanceof IASTBinaryExpression) {
                                IASTBinaryExpression bexp = (IASTBinaryExpression) exp;

                                System.out.println("    binary exp: "
                                        + bexp.getOperand1() + " "
                                        + bexp.getOperator() + " "
                                        + bexp.getOperand2());
                            }
                            String expStr = exp.getRawSignature();
                            IType type = exp.getExpressionType();
                        }
                    } else {
                        System.out.println("   ->" + rawSig);
                    }
                }
            }
            return PROCESS_CONTINUE;
        }

        public int visit(IASTDeclaration decl) {

            System.out.println("Visiting decl: " + decl.getRawSignature());
            return PROCESS_CONTINUE;
        }

        public int visit(IASTComment comm) {
            System.out.println("Visiting comm: " + comm.getRawSignature());
            return PROCESS_CONTINUE;
        }

        public int visit(IASTDeclarator dtor) {
            System.out.println("Visiting dtor: " + dtor.getRawSignature());
            return PROCESS_CONTINUE;
        }

        public int visit(IASTDeclSpecifier dspe) {
            System.out.println("Visiting dspe: " + dspe.getRawSignature());
            return PROCESS_CONTINUE;
        }

        public int visit(IASTEnumerator enmt) {
            System.out.println("Visiting enmt: " + enmt.getRawSignature());
            return PROCESS_CONTINUE;
        }

        public int visit(IASTExpression expr) {
            System.out.println("Visiting expr: " + expr.getRawSignature());
            return PROCESS_CONTINUE;
        }

        public int visit(IASTInitializer init) {
            System.out.println("Visiting init: " + init.getRawSignature());
            return PROCESS_CONTINUE;
        }

        public int visit(IASTParameterDeclaration para) {
            System.out.println("Visiting para: " + para.getRawSignature());
            return PROCESS_CONTINUE;
        }

        public int visit(IASTProblem prob) {
            System.out.println("Visiting prob: " + prob.getRawSignature());
            return PROCESS_CONTINUE;
        }

        public int visit(IASTTranslationUnit tran) {
            System.out.println("Visiting tran: " + tran.getRawSignature());
            return PROCESS_CONTINUE;
        }

        public int visit(IASTTypeId type) {
            System.out.println("Visiting type: " + type.getRawSignature());
            return PROCESS_CONTINUE;
        }


    /*  public int leave(IASTName name) {
            System.out.println("Leaving name " + name.getRawSignature());
            return ASTVisitor.PROCESS_CONTINUE;
        }

        public int leave(IASTStatement stmt) {
            System.out.println(" Leaving stmt: " + stmt.getRawSignature());
            return PROCESS_CONTINUE;
        }


        public int leave(IASTDeclaration decl) {
            System.out.println("Leaving decl: " + decl.getRawSignature());
            return PROCESS_CONTINUE;
        }

        public int leave(IASTComment comm) {
            System.out.println("Leaving comm: " + comm.getRawSignature());
            return PROCESS_CONTINUE;
        }

        public int leave(IASTDeclarator dtor) {
            System.out.println("Leaving dtor: " + dtor.getRawSignature());
            return PROCESS_CONTINUE;
        }

        public int leave(IASTDeclSpecifier dspe) {
            System.out.println("Leaving dspe: " + dspe.getRawSignature());
            return PROCESS_CONTINUE;
        }

        public int leave(IASTEnumerator enmt) {
            System.out.println("Leaving enmt: " + enmt.getRawSignature());
            return PROCESS_CONTINUE;
        }

        public int leave(IASTExpression expr) {
            System.out.println("Leaving expr: " + expr.getRawSignature());
            return PROCESS_CONTINUE;
        }

        public int leave(IASTInitializer init) {
            System.out.println("Leaving init: " + init.getRawSignature());
            return PROCESS_CONTINUE;
        }

        public int leave(IASTParameterDeclaration para) {
            System.out.println("Leaving para: " + para.getRawSignature());
            return PROCESS_CONTINUE;
        }

        public int leave(IASTProblem prob) {
            System.out.println("Leaving prob: " + prob.getRawSignature());
            return PROCESS_CONTINUE;
        }

        public int leave(IASTTranslationUnit tran) {
            System.out.println("Leaving tran: " + tran.getRawSignature());
            return PROCESS_CONTINUE;
        }

        public int leave(IASTTypeId type) {
            System.out.println("Leaving type: " + type.getRawSignature());
            return PROCESS_CONTINUE;
        }*/
    };
    visitor.shouldVisitNames = true;
    visitor.shouldVisitDeclarations = true;
    visitor.shouldVisitStatements = true;
    visitor.shouldVisitComments = true;
    visitor.shouldVisitDeclarators = true;
    visitor.shouldVisitDeclSpecifiers = true;
    visitor.shouldVisitEnumerators = true;
    visitor.shouldVisitExpressions = true;
    visitor.shouldVisitInitializers = true;
    visitor.shouldVisitParameterDeclarations = true;
    visitor.shouldVisitProblems = true;
    visitor.shouldVisitTranslationUnit = true;
    visitor.shouldVisitTypeIds = true;
    translationUnit.accept(visitor);

}

}

4

0 回答 0