Added Kotlin support to CPD.

The tokenizer uses the ANTLR4 grammar based on the one at https://github.com/shadrina/kotlin-grammar-antlr4 (via https://github.com/antlr/grammars-v4/).
This commit is contained in:
Maikel Steneker
2018-10-12 12:21:56 +01:00
parent e9d78ede3c
commit ff55a230d5
9 changed files with 2425 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,18 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cpd;
/**
* Language implementation for Kotlin
*/
public class KotlinLanguage extends AbstractLanguage {
/**
* Creates a new Kotlin Language instance.
*/
public KotlinLanguage() {
super("Kotlin", "kotlin", new KotlinTokenizer(), ".kt");
}
}

View File

@ -0,0 +1,83 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cpd;
import net.sourceforge.pmd.lang.kotlin.antlr4.Kotlin;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.Token;
import net.sourceforge.pmd.lang.ast.TokenMgrError;
//import net.sourceforge.pmd.lang.kotlin.antlr4.KotlinLexer;
/**
* The Kotlin Tokenizer
*/
public class KotlinTokenizer implements Tokenizer {
@Override
public void tokenize(SourceCode sourceCode, Tokens tokenEntries) {
StringBuilder buffer = sourceCode.getCodeBuffer();
try {
ANTLRInputStream ais = new ANTLRInputStream(buffer.toString());
Kotlin lexer = new Kotlin(ais);
lexer.removeErrorListeners();
lexer.addErrorListener(new ErrorHandler());
Token token = lexer.nextToken();
while (token.getType() != Token.EOF) {
if (token.getChannel() != Lexer.HIDDEN) {
TokenEntry tokenEntry = new TokenEntry(token.getText(), sourceCode.getFileName(), token.getLine());
tokenEntries.add(tokenEntry);
}
token = lexer.nextToken();
}
} catch (ANTLRSyntaxError err) {
// Wrap exceptions of the Kotlin tokenizer in a TokenMgrError, so
// they are correctly handled
// when CPD is executed with the '--skipLexicalErrors' command line
// option
throw new TokenMgrError("Lexical error in file " + sourceCode.getFileName() + " at line " + err.getLine()
+ ", column " + err.getColumn() + ". Encountered: " + err.getMessage(),
TokenMgrError.LEXICAL_ERROR);
} finally {
tokenEntries.add(TokenEntry.getEOF());
}
}
private static class ErrorHandler extends BaseErrorListener {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine,
String msg, RecognitionException ex) {
throw new ANTLRSyntaxError(msg, line, charPositionInLine, ex);
}
}
private static class ANTLRSyntaxError extends RuntimeException {
private static final long serialVersionUID = 1L;
private final int line;
private final int column;
ANTLRSyntaxError(String msg, int line, int column, RecognitionException cause) {
super(msg, cause);
this.line = line;
this.column = column;
}
public int getLine() {
return line;
}
public int getColumn() {
return column;
}
}
}

View File

@ -0,0 +1,26 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.kotlin;
import net.sourceforge.pmd.lang.BaseLanguageModule;
/**
* Language Module for Kotlin
*/
public class KotlinLanguageModule extends BaseLanguageModule {
/** The name. */
public static final String NAME = "Kotlin";
/** The terse name. */
public static final String TERSE_NAME = "kotlin";
/**
* Create a new instance of Kotlin Language Module.
*/
public KotlinLanguageModule() {
super(NAME, null, TERSE_NAME, null, "kotlin");
addVersion("", null, true);
}
}

View File

@ -0,0 +1 @@
net.sourceforge.pmd.cpd.KotlinLanguage

View File

@ -0,0 +1 @@
net.sourceforge.pmd.lang.kotlin.KotlinLanguageModule