8210596: jshell does not support raw string literals

Reviewed-by: jlahoda, jlaskey
This commit is contained in:
Robert Field 2018-09-17 08:37:47 -07:00
parent b1375858c6
commit f4e1502e03
5 changed files with 62 additions and 8 deletions

@ -31,6 +31,7 @@ import com.sun.tools.javac.parser.ScannerFactory;
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 @@ class CompletenessAnalyzer {
private CaLog(Context context, PrintWriter pw) {
super(context, pw);
this.source = DiagnosticSource.NO_SOURCE;
}
@Override

@ -67,7 +67,7 @@ class MaskCommentsAndModifiers {
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 @@ class MaskCommentsAndModifiers {
return sbMask.toString();
}
boolean endsWithOpenComment() {
return openComment;
boolean endsWithOpenToken() {
return openToken;
}
/****** private implementation methods ******/
@ -142,7 +142,7 @@ class MaskCommentsAndModifiers {
private void next() {
switch (c) {
case '\'':
case '"':
case '"': {
maskModifiers = false;
write(c);
int match = c;
@ -154,6 +154,38 @@ class MaskCommentsAndModifiers {
}
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 @@ class MaskCommentsAndModifiers {
prevc = c;
}
writeMask(c);
openComment = c < 0;
openToken = c < 0;
break;
case '/':
writeMask('/');

@ -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 @@ class SourceCodeAnalysisImpl extends SourceCodeAnalysis {
@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');
}

@ -85,4 +85,9 @@ public class ToolLocalSimpleTest extends ToolSimpleTest {
);
}
@Test
public void testRawString() {
// can't set --enable-preview for local, ignore
}
}

@ -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
@ -74,6 +74,22 @@ public class ToolSimpleTest extends ReplToolTesting {
);
}
@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(