Created
February 22, 2016 10:03
-
-
Save oguna/937fd65bda4f955b7b4e to your computer and use it in GitHub Desktop.
PMDを用いたJavaソースコードのトークン化とASTツリー生成
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import net.sourceforge.pmd.lang.ParserOptions; | |
| import net.sourceforge.pmd.lang.ast.JavaCharStream; | |
| import net.sourceforge.pmd.lang.java.Java18Parser; | |
| import net.sourceforge.pmd.lang.java.ast.*; | |
| import java.io.BufferedReader; | |
| import java.io.IOException; | |
| import java.nio.file.Files; | |
| import java.nio.file.Path; | |
| import java.nio.file.Paths; | |
| public class ManyTokenizer { | |
| static class SimpleJavaParserVisitor extends JavaParserVisitorAdapter { | |
| int depth = 0; | |
| @Override | |
| public Object visit(JavaNode node, Object data) { | |
| for (int i=0; i<depth; i++) { | |
| System.out.print(" "); | |
| } | |
| System.out.println(node.toString()); | |
| depth++; | |
| Object returnValue = super.visit(node, data); | |
| depth--; | |
| return returnValue; | |
| } | |
| } | |
| public static void main(String[] args) throws IOException { | |
| Path path = Paths.get("src/main/java/ManyTokenizer.java"); | |
| try (BufferedReader br = Files.newBufferedReader(path)) { | |
| JavaCharStream javaCharStream = new JavaCharStream(br); | |
| JavaParserTokenManager javaParserTokenManager = new JavaParserTokenManager(javaCharStream); | |
| Token token; | |
| while ((token = javaParserTokenManager.getNextToken()).kind > 0) { | |
| String desc = String.format("%4d:%-4d %s", token.beginLine, token.beginColumn, token.image); | |
| System.out.println(desc); | |
| } | |
| } | |
| try (BufferedReader br = Files.newBufferedReader(path)) { | |
| ParserOptions options = new ParserOptions(); | |
| Java18Parser java18Parser = new Java18Parser(options); | |
| ASTCompilationUnit unit = (ASTCompilationUnit) java18Parser.parse(path.getFileName().toString(), br); | |
| SimpleJavaParserVisitor visitor = new SimpleJavaParserVisitor(); | |
| visitor.visit(unit, null); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment