8210596: jshell does not support raw string literals
authorrfield
Mon, 17 Sep 2018 08:37:47 -0700
changeset 51766 3ca7d5385653
parent 51765 e10ade04afe5
child 51767 497950fd69a7
8210596: jshell does not support raw string literals Reviewed-by: jlahoda, jlaskey
src/jdk.jshell/share/classes/jdk/jshell/CompletenessAnalyzer.java
src/jdk.jshell/share/classes/jdk/jshell/MaskCommentsAndModifiers.java
src/jdk.jshell/share/classes/jdk/jshell/SourceCodeAnalysisImpl.java
test/langtools/jdk/jshell/ToolLocalSimpleTest.java
test/langtools/jdk/jshell/ToolSimpleTest.java
--- a/src/jdk.jshell/share/classes/jdk/jshell/CompletenessAnalyzer.java	Mon Sep 17 10:53:56 2018 +0200
+++ b/src/jdk.jshell/share/classes/jdk/jshell/CompletenessAnalyzer.java	Mon Sep 17 08:37:47 2018 -0700
@@ -31,6 +31,7 @@
 import com.sun.tools.javac.parser.Tokens.Token;
 import com.sun.tools.javac.parser.Tokens.TokenKind;
 import com.sun.tools.javac.util.Context;
+import com.sun.tools.javac.util.DiagnosticSource;
 import com.sun.tools.javac.util.JCDiagnostic;
 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
@@ -121,6 +122,7 @@
 
         private CaLog(Context context, PrintWriter pw) {
             super(context, pw);
+            this.source = DiagnosticSource.NO_SOURCE;
         }
 
         @Override
--- a/src/jdk.jshell/share/classes/jdk/jshell/MaskCommentsAndModifiers.java	Mon Sep 17 10:53:56 2018 +0200
+++ b/src/jdk.jshell/share/classes/jdk/jshell/MaskCommentsAndModifiers.java	Mon Sep 17 08:37:47 2018 -0700
@@ -67,7 +67,7 @@
     private boolean maskModifiers;
 
     // Does the string end with an unclosed '/*' style comment?
-    private boolean openComment = false;
+    private boolean openToken = false;
 
     MaskCommentsAndModifiers(String s, boolean maskModifiers) {
         this.str = s;
@@ -88,8 +88,8 @@
         return sbMask.toString();
     }
 
-    boolean endsWithOpenComment() {
-        return openComment;
+    boolean endsWithOpenToken() {
+        return openToken;
     }
 
     /****** private implementation methods ******/
@@ -142,7 +142,7 @@
     private void next() {
         switch (c) {
             case '\'':
-            case '"':
+            case '"': {
                 maskModifiers = false;
                 write(c);
                 int match = c;
@@ -154,6 +154,38 @@
                 }
                 write(c); // write match // line-end
                 break;
+            }
+            case '`': { // RawString
+                maskModifiers = false;
+                int backtickCount = 0;
+                do {
+                    write(c);
+                    ++backtickCount;
+                    read();
+                } while (c == '`');
+                while (true) {
+                    if (c == '`') {
+                        int cnt = 0;
+                        do {
+                            write(c);
+                            ++cnt;
+                            read();
+                        } while (c == '`');
+                        if (cnt == backtickCount) {
+                            unread();
+                            break;
+                        }
+                    } else {
+                        write(c);
+                        if (c < 0) {
+                            openToken = true;
+                            break;
+                        }
+                        read();
+                    }
+                }
+                break;
+            }
             case '/':
                 read();
                 switch (c) {
@@ -166,7 +198,7 @@
                             prevc = c;
                         }
                         writeMask(c);
-                        openComment = c < 0;
+                        openToken = c < 0;
                         break;
                     case '/':
                         writeMask('/');
--- a/src/jdk.jshell/share/classes/jdk/jshell/SourceCodeAnalysisImpl.java	Mon Sep 17 10:53:56 2018 +0200
+++ b/src/jdk.jshell/share/classes/jdk/jshell/SourceCodeAnalysisImpl.java	Mon Sep 17 08:37:47 2018 -0700
@@ -25,7 +25,6 @@
 
 package jdk.jshell;
 
-import jdk.jshell.SourceCodeAnalysis.Completeness;
 import com.sun.source.tree.AssignmentTree;
 import com.sun.source.tree.ClassTree;
 import com.sun.source.tree.CompilationUnitTree;
@@ -170,7 +169,7 @@
     @Override
     public CompletionInfo analyzeCompletion(String srcInput) {
         MaskCommentsAndModifiers mcm = new MaskCommentsAndModifiers(srcInput, false);
-        if (mcm.endsWithOpenComment()) {
+        if (mcm.endsWithOpenToken()) {
             proc.debug(DBG_COMPA, "Incomplete (open comment): %s\n", srcInput);
             return new CompletionInfoImpl(DEFINITELY_INCOMPLETE, null, srcInput + '\n');
         }
--- a/test/langtools/jdk/jshell/ToolLocalSimpleTest.java	Mon Sep 17 10:53:56 2018 +0200
+++ b/test/langtools/jdk/jshell/ToolLocalSimpleTest.java	Mon Sep 17 08:37:47 2018 -0700
@@ -85,4 +85,9 @@
         );
     }
 
+    @Test
+    public void testRawString() {
+        // can't set --enable-preview for local, ignore
+    }
+
 }
--- a/test/langtools/jdk/jshell/ToolSimpleTest.java	Mon Sep 17 10:53:56 2018 +0200
+++ b/test/langtools/jdk/jshell/ToolSimpleTest.java	Mon Sep 17 08:37:47 2018 -0700
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8153716 8143955 8151754 8150382 8153920 8156910 8131024 8160089 8153897 8167128 8154513 8170015 8170368 8172102 8172103  8165405 8173073 8173848 8174041 8173916 8174028 8174262 8174797 8177079 8180508 8177466 8172154 8192979 8191842 8198573 8198801
+ * @bug 8153716 8143955 8151754 8150382 8153920 8156910 8131024 8160089 8153897 8167128 8154513 8170015 8170368 8172102 8172103  8165405 8173073 8173848 8174041 8173916 8174028 8174262 8174797 8177079 8180508 8177466 8172154 8192979 8191842 8198573 8198801 8210596
  * @summary Simple jshell tool tests
  * @modules jdk.compiler/com.sun.tools.javac.api
  *          jdk.compiler/com.sun.tools.javac.main
@@ -75,6 +75,22 @@
     }
 
     @Test
+    public void testRawString() {
+         test(false, new String[]{"--enable-preview", "--no-startup"},
+                 (a) -> assertCommand(a, "String s = `abc`", "s ==> \"abc\""),
+                 (a) -> assertCommand(a, "String a = `abc", ""),
+                 (a) -> assertCommand(a, "def`", "a ==> \"abc\\ndef\""),
+                 (a) -> assertCommand(a, "String bj = ``Hi, `Bob` and ```Jim```.``", "bj ==> \"Hi, `Bob` and ```Jim```.\""),
+                 (a) -> assertCommand(a, "String hw = ````````````", ""),
+                 (a) -> assertCommand(a, "Hello, world", ""),
+                 (a) -> assertCommand(a, "````````````;", "hw ==> \"\\nHello, world\\n\""),
+                 (a) -> assertCommand(a, "String uc = `\\u000d\\u000a`", "uc ==> \"\\\\u000d\\\\u000a\""),
+                 (a) -> assertCommand(a, "String es = `\\(.\\)\\1`", "es ==> \"\\\\(.\\\\)\\\\1\""),
+                 (a) -> assertCommand(a, "String end = `abc`+`def`+`ghi`", "end ==> \"abcdefghi\"")
+        );
+    }
+
+    @Test
     public void testLessThan() {
         test(
                 (a) -> assertCommand(a, "45", "$1 ==> 45"),