Yield UnclosedTextLiteral syntax errors (#7340)

Fixes #7279 by detecting missing `getClose()` and yielding an `UnclosedTextLiteral`.

# Important Notes
Special care must be taken for _text blocks_. They have `null` `getClose()`.
This commit is contained in:
Jaroslav Tulach 2023-07-21 13:37:30 +02:00 committed by GitHub
parent 8d37596c1d
commit 76457b0d58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 5 deletions

View File

@ -1227,11 +1227,21 @@ final class TreeToIr {
}
IR.Literal translateLiteral(Tree.TextLiteral txt) throws SyntaxException {
if (txt.getClose() == null) {
if (txt.getOpen() == null || switch (txt.getOpen().codeRepr()) {
case "'''" -> false;
case "\"\"\"" -> false;
default -> true;
}) {
throw new SyntaxException(txt, IR$Error$Syntax$UnclosedTextLiteral$.MODULE$);
}
}
// Splices are not yet supported in the IR.
var value = buildTextConstant(txt, txt.getElements());
return new IR$Literal$Text(value, getIdentifiedLocation(txt), meta(), diag());
}
String buildTextConstant(Tree at, Iterable<TextElement> elements) throws SyntaxException {
private String buildTextConstant(Tree at, Iterable<TextElement> elements) throws SyntaxException {
var sb = new StringBuilder();
TextElement error = null;
for (var t : elements) {

View File

@ -1,17 +1,16 @@
package org.enso.compiler;
import java.util.Objects;
import org.enso.compiler.core.IR;
import org.enso.compiler.core.IR$Error$Syntax;
import org.enso.compiler.core.IR$Error$Syntax$InvalidEscapeSequence$;
import org.enso.compiler.core.IR$Error$Syntax$Reason;
import org.enso.compiler.core.IR$Error$Syntax$InvalidImport;
import org.enso.compiler.core.IR$Error$Syntax$InvalidExport;
import org.enso.compiler.core.IR$Error$Syntax$InvalidImport;
import org.enso.compiler.core.IR$Error$Syntax$Reason;
import org.enso.compiler.core.IR$Error$Syntax$UnclosedTextLiteral$;
import org.enso.compiler.core.IR$Error$Syntax$UnexpectedExpression$;
import org.enso.compiler.core.IR$Error$Syntax$UnrecognizedToken$;
import org.enso.compiler.core.IR$Error$Syntax$UnsupportedSyntax;
import org.enso.syntax.text.Location;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
@ -19,6 +18,39 @@ import scala.collection.immutable.List;
public class ErrorCompilerTest extends CompilerTest {
@Test
public void unfinishedLiteral1() throws Exception {
var ir = parse("""
foo = "unfinished literal...
""");
assertSingleSyntaxError(ir, IR$Error$Syntax$UnclosedTextLiteral$.MODULE$, "Unclosed text literal", 6, 28);
}
@Test
public void unfinishedLiteral2() throws Exception {
var ir = parse("""
foo = 'unfinished literal...
""");
assertSingleSyntaxError(ir, IR$Error$Syntax$UnclosedTextLiteral$.MODULE$, "Unclosed text literal", 6, 28);
}
@Test
public void unpairedLiteral1() throws Exception {
var ir = parse("""
foo = "unpaired literal'
""");
assertSingleSyntaxError(ir, IR$Error$Syntax$UnclosedTextLiteral$.MODULE$, "Unclosed text literal", 6, 24);
}
@Test
public void unpairedLiteral2() throws Exception {
var ir = parse("""
foo = 'unpaired literal"
""");
assertSingleSyntaxError(ir, IR$Error$Syntax$UnclosedTextLiteral$.MODULE$, "Unclosed text literal", 6, 24);
}
@Test
public void spaceRequired() throws Exception {
var ir = parse("foo = if cond.x else.y");