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

Added documentation for R module

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

+ 15 - 13
jplag.frontend.R/src/main/java/de/jplag/R/JplagRListener.java

@@ -10,26 +10,28 @@ import de.jplag.R.grammar.RFilterListener;
 import de.jplag.R.grammar.RListener;
 import de.jplag.R.grammar.RParser;
 
-/*
- * This class is responsible for implementing the RListener methods generated by ANTLR4 in such a way that every time a
- * token of the language is recognized, the corresponding "enter" and "exit" methods will be called. If it is an
- * important token (i.e. it is important to consider it when identifying plagiarisms), then all there is to do is to
- * call "parser.add" in its corresponding method using its constant. Esta clase se encarga de implementar los métodos
- * clase RListener que se genera por ANTLR4 de forma que cada vez que se reconozca un Token del lenguaje se entrará en
- * su "enter<NombredelToken>" y "exit<NombredelToken>" y en caso de ser un token importante (es decir, que sea relevante
- * considerarlo a la hora de identificar plagio) tan solo tendremos que irnos a su método he implementarlo haciendo un
- * parser.add o un parser.addEnd (en caso de que sea un exit) con el nombre del token que le hayamos asociado en
- * RToken.java y RTokenConstants.java .
+/**
+ * Listener class for visiting the R ANTLR parse tree. Transforms the visited ANTLR token into JPlag tokens.
+ * Based on an R frontend for JPlag v2.15 by Olmo Kramer, see their <a href="https://github.com/CodeGra-de/jplag/tree/master/jplag.frontend.R">JPlag fork</a>.
+ * @author Robin Maisch
  */
-
 public class JplagRListener implements RListener, RFilterListener, RTokenConstants {
 
     private final RParserAdapter parserAdapter;
 
+    /**
+     * Creates the listener.
+     * @param parserAdapter the JPlag parser adapter which receives the transformed tokens.
+     */
     public JplagRListener(RParserAdapter parserAdapter) {
         this.parserAdapter = parserAdapter;
     }
 
+    /**
+     * Transforms an ANTLR Token into a JPlag token and transfers it to the token adapter.
+     * @param targetType the type of the JPlag token to be created.
+     * @param token the ANTLR token.
+     */
     private void transformToken(int targetType, Token token) {
         parserAdapter.addToken(targetType, token.getLine(), token.getCharPositionInLine() + 1, token.getText().length());
     }
@@ -315,12 +317,12 @@ public class JplagRListener implements RListener, RFilterListener, RTokenConstan
     }
 
     @Override
-    public void enterElem(RFilter.ElemContext ctx) {
+    public void enterElement(RFilter.ElementContext ctx) {
 
     }
 
     @Override
-    public void exitElem(RFilter.ElemContext ctx) {
+    public void exitElement(RFilter.ElementContext ctx) {
 
     }
 

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

@@ -5,12 +5,15 @@ import java.io.File;
 import de.jplag.ErrorConsumer;
 import de.jplag.TokenList;
 
+/**
+ * This represents the R language as a language supported by JPlag.
+ */
 public class Language implements de.jplag.Language {
 
     public static final String NAME = "R Parser";
     public static final String SHORT_NAME = "R";
     public static final int DEFAULT_MIN_TOKEN_MATCH = 8;
-    private RParserAdapter parserAdapter;
+    private final RParserAdapter parserAdapter;
 
     public Language(ErrorConsumer consumer) {
         this.parserAdapter = new RParserAdapter(consumer);

+ 17 - 0
jplag.frontend.R/src/main/java/de/jplag/R/RParserAdapter.java

@@ -26,10 +26,20 @@ public class RParserAdapter extends AbstractParser implements RTokenConstants {
     private String currentFile;
     private TokenList tokens;
 
+    /**
+     * Creates the RParserAdapter
+     * @param errorConsumer the ErrorConsumer that parser errors are passed on to.
+     */
     public RParserAdapter(ErrorConsumer errorConsumer) {
         super(errorConsumer);
     }
 
+    /**
+     * 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();
         errors = 0;
@@ -73,6 +83,13 @@ public class RParserAdapter extends AbstractParser implements RTokenConstants {
         return true;
     }
 
+    /**
+     * Adds a new {@link de.jplag.Token} to the current {@link TokenList}.
+     * @param type the type of the new {@link de.jplag.Token}
+     * @param line the line of the Token in the current file
+     * @param start the start column of the Token in the line
+     * @param end the end column of the Token in the line
+     */
     /* package-private */ void addToken(int type, int line, int start, int end) {
         tokens.addToken(new RToken(type, currentFile, line, start, end - start));
 

+ 34 - 83
jplag.frontend.R/src/main/java/de/jplag/R/RToken.java

@@ -1,96 +1,47 @@
 package de.jplag.R;
 
+/**
+ * This class represents the occurrence of an R Token in the source code.
+ * Based on an R frontend for JPlag v2.15 by Olmo Kramer, see their <a href="https://github.com/CodeGra-de/jplag/tree/master/jplag.frontend.R">JPlag fork</a>.
+ * @author Robin Maisch
+ */
 public class RToken extends de.jplag.Token implements RTokenConstants {
-    private static final long serialVersionUID = 1L;
-    private int line, column, length;
 
     public RToken(int type, String file, int line, int column, int length) {
         super(type, file, line, column, length);
     }
 
-    public int getLine() {
-        return line;
-    }
-
-    public int getColumn() {
-        return column;
-    }
-
-    public int getLength() {
-        return length;
-    }
-
-    public void setLine(int line) {
-        this.line = line;
-    }
-
-    public void setColumn(int column) {
-        this.column = column;
-    }
-
-    public void setLength(int length) {
-        this.length = length;
-    }
-
     @Override
     public String type2string() {
-        switch (this.type) {
-            case FILE_END:
-                return "***********";
-            case SEPARATOR_TOKEN:
-                return "METHOD_SEPARATOR";
-            case BEGIN_FUNCTION:
-                return "FUNCTION{  ";
-            case END_FUNCTION:
-                return "}FUNCTION  ";
-            case FUNCTION_CALL:
-                return "FUNCTION() ";
-            case NUMBER:
-                return "NUMBER     ";
-            case STRING:
-                return "STRING     ";
-            case BOOL:
-                return "BOOL       ";
-            case ASSIGN:
-                return "ASSIGN     ";
-            case ASSIGN_FUNC:
-                return "ASSIGN_FUNC";
-            case ASSIGN_LIST:
-                return "ASSIGN_LIST";
-            case HELP:
-                return "HELP       ";
-            case INDEX:
-                return "INDEX      ";
-            case PACKAGE:
-                return "PACKAGE    ";
-            case IF_BEGIN:
-                return "IF{        ";
-            case IF_END:
-                return "}IF        ";
-            case FOR_BEGIN:
-                return "FOR{       ";
-            case FOR_END:
-                return "}FOR       ";
-            case WHILE_BEGIN:
-                return "WHILE{     ";
-            case WHILE_END:
-                return "}WHILE     ";
-            case REPEAT_BEGIN:
-                return "REPEAT{    ";
-            case REPEAT_END:
-                return "}REPEAT    ";
-            case NEXT:
-                return "NEXT       ";
-            case BREAK:
-                return "BREAK      ";
-            case COMPOUND_BEGIN:
-                return "COMPOUND{   ";
-            case COMPOUND_END:
-                return "}COMPOUND   ";
-            default:
-                System.err.println("*UNKNOWN: " + type);
-                return "*UNKNOWN" + type;
-        }
+        return switch (this.type) {
+            case FILE_END -> "***********";
+            case SEPARATOR_TOKEN -> "METHOD_SEPARATOR";
+            case BEGIN_FUNCTION -> "FUNCTION{";
+            case END_FUNCTION -> "}FUNCTION";
+            case FUNCTION_CALL -> "FUNCTION()";
+            case NUMBER -> "NUMBER";
+            case STRING -> "STRING";
+            case BOOL -> "BOOL";
+            case ASSIGN -> "ASSIGN";
+            case ASSIGN_FUNC -> "ASSIGN_FUNC";
+            case ASSIGN_LIST -> "ASSIGN_LIST";
+            case HELP -> "HELP";
+            case INDEX -> "INDEX";
+            case PACKAGE -> "PACKAGE";
+            case IF_BEGIN -> "IF{";
+            case IF_END -> "}IF";
+            case FOR_BEGIN -> "FOR{";
+            case FOR_END -> "}FOR";
+            case WHILE_BEGIN -> "WHILE{";
+            case WHILE_END -> "}WHILE";
+            case REPEAT_BEGIN -> "REPEAT{";
+            case REPEAT_END -> "}REPEAT";
+            case NEXT -> "NEXT";
+            case BREAK -> "BREAK";
+            case COMPOUND_BEGIN -> "COMPOUND{";
+            case COMPOUND_END -> "}COMPOUND";
+            default -> "<UNKNOWN%d>".formatted(type);
+        };
     }
 
 }

+ 3 - 1
jplag.frontend.R/src/main/java/de/jplag/R/RTokenConstants.java

@@ -3,7 +3,9 @@ package de.jplag.R;
 import de.jplag.TokenConstants;
 
 /**
- * Tokens in R that are deemed important when comparing submissions for plagiarisms
+ * Tokens in R that are deemed important when comparing submissions for plagiarisms.
+ * Based on an R frontend for JPlag v2.15 by Olmo Kramer, see their <a href="https://github.com/CodeGra-de/jplag/tree/master/jplag.frontend.R">JPlag fork</a>.
+ * @author Robin Maisch
  */
 public interface RTokenConstants extends TokenConstants {
     int FILE_END = 0;