5151import com .sun .tools .javac .parser .Tokens .Comment .CommentStyle ;
5252import com .sun .tools .javac .tree .DCTree .DCDocComment ;
5353import com .sun .tools .javac .tree .JCTree ;
54- import com .sun .tools .javac .tree .TreeInfo ;
5554import com .sun .tools .javac .tree .JCTree .JCAnnotatedType ;
5655import com .sun .tools .javac .tree .JCTree .JCAnnotation ;
5756import com .sun .tools .javac .tree .JCTree .JCAnyPattern ;
@@ -2010,6 +2009,16 @@ private Expression convertExpression(JCExpression javac) {
20102009
20112010 // Handle errors or default situation
20122011 if (javac instanceof JCErroneous error ) {
2012+ int pos = javac .getPreferredPosition ();
2013+ char c = this .rawText .length () > pos ? this .rawText .charAt (pos ) : 0 ;
2014+ if (error .getErrorTrees ().isEmpty () && c == '"' ) {
2015+ int newLine = this .rawText .indexOf ('\n' , pos );
2016+ int lineEnd = newLine == -1 ? this .rawText .length () - 1 : newLine ;
2017+ String litText = this .rawText .substring (pos +1 , lineEnd );
2018+ Expression res = convertStringToLiteral (litText , pos , lineEnd , null );
2019+ res .setSourceRange (pos , lineEnd - pos );
2020+ return res ;
2021+ }
20132022 if (error .getErrorTrees ().size () == 1 ) {
20142023 JCTree tree = error .getErrorTrees ().get (0 );
20152024 if (tree instanceof JCExpression nestedExpr ) {
@@ -2267,43 +2276,7 @@ private Expression convertLiteral(JCLiteral literal) {
22672276 }
22682277 }
22692278 if (value instanceof String string ) {
2270- boolean malformed = false ;
2271- if (this .rawText .charAt (literal .pos ) == '"'
2272- && this .rawText .charAt (literal .pos + 1 ) == '"'
2273- && this .rawText .charAt (literal .pos + 2 ) == '"' ) {
2274- if (this .ast .apiLevel () > AST .JLS14 ) {
2275- TextBlock res = this .ast .newTextBlock ();
2276- commonSettings (res , literal );
2277- String rawValue = this .rawText .substring (literal .pos , literal .getEndPosition (this .javacCompilationUnit .endPositions ));
2278- res .internalSetEscapedValue (rawValue , string );
2279- return res ;
2280- }
2281- malformed = true ;
2282- }
2283- StringLiteral res = this .ast .newStringLiteral ();
2284- commonSettings (res , literal );
2285- int startPos = res .getStartPosition ();
2286- int len = res .getLength ();
2287- if ( string .length () != len && len > 2 ) {
2288- try {
2289- string = this .rawText .substring (startPos , startPos + len );
2290- if (!string .startsWith ("\" " )) {
2291- string = '"' + string ;
2292- }
2293- if (!string .endsWith ("\" " )) {
2294- string = string + '"' ;
2295- }
2296- res .internalSetEscapedValue (string );
2297- } catch (IndexOutOfBoundsException ignore ) {
2298- res .setLiteralValue (string ); // TODO: we want the token here
2299- }
2300- } else {
2301- res .setLiteralValue (string ); // TODO: we want the token here
2302- }
2303- if (malformed ) {
2304- res .setFlags (res .getFlags () | ASTNode .MALFORMED );
2305- }
2306- return res ;
2279+ return convertStringToLiteral (string , literal .pos , literal .getEndPosition (this .javacCompilationUnit .endPositions ), literal );
23072280 }
23082281 if (value instanceof Boolean string ) {
23092282 BooleanLiteral res = this .ast .newBooleanLiteral (string .booleanValue ());
@@ -2324,6 +2297,45 @@ private Expression convertLiteral(JCLiteral literal) {
23242297 throw new UnsupportedOperationException ("Not supported yet " + literal + "\n of type" + literal .getClass ().getName ());
23252298 }
23262299
2300+ private Expression convertStringToLiteral (String string , int pos , int endPos , JCLiteral literal ) {
2301+ boolean malformed = false ;
2302+ if (this .rawText .charAt (pos ) == '"'
2303+ && this .rawText .charAt (pos + 1 ) == '"'
2304+ && this .rawText .charAt (pos + 2 ) == '"' ) {
2305+ if (this .ast .apiLevel () > AST .JLS14 ) {
2306+ TextBlock res = this .ast .newTextBlock ();
2307+ commonSettings (res , literal );
2308+ String rawValue = this .rawText .substring (pos , endPos );
2309+ res .internalSetEscapedValue (rawValue , string );
2310+ return res ;
2311+ }
2312+ malformed = true ;
2313+ }
2314+ StringLiteral res = this .ast .newStringLiteral ();
2315+ commonSettings (res , literal );
2316+ int startPos = res .getStartPosition ();
2317+ int len = res .getLength ();
2318+ if ( string .length () != len && len > 2 ) {
2319+ try {
2320+ string = this .rawText .substring (startPos , startPos + len );
2321+ if (!string .startsWith ("\" " )) {
2322+ string = '"' + string ;
2323+ }
2324+ if (!string .endsWith ("\" " )) {
2325+ string = string + '"' ;
2326+ }
2327+ res .internalSetEscapedValue (string );
2328+ } catch (IndexOutOfBoundsException ignore ) {
2329+ res .setLiteralValue (string ); // TODO: we want the token here
2330+ }
2331+ } else {
2332+ res .setLiteralValue (string ); // TODO: we want the token here
2333+ }
2334+ if (malformed ) {
2335+ res .setFlags (res .getFlags () | ASTNode .MALFORMED );
2336+ }
2337+ return res ;
2338+ }
23272339 private Statement convertStatement (JCStatement javac , ASTNode parent ) {
23282340 int endPos = TreeInfo .getEndPos (javac , this .javacCompilationUnit .endPositions );
23292341 int preferredPos = javac .getPreferredPosition ();
0 commit comments