forked from phoedos/pmd
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:
@ -117,6 +117,11 @@
|
|||||||
<artifactId>pmd-visualforce</artifactId>
|
<artifactId>pmd-visualforce</artifactId>
|
||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.sourceforge.pmd</groupId>
|
||||||
|
<artifactId>pmd-kotlin</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>net.sourceforge.pmd</groupId>
|
<groupId>net.sourceforge.pmd</groupId>
|
||||||
<artifactId>pmd-matlab</artifactId>
|
<artifactId>pmd-matlab</artifactId>
|
||||||
|
57
pmd-kotlin/pom.xml
Normal file
57
pmd-kotlin/pom.xml
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>pmd-kotlin</artifactId>
|
||||||
|
<name>PMD Kotlin</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>net.sourceforge.pmd</groupId>
|
||||||
|
<artifactId>pmd</artifactId>
|
||||||
|
<version>6.4.0</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.antlr</groupId>
|
||||||
|
<artifactId>antlr4-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-resources-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<useDefaultDelimiters>false</useDefaultDelimiters>
|
||||||
|
<delimiters>
|
||||||
|
<delimiter>${*}</delimiter>
|
||||||
|
</delimiters>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.antlr</groupId>
|
||||||
|
<artifactId>antlr4-runtime</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.sourceforge.pmd</groupId>
|
||||||
|
<artifactId>pmd-core</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-io</groupId>
|
||||||
|
<artifactId>commons-io</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.sourceforge.pmd</groupId>
|
||||||
|
<artifactId>pmd-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
File diff suppressed because it is too large
Load Diff
@ -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");
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
net.sourceforge.pmd.cpd.KotlinLanguage
|
@ -0,0 +1 @@
|
|||||||
|
net.sourceforge.pmd.lang.kotlin.KotlinLanguageModule
|
1
pom.xml
1
pom.xml
@ -985,6 +985,7 @@ Additionally it includes CPD, the copy-paste-detector. CPD finds duplicated code
|
|||||||
<module>pmd-java</module>
|
<module>pmd-java</module>
|
||||||
<module>pmd-javascript</module>
|
<module>pmd-javascript</module>
|
||||||
<module>pmd-jsp</module>
|
<module>pmd-jsp</module>
|
||||||
|
<module>pmd-kotlin</module>
|
||||||
<module>pmd-matlab</module>
|
<module>pmd-matlab</module>
|
||||||
<module>pmd-objectivec</module>
|
<module>pmd-objectivec</module>
|
||||||
<module>pmd-perl</module>
|
<module>pmd-perl</module>
|
||||||
|
Reference in New Issue
Block a user