From 4e72eef49576c5b28e41049c6a9784a4a05eca56 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 20 May 2017 21:28:39 +0200 Subject: [PATCH] [java] Parser Error for empty statements Fixes #378 --- pmd-java/etc/grammar/Java.jjt | 13 +++++++---- .../pmd/lang/java/ast/ParserCornersTest.java | 23 +++++++++++++++++++ src/site/markdown/overview/changelog.md | 2 ++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index 4c84fe58df..fefd84165f 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -1,4 +1,9 @@ /** + * Allow empty statements (";") between package, import + * and type declarations. + * Bug #378 + * Andreas Dangel 05/2017 + *==================================================================== * Allow method references to specify generics also for * constructor references ("new"). * Bug #309 @@ -1252,9 +1257,9 @@ TOKEN : ASTCompilationUnit CompilationUnit() : {} { - [ LOOKAHEAD( ( Annotation() )* "package" ) PackageDeclaration() ] - ( ImportDeclaration() )* - ( TypeDeclaration() )* + [ LOOKAHEAD( ( Annotation() )* "package" ) PackageDeclaration() ( EmptyStatement() )* ] + ( ImportDeclaration() ( EmptyStatement() )* )* + ( TypeDeclaration() ( EmptyStatement() )* )* ( < "\u001a" > )? ( < "~[]" > )? @@ -1317,8 +1322,6 @@ void TypeDeclaration(): int modifiers; } { - ";" -| modifiers = Modifiers() ( ClassOrInterfaceDeclaration(modifiers) diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ParserCornersTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ParserCornersTest.java index 31a29eb39d..251a254935 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ParserCornersTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ParserCornersTest.java @@ -194,6 +194,29 @@ public class ParserCornersTest extends ParserTst { Assert.assertEquals(0, cu.findDescendantsOfType(ASTCastExpression.class).size()); } + /** + * Empty statements should be allowed. + * @throws Exception + * @see https://github.com/pmd/pmd/issues/378 + */ + @Test + public void testParseEmptyStatements() throws Exception { + String code = "import a;;import b; public class Foo {}"; + ASTCompilationUnit cu = parseJava18(code); + assertNotNull(cu); + Assert.assertEquals(ASTEmptyStatement.class, cu.jjtGetChild(1).getClass()); + + String code2 = "package c;; import a; import b; public class Foo {}"; + ASTCompilationUnit cu2 = parseJava18(code2); + assertNotNull(cu2); + Assert.assertEquals(ASTEmptyStatement.class, cu2.jjtGetChild(1).getClass()); + + String code3 = "package c; import a; import b; public class Foo {};"; + ASTCompilationUnit cu3 = parseJava18(code3); + assertNotNull(cu3); + Assert.assertEquals(ASTEmptyStatement.class, cu3.jjtGetChild(4).getClass()); + } + private String readAsString(String resource) { InputStream in = ParserCornersTest.class.getResourceAsStream(resource); try { diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index 18fac2e101..e47a56ab54 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -38,6 +38,8 @@ This is a minor release. * General * [#377](https://github.com/pmd/pmd/issues/377): \[core] Use maven wrapper and upgrade to maven 3.5.0 * [#376](https://github.com/pmd/pmd/issues/376): \[core] Improve build time on travis +* java + * [#378](https://github.com/pmd/pmd/issues/378): \[java] Parser Error for empty statements * java-coupling * [#1427](https://sourceforge.net/p/pmd/bugs/1427/): \[java] Law of Demeter violations for the Builder pattern * java-design