From: Svjatoslav Agejenko Date: Sat, 6 Jul 2013 20:27:21 +0000 (+0300) Subject: java syntax parser X-Git-Tag: javainspect-1.1~13 X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=176eff8b9757d8097c5df58782bb4ba8be56b2a5;p=javainspect.git java syntax parser --- diff --git a/src/main/java/eu/svjatoslav/inspector/java/methods/Import.java b/src/main/java/eu/svjatoslav/inspector/java/methods/Import.java new file mode 100644 index 0000000..537b86c --- /dev/null +++ b/src/main/java/eu/svjatoslav/inspector/java/methods/Import.java @@ -0,0 +1,8 @@ +package eu.svjatoslav.inspector.java.methods; + +public class Import { + + String path; + boolean isStatic = false; + +} diff --git a/src/main/java/eu/svjatoslav/inspector/java/methods/JavaFile.java b/src/main/java/eu/svjatoslav/inspector/java/methods/JavaFile.java index 65cf3bb..d517537 100644 --- a/src/main/java/eu/svjatoslav/inspector/java/methods/JavaFile.java +++ b/src/main/java/eu/svjatoslav/inspector/java/methods/JavaFile.java @@ -1,17 +1,83 @@ package eu.svjatoslav.inspector.java.methods; +import java.io.BufferedReader; import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import eu.svjatoslav.inspector.tokenizer.Tokenizer; +import eu.svjatoslav.inspector.tokenizer.TokenizerMatch; public class JavaFile { + private final List imports = new ArrayList(); + private final File file; - public JavaFile(final File file) { + StringBuffer contents = new StringBuffer(); + + public JavaFile(final File file) throws IOException { this.file = file; + parse(); + } + + public void parse() throws IOException { + System.out.println("java file: " + file); + + readFile(); + + final Tokenizer tokenizer = new Tokenizer(contents.toString()); + tokenizer.addTerminator(" ", true); + tokenizer.addTerminator("\t", true); + tokenizer.addTerminator("\n", true); + + tokenizer.addTerminator(";", false); + + while (true) { + final TokenizerMatch match = tokenizer.getToken(); + if (match == null) + break; + + if (match.token.equals("import")) + parseImport(tokenizer); + } + + } + + private void parseImport(final Tokenizer tokenizer) { + final Import imp = new Import(); + + final TokenizerMatch match = tokenizer.getToken(); + + if (match.token.equals("static")) { + imp.isStatic = true; + imp.path = tokenizer.getToken().token; + } else + imp.path = match.token; + + imports.add(imp); } - public void parse() { + private void readFile() throws FileNotFoundException, IOException { + final FileReader fileReader = new FileReader(file); + + final BufferedReader bufferedReader = new BufferedReader(fileReader); + + while (true) { + final String line = bufferedReader.readLine(); + + if (line == null) + break; + + contents.append(line); + contents.append("\n"); + } + bufferedReader.close(); + fileReader.close(); } } diff --git a/src/main/java/eu/svjatoslav/inspector/java/methods/Project.java b/src/main/java/eu/svjatoslav/inspector/java/methods/Project.java index 5387cf4..2688e78 100644 --- a/src/main/java/eu/svjatoslav/inspector/java/methods/Project.java +++ b/src/main/java/eu/svjatoslav/inspector/java/methods/Project.java @@ -1,6 +1,7 @@ package eu.svjatoslav.inspector.java.methods; import java.io.File; +import java.io.IOException; import eu.svjatoslav.commons.file.FilePathParser; @@ -42,10 +43,13 @@ public class Project { public void parseFile(final File file) { final String fileExtension = FilePathParser.getFileExtension(file); - if ("java".equalsIgnoreCase(fileExtension)){ - JavaFile javaFile = new JavaFile(file); - // oeu - } + if ("java".equalsIgnoreCase(fileExtension)) + try { + final JavaFile javaFile = new JavaFile(file); + } catch (final IOException e) { + System.out.println("Error parsing file: " + file.toString() + + ", " + e.toString()); + } } } diff --git a/src/main/java/eu/svjatoslav/inspector/tokenizer/Terminator.java b/src/main/java/eu/svjatoslav/inspector/tokenizer/Terminator.java new file mode 100644 index 0000000..ae49c41 --- /dev/null +++ b/src/main/java/eu/svjatoslav/inspector/tokenizer/Terminator.java @@ -0,0 +1,12 @@ +package eu.svjatoslav.inspector.tokenizer; + +public class Terminator { + + String value; + boolean empty; + + public Terminator(final String value, final boolean empty) { + this.value = value; + this.empty = empty; + } +} diff --git a/src/main/java/eu/svjatoslav/inspector/tokenizer/Tokenizer.java b/src/main/java/eu/svjatoslav/inspector/tokenizer/Tokenizer.java new file mode 100644 index 0000000..97f1224 --- /dev/null +++ b/src/main/java/eu/svjatoslav/inspector/tokenizer/Tokenizer.java @@ -0,0 +1,69 @@ +package eu.svjatoslav.inspector.tokenizer; + +import java.util.ArrayList; +import java.util.List; + +public class Tokenizer { + + private final List terminators = new ArrayList(); + private final String source; + + private int currentIndex = 0; + + public Tokenizer(final String source) { + this.source = source; + } + + public void addTerminator(final String terminator, final boolean empty) { + + terminators.add(new Terminator(terminator, empty)); + } + + public TokenizerMatch getToken() { + final StringBuffer result = new StringBuffer(); + + while (true) { + if (currentIndex >= source.length()) + return null; + + boolean accumulateCurrentChar = true; + + findTerminator: for (final Terminator terminator : terminators) + if (terminatorMatches(terminator)) + // empty space detected + if (terminator.empty) { + currentIndex += terminator.value.length(); + if (result.length() > 0) + return new TokenizerMatch(result.toString(), + terminator); + else { + accumulateCurrentChar = false; + break findTerminator; + } + } else if (result.length() > 0) + return new TokenizerMatch(result.toString(), terminator); + else { + currentIndex += terminator.value.length(); + return new TokenizerMatch(terminator.value, terminator); + } + + if (accumulateCurrentChar) { + result.append(source.charAt(currentIndex)); + currentIndex++; + } + } + + } + + public boolean terminatorMatches(final Terminator terminator) { + if ((currentIndex + terminator.value.length()) > source.length()) + return false; + + for (int i = 0; i < terminator.value.length(); i++) + if (terminator.value.charAt(i) != source.charAt(i + currentIndex)) + return false; + + return true; + } + +} diff --git a/src/main/java/eu/svjatoslav/inspector/tokenizer/TokenizerMatch.java b/src/main/java/eu/svjatoslav/inspector/tokenizer/TokenizerMatch.java new file mode 100644 index 0000000..158a9d9 --- /dev/null +++ b/src/main/java/eu/svjatoslav/inspector/tokenizer/TokenizerMatch.java @@ -0,0 +1,12 @@ +package eu.svjatoslav.inspector.tokenizer; + +public class TokenizerMatch { + + public String token; + public Terminator terminator; + + public TokenizerMatch(final String token, final Terminator terminator) { + this.token = token; + this.terminator = terminator; + } +}