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

Shallow clean-up of all frontends to improve code quality.

Timur Sağlam 4 лет назад
Родитель
Сommit
6292218902

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

@@ -6,18 +6,18 @@ package de.jplag;
 public abstract class AbstractParser {
     protected ErrorConsumer errorConsumer;
     protected int errors = 0;
-    private int errorsSum = 0;
+    private int numberOfErrors = 0;
 
     public boolean hasErrors() {
         return errors != 0;
     }
 
     public int errorsCount() {
-        return errorsSum;
+        return numberOfErrors;
     }
 
     protected void parseEnd() {
-        errorsSum += errors;
+        numberOfErrors += errors;
     }
 
     public ErrorConsumer getErrorConsumer() {

+ 9 - 8
jplag.frontend-utils/src/main/java/de/jplag/Token.java

@@ -6,21 +6,22 @@ package de.jplag;
  * The language parsers decide what is a token and what is not.
  */
 public abstract class Token {
-    private boolean basecode = false;
+    private int line;
     private int column;
+    private int length;
     private String file;
-    private int hash = -1;// hash-value. set and used by main algorithm (GSTiling)
 
-    private int length;
-    private int line;
     private boolean marked;
+    private boolean basecode = false;
+    private int hash = -1;// hash-value. set and used by main algorithm (GSTiling)
+
     protected int type;
 
     /**
      * Creates a token without information about the column or the length of the token in the line.
      * @param type is the token type.
      * @param file is the name of the source code file.
-     * @param line is the line in the source code where the token resides. Cannot be smaller than 1.
+     * @param line is the line index in the source code where the token resides. Cannot be smaller than 1.
      */
     public Token(int type, String file, int line) {
         this.type = type;
@@ -32,9 +33,9 @@ public abstract class Token {
      * Creates a token with column and length information.
      * @param type is the token type.
      * @param file is the name of the source code file.
-     * @param line is the line in the source code where the token resides.
-     * @param column is the line index in the line, meaning where the token starts
-     * @param length is the length of the token in the line.
+     * @param line is the line index in the source code where the token resides. Cannot be smaller than 1.
+     * @param column is the column index, meaning where the token starts in the line.
+     * @param length is the length of the token in the source code.
      */
     public Token(int type, String file, int line, int column, int length) {
         this(type, file, line);

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

@@ -20,8 +20,7 @@ public class Language implements de.jplag.Language {
 
     @Override
     public String[] suffixes() {
-        String[] res = {".TXT", ".txt", ".ASC", ".asc", ".TEX", ".tex"};
-        return res;
+        return new String[] {".TXT", ".txt", ".ASC", ".asc", ".TEX", ".tex"};
     }
 
     @Override
@@ -31,7 +30,7 @@ public class Language implements de.jplag.Language {
 
     @Override
     public String getName() {
-        return "Character AbstractParser";
+        return "Character Parser";
     }
 
     @Override

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

@@ -10,16 +10,16 @@ import de.jplag.TokenList;
 
 public class Parser extends AbstractParser implements TokenConstants {
 
-    private TokenList struct;
+    private TokenList tokens;
 
-    public TokenList parse(File dir, String files[]) {
-        struct = new TokenList();
+    public TokenList parse(File directory, String files[]) {
+        tokens = new TokenList();
         errors = 0;
         for (String file : files) {
             getErrorConsumer().print(null, "Parsing file " + file);
-            if (!parseFile(dir, file))
+            if (!parseFile(directory, file))
                 errors++;
-            struct.addToken(new CharToken(FILE_END, file, this));
+            tokens.addToken(new CharToken(FILE_END, file, this));
         }
         if (errors == 0)
             errorConsumer.print(null, "OK");
@@ -27,7 +27,7 @@ public class Parser extends AbstractParser implements TokenConstants {
             errorConsumer.print(null, errors + " ERROR" + (errors > 1 ? "S" : ""));
 
         this.parseEnd();
-        return struct;
+        return tokens;
     }
 
     private boolean parseFile(File dir, String file) {
@@ -44,7 +44,7 @@ public class Parser extends AbstractParser implements TokenConstants {
 
                 for (int i = 0; i < length; i++) {
                     if (buffer[i] <= 127 && (type = mapping[buffer[i]]) > 1) {
-                        struct.addToken(new CharToken(type, file, offset + i, buffer[i], this));
+                        tokens.addToken(new CharToken(type, file, offset + i, buffer[i], this));
                     }
                 }
                 offset += length;

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

@@ -20,8 +20,7 @@ public class Language implements de.jplag.Language {
 
     @Override
     public String[] suffixes() {
-        String[] res = {".cpp", ".CPP", ".cxx", ".CXX", ".c++", ".C++", ".c", ".C", ".cc", ".CC", ".h", ".H", ".hpp", ".HPP", ".hh", ".HH"};
-        return res;
+        return new String[] {".cpp", ".CPP", ".cxx", ".CXX", ".c++", ".C++", ".c", ".C", ".cc", ".CC", ".h", ".H", ".hpp", ".HPP", ".hh", ".HH"};
     }
 
     @Override

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

@@ -16,8 +16,7 @@ public class Language implements de.jplag.Language {
 
     @Override
     public String[] suffixes() {
-        String[] res = {".cs", ".CS"};
-        return res;
+        return new String[] {".cs", ".CS"};
     }
 
     @Override
@@ -27,7 +26,7 @@ public class Language implements de.jplag.Language {
 
     @Override
     public String getName() {
-        return "C# 1.2 AbstractParser";
+        return "C# 1.2 Parser";
     }
 
     @Override

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

@@ -12,27 +12,27 @@ import de.jplag.csharp.grammar.CSharpLexer;
 import de.jplag.csharp.grammar.CSharpParser;
 
 public class Parser extends AbstractParser implements CSharpTokenConstants {
-    private TokenList struct;
+    private TokenList tokens;
     private String currentFile;
 
-    public TokenList parse(File dir, String files[]) {
-        struct = new TokenList();
+    public TokenList parse(File directory, String files[]) {
+        tokens = new TokenList();
         errors = 0;
         for (int i = 0; i < files.length; i++) {
-            if (!parseFile(dir, files[i]))
+            if (!parseFile(directory, files[i]))
                 errors++;
-            struct.addToken(new CSharpToken(FILE_END, files[i], -1, -1, -1));
+            tokens.addToken(new CSharpToken(FILE_END, files[i], -1, -1, -1));
         }
         this.parseEnd();
-        return struct;
+        return tokens;
     }
 
     private boolean parseFile(File dir, String file) {
         try {
-            FileInputStream fis = new FileInputStream(new File(dir, file));
+            FileInputStream input = new FileInputStream(new File(dir, file));
             currentFile = file;
             // Create a scanner that reads from the input stream passed to us
-            CSharpLexer lexer = new CSharpLexer(new UnicodeReader(fis, StandardCharsets.UTF_8));
+            CSharpLexer lexer = new CSharpLexer(new UnicodeReader(input, StandardCharsets.UTF_8));
             lexer.setFilename(file);
             lexer.setTabSize(1);
 
@@ -44,7 +44,7 @@ public class Parser extends AbstractParser implements CSharpTokenConstants {
             parser.compilation_unit();
 
             // close file
-            fis.close();
+            input.close();
         } catch (Exception e) {
             getErrorConsumer().addError("  Parsing Error in '" + file + "':\n  " + e.toString());
             return false;
@@ -52,15 +52,15 @@ public class Parser extends AbstractParser implements CSharpTokenConstants {
         return true;
     }
 
-    private void add(int type, Token tok) {
-        if (tok == null) {
+    private void add(int type, Token token) {
+        if (token == null) {
             System.out.println("tok == null  ERROR!");
             return;
         }
-        struct.addToken(new CSharpToken(type, currentFile, tok.getLine(), tok.getColumn(), tok.getText().length()));
+        tokens.addToken(new CSharpToken(type, currentFile, token.getLine(), token.getColumn(), token.getText().length()));
     }
 
-    public void add(int type, CSharpParser p) {
-        add(type, p.getLastConsumedToken());
+    public void add(int type, CSharpParser parser) {
+        add(type, parser.getLastConsumedToken());
     }
 }

+ 2 - 2
jplag.frontend.java/src/main/java/de/jplag/java/JavaToken.java

@@ -4,8 +4,8 @@ import de.jplag.Token;
 
 public class JavaToken extends Token implements JavaTokenConstants {
 
-    public JavaToken(int type, String file, int line, int col, int length) {
-        super(type, file, line, col, length);
+    public JavaToken(int type, String file, int line, int column, int length) {
+        super(type, file, line, column, length);
     }
 
     @Override

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

@@ -18,8 +18,7 @@ public class Language implements de.jplag.Language {
 
     @Override
     public String[] suffixes() {
-        String[] res = {".java", ".JAVA"};
-        return res;
+        return new String[] {".java", ".JAVA"};
     }
 
     @Override

+ 7 - 7
jplag.frontend.java/src/main/java/de/jplag/java/Parser.java

@@ -6,23 +6,23 @@ import de.jplag.AbstractParser;
 import de.jplag.TokenList;
 
 public class Parser extends AbstractParser {
-    private TokenList tokenList;
+    private TokenList tokens;
 
-    public TokenList parse(File dir, String files[]) {
-        tokenList = new TokenList();
+    public TokenList parse(File directory, String files[]) {
+        tokens = new TokenList();
         errors = 0;
         File pathedFiles[] = new File[files.length];
         for (int i = 0; i < files.length; i++) {
-            pathedFiles[i] = new File(dir, files[i]);
+            pathedFiles[i] = new File(directory, files[i]);
         }
         JavacAdapter javac = new JavacAdapter();
-        errors += javac.parseFiles(dir, pathedFiles, this);
+        errors += javac.parseFiles(directory, pathedFiles, this);
         this.parseEnd();
-        return tokenList;
+        return tokens;
     }
 
     public void add(int type, String filename, long line, long col, long length) {
-        tokenList.addToken(new JavaToken(type, filename, (int) line, (int) col, (int) length));
+        tokens.addToken(new JavaToken(type, filename, (int) line, (int) col, (int) length));
     }
 
     public void errorsInc() {

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

@@ -16,8 +16,7 @@ public class Language implements de.jplag.Language {
 
     @Override
     public String[] suffixes() {
-        String[] res = {".py"};
-        return res;
+        return new String[] {".py"};
     }
 
     @Override
@@ -27,7 +26,7 @@ public class Language implements de.jplag.Language {
 
     @Override
     public String getName() {
-        return "Python3 AbstractParser";
+        return "Python3 Parser";
     }
 
     @Override

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

@@ -20,32 +20,32 @@ import de.jplag.python3.grammar.Python3Parser.File_inputContext;
 
 public class Parser extends AbstractParser implements Python3TokenConstants {
 
-    private TokenList struct = new TokenList();
+    private TokenList tokens = new TokenList();
     private String currentFile;
 
-    public TokenList parse(File dir, String files[]) {
-        struct = new TokenList();
+    public TokenList parse(File directory, String files[]) {
+        tokens = new TokenList();
         errors = 0;
         for (int i = 0; i < files.length; i++) {
             getErrorConsumer().print(null, "Parsing file " + files[i]);
-            if (!parseFile(dir, files[i])) {
+            if (!parseFile(directory, files[i])) {
                 errors++;
             }
             System.gc();// Emeric
-            struct.addToken(new Python3Token(FILE_END, files[i], -1, -1, -1));
+            tokens.addToken(new Python3Token(FILE_END, files[i], -1, -1, -1));
         }
         this.parseEnd();
-        return struct;
+        return tokens;
     }
 
-    private boolean parseFile(File dir, String file) {
-        BufferedInputStream fis;
+    private boolean parseFile(File directory, String file) {
+        BufferedInputStream inputStream;
 
         CharStream input;
         try {
-            fis = new BufferedInputStream(new FileInputStream(new File(dir, file)));
+            inputStream = new BufferedInputStream(new FileInputStream(new File(directory, file)));
             currentFile = file;
-            input = CharStreams.fromStream(fis);
+            input = CharStreams.fromStream(inputStream);
 
             // create a lexer that feeds off of input CharStream
             Python3Lexer lexer = new Python3Lexer(input);
@@ -71,13 +71,13 @@ public class Parser extends AbstractParser implements Python3TokenConstants {
         return true;
     }
 
-    public void add(int type, Token tok) {
-        struct.addToken(new Python3Token(type, (currentFile == null ? "null" : currentFile), tok.getLine(), tok.getCharPositionInLine() + 1,
-                tok.getText().length()));
+    public void add(int type, Token token) {
+        tokens.addToken(new Python3Token(type, (currentFile == null ? "null" : currentFile), token.getLine(), token.getCharPositionInLine() + 1,
+                token.getText().length()));
     }
 
-    public void addEnd(int type, Token tok) {
-        struct.addToken(new Python3Token(type, (currentFile == null ? "null" : currentFile), tok.getLine(),
-                struct.getToken(struct.size() - 1).getColumn() + 1, 0));
+    public void addEnd(int type, Token token) {
+        tokens.addToken(new Python3Token(type, (currentFile == null ? "null" : currentFile), token.getLine(),
+                tokens.getToken(tokens.size() - 1).getColumn() + 1, 0));
     }
 }

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

@@ -22,13 +22,12 @@ public class Language implements de.jplag.Language {
 
     @Override
     public String[] suffixes() {
-        String[] res = {".scm", ".SCM", ".ss", ".SS"};
-        return res;
+        return new String[] {".scm", ".SCM", ".ss", ".SS"};
     }
 
     @Override
     public String getName() {
-        return "SchemeR4RS AbstractParser [basic markup]";
+        return "SchemeR4RS Parser [basic markup]";
     }
 
     @Override

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

@@ -8,25 +8,25 @@ import de.jplag.TokenList;
 public class Parser extends AbstractParser implements SchemeTokenConstants {
     private String currentFile;
 
-    private TokenList struct;
+    private TokenList tokens;
 
     public TokenList parse(File directory, String files[]) {
-        struct = new TokenList();
+        tokens = new TokenList();
         errors = 0;
         for (int i = 0; i < files.length; i++) {
             currentFile = files[i];
             getErrorConsumer().print(null, "Parsing file " + files[i]);
             if (!SchemeParser.parseFile(directory, files[i], null, this))
                 errors++;
-            struct.addToken(new SchemeToken(FILE_END, currentFile));
+            tokens.addToken(new SchemeToken(FILE_END, currentFile));
         }
         this.parseEnd();
-        return struct;
+        return tokens;
     }
 
     public void add(int type, Token token) {
         int length = token.endColumn - token.beginColumn + 1;
-        struct.addToken(new SchemeToken(type, currentFile, token.beginLine, token.endLine, length));
+        tokens.addToken(new SchemeToken(type, currentFile, token.beginLine, token.endLine, length));
     }
 
 }

+ 2 - 2
jplag.frontend.text/src/main/antlr/text.g

@@ -58,7 +58,7 @@ options
             InputState state = (InputState) inputState;
             if (text.length() == 0) {
                 // remember token start column
-                state.tokColumn = state.column;
+                state.tokenColumn = state.column;
             }
             state.column++;
         }
@@ -67,7 +67,7 @@ options
 
     protected Token makeToken(int t) {
         ParserToken token = (ParserToken) super.makeToken(t);
-        token.setColumn(((InputState) inputState).tokColumn);
+        token.setColumn(((InputState) inputState).tokenColumn);
         return token;
     }
 }

+ 7 - 7
jplag.frontend.text/src/main/java/de/jplag/text/InputState.java

@@ -12,22 +12,22 @@ import antlr.LexerSharedInputState;
  */
 public class InputState extends LexerSharedInputState {
     public int column = 0;
-    public int tokColumn = 0;
+    public int tokenColumn = 0;
 
-    public InputState(InputBuffer inbuf) {
-        super(inbuf);
+    public InputState(InputBuffer inputBuffer) {
+        super(inputBuffer);
         column = 1;
         line = 1;
     }
 
-    public InputState(InputStream in) {
-        super(in);
+    public InputState(InputStream inputStream) {
+        super(inputStream);
         column = 1;
         line = 1;
     }
 
-    public InputState(Reader in) {
-        super(in);
+    public InputState(Reader inputReader) {
+        super(inputReader);
         column = 1;
         line = 1;
     }

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

@@ -24,13 +24,12 @@ public class Language implements de.jplag.Language {
 
     @Override
     public String[] suffixes() {
-        String[] res = {".TXT", ".txt", ".ASC", ".asc", ".TEX", ".tex"};
-        return res;
+        return new String[] {".TXT", ".txt", ".ASC", ".asc", ".TEX", ".tex"};
     }
 
     @Override
     public String getName() {
-        return "Text AbstractParser";
+        return "Text Parser";
     }
 
     @Override

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

@@ -15,30 +15,46 @@ public class Parser extends AbstractParser implements TokenConstants {
     protected Hashtable<String, Integer> table = new Hashtable<>();
     protected int serial = 1; // 0 is FILE_END token
 
-    private TokenList struct;
+    private TokenList tokens;
 
     private String currentFile;
 
-    public TokenList parse(File dir, String files[]) {
-        struct = new TokenList();
+    private boolean runOut = false;
+
+    public TokenList parse(File directory, String files[]) {
+        tokens = new TokenList();
         errors = 0;
         for (String file : files) {
             getErrorConsumer().print("", "Parsing file " + file);
-            if (!parseFile(dir, file))
+            if (!parseFile(directory, file))
                 errors++;
-            struct.addToken(new TextToken(FILE_END, file, this));
+            tokens.addToken(new TextToken(FILE_END, file, this));
         }
 
-        TokenList tmp = struct;
-        struct = null;
+        TokenList tmp = tokens;
+        tokens = null;
         this.parseEnd();
         return tmp;
     }
 
-    private boolean parseFile(File dir, String file) {
+    public void add(Token token) {
+        ParserToken parserToken = (ParserToken) token;
+        tokens.addToken(new TextToken(token.getText(), currentFile, parserToken.getLine(), parserToken.getColumn(), parserToken.getLength(), this));
+    }
+
+    public void outOfSerials() {
+        if (runOut)
+            return;
+        runOut = true;
+        errors++;
+        errorConsumer.print("ERROR: Out of serials!", null);
+        System.out.println("de.jplag.text.Parser: ERROR: Out of serials!");
+    }
+
+    private boolean parseFile(File directory, String file) {
         InputState inputState = null;
         try {
-            FileInputStream inputStream = new FileInputStream(new File(dir, file));
+            FileInputStream inputStream = new FileInputStream(new File(directory, file));
             currentFile = file;
             // Create a scanner that reads from the input stream passed to us
             inputState = new InputState(inputStream);
@@ -63,20 +79,4 @@ public class Parser extends AbstractParser implements TokenConstants {
         }
         return true;
     }
-
-    public void add(Token tok) {
-        ParserToken ptok = (ParserToken) tok;
-        struct.addToken(new TextToken(tok.getText(), currentFile, ptok.getLine(), ptok.getColumn(), ptok.getLength(), this));
-    }
-
-    private boolean runOut = false;
-
-    public void outOfSerials() {
-        if (runOut)
-            return;
-        runOut = true;
-        errors++;
-        errorConsumer.print("ERROR: Out of serials!", null);
-        System.out.println("de.jplag.text.Parser: ERROR: Out of serials!");
-    }
 }

+ 13 - 14
jplag.frontend.text/src/main/java/de/jplag/text/ParserToken.java

@@ -6,23 +6,22 @@ public class ParserToken extends Token {
     /**
      * This variable holds the line number of the current token.
      */
-    private int _line = -1;
+    private int line = -1;
 
     /**
      * This variable holds the column of the current token in its line.
      */
-    private int _column = -1;
+    private int column = -1;
 
     /**
      * This variable holds the label of the current token.
      */
-    private String _text = null;
+    private String text = null;
 
     /**
      * This variable holds the identifier of the current token.
      */
-
-    private int _id = -1;
+    private int id = -1;
 
     public ParserToken() {
         super();
@@ -30,44 +29,44 @@ public class ParserToken extends Token {
 
     @Override
     public void setLine(int line) {
-        _line = line;
+        this.line = line;
     }
 
     @Override
     public void setColumn(int column) {
-        _column = column;
+        this.column = column;
     }
 
     public void setID(int id) {
-        _id = id;
+        this.id = id;
     }
 
     @Override
     public void setText(String text) {
-        _text = (text != null ? text.intern() : null);
+        this.text = (text != null ? text.intern() : null);
     }
 
     @Override
     public int getColumn() {
-        return _column;
+        return column;
     }
 
     @Override
     public int getLine() {
-        return _line;
+        return line;
     }
 
     @Override
     public String getText() {
-        return _text;
+        return text;
     }
 
     public int getID() {
-        return _id;
+        return id;
     }
 
     public int getLength() {
-        return _text.length();
+        return text.length();
     }
 
     @Override