Skip to content

Commit

Permalink
Remove support for the String Templates preview feature
Browse files Browse the repository at this point in the history
Rollback of 2b0a592. I kept some changes to `IntegrationTestSupport` and `LowerIntegrationTest` related to testing preview features, which may be needed again in the future.

See https://bugs.openjdk.org/browse/JDK-8329949

#303

PiperOrigin-RevId: 634284408
  • Loading branch information
cushon authored and Javac Team committed May 16, 2024
1 parent 4822097 commit fed74bf
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 173 deletions.
53 changes: 8 additions & 45 deletions java/com/google/turbine/parse/StreamLexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -415,24 +415,17 @@ public Token next() {
}
readFrom();
StringBuilder sb = new StringBuilder();
Token stringToken = Token.STRING_LITERAL;
STRING:
while (true) {
switch (ch) {
case '\\':
eat();
if (ch == '{') {
eat();
stringTemplate(sb);
stringToken = Token.STRING_TEMPLATE;
} else {
sb.append(escape());
}
sb.append(escape());
continue STRING;
case '"':
saveValue(sb.toString());
eat();
return stringToken;
return Token.STRING_LITERAL;
case '\n':
throw error(ErrorKind.UNTERMINATED_STRING);
case ASCII_SUB:
Expand All @@ -457,29 +450,6 @@ public Token next() {
}
}

// String templates aren't compile-time constants, so they don't affect the API. Advance through
// the entire template, dropping any contained \{ ... }, and tokenize it as a single string
// literal.
private void stringTemplate(StringBuilder sb) {
sb.append("{}");
int depth = 1;
while (depth > 0) {
Token next = next();
switch (next) {
case LBRACE:
depth++;
break;
case RBRACE:
depth--;
break;
case EOF:
return;
default:
break;
}
}
}

private Token textBlock() {
OUTER:
while (true) {
Expand Down Expand Up @@ -508,7 +478,6 @@ private Token textBlock() {
}
readFrom();
StringBuilder sb = new StringBuilder();
Token stringToken = Token.STRING_LITERAL;
while (true) {
switch (ch) {
case '"':
Expand All @@ -527,23 +496,17 @@ private Token textBlock() {
value = stripIndent(value);
value = translateEscapes(value);
saveValue(value);
return stringToken;
return Token.STRING_LITERAL;
case '\\':
// Escapes are handled later (after stripping indentation), but we need to ensure
// that \" escapes don't count towards the closing delimiter of the text block.
sb.appendCodePoint(ch);
eat();
if (ch == '{') {
eat();
stringTemplate(sb);
stringToken = Token.STRING_TEMPLATE;
} else {
sb.append('\\');
if (ch == ASCII_SUB && reader.done()) {
return Token.EOF;
}
sb.appendCodePoint(ch);
eat();
if (ch == ASCII_SUB && reader.done()) {
return Token.EOF;
}
sb.appendCodePoint(ch);
eat();
continue;
case ASCII_SUB:
if (reader.done()) {
Expand Down
1 change: 0 additions & 1 deletion java/com/google/turbine/parse/Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public enum Token {
DOUBLE_LITERAL("<double literal>"),
CHAR_LITERAL("<char literal>"),
STRING_LITERAL("<string literal>"),
STRING_TEMPLATE("<string template>"),
AT("@"),
EQ("=="),
ASSIGN("="),
Expand Down
7 changes: 2 additions & 5 deletions javatests/com/google/turbine/lower/LowerIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,9 @@ public class LowerIntegrationTest {
entry("sealed_nested.test", 17),
entry("textblock.test", 15),
entry("textblock2.test", 15),
entry("B306423115.test", 15),
entry("string_template.test", 21));
entry("B306423115.test", 15));

private static final ImmutableSet<String> SOURCE_VERSION_PREVIEW =
ImmutableSet.of("string_template.test");
private static final ImmutableSet<String> SOURCE_VERSION_PREVIEW = ImmutableSet.of();

@Parameters(name = "{index}: {0}")
public static Iterable<Object[]> parameters() {
Expand Down Expand Up @@ -313,7 +311,6 @@ public static Iterable<Object[]> parameters() {
"strictfp.test",
"string.test",
"string_const.test",
"string_template.test",
"superabstract.test",
"supplierfunction.test",
"tbound.test",
Expand Down
38 changes: 0 additions & 38 deletions javatests/com/google/turbine/lower/LowerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.google.turbine.lower;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.TruthJUnit.assume;
import static com.google.turbine.testing.TestClassPaths.TURBINE_BOOTCLASSPATH;
import static com.google.turbine.testing.TestResources.getResource;
import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -57,7 +56,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -753,42 +751,6 @@ public FieldVisitor visitField(
assertThat(fields).containsExactly("y");
}

// Ensure we don't emit bogus ConstantValues for string templates with a missing processor
@Test
public void stringTemplate() throws Exception {
assume().that(Runtime.version().feature()).isAtLeast(21);
BindingResult bound =
Binder.bind(
ImmutableList.of(
Parser.parse(
"class Test {\n" //
+ " public static final String X = \"hello \\{ \"world\" }\";\n"
+ "}")),
ClassPathBinder.bindClasspath(ImmutableList.of()),
TURBINE_BOOTCLASSPATH,
/* moduleVersion= */ Optional.empty());
ImmutableMap<String, byte[]> lowered =
Lower.lowerAll(
Lower.LowerOptions.createDefault(),
bound.units(),
bound.modules(),
bound.classPathEnv())
.bytes();
Map<String, Object> fields = new HashMap<>();
new ClassReader(lowered.get("Test"))
.accept(
new ClassVisitor(Opcodes.ASM9) {
@Override
public FieldVisitor visitField(
int access, String name, String descriptor, String signature, Object value) {
fields.put(name, value);
return null;
}
},
0);
assertThat(fields).containsExactly("X", null);
}

static String lines(String... lines) {
return Joiner.on(System.lineSeparator()).join(lines);
}
Expand Down
49 changes: 0 additions & 49 deletions javatests/com/google/turbine/lower/testdata/string_template.test

This file was deleted.

35 changes: 0 additions & 35 deletions javatests/com/google/turbine/parse/LexerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,6 @@ public static List<String> lex(String input) {
break;
case CHAR_LITERAL:
case STRING_LITERAL:
case STRING_TEMPLATE:
tokenString =
String.format(
"%s(%s)",
Expand Down Expand Up @@ -424,38 +423,4 @@ public void textBlockEOF() {
assertThat(lexer.next()).isEqualTo(Token.EOF);
assertThat(lexer.stringValue()).isEqualTo("\\");
}

@Test
public void stringTemplate() {
assertThat(lex("STR.\"\\{X}\""))
.containsExactly("IDENT(STR)", "DOT", "STRING_TEMPLATE({})", "EOF");
}

@Test
public void stringTemplateNested() {
assertThat(lex("STR.\"template \\{example.foo()+ STR.\"templateInner\\{example}\"}xxx }\""))
.containsExactly("IDENT(STR)", "DOT", "STRING_TEMPLATE(template {}xxx })", "EOF");
}

@Test
public void stringTemplateNestedBraces() {
assertThat(lex("STR.\"\\{ new Object() {} }\" + \"\""))
.containsExactly(
"IDENT(STR)", "DOT", "STRING_TEMPLATE({})", "PLUS", "STRING_LITERAL()", "EOF");
}

@Test
public void stringTemplateBraces() {
assertThat(lex("\"foo \\{'{'}\"")).containsExactly("STRING_TEMPLATE(foo {})", "EOF");
assertThat(lex("\"foo \\{\"}\"}\"")).containsExactly("STRING_TEMPLATE(foo {})", "EOF");
assertThat(lex("\"foo \\{new Bar[]{}}\"")).containsExactly("STRING_TEMPLATE(foo {})", "EOF");
assertThat(lex("\"foo \\{\"bar \\{'}'}\"}\""))
.containsExactly("STRING_TEMPLATE(foo {})", "EOF");
}

@Test
public void textBlockStringTemplate() {
assertThat(lex("STR.\"\"\"\n\\{X}\"\"\""))
.containsExactly("IDENT(STR)", "DOT", "STRING_TEMPLATE({})", "EOF");
}
}

0 comments on commit fed74bf

Please sign in to comment.