Added Dart support to CPD.

The tokenizer uses an ANTLR4 grammar based on the one at https://github.com/antlr/grammars-v4/tree/master/dart2.
This commit is contained in:
Maikel Steneker
2019-04-05 15:19:28 +02:00
parent 58677de21d
commit 8db2026735
7 changed files with 1147 additions and 0 deletions

57
pmd-dart/pom.xml Normal file
View 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-dart</artifactId>
<name>PMD Dart</name>
<parent>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd</artifactId>
<version>6.14.0-SNAPSHOT</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

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 Dart
*/
public class DartLanguage extends AbstractLanguage {
/**
* Creates a new Dart Language instance.
*/
public DartLanguage() {
super("Dart", "dart", new DartTokenizer(), ".dart");
}
}

View File

@ -0,0 +1,70 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cpd;
import org.antlr.v4.runtime.CharStream;
import net.sourceforge.pmd.cpd.token.AntlrToken;
import net.sourceforge.pmd.cpd.token.AntlrTokenFilter;
import net.sourceforge.pmd.lang.antlr.AntlrTokenManager;
import net.sourceforge.pmd.lang.dart.antlr4.Dart2Lexer;
/**
* The Dart Tokenizer
*/
public class DartTokenizer extends AntlrTokenizer {
@Override
protected AntlrTokenManager getLexerForSource(SourceCode sourceCode) {
CharStream charStream = AntlrTokenizer.getCharStreamFromSourceCode(sourceCode);
return new AntlrTokenManager(new Dart2Lexer(charStream), sourceCode.getFileName());
}
@Override
protected AntlrTokenFilter getTokenFilter(final AntlrTokenManager tokenManager) {
return new DartTokenFilter(tokenManager);
}
/**
* The {@link DartTokenFilter} extends the {@link AntlrTokenFilter} to discard
* Dart-specific tokens.
* <p>
* By default, it discards package and import statements, and
* enables annotation-based CPD suppression.
* </p>
*/
private static class DartTokenFilter extends AntlrTokenFilter {
private boolean discardingPackageAndImport = false;
private boolean discardingNL = false;
/* default */ DartTokenFilter(final AntlrTokenManager tokenManager) {
super(tokenManager);
}
@Override
protected void analyzeToken(final AntlrToken currentToken) {
skipPackageAndImport(currentToken);
skipNewLines(currentToken);
}
private void skipPackageAndImport(final AntlrToken currentToken) {
final int type = currentToken.getType();
/*if (type == Dart.PACKAGE || type == Dart.IMPORT) {
discardingPackageAndImport = true;
} else if (discardingPackageAndImport && (type == Dart.SEMICOLON || type == Dart.NL)) {
discardingPackageAndImport = false;
}*/
}
private void skipNewLines(final AntlrToken currentToken) {
discardingNL = false;//currentToken.getType() == Dart.NL;
}
@Override
protected boolean isLanguageSpecificDiscarding() {
return discardingPackageAndImport || discardingNL;
}
}
}

View File

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

View File

@ -107,6 +107,11 @@
<artifactId>pmd-cs</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-dart</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-fortran</artifactId>

View File

@ -1130,6 +1130,7 @@ Additionally it includes CPD, the copy-paste-detector. CPD finds duplicated code
<module>pmd-core</module>
<module>pmd-cpp</module>
<module>pmd-cs</module>
<module>pmd-dart</module>
<module>pmd-dist</module>
<module>pmd-fortran</module>
<module>pmd-go</module>