Browse Source

Clean up, remove INCR, DECR token types, add documentation

Robin Maisch 4 years ago
parent
commit
0b1ed7af95

+ 0 - 2
jplag.frontend.kotlin/src/main/java/de/jplag/kotlin/KotlinListener.java

@@ -325,8 +325,6 @@ public class KotlinListener extends KotlinParserBaseListener {
             case "return", "return@" -> Optional.of(RETURN);
             case "continue", "continue@" -> Optional.of(CONTINUE);
             case "break", "break@" -> Optional.of(BREAK);
-            case "++" -> Optional.of(INCR);
-            case "--" -> Optional.of(DECR);
             default -> Optional.empty();
         };
 

+ 18 - 1
jplag.frontend.kotlin/src/main/java/de/jplag/kotlin/KotlinParserAdapter.java

@@ -21,10 +21,20 @@ public class KotlinParserAdapter extends AbstractParser {
     private String currentFile;
     private TokenList tokens;
 
+    /**
+     * Creates the KotlinParserAdapter
+     * @param consumer the ErrorConsumer that parser errors are passed on to.
+     */
     public KotlinParserAdapter(ErrorConsumer consumer) {
         super(consumer);
     }
 
+    /**
+     * Parsers a list of files into a single {@link TokenList}.
+     * @param directory the directory of the files.
+     * @param fileNames the file names of the files.
+     * @return a {@link TokenList} containing all tokens of all files.
+     */
     public TokenList parse(File directory, String[] fileNames) {
         tokens = new TokenList();
         for (String file : fileNames) {
@@ -60,7 +70,14 @@ public class KotlinParserAdapter extends AbstractParser {
         return true;
     }
 
-    public void addToken(int tokenType, int line, int column, int length) {
+    /**
+     * Adds a new {@link de.jplag.Token} to the current {@link TokenList}.
+     * @param tokenType the type of the new {@link de.jplag.Token}
+     * @param line the line of the Token in the current file
+     * @param column the start column of the Token in the line
+     * @param length the length of the Token
+     */
+    /* package-private */ void addToken(int tokenType, int line, int column, int length) {
         tokens.addToken(new KotlinToken(tokenType, currentFile, line, column, length));
     }
 }

+ 0 - 2
jplag.frontend.kotlin/src/main/java/de/jplag/kotlin/KotlinToken.java

@@ -63,8 +63,6 @@ public class KotlinToken extends Token {
             case RETURN -> "RETURN";
             case CONTINUE -> "CONTINUE";
             case BREAK -> "BREAK";
-            case INCR -> "INCR";
-            case DECR -> "DECR";
             default -> "<UNKNOWN%d>".formatted(type);
         };
     }

+ 1 - 4
jplag.frontend.kotlin/src/main/java/de/jplag/kotlin/KotlinTokenConstants.java

@@ -70,8 +70,5 @@ public interface KotlinTokenConstants extends TokenConstants {
     int CONTINUE = 48;
     int BREAK = 49;
 
-    int INCR = 50;
-    int DECR = 51;
-
-    int NUMBER_DIFF_TOKENS = 52;
+    int NUMBER_DIFF_TOKENS = 50;
 }

+ 11 - 7
jplag.frontend.kotlin/src/test/java/de/jplag/kotlin/KotlinFrontendTest.java

@@ -66,19 +66,24 @@ public class KotlinFrontendTest {
             logger.info(output);
 
             testSourceCoverage(fileName, tokens);
-            if (fileName.equals(COMPLETE_TEST_FILE))
+            if (fileName.equals(COMPLETE_TEST_FILE)) {
                 testTokenCoverage(tokens, fileName);
+            }
+
         }
     }
 
+    /**
+     * Confirms that every type of KotlinToken has a Sting representation associated to it.
+     */
     @Test
-    public void testToken2String() {
-        var missingToken = IntStream.range(0, language.numberOfTokens())
+    void testToken2String() {
+        var missingTokens = IntStream.range(0, language.numberOfTokens())
                 .mapToObj(type -> new KotlinToken(type, NOT_SET_STRING, NOT_SET, NOT_SET, NOT_SET))
                 .filter(token -> token.type2string().contains("UNKNOWN")).toList();
 
-        if (!missingToken.isEmpty()) {
-            var typeList = missingToken.stream().map(Token::getType).map(Object::toString).collect(Collectors.joining(", "));
+        if (!missingTokens.isEmpty()) {
+            var typeList = missingTokens.stream().map(Token::getType).map(Object::toString).collect(Collectors.joining(", "));
             fail("Found token types with no string representation: %s".formatted(typeList));
         }
 
@@ -103,8 +108,7 @@ public class KotlinFrontendTest {
             if (codeLines.length > tokenLines.length) {
                 var diffLine = IntStream.range(0, codeLines.length)
                         .dropWhile(lineIdx -> lineIdx < tokenLines.length && codeLines[lineIdx] == tokenLines[lineIdx]).findFirst();
-                diffLine.ifPresent(
-                        lineIdx -> fail("Line %d of file '%s' is not represented in the token list.".formatted(codeLines[lineIdx], fileName)));
+                diffLine.ifPresent(lineIdx -> fail("Line %d of file '%s' is not represented in the token list.".formatted(codeLines[lineIdx], fileName)));
             }
             assertArrayEquals(codeLines, tokenLines);
         } catch (IOException exception) {

+ 2 - 4
jplag.frontend.kotlin/src/test/resources/de/jplag/kotlin/Complete.kt

@@ -26,7 +26,7 @@ class Complete() {
     }
 
     fun get() : Inner {
-        Inner.accessCount++;
+        Inner.accessCount = Inner.accessCount + 1;
         return Inner
     }
 
@@ -68,7 +68,7 @@ fun main(args : Array<String>) {
             println("Finally!")
             break
         }
-        int++
+        int = int + 2;
     }
 
     try {
@@ -93,7 +93,5 @@ fun main(args : Array<String>) {
         }
     }
 
-    int--
-    println(int)
 }
 

+ 1 - 1
jplag.frontend.kotlin/src/test/resources/de/jplag/kotlin/Game.kt

@@ -44,4 +44,4 @@ fun readBoolean() : Boolean {
         input = readLine().toString();
     }
     return Regex("yY").matches(input)
-}
+}