@@ -1897,6 +1897,16 @@ private Expression convertExpression(JCExpression javac) {
18971897
18981898 // Handle errors or default situation
18991899 if (javac instanceof JCErroneous error ) {
1900+ int pos = javac .getPreferredPosition ();
1901+ char c = this .rawText .length () > pos ? this .rawText .charAt (pos ) : 0 ;
1902+ if (error .getErrorTrees ().isEmpty () && c == '"' ) {
1903+ int newLine = this .rawText .indexOf ('\n' , pos );
1904+ int lineEnd = newLine == -1 ? this .rawText .length () - 1 : newLine ;
1905+ String litText = this .rawText .substring (pos +1 , lineEnd );
1906+ Expression res = convertStringToLiteral (litText , pos , lineEnd , null );
1907+ res .setSourceRange (pos , lineEnd - pos );
1908+ return res ;
1909+ }
19001910 if (error .getErrorTrees ().size () == 1 ) {
19011911 JCTree tree = error .getErrorTrees ().get (0 );
19021912 if (tree instanceof JCExpression nestedExpr ) {
@@ -2144,43 +2154,7 @@ private Expression convertLiteral(JCLiteral literal) {
21442154 }
21452155 }
21462156 if (value instanceof String string ) {
2147- boolean malformed = false ;
2148- if (this .rawText .charAt (literal .pos ) == '"'
2149- && this .rawText .charAt (literal .pos + 1 ) == '"'
2150- && this .rawText .charAt (literal .pos + 2 ) == '"' ) {
2151- if (this .ast .apiLevel () > AST .JLS14 ) {
2152- TextBlock res = this .ast .newTextBlock ();
2153- commonSettings (res , literal );
2154- String rawValue = this .rawText .substring (literal .pos , literal .getEndPosition (this .javacCompilationUnit .endPositions ));
2155- res .internalSetEscapedValue (rawValue , string );
2156- return res ;
2157- }
2158- malformed = true ;
2159- }
2160- StringLiteral res = this .ast .newStringLiteral ();
2161- commonSettings (res , literal );
2162- int startPos = res .getStartPosition ();
2163- int len = res .getLength ();
2164- if ( string .length () != len && len > 2 ) {
2165- try {
2166- string = this .rawText .substring (startPos , startPos + len );
2167- if (!string .startsWith ("\" " )) {
2168- string = '"' + string ;
2169- }
2170- if (!string .endsWith ("\" " )) {
2171- string = string + '"' ;
2172- }
2173- res .internalSetEscapedValue (string );
2174- } catch (IndexOutOfBoundsException ignore ) {
2175- res .setLiteralValue (string ); // TODO: we want the token here
2176- }
2177- } else {
2178- res .setLiteralValue (string ); // TODO: we want the token here
2179- }
2180- if (malformed ) {
2181- res .setFlags (res .getFlags () | ASTNode .MALFORMED );
2182- }
2183- return res ;
2157+ return convertStringToLiteral (string , literal .pos , literal .getEndPosition (this .javacCompilationUnit .endPositions ), literal );
21842158 }
21852159 if (value instanceof Boolean string ) {
21862160 BooleanLiteral res = this .ast .newBooleanLiteral (string .booleanValue ());
@@ -2201,6 +2175,45 @@ private Expression convertLiteral(JCLiteral literal) {
22012175 throw new UnsupportedOperationException ("Not supported yet " + literal + "\n of type" + literal .getClass ().getName ());
22022176 }
22032177
2178+ private Expression convertStringToLiteral (String string , int pos , int endPos , JCLiteral literal ) {
2179+ boolean malformed = false ;
2180+ if (this .rawText .charAt (pos ) == '"'
2181+ && this .rawText .charAt (pos + 1 ) == '"'
2182+ && this .rawText .charAt (pos + 2 ) == '"' ) {
2183+ if (this .ast .apiLevel () > AST .JLS14 ) {
2184+ TextBlock res = this .ast .newTextBlock ();
2185+ commonSettings (res , literal );
2186+ String rawValue = this .rawText .substring (pos , endPos );
2187+ res .internalSetEscapedValue (rawValue , string );
2188+ return res ;
2189+ }
2190+ malformed = true ;
2191+ }
2192+ StringLiteral res = this .ast .newStringLiteral ();
2193+ commonSettings (res , literal );
2194+ int startPos = res .getStartPosition ();
2195+ int len = res .getLength ();
2196+ if ( string .length () != len && len > 2 ) {
2197+ try {
2198+ string = this .rawText .substring (startPos , startPos + len );
2199+ if (!string .startsWith ("\" " )) {
2200+ string = '"' + string ;
2201+ }
2202+ if (!string .endsWith ("\" " )) {
2203+ string = string + '"' ;
2204+ }
2205+ res .internalSetEscapedValue (string );
2206+ } catch (IndexOutOfBoundsException ignore ) {
2207+ res .setLiteralValue (string ); // TODO: we want the token here
2208+ }
2209+ } else {
2210+ res .setLiteralValue (string ); // TODO: we want the token here
2211+ }
2212+ if (malformed ) {
2213+ res .setFlags (res .getFlags () | ASTNode .MALFORMED );
2214+ }
2215+ return res ;
2216+ }
22042217 private Statement convertStatement (JCStatement javac , ASTNode parent ) {
22052218 int endPos = TreeInfo .getEndPos (javac , this .javacCompilationUnit .endPositions );
22062219 int preferredPos = javac .getPreferredPosition ();
0 commit comments