1
0
forked from phoedos/pmd

Merge branch 'pr-1386'

This commit is contained in:
Juan Martín Sotuyo Dodero
2018-10-15 01:50:55 -03:00
10 changed files with 2119 additions and 23 deletions
docs/pages
pmd-core/src/main/java/net/sourceforge/pmd/cpd/token
pmd-go
pom.xml
src
main
antlr4/net/sourceforge/pmd/lang/go/antlr4
java/net/sourceforge/pmd
resources/META-INF/services
test
java/net/sourceforge/pmd
resources/net/sourceforge/pmd/cpd

@ -43,6 +43,7 @@ as comments are recognized as such and ignored.
* [#1376](https://github.com/pmd/pmd/pull/1376): \[core] Upgrading Apache Commons IO from 2.4 to 2.6 - [Thunderforge](https://github.com/Thunderforge)
* [#1378](https://github.com/pmd/pmd/pull/1378): \[core] Upgrading Apache Commons Lang 3 from 3.7 to 3.8.1 - [Thunderforge](https://github.com/Thunderforge)
* [#1383](https://github.com/pmd/pmd/pull/1383): \[java] Improved message for GuardLogStatement rule - [Felix Lampe](https://github.com/fblampe)
* [#1386](https://github.com/pmd/pmd/pull/1386): \[go] [cpd] Add CPD support for Antlr based grammar on Golang - [Matías Fraga](https://github.com/matifraga)
{% endtocmaker %}

@ -9,8 +9,6 @@ import org.antlr.v4.runtime.Token;
import net.sourceforge.pmd.lang.ast.GenericToken;
import com.beust.jcommander.internal.Nullable;
/**
* Generic Antlr representation of a token.
*/
@ -25,7 +23,7 @@ public class AntlrToken implements GenericToken {
* @param token The antlr token implementation
* @param previousComment The previous comment
*/
public AntlrToken(final Token token, @Nullable final AntlrToken previousComment) {
public AntlrToken(final Token token, final AntlrToken previousComment) {
this.token = token;
this.previousComment = previousComment;
}

@ -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

@ -4,29 +4,19 @@
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());
}
}

@ -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 Golang Language Module.
*/
public GoLanguageModule() {
super(NAME, null, TERSE_NAME, null, "go");
addVersion("1", null, true);
}
}

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

@ -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 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(), }, });
}
}

@ -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