From f3e4a77105f81dbc3cc93dc6a2ce78941061a314 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Fri, 6 Sep 2019 15:21:08 +0200 Subject: [PATCH] [java] Adjust grammar for text blocks Refs #1973 --- pmd-java/etc/grammar/Java.jjt | 39 ++++++++----------- .../pmd/lang/java/ast/Java13Test.java | 3 +- .../jdkversiontests/java13/TextBlocks.java | 7 ++++ 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index 8f805e3073..8644268f9e 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -491,7 +491,8 @@ TOKEN_MGR_DECLS : SPECIAL_TOKEN : { - " " | "\t" | "\n" | "\r" | "\f" + < HORIZONTAL_WHITESPACE: [" ", "\t", "\f"] > +| < LINE_TERMINATOR: "\n" | "\r" | "\r\n" > } SPECIAL_TOKEN : @@ -655,34 +656,28 @@ TOKEN : ) "'" > +| < #STRING_ESCAPE: + "\\" + ( ["n","t","b","r","f","\\","'","\""] + // octal escapes + | ["0"-"7"] ( ["0"-"7"] )? + | ["0"-"3"] ["0"-"7"] ["0"-"7"] + ) + > | < STRING_LITERAL: "\"" ( (~["\"","\\","\n","\r"]) - | ("\\" - ( ["n","t","b","r","f","\\","'","\""] - | ["0"-"7"] ( ["0"-"7"] )? - | ["0"-"3"] ["0"-"7"] ["0"-"7"] - ) - ) + | )* "\"" > -} - -MORE : -{ - < "\"\"\"" > : IN_TEXT_BLOCK -} - - TOKEN : -{ - < TEXT_BLOCK_LITERAL: "\"\"\""> : DEFAULT -} - - MORE : -{ - < ~[] > +| + < TEXT_BLOCK_LITERAL: + "\"\"\"" ()* + ( ~["\"", "\\"] | "\"" ~["\""] | "\"\"" ~["\""] | )* + "\"\"\"" + > } /* IDENTIFIERS */ diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/Java13Test.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/Java13Test.java index df00d86777..f05fe41109 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/Java13Test.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/Java13Test.java @@ -74,7 +74,7 @@ public class Java13Test { ASTCompilationUnit compilationUnit = ParserTstUtil.parseAndTypeResolveJava("13", loadSource("TextBlocks.java")); Assert.assertNotNull(compilationUnit); List literals = compilationUnit.findDescendantsOfType(ASTLiteral.class); - Assert.assertEquals(9, literals.size()); + Assert.assertEquals(10, literals.size()); for (int i = 0; i < 8; i++) { ASTLiteral literal = literals.get(i); Assert.assertTrue(literal.isTextBlock()); @@ -83,6 +83,7 @@ public class Java13Test { + "

Hello, world

\n" + " \n" + " \n" + " \"\"\"", literals.get(0).getImage()); Assert.assertFalse(literals.get(8).isTextBlock()); + Assert.assertTrue(literals.get(9).isTextBlock()); } @Test(expected = ParseException.class) diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java13/TextBlocks.java b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java13/TextBlocks.java index 69c76cb9c6..0b24cfbe34 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java13/TextBlocks.java +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java13/TextBlocks.java @@ -44,5 +44,12 @@ public class TextBlocks { """; // the two characters \ LF String normalStringLiteral = "test"; + + String code = + """ + String text = \""" + A text block inside a text block + \"""; + """; } } \ No newline at end of file