diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 8b53655c31..5e8fe25beb 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -16,6 +16,8 @@ This is a {{ site.pmd.release_type }} release. ### Fixed Issues +* go + * [#1751](https://github.com/pmd/pmd/issues/1751): \[go] Parsing errors encountered with escaped backslash * java * [#1729](https://github.com/pmd/pmd/issues/1729): \[java] JavaRuleViolation loses information in `className` field when class has package-private access level * java-bestpractices diff --git a/pmd-go/src/main/antlr4/net/sourceforge/pmd/lang/go/antlr4/Golang.g4 b/pmd-go/src/main/antlr4/net/sourceforge/pmd/lang/go/antlr4/Golang.g4 index ec54cbac43..673c0b6682 100644 --- a/pmd-go/src/main/antlr4/net/sourceforge/pmd/lang/go/antlr4/Golang.g4 +++ b/pmd-go/src/main/antlr4/net/sourceforge/pmd/lang/go/antlr4/Golang.g4 @@ -917,10 +917,10 @@ RUNE_LIT //unicode_value = unicode_char | little_u_value | big_u_value | escaped_char . fragment UNICODE_VALUE - : UNICODE_CHAR - | LITTLE_U_VALUE + : LITTLE_U_VALUE | BIG_U_VALUE | ESCAPED_CHAR + | UNICODE_CHAR ; //byte_value = octal_byte_value | hex_byte_value . diff --git a/pmd-go/src/test/java/net/sourceforge/pmd/cpd/EdgeCasesTokenizerTest.java b/pmd-go/src/test/java/net/sourceforge/pmd/cpd/EdgeCasesTokenizerTest.java new file mode 100644 index 0000000000..1d56e989f0 --- /dev/null +++ b/pmd-go/src/test/java/net/sourceforge/pmd/cpd/EdgeCasesTokenizerTest.java @@ -0,0 +1,29 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.cpd; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +import org.apache.commons.io.IOUtils; +import org.junit.Test; + +public class EdgeCasesTokenizerTest { + + private String getSampleCode(final String filename) throws IOException { + return IOUtils.toString(GoTokenizer.class.getResourceAsStream(filename), StandardCharsets.UTF_8); + } + + @Test + public void testEscapedBackSlash() throws IOException { + // See https://github.com/pmd/pmd/issues/1751 + final String filename = "issue1751.go"; + final GoTokenizer tokenizer = new GoTokenizer(); + final SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader(getSampleCode(filename), filename)); + + final Tokens tokens = new Tokens(); + tokenizer.tokenize(sourceCode, tokens); // it should simply not fail + } +} diff --git a/pmd-go/src/test/resources/net/sourceforge/pmd/cpd/issue1751.go b/pmd-go/src/test/resources/net/sourceforge/pmd/cpd/issue1751.go new file mode 100644 index 0000000000..1e0042e68a --- /dev/null +++ b/pmd-go/src/test/resources/net/sourceforge/pmd/cpd/issue1751.go @@ -0,0 +1,6 @@ +func test(in *Value, param *Value) (*Value, *Error) { + output := strings.Replace(in.String(), "\\", "\\\\", -1) + output = strings.Replace(output, "\"", "\\\"", -1) + output = strings.Replace(output, "'", "\\'", -1) + return AsValue(output), nil +}