Add CPD support for antlr based grammar on Golang

This commit is contained in:
Matias Fraga
2018-10-14 19:00:12 -03:00
parent 03983d8481
commit 56809ca824
8 changed files with 2120 additions and 20 deletions

View File

@ -12,6 +12,10 @@
<build>
<plugins>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
@ -24,6 +28,10 @@
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-core</artifactId>

File diff suppressed because it is too large Load Diff

View File

@ -4,29 +4,20 @@
package net.sourceforge.pmd.cpd;
import java.util.ArrayList;
import org.antlr.v4.runtime.CharStream;
import net.sourceforge.pmd.lang.antlr.AntlrTokenManager;
import net.sourceforge.pmd.lang.go.antlr4.GolangLexer;
/**
* Implements a tokenizer for the Go Language.
*
* @author oinume@gmail.com
* The Go tokenizer.
*/
public class GoTokenizer extends AbstractTokenizer {
public class GoTokenizer extends AntlrTokenizer {
/**
* Creates a new {@link GoTokenizer}
*/
public GoTokenizer() {
// setting markers for "string" in Go
this.stringToken = new ArrayList<String>();
this.stringToken.add("\"");
this.stringToken.add("`");
// setting markers for 'ignorable character' in Go
this.ignorableCharacter = new ArrayList<String>();
this.ignorableCharacter.add(";");
// setting markers for 'ignorable string' in Go
this.ignorableStmt = new ArrayList<String>();
@Override
protected AntlrTokenManager getLexerForSource(SourceCode sourceCode) {
CharStream charStream = AntlrTokenizer.getCharStreamFromSourceCode(sourceCode);
return new AntlrTokenManager(new GolangLexer(charStream), sourceCode.getFileName());
}
}

View File

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

View File

@ -0,0 +1 @@
net.sourceforge.pmd.lang.go.GoLanguageModule

View File

@ -0,0 +1,29 @@
/**
* 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 org.junit.runners.Parameterized.Parameters;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersion;
import net.sourceforge.pmd.lang.go.GoLanguageModule;
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[][] { { GoLanguageModule.NAME, GoLanguageModule.TERSE_NAME, "",
LanguageRegistry.getLanguage(GoLanguageModule.NAME).getDefaultVersion(), }, });
}
}

View File

@ -0,0 +1,36 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cpd;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Test;
import net.sourceforge.pmd.testframework.AbstractTokenizerTest;
public class GoTokenizerTest extends AbstractTokenizerTest {
private static final String FILENAME = "btrfs.go";
@Before
@Override
public void buildTokenizer() throws IOException {
this.tokenizer = new GoTokenizer();
this.sourceCode = new SourceCode(new SourceCode.StringCodeLoader(this.getSampleCode(), FILENAME));
}
@Override
public String getSampleCode() throws IOException {
return IOUtils.toString(GoTokenizer.class.getResourceAsStream(FILENAME));
}
@Test
public void tokenizeTest() throws IOException {
this.expectedTokenCount = 3517;
super.tokenizeTest();
}
}

File diff suppressed because it is too large Load Diff