Added support for Swift to CPD.
The tokenizer uses the ANTLR4 grammar of the Tailor static analyzer for Swift. (https://github.com/sleekbyte/tailor)
This commit is contained in:
parent
aab525196c
commit
92678c0c0a
@ -128,6 +128,11 @@
|
||||
<artifactId>pmd-ruby</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.sourceforge.pmd</groupId>
|
||||
<artifactId>pmd-swift</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.sourceforge.pmd</groupId>
|
||||
<artifactId>pmd-vm</artifactId>
|
||||
|
72
pmd-swift/pom.xml
Normal file
72
pmd-swift/pom.xml
Normal file
@ -0,0 +1,72 @@
|
||||
<?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-swift</artifactId>
|
||||
<name>PMD Swift</name>
|
||||
|
||||
<parent>
|
||||
<groupId>net.sourceforge.pmd</groupId>
|
||||
<artifactId>pmd</artifactId>
|
||||
<version>5.3.7-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<antlr.version>4.5.2-1</antlr.version>
|
||||
<config.basedir>${basedir}/../pmd-core</config.basedir>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.antlr</groupId>
|
||||
<artifactId>antlr4-maven-plugin</artifactId>
|
||||
<version>${antlr.version}</version>
|
||||
<configuration>
|
||||
<encoding>UTF-8</encoding>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>antlr</id>
|
||||
<goals>
|
||||
<goal>antlr4</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</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>
|
||||
<version>${antlr.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>net.sourceforge.pmd</groupId>
|
||||
<artifactId>pmd-core</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,19 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
package net.sourceforge.pmd.cpd;
|
||||
|
||||
import net.sourceforge.pmd.cpd.SwiftTokenizer;
|
||||
|
||||
/**
|
||||
* Language implementation for Swift
|
||||
*/
|
||||
public class SwiftLanguage extends AbstractLanguage {
|
||||
|
||||
/**
|
||||
* Creates a new Swift Language instance.
|
||||
*/
|
||||
public SwiftLanguage() {
|
||||
super("Swift", "swift", new SwiftTokenizer(), ".swift");
|
||||
}
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
package net.sourceforge.pmd.cpd;
|
||||
|
||||
import net.sourceforge.pmd.lang.ast.TokenMgrError;
|
||||
import net.sourceforge.pmd.lang.swift.antlr4.SwiftLexer;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* The Swift Tokenizer
|
||||
*/
|
||||
public class SwiftTokenizer implements Tokenizer {
|
||||
|
||||
@Override
|
||||
public void tokenize(SourceCode sourceCode, Tokens tokenEntries) {
|
||||
StringBuilder buffer = sourceCode.getCodeBuffer();
|
||||
|
||||
try {
|
||||
ANTLRInputStream ais = new ANTLRInputStream(buffer.toString());
|
||||
SwiftLexer lexer = new SwiftLexer(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 Swift 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;
|
||||
|
||||
public 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,25 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
package net.sourceforge.pmd.lang.swift;
|
||||
|
||||
import net.sourceforge.pmd.lang.BaseLanguageModule;
|
||||
|
||||
/**
|
||||
* Language Module for Swift
|
||||
*/
|
||||
public class SwiftLanguageModule extends BaseLanguageModule {
|
||||
|
||||
/** The name. */
|
||||
public static final String NAME = "Swift";
|
||||
/** The terse name. */
|
||||
public static final String TERSE_NAME = "swift";
|
||||
|
||||
/**
|
||||
* Create a new instance of Swift Language Module.
|
||||
*/
|
||||
public SwiftLanguageModule() {
|
||||
super(NAME, null, TERSE_NAME, null, "swift");
|
||||
addVersion("", null, true);
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
net.sourceforge.pmd.cpd.SwiftLanguage
|
@ -0,0 +1 @@
|
||||
net.sourceforge.pmd.lang.swift.SwiftLanguageModule
|
3
pmd-swift/src/site/markdown/index.md
Normal file
3
pmd-swift/src/site/markdown/index.md
Normal file
@ -0,0 +1,3 @@
|
||||
# PMD Swift
|
||||
|
||||
Only CPD is supported. There are no PMD rules for Swift.
|
@ -0,0 +1,27 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
package net.sourceforge.pmd;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import net.sourceforge.pmd.lang.LanguageRegistry;
|
||||
import net.sourceforge.pmd.lang.LanguageVersion;
|
||||
import net.sourceforge.pmd.lang.swift.SwiftLanguageModule;
|
||||
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
|
||||
public class LanguageVersionTest extends AbstractLanguageVersionTest {
|
||||
|
||||
public LanguageVersionTest(String name, String terseName, String version, LanguageVersion expected) {
|
||||
super(name, terseName, version, expected);
|
||||
}
|
||||
|
||||
@Parameters
|
||||
public static Collection<Object[]> data() {
|
||||
return Arrays.asList(new Object[][] {
|
||||
{ SwiftLanguageModule.NAME, SwiftLanguageModule.TERSE_NAME, "", LanguageRegistry.getLanguage(SwiftLanguageModule.NAME).getDefaultVersion() }
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
package net.sourceforge.pmd.cpd;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import net.sourceforge.pmd.testframework.AbstractTokenizerTest;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class SwiftTokenizerTest extends AbstractTokenizerTest {
|
||||
|
||||
private static final String FILENAME = "BTree.swift";
|
||||
|
||||
@Before
|
||||
@Override
|
||||
public void buildTokenizer() throws IOException {
|
||||
this.tokenizer = new SwiftTokenizer();
|
||||
this.sourceCode = new SourceCode(new SourceCode.StringCodeLoader(this.getSampleCode(), FILENAME));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSampleCode() throws IOException {
|
||||
return IOUtils.toString(SwiftTokenizer.class.getResourceAsStream(FILENAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenizeTest() throws IOException {
|
||||
this.expectedTokenCount = 3811;
|
||||
super.tokenizeTest();
|
||||
}
|
||||
}
|
||||
|
890
pmd-swift/src/test/resources/net/sourceforge/pmd/cpd/BTree.swift
Normal file
890
pmd-swift/src/test/resources/net/sourceforge/pmd/cpd/BTree.swift
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user