[java] Adjust grammar for text blocks

Refs #1973
This commit is contained in:
Andreas Dangel
2019-09-06 15:21:08 +02:00
parent 604ef548c9
commit f3e4a77105
3 changed files with 26 additions and 23 deletions

View File

@ -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"]
)
)
| <STRING_ESCAPE>
)*
"\""
>
}
MORE :
{
< "\"\"\"" > : IN_TEXT_BLOCK
}
<IN_TEXT_BLOCK> TOKEN :
{
< TEXT_BLOCK_LITERAL: "\"\"\""> : DEFAULT
}
<IN_TEXT_BLOCK> MORE :
{
< ~[] >
|
< TEXT_BLOCK_LITERAL:
"\"\"\"" (<HORIZONTAL_WHITESPACE>)* <LINE_TERMINATOR>
( ~["\"", "\\"] | "\"" ~["\""] | "\"\"" ~["\""] | <STRING_ESCAPE> )*
"\"\"\""
>
}
/* IDENTIFIERS */

View File

@ -74,7 +74,7 @@ public class Java13Test {
ASTCompilationUnit compilationUnit = ParserTstUtil.parseAndTypeResolveJava("13", loadSource("TextBlocks.java"));
Assert.assertNotNull(compilationUnit);
List<ASTLiteral> 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 {
+ " <p>Hello, world</p>\n" + " </body>\n"
+ " </html>\n" + " \"\"\"", literals.get(0).getImage());
Assert.assertFalse(literals.get(8).isTextBlock());
Assert.assertTrue(literals.get(9).isTextBlock());
}
@Test(expected = ParseException.class)

View File

@ -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
\""";
""";
}
}