Просмотр исходного кода

Clean-up the interfaces the frontend utilities and add more JDoc comments.

Timur Sağlam 4 лет назад
Родитель
Сommit
c12e1cee63
21 измененных файлов с 102 добавлено и 56 удалено
  1. 16 7
      jplag.frontend-utils/src/main/java/de/jplag/AbstractParser.java
  2. 4 1
      jplag.frontend-utils/src/main/java/de/jplag/Language.java
  3. 63 27
      jplag.frontend-utils/src/main/java/de/jplag/Token.java
  4. 1 1
      jplag.frontend-utils/src/main/java/de/jplag/TokenConstants.java
  5. 2 1
      jplag.frontend-utils/src/main/java/de/jplag/TokenHashMap.java
  6. 5 1
      jplag.frontend-utils/src/main/java/de/jplag/TokenList.java
  7. 1 1
      jplag.frontend.chars/src/main/java/de/jplag/chars/Language.java
  8. 0 1
      jplag.frontend.chars/src/main/java/de/jplag/chars/Parser.java
  9. 1 1
      jplag.frontend.cpp/src/main/java/de/jplag/cpp/Language.java
  10. 0 1
      jplag.frontend.cpp/src/main/java/de/jplag/cpp/Scanner.java
  11. 1 1
      jplag.frontend.csharp-1.2/src/main/java/de/jplag/csharp/Language.java
  12. 0 1
      jplag.frontend.csharp-1.2/src/main/java/de/jplag/csharp/Parser.java
  13. 1 1
      jplag.frontend.java/src/main/java/de/jplag/java/Language.java
  14. 0 1
      jplag.frontend.java/src/main/java/de/jplag/java/Parser.java
  15. 1 1
      jplag.frontend.python-3/src/main/java/de/jplag/python3/Language.java
  16. 0 1
      jplag.frontend.python-3/src/main/java/de/jplag/python3/Parser.java
  17. 1 1
      jplag.frontend.scheme/src/main/java/de/jplag/scheme/Language.java
  18. 0 1
      jplag.frontend.scheme/src/main/java/de/jplag/scheme/Parser.java
  19. 1 1
      jplag.frontend.text/src/main/java/de/jplag/text/Language.java
  20. 0 1
      jplag.frontend.text/src/main/java/de/jplag/text/Parser.java
  21. 4 4
      jplag/src/main/java/de/jplag/GreedyStringTiling.java

+ 16 - 7
jplag.frontend-utils/src/main/java/de/jplag/AbstractParser.java

@@ -1,29 +1,38 @@
 package de.jplag;
 
 /**
+ * Abstract parser class. Counts errors and manages an error consumer.
  * @author Emeric Kwemou
  */
 public abstract class AbstractParser {
     protected ErrorConsumer errorConsumer;
     protected int errors = 0;
-    private int numberOfErrors = 0;
 
+    /**
+     * @return true if there currently are some errors.
+     */
     public boolean hasErrors() {
         return errors != 0;
     }
 
-    public int errorsCount() {
-        return numberOfErrors;
-    }
-
-    protected void parseEnd() {
-        numberOfErrors += errors;
+    /**
+     * @return number of total errors.
+     */
+    public int errorCount() {
+        return errors;
     }
 
+    /**
+     * @return the error consumer that collects and prints errors.
+     */
     public ErrorConsumer getErrorConsumer() {
         return errorConsumer;
     }
 
+    /**
+     * Setter for the error consumer that collects and prints errors.
+     * @param errorConsumer is the consumer to set.
+     */
     public void setErrorConsumer(ErrorConsumer errorConsumer) {
         this.errorConsumer = errorConsumer;
     }

+ 4 - 1
jplag.frontend-utils/src/main/java/de/jplag/Language.java

@@ -29,8 +29,11 @@ public interface Language {
 
     /**
      * Parses a set files in a directory.
+     * @param directory is the directory where the files are located.
+     * @param files are the names of the files to parse.
+     * @return the list of parsed JPlag tokens.
      */
-    TokenList parse(File dir, String[] files);
+    TokenList parse(File directory, String[] files);
 
     /**
      * Whether errors were found during the last {@link #parse}.

+ 63 - 27
jplag.frontend-utils/src/main/java/de/jplag/Token.java

@@ -13,7 +13,7 @@ public abstract class Token {
 
     private boolean marked;
     private boolean basecode = false;
-    private int hash = -1;// hash-value. set and used by main algorithm (GSTiling)
+    private int hash = -1; // hash-value. set and used by main algorithm (GSTiling)
 
     protected int type;
 
@@ -43,12 +43,19 @@ public abstract class Token {
         this.length = length;
     }
 
-    public String getFile() {
-        return file;
+    /**
+     * Returns the character index which denotes where the code sections represented by this token starts in the line.
+     * @return the character index in the line.
+     */
+    public int getColumn() {
+        return column;
     }
 
-    public int getHash() {
-        return hash;
+    /**
+     * @return the name of the file where the source code that the token represents is located in.
+     */
+    public String getFile() {
+        return file;
     }
 
     // this is made to distinguish the character front end. Maybe other front ends can use it too?
@@ -56,59 +63,88 @@ public abstract class Token {
         return -1;
     }
 
-    public int getLine() {
-        return line;
-    }
-
-    public int getColumn() {
-        return column;
-    }
-
+    /**
+     * Gives the length if the code sections represented by this token.
+     * @return the length in characters.
+     */
     public int getLength() {
         return length;
     }
 
-    public boolean isBasecode() {
-        return basecode;
-    }
-
-    public boolean isMarked() {
-        return marked;
-    }
-
-    public void setLine(int line) {
-        this.line = line;
+    /**
+     * Gives the line index denoting in which line the code sections represented by this token starts.
+     * @return the line index.
+     */
+    public int getLine() {
+        return line;
     }
 
+    /**
+     * Sets the character index which denotes where the code sections represented by this token starts in the line.
+     * @param column is the index in characters to set.
+     */
     public void setColumn(int column) {
         this.column = column;
     }
 
+    /**
+     * Sets the length if the code sections represented by this token.
+     * @param length is the length in characters to set.
+     */
     public void setLength(int length) {
         this.length = length;
     }
 
+    /**
+     * Sets the line index denoting in which line the code sections represented by this token starts.
+     * @param line is the line index to set.
+     */
+    public void setLine(int line) {
+        this.line = line;
+    }
+
     @Override
     public String toString() {
         return type2string();
     }
 
+    /**
+     * @return a string representation depending on the type of the token.
+     */
     protected abstract String type2string();
 
-    /* Package Private */ boolean setBasecode(boolean basecode) {
+    /* package-private */ int getHash() {
+        return hash;
+    }
+
+    /**
+     * @return whether this token is part of a basecode.
+     */
+    /* package-private */ boolean isBasecode() {
+        return basecode;
+    }
+
+    /**
+     * @return whether this token is marked by the comparison algorithm.
+     */
+    /* package-private */ boolean isMarked() {
+        return marked;
+    }
+
+    /* package-private */ boolean setBasecode(boolean basecode) {
         this.basecode = basecode;
         return basecode;
     }
 
-    /* Package Private */ void setFile(String file) {
+    /* package-private */ void setFile(String file) {
         this.file = file;
     }
 
-    /* Package Private */ void setHash(int hash) {
+    /* package-private */ void setHash(int hash) {
         this.hash = hash;
     }
 
-    /* Package Private */ boolean setMarked(boolean marked) {
+    /* package-private */ boolean setMarked(boolean marked) {
         this.marked = marked;
         return marked;
     }

+ 1 - 1
jplag.frontend-utils/src/main/java/de/jplag/TokenConstants.java

@@ -10,7 +10,7 @@ public interface TokenConstants {
     int FILE_END = 0;
 
     /**
-     * Used to optionally separate methods from each other with an always marked token
+     * Used to optionally separate methods from each other with an always marked token.
      */
     int SEPARATOR_TOKEN = 1;
 }

+ 2 - 1
jplag.frontend-utils/src/main/java/de/jplag/TokenHashMap.java

@@ -13,6 +13,7 @@ import java.util.Map;
  * that is larger or equal to the specified size (see {@link TokenHashMap#TokenHashMap(int)}).
  */
 public class TokenHashMap {
+    private static final int CERTAINTY = 100;
     private final Map<Integer, List<Integer>> mappedEntries;
     private final int primeNumber;
 
@@ -67,7 +68,7 @@ public class TokenHashMap {
         }
         for (int possiblePrime = number; possiblePrime < 2 * number; possiblePrime++) { // Bertrand's postulate
             BigInteger bigInt = BigInteger.valueOf(possiblePrime);
-            if (bigInt.isProbablePrime(100)) {
+            if (bigInt.isProbablePrime(CERTAINTY)) {
                 return possiblePrime;
             }
         }

+ 5 - 1
jplag.frontend-utils/src/main/java/de/jplag/TokenList.java

@@ -9,7 +9,7 @@ import java.util.List;
 public class TokenList {
     private final List<Token> tokens;
     TokenHashMap tokenHashes = null;
-    int hash_length = -1;
+    int hashLength = -1;
 
     public TokenList() {
         tokens = new ArrayList<>();
@@ -19,6 +19,10 @@ public class TokenList {
         return tokens.size();
     }
 
+    /**
+     * Adds an token to the list.
+     * @param token is the token to add.
+     */
     public final void addToken(Token token) {
         if (tokens.size() > 0) {
             Token lastToken = tokens.get(tokens.size() - 1);

+ 1 - 1
jplag.frontend.chars/src/main/java/de/jplag/chars/Language.java

@@ -23,7 +23,7 @@ public class Language implements de.jplag.Language {
 
     @Override
     public int errorCount() {
-        return this.parser.errorsCount();
+        return this.parser.errorCount();
     }
 
     @Override

+ 0 - 1
jplag.frontend.chars/src/main/java/de/jplag/chars/Parser.java

@@ -26,7 +26,6 @@ public class Parser extends AbstractParser {
         else
             errorConsumer.print(null, errors + " ERROR" + (errors > 1 ? "S" : ""));
 
-        this.parseEnd();
         return tokens;
     }
 

+ 1 - 1
jplag.frontend.cpp/src/main/java/de/jplag/cpp/Language.java

@@ -15,7 +15,7 @@ public class Language implements de.jplag.Language {
 
     @Override
     public int errorCount() {
-        return this.scanner.errorsCount();
+        return this.scanner.errorCount();
     }
 
     @Override

+ 0 - 1
jplag.frontend.cpp/src/main/java/de/jplag/cpp/Scanner.java

@@ -21,7 +21,6 @@ public class Scanner extends AbstractParser {
             }
             tokens.addToken(new CPPToken(CPPTokenConstants.FILE_END, currentFile));
         }
-        this.parseEnd();
         return tokens;
     }
 

+ 1 - 1
jplag.frontend.csharp-1.2/src/main/java/de/jplag/csharp/Language.java

@@ -21,7 +21,7 @@ public class Language implements de.jplag.Language {
 
     @Override
     public int errorCount() {
-        return this.parser.errorsCount();
+        return this.parser.errorCount();
     }
 
     @Override

+ 0 - 1
jplag.frontend.csharp-1.2/src/main/java/de/jplag/csharp/Parser.java

@@ -23,7 +23,6 @@ public class Parser extends AbstractParser {
                 errors++;
             tokens.addToken(new CSharpToken(CSharpTokenConstants.FILE_END, files[i], -1, -1, -1));
         }
-        this.parseEnd();
         return tokens;
     }
 

+ 1 - 1
jplag.frontend.java/src/main/java/de/jplag/java/Language.java

@@ -68,7 +68,7 @@ public class Language implements de.jplag.Language {
 
     @Override
     public int errorCount() {
-        return this.parser.errorsCount();
+        return this.parser.errorCount();
     }
 
 }

+ 0 - 1
jplag.frontend.java/src/main/java/de/jplag/java/Parser.java

@@ -16,7 +16,6 @@ public class Parser extends AbstractParser {
         errors = 0;
         var pathedFiles = Arrays.stream(files).map(it -> new File(directory, it)).collect(toList());
         errors += new JavacAdapter().parseFiles(directory, pathedFiles, this);
-        this.parseEnd();
         return tokens;
     }
 

+ 1 - 1
jplag.frontend.python-3/src/main/java/de/jplag/python3/Language.java

@@ -21,7 +21,7 @@ public class Language implements de.jplag.Language {
 
     @Override
     public int errorCount() {
-        return this.parser.errorsCount();
+        return this.parser.errorCount();
     }
 
     @Override

+ 0 - 1
jplag.frontend.python-3/src/main/java/de/jplag/python3/Parser.java

@@ -33,7 +33,6 @@ public class Parser extends AbstractParser {
             }
             tokens.addToken(new Python3Token(Python3TokenConstants.FILE_END, files[i], -1, -1, -1));
         }
-        this.parseEnd();
         return tokens;
     }
 

+ 1 - 1
jplag.frontend.scheme/src/main/java/de/jplag/scheme/Language.java

@@ -15,7 +15,7 @@ public class Language implements de.jplag.Language {
 
     @Override
     public int errorCount() {
-        return this.parser.errorsCount();
+        return this.parser.errorCount();
     }
 
     private final de.jplag.scheme.Parser parser; // Not yet instantiated? See constructor!

+ 0 - 1
jplag.frontend.scheme/src/main/java/de/jplag/scheme/Parser.java

@@ -20,7 +20,6 @@ public class Parser extends AbstractParser {
                 errors++;
             tokens.addToken(new SchemeToken(SchemeTokenConstants.FILE_END, currentFile));
         }
-        this.parseEnd();
         return tokens;
     }
 

+ 1 - 1
jplag.frontend.text/src/main/java/de/jplag/text/Language.java

@@ -16,7 +16,7 @@ public class Language implements de.jplag.Language {
 
     @Override
     public int errorCount() {
-        return this.parser.errorsCount();
+        return this.parser.errorCount();
     }
 
     @Override

+ 0 - 1
jplag.frontend.text/src/main/java/de/jplag/text/Parser.java

@@ -33,7 +33,6 @@ public class Parser extends AbstractParser {
 
         TokenList tmp = tokens;
         tokens = null;
-        this.parseEnd();
         return tmp;
     }
 

+ 4 - 4
jplag/src/main/java/de/jplag/GreedyStringTiling.java

@@ -25,7 +25,7 @@ public class GreedyStringTiling {
     }
 
     /**
-     * Creating hashes in linear time. The hash-code will be written in every Token for the next &lt;hash_length&gt; token
+     * Creating hashes in linear time. The hash-code will be written in every Token for the next &lt;hashLength&gt; token
      * (includes the Token itself).
      * @param tokenList contains the tokens.
      * @param hashLength is the hash length (condition: 1 &lt; hashLength &lt; 26)
@@ -86,7 +86,7 @@ public class GreedyStringTiling {
                 }
             }
         }
-        tokenList.hash_length = hashLength;
+        tokenList.hashLength = hashLength;
     }
 
     public final JPlagComparison compare(Submission firstSubmission, Submission secondSubmission) {
@@ -139,10 +139,10 @@ public class GreedyStringTiling {
         markTokens(second, isBaseCodeComparison);
 
         // create hashes:
-        if (first.hash_length != minimumTokenMatch) {
+        if (first.hashLength != minimumTokenMatch) {
             createHashes(first, minimumTokenMatch, isBaseCodeComparison); // don't make table if it is not a base code comparison
         }
-        if (second.hash_length != minimumTokenMatch || second.tokenHashes == null) {
+        if (second.hashLength != minimumTokenMatch || second.tokenHashes == null) {
             createHashes(second, minimumTokenMatch, true);
         }