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

Add documentation, move GoTokenTestUtils

Robin Maisch 4 лет назад
Родитель
Сommit
3936f20fcb

+ 0 - 4
jplag.frontend.golang/src/main/java/de/jplag/golang/GoParserAdapter.java

@@ -1,8 +1,5 @@
 package de.jplag.golang;
 package de.jplag.golang;
 
 
-import static de.jplag.TokenConstants.FILE_END;
-import static de.jplag.golang.grammar.GoTokenUtils.getDummyToken;
-
 import java.io.File;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.IOException;
@@ -28,7 +25,6 @@ public class GoParserAdapter extends AbstractParser {
             if (!parseFile(directory, file)) {
             if (!parseFile(directory, file)) {
                 errors++;
                 errors++;
             }
             }
-            tokens.addToken(getDummyToken(FILE_END, file));
         }
         }
         return tokens;
         return tokens;
     }
     }

+ 12 - 0
jplag.frontend.golang/src/main/java/de/jplag/golang/JPlagGoListener.java

@@ -474,6 +474,11 @@ public class JPlagGoListener extends GoParserBaseListener {
             }
             }
             case "{" -> transformToken(getCurrentContext().getBegin(), token);
             case "{" -> transformToken(getCurrentContext().getBegin(), token);
             case "}" -> transformToken(getCurrentContext().getEnd(), token);
             case "}" -> transformToken(getCurrentContext().getEnd(), token);
+            case "<EOF>" -> {
+                if (node.getParent() instanceof GoParser.SourceFileContext) {
+                    transformToken(FILE_END, token);
+                }
+            }
             default -> {
             default -> {
                 // do nothing.
                 // do nothing.
             }
             }
@@ -481,6 +486,13 @@ public class JPlagGoListener extends GoParserBaseListener {
         super.visitTerminal(node);
         super.visitTerminal(node);
     }
     }
 
 
+    /**
+     * This enumeration provides sets of information regarding different types of nesting structures in Go. Each element is
+     * a tuple of a token for the beginning of a block, the end of the block, and optionally, for the elements contained.
+     * <p>
+     * As the Go parser does not differentiate between different kinds of blocks, a stack of these GoBlockContexts is
+     * required to be able to assign the correct token types for each block.
+     */
     private enum GoBlockContext {
     private enum GoBlockContext {
         ARRAY_BODY(ARRAY_BODY_BEGIN, ARRAY_BODY_END, ARRAY_ELEMENT),
         ARRAY_BODY(ARRAY_BODY_BEGIN, ARRAY_BODY_END, ARRAY_ELEMENT),
         STRUCT_BODY(STRUCT_BODY_BEGIN, STRUCT_BODY_END, MEMBER_DECLARATION),
         STRUCT_BODY(STRUCT_BODY_BEGIN, STRUCT_BODY_END, MEMBER_DECLARATION),

+ 2 - 2
jplag.frontend.golang/src/main/java/de/jplag/golang/Language.java

@@ -1,9 +1,9 @@
 package de.jplag.golang;
 package de.jplag.golang;
 
 
-import de.jplag.TokenList;
-
 import java.io.File;
 import java.io.File;
 
 
+import de.jplag.TokenList;
+
 public class Language implements de.jplag.Language {
 public class Language implements de.jplag.Language {
 
 
     private static final String NAME = "Go Parser";
     private static final String NAME = "Go Parser";

+ 12 - 12
jplag.frontend.golang/src/test/java/de/jplag/golang/GoFrontendTest.java

@@ -1,14 +1,7 @@
 package de.jplag.golang;
 package de.jplag.golang;
 
 
-import de.jplag.Token;
-import de.jplag.TokenConstants;
-import de.jplag.TokenList;
-import de.jplag.TokenPrinter;
-import de.jplag.golang.grammar.GoTokenUtils;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import static de.jplag.golang.GoTokenTestUtils.getDummyToken;
+import static org.junit.jupiter.api.Assertions.fail;
 
 
 import java.io.File;
 import java.io.File;
 import java.io.IOException;
 import java.io.IOException;
@@ -22,8 +15,15 @@ import java.util.stream.Collectors;
 import java.util.stream.IntStream;
 import java.util.stream.IntStream;
 import java.util.stream.StreamSupport;
 import java.util.stream.StreamSupport;
 
 
-import static de.jplag.golang.grammar.GoTokenUtils.getDummyToken;
-import static org.junit.jupiter.api.Assertions.fail;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import de.jplag.Token;
+import de.jplag.TokenConstants;
+import de.jplag.TokenList;
+import de.jplag.TokenPrinter;
 
 
 class GoFrontendTest {
 class GoFrontendTest {
     /**
     /**
@@ -75,7 +75,7 @@ class GoFrontendTest {
      */
      */
     @Test
     @Test
     void testTokenToString() {
     void testTokenToString() {
-        var missingTokens = IntStream.range(0, language.numberOfTokens()).mapToObj(GoTokenUtils::getDummyToken)
+        var missingTokens = IntStream.range(0, language.numberOfTokens()).mapToObj(GoTokenTestUtils::getDummyToken)
                 .filter(token -> token.type2string().contains("UNKNOWN")).toList();
                 .filter(token -> token.type2string().contains("UNKNOWN")).toList();
 
 
         if (!missingTokens.isEmpty()) {
         if (!missingTokens.isEmpty()) {

+ 3 - 5
jplag.frontend.golang/src/main/java/de/jplag/golang/grammar/GoTokenUtils.java → jplag.frontend.golang/src/test/java/de/jplag/golang/GoTokenTestUtils.java

@@ -1,13 +1,11 @@
-package de.jplag.golang.grammar;
+package de.jplag.golang;
 
 
-import de.jplag.golang.GoToken;
-
-public class GoTokenUtils {
+public class GoTokenTestUtils {
 
 
     private static final int NOT_SET = -1;
     private static final int NOT_SET = -1;
     private static final String EMPTY_STRING = "";
     private static final String EMPTY_STRING = "";
 
 
-    private GoTokenUtils() {
+    private GoTokenTestUtils() {
         // Utility class, should not be instantiated.
         // Utility class, should not be instantiated.
     }
     }