recognize java comments
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Thu, 11 Jul 2013 21:48:10 +0000 (00:48 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Thu, 11 Jul 2013 21:48:10 +0000 (00:48 +0300)
.classpath
src/main/java/eu/svjatoslav/inspector/java/methods/ClassReference.java
src/main/java/eu/svjatoslav/inspector/java/methods/Clazz.java
src/main/java/eu/svjatoslav/inspector/java/methods/JavaFile.java
src/main/java/eu/svjatoslav/inspector/java/methods/Modifiers.java
src/main/java/eu/svjatoslav/inspector/tokenizer/Terminator.java
src/main/java/eu/svjatoslav/inspector/tokenizer/Tokenizer.java

index fd7ad7f..5adac7b 100644 (file)
@@ -6,19 +6,19 @@
                        <attribute name="maven.pomderived" value="true"/>
                </attributes>
        </classpathentry>
-       <classpathentry kind="src" output="target/test-classes" path="src/test/java">
+       <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
                <attributes>
-                       <attribute name="optional" value="true"/>
                        <attribute name="maven.pomderived" value="true"/>
                </attributes>
        </classpathentry>
-       <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
+       <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
                <attributes>
                        <attribute name="maven.pomderived" value="true"/>
                </attributes>
        </classpathentry>
-       <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
+       <classpathentry kind="src" output="target/test-classes" path="src/test/java">
                <attributes>
+                       <attribute name="optional" value="true"/>
                        <attribute name="maven.pomderived" value="true"/>
                </attributes>
        </classpathentry>
index 43d7625..1a943f5 100644 (file)
@@ -14,20 +14,20 @@ public class ClassReference {
 
        public ClassReference(final Tokenizer tokenizer)
                        throws InvalidSyntaxException {
-               name = tokenizer.getToken().token;
+               name = tokenizer.getNextToken().token;
 
-               if (!tokenizer.isNextToken("<"))
+               if (!tokenizer.probeNextToken("<"))
                        return;
 
                while (true) {
                        final ClassReference parameterType = new ClassReference(tokenizer);
                        typeParameters.add(parameterType);
 
-                       if (!tokenizer.isNextToken(","))
+                       if (!tokenizer.probeNextToken(","))
                                break;
                }
 
-               tokenizer.expectToken(">");
+               tokenizer.expectNextToken(">");
        }
 
        @Override
index fda73d1..3c5333b 100644 (file)
@@ -11,17 +11,21 @@ public class Clazz {
 
        private final String packageName;
        private final String className;
+       private final boolean isInterface;
 
        public ClassReference superClass;
        public List<ClassReference> implementedInterfaces = new ArrayList<ClassReference>();
 
        public Clazz(final String packageName, final String className,
-                       final Tokenizer tokenizer) throws InvalidSyntaxException {
+                       final Tokenizer tokenizer, final boolean isInterface)
+                       throws InvalidSyntaxException {
+
                this.packageName = packageName;
                this.className = className;
+               this.isInterface = isInterface;
 
                while (true) {
-                       final TokenizerMatch match = tokenizer.getToken();
+                       final TokenizerMatch match = tokenizer.getNextToken();
 
                        if ("extends".equals(match.token)) {
                                superClass = new ClassReference(tokenizer);
@@ -32,7 +36,7 @@ public class Clazz {
                                while (true) {
                                        implementedInterfaces.add(new ClassReference(tokenizer));
 
-                                       if (tokenizer.isNextToken(","))
+                                       if (tokenizer.probeNextToken(","))
                                                continue;
 
                                        break;
@@ -49,14 +53,21 @@ public class Clazz {
        }
 
        public void parseClassBody(final Tokenizer tokenizer) {
-               tokenizer.skipUtilEnd();
+               tokenizer.skipUntilDataEnd();
        }
 
        @Override
        public String toString() {
                final EnumerationBuffer result = new EnumerationBuffer();
 
-               result.append(packageName + " -> " + className + "\n");
+               result.append(packageName + " -> " + className + " ");
+
+               if (isInterface)
+                       result.append("(interface)");
+               else
+                       result.append("(class)");
+               result.append("\n");
+
                if (superClass != null)
                        result.append("    super: " + superClass.toString() + "\n");
 
index 400ff87..ea05f01 100644 (file)
@@ -33,6 +33,8 @@ public class JavaFile {
                readFile();
 
                final Tokenizer tokenizer = new Tokenizer(contents.toString());
+
+               // empty space
                tokenizer.addTerminator(" ", true);
                tokenizer.addTerminator("\t", true);
                tokenizer.addTerminator("\n", true);
@@ -48,10 +50,14 @@ public class JavaFile {
                tokenizer.addTerminator(">", false);
                tokenizer.addTerminator(",", false);
 
+               // comments
+               tokenizer.addTerminator("//", "\n", true);
+               tokenizer.addTerminator("/*", "*/", true);
+
                final Modifiers modifiers = new Modifiers();
 
                while (true) {
-                       final TokenizerMatch match = tokenizer.getToken();
+                       final TokenizerMatch match = tokenizer.getNextToken();
                        if (match == null)
                                break;
 
@@ -74,6 +80,11 @@ public class JavaFile {
                                continue;
                        }
 
+                       if ("interface".equals(match.token)) {
+                               parseInterface(tokenizer);
+                               continue;
+                       }
+
                        System.out.println("    " + modifiers.toString() + " "
                                        + match.token);
                        modifiers.reset();
@@ -85,10 +96,10 @@ public class JavaFile {
        private void parseClass(final Tokenizer tokenizer)
                        throws InvalidSyntaxException {
 
-               final TokenizerMatch match = tokenizer.getToken();
-               final Clazz clazz = new Clazz(packageName, match.token, tokenizer);
+               final TokenizerMatch match = tokenizer.getNextToken();
+               final Clazz clazz = new Clazz(packageName, match.token, tokenizer,
+                               false);
                System.out.println(clazz.toString());
-
        }
 
        private void parseImport(final Tokenizer tokenizer)
@@ -96,27 +107,35 @@ public class JavaFile {
 
                final Import imp = new Import();
 
-               final TokenizerMatch match = tokenizer.getToken();
+               final TokenizerMatch match = tokenizer.getNextToken();
 
                if (match.token.equals("static")) {
                        imp.isStatic = true;
-                       imp.path = tokenizer.getToken().token;
+                       imp.path = tokenizer.getNextToken().token;
                } else
                        imp.path = match.token;
 
                imports.add(imp);
 
-               tokenizer.expectToken(";");
+               tokenizer.expectNextToken(";");
+       }
+
+       private void parseInterface(final Tokenizer tokenizer)
+                       throws InvalidSyntaxException {
+
+               final TokenizerMatch match = tokenizer.getNextToken();
+               final Clazz clazz = new Clazz(packageName, match.token, tokenizer, true);
+               System.out.println(clazz.toString());
        }
 
        private void parsePackage(final Tokenizer tokenizer)
                        throws InvalidSyntaxException {
 
-               final TokenizerMatch match = tokenizer.getToken();
+               final TokenizerMatch match = tokenizer.getNextToken();
 
                packageName = match.token;
 
-               tokenizer.expectToken(";");
+               tokenizer.expectNextToken(";");
        }
 
        private void readFile() throws FileNotFoundException, IOException {
@@ -140,7 +159,11 @@ public class JavaFile {
 
        public void skipUntilSemicolon(final Tokenizer tokenizer) {
                while (true) {
-                       final TokenizerMatch token = tokenizer.getToken();
+                       final TokenizerMatch token = tokenizer.getNextToken();
+
+                       if (token == null)
+                               return;
+
                        if (token.token.equals(";"))
                                return;
                }
index 747d7db..947b3f4 100644 (file)
@@ -19,6 +19,8 @@ public class Modifiers {
 
        boolean isFinal = false;
 
+       boolean isAbstract = false;
+
        public boolean parseModifier(final String string) {
                for (final Access access : Access.values())
                        if (access.name.equals(string)) {
@@ -36,6 +38,11 @@ public class Modifiers {
                        return true;
                }
 
+               if ("abstract".equals(string)) {
+                       isAbstract = true;
+                       return true;
+               }
+
                return false;
        }
 
index ae49c41..8848775 100644 (file)
@@ -2,11 +2,21 @@ package eu.svjatoslav.inspector.tokenizer;
 
 public class Terminator {
 
-       String value;
-       boolean empty;
+       String startSequence;
+       String endSequence;
+       boolean ignoreTerminator;
 
-       public Terminator(final String value, final boolean empty) {
-               this.value = value;
-               this.empty = empty;
+       public Terminator(final String startPattern, final boolean ignoreTerminator) {
+               this.startSequence = startPattern;
+               this.ignoreTerminator = ignoreTerminator;
        }
+
+       public Terminator(final String startSequence, final String endSequence,
+                       final boolean ignoreTerminator) {
+
+               this.startSequence = startSequence;
+               this.endSequence = endSequence;
+               this.ignoreTerminator = ignoreTerminator;
+       }
+
 }
index 8a42776..ec13b1a 100644 (file)
@@ -17,19 +17,26 @@ public class Tokenizer {
                this.source = source;
        }
 
-       public void addTerminator(final String terminator, final boolean empty) {
+       public void addTerminator(final String startSequence,
+                       final boolean ignoreTerminator) {
+               terminators.add(new Terminator(startSequence, ignoreTerminator));
+       }
 
-               terminators.add(new Terminator(terminator, empty));
+       public void addTerminator(final String startSequence,
+                       final String endSequence, final boolean ignoreTerminator) {
+               terminators.add(new Terminator(startSequence, endSequence,
+                               ignoreTerminator));
        }
 
-       public void expectToken(final String value) throws InvalidSyntaxException {
-               final TokenizerMatch match = getToken();
+       public void expectNextToken(final String value)
+                       throws InvalidSyntaxException {
+               final TokenizerMatch match = getNextToken();
                if (!value.equals(match.token))
                        throw new InvalidSyntaxException("Expected \"" + value
                                        + "\" but got \"" + match.token + "\" instead.");
        }
 
-       public TokenizerMatch getToken() {
+       public TokenizerMatch getNextToken() {
                tokenIndexes.push(currentIndex);
                final StringBuffer result = new StringBuffer();
 
@@ -40,10 +47,14 @@ public class Tokenizer {
                        boolean accumulateCurrentChar = true;
 
                        findTerminator: for (final Terminator terminator : terminators)
-                               if (terminatorMatches(terminator))
-                                       // empty space detected
-                                       if (terminator.empty) {
-                                               currentIndex += terminator.value.length();
+                               if (sequenceMatches(terminator.startSequence))
+
+                                       if (terminator.ignoreTerminator) {
+                                               currentIndex += terminator.startSequence.length();
+
+                                               if (terminator.endSequence != null)
+                                                       skipUntilSequence(terminator.endSequence);
+
                                                if (result.length() > 0)
                                                        return new TokenizerMatch(result.toString(),
                                                                        terminator);
@@ -54,8 +65,9 @@ public class Tokenizer {
                                        } else if (result.length() > 0)
                                                return new TokenizerMatch(result.toString(), terminator);
                                        else {
-                                               currentIndex += terminator.value.length();
-                                               return new TokenizerMatch(terminator.value, terminator);
+                                               currentIndex += terminator.startSequence.length();
+                                               return new TokenizerMatch(terminator.startSequence,
+                                                               terminator);
                                        }
 
                        if (accumulateCurrentChar) {
@@ -66,32 +78,43 @@ public class Tokenizer {
 
        }
 
-       public boolean isNextToken(final String token) {
-               if (token.equals(getToken().token))
+       public boolean probeNextToken(final String token) {
+               if (token.equals(getNextToken().token))
                        return true;
 
-               rollbackToken();
+               unreadToken();
                return false;
        }
 
-       public void rollbackToken() {
-               currentIndex = tokenIndexes.pop();
+       public boolean sequenceMatches(final String sequence) {
+               if ((currentIndex + sequence.length()) > source.length())
+                       return false;
+
+               for (int i = 0; i < sequence.length(); i++)
+                       if (sequence.charAt(i) != source.charAt(i + currentIndex))
+                               return false;
+
+               return true;
        }
 
-       public void skipUtilEnd() {
+       public void skipUntilDataEnd() {
                tokenIndexes.push(currentIndex);
                currentIndex = source.length();
        }
 
-       public boolean terminatorMatches(final Terminator terminator) {
-               if ((currentIndex + terminator.value.length()) > source.length())
-                       return false;
+       public void skipUntilSequence(final String sequence) {
+               while (currentIndex < source.length()) {
+                       if (sequenceMatches(sequence)) {
+                               currentIndex += sequence.length();
+                               return;
+                       }
 
-               for (int i = 0; i < terminator.value.length(); i++)
-                       if (terminator.value.charAt(i) != source.charAt(i + currentIndex))
-                               return false;
+                       currentIndex++;
+               }
+       }
 
-               return true;
+       public void unreadToken() {
+               currentIndex = tokenIndexes.pop();
        }
 
 }