Skip to content

Commit

Permalink
Support textblock in Annotations (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfscholte authored Nov 23, 2024
1 parent 5506daf commit 1112792
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/grammar/lexer.flex
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ UnicodeChar = \\u[a-fA-F0-9]{4}
Id = ([:jletter:]|{UnicodeChar}) ([:jletterdigit:]|{UnicodeChar})*
JavadocEnd = "*"+ "/"

%state JAVADOC JAVADOCTAG JAVADOCLINE CODEBLOCK PARENBLOCK ASSIGNMENT STRING CHAR SINGLELINECOMMENT MULTILINECOMMENT ANNOTATION ANNOSTRING ANNOCHAR ARGUMENTS NAME
%state JAVADOC JAVADOCTAG JAVADOCLINE CODEBLOCK PARENBLOCK ASSIGNMENT STRING CHAR SINGLELINECOMMENT MULTILINECOMMENT ANNOTATION ANNOSTRING ANNOCHAR ARGUMENTS NAME
%state ANNOTATIONTYPE ENUM MODULE RECORD TYPE ANNOTATIONNOARG ATANNOTATION
%state NAME_OR_MODIFIER

Expand Down Expand Up @@ -511,6 +511,7 @@ JavadocEnd = "*"+ "/"
"++" { return Parser.PLUSPLUS; }
"--" { return Parser.MINUSMINUS; }

"\"\"\"" { appendingToCodeBody=true; codeBody.append("\"\"\""); pushState(ANNOSTRING); }
"\"" { appendingToCodeBody=true; codeBody.append("\""); pushState(ANNOSTRING); }
"\'" { appendingToCodeBody=true; codeBody.append("\'"); pushState(ANNOCHAR); }

Expand Down Expand Up @@ -565,6 +566,7 @@ JavadocEnd = "*"+ "/"
}

<ANNOSTRING> {
"\"\"\"" { codeBody.append("\"\"\""); popState(); appendingToCodeBody=false; return Parser.TEXTBLOCK; }
"\"" { codeBody.append("\""); popState(); appendingToCodeBody=false; return Parser.STRING_LITERAL; }
"\\\"" { codeBody.append("\\\""); }
"\\\\" { codeBody.append("\\\\"); }
Expand Down
6 changes: 5 additions & 1 deletion src/grammar/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import java.util.Stack;
%token <sval> INTEGER_LITERAL
%token <sval> FLOAT_LITERAL
%token <sval> CHAR_LITERAL
%token <sval> STRING_LITERAL
%token <sval> STRING_LITERAL, TEXTBLOCK
%token <ival> VERTLINE2 AMPERSAND2 VERTLINE CIRCUMFLEX AMPERSAND EQUALS2 NOTEQUALS
%token <ival> LESSTHAN GREATERTHAN LESSEQUALS GREATEREQUALS LESSTHAN2 GREATERTHAN2 GREATERTHAN3
%token <ival> PLUS MINUS STAR SLASH PERCENT TILDE EXCLAMATION
Expand Down Expand Up @@ -2019,6 +2019,10 @@ Literal: INTEGER_LITERAL
{
String s = lexer.getCodeBody();
$$ = new ConstantDef(s, Character.class);
}
| TEXTBLOCK {
String s = lexer.getCodeBody();
$$ = new ConstantDef(s, String.class);
}
| STRING_LITERAL
{
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/com/thoughtworks/qdox/AnnotationsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,19 @@ public void testDoubleEscapedString()
String source = "public class Foo {\n" + "@SuppressWarnings({\"abc\\\\d\"})\n" + "private void bar() { } }";
builder.addSource( new StringReader( source ) );
}

@Test
public void testTextBlock() {
JavaProjectBuilder builder = new JavaProjectBuilder();
String source = "@SuppressWarnings(\"\"\"\n"
+ " Lorem ipsum dolor sit amet\n"
+ " \"\"\")\n"
+ "public class Thingy {\n"
+ "}";
builder.addSource( new StringReader( source ) );
Assertions.assertEquals("\"\"\"\n"
+ " Lorem ipsum dolor sit amet\n"
+ " \"\"\"", builder.getClassByName( "Thingy" ).getAnnotations().get( 0 ).getProperty( "value" ).getParameterValue());

}
}

0 comments on commit 1112792

Please sign in to comment.