Skip to content

Commit

Permalink
Avoid _ after . (#7657)
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach authored Aug 26, 2023
1 parent 37c487f commit dd7ab1d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,7 @@
import java.util.ArrayList;
import java.util.Objects;
import java.util.UUID;
import org.enso.compiler.core.IR;
import org.enso.compiler.core.IR$Application$Literal$Sequence;
import org.enso.compiler.core.IR$Application$Operator$Binary;
import org.enso.compiler.core.IR$Application$Operator$Section$Left;
import org.enso.compiler.core.IR$Application$Operator$Section$Right;
import org.enso.compiler.core.IR$Application$Operator$Section$Sides;
import org.enso.compiler.core.IR$Application$Prefix;
import org.enso.compiler.core.IR$CallArgument$Specified;
import org.enso.compiler.core.IR$Case$Branch;
import org.enso.compiler.core.IR$Case$Expr;
import org.enso.compiler.core.IR$Comment$Documentation;
import org.enso.compiler.core.IR$DefinitionArgument$Specified;
import org.enso.compiler.core.IR$Error$Syntax;
import org.enso.compiler.core.IR$Error$Syntax$InvalidForeignDefinition;
import org.enso.compiler.core.IR$Error$Syntax$UnexpectedDeclarationInType$;
import org.enso.compiler.core.IR$Error$Syntax$UnexpectedExpression$;
import org.enso.compiler.core.IR$Error$Syntax$InvalidEscapeSequence$;
import org.enso.compiler.core.IR$Error$Syntax$EmptyParentheses$;
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$Reason;
import org.enso.compiler.core.IR$Error$Syntax$UnrecognizedToken$;
import org.enso.compiler.core.IR$Error$Syntax$UnsupportedSyntax;
import org.enso.compiler.core.IR$Expression$Binding;
import org.enso.compiler.core.IR$Expression$Block;
import org.enso.compiler.core.IR$Foreign$Definition;
import org.enso.compiler.core.IR$Function$Lambda;
import org.enso.compiler.core.IR$Function$Binding;
import org.enso.compiler.core.IR$Literal$Text;
import org.enso.compiler.core.IR$Literal$Number;
import org.enso.compiler.core.IR$Module$Scope$Definition;
import org.enso.compiler.core.IR$Module$Scope$Definition$Data;
import org.enso.compiler.core.IR$Module$Scope$Definition$Method$Binding;
import org.enso.compiler.core.IR$Module$Scope$Definition$SugaredType;
import org.enso.compiler.core.IR$Module$Scope$Export;
import org.enso.compiler.core.IR$Module$Scope$Export$Module;
import org.enso.compiler.core.IR$Module$Scope$Import;
import org.enso.compiler.core.IR$Module$Scope$Import$Module;
import org.enso.compiler.core.IR$Module$Scope$Import$Polyglot;
import org.enso.compiler.core.IR$Module$Scope$Import$Polyglot$Java;
import org.enso.compiler.core.IR$Name$BuiltinAnnotation;
import org.enso.compiler.core.IR$Name$GenericAnnotation;
import org.enso.compiler.core.IR$Name$Blank;
import org.enso.compiler.core.IR$Name$Literal;
import org.enso.compiler.core.IR$Name$Self;
import org.enso.compiler.core.IR$Name$SelfType;
import org.enso.compiler.core.IR$Name$MethodReference;
import org.enso.compiler.core.IR$Name$Qualified;
import org.enso.compiler.core.IR$Pattern$Constructor;
import org.enso.compiler.core.IR$Pattern$Documentation;
import org.enso.compiler.core.IR$Pattern$Name;
import org.enso.compiler.core.IR$Pattern$Literal;
import org.enso.compiler.core.IR$Pattern$Type;
import org.enso.compiler.core.IR$Type$Ascription;
import org.enso.compiler.core.IR$Type$Function;

import org.enso.compiler.core.IR.IdentifiedLocation;
import org.enso.compiler.core.ir.DiagnosticStorage;
import org.enso.compiler.core.ir.MetadataStorage;
Expand All @@ -71,6 +17,7 @@
import org.enso.syntax2.Tree;

import org.enso.syntax2.Tree.Invalid;

import scala.Option;
import scala.collection.immutable.LinearSeq;
import scala.collection.immutable.List;
Expand Down Expand Up @@ -1470,9 +1417,20 @@ private IR.Name qualifiedNameSegment(Tree tree, boolean generateId) throws Synta
}
private List<IR.Name> qualifiedNameSegments(Tree t, boolean generateId) throws SyntaxException {
List<IR.Name> result = nil();
var first = true;
for (var segment : unrollOprRhs(t, ".")) {
var qns = qualifiedNameSegment(segment, generateId);
var qns = switch (qualifiedNameSegment(segment, generateId)) {
case IR$Name$Blank underscore -> {
if (first) {
yield underscore;
} else {
throw new SyntaxException(segment, IR$Error$Syntax$InvalidUnderscore$.MODULE$);
}
}
case IR.Name any -> any;
};
result = cons(qns, result);
first = false;
}
return result.reverse();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7809,6 +7809,11 @@ object IR {
s"Syntax is not supported yet: $syntaxName"
}

case object InvalidUnderscore extends Reason {
override def explanation: String =
s"Invalid use of _"
}

case object InvalidPattern extends Reason {
override def explanation: String =
s"Cannot define a pattern outside a pattern context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.enso.compiler.core.IR$Error$Syntax$InvalidEscapeSequence$;
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$InvalidUnderscore$;
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$;
Expand All @@ -14,6 +15,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;

import scala.collection.immutable.List;

public class ErrorCompilerTest extends CompilerTest {
Expand All @@ -27,6 +29,26 @@ public void unfinishedLiteral1() throws Exception {
assertSingleSyntaxError(ir, IR$Error$Syntax$UnclosedTextLiteral$.MODULE$, "Unclosed text literal", 6, 28);
}

@Test
public void dotUnderscore() throws Exception {
var ir = parse("""
run op =
op._
""");

assertSingleSyntaxError(ir, IR$Error$Syntax$InvalidUnderscore$.MODULE$, "Invalid use of _", 14, 15);
}

@Test
public void dotUnderscore2() throws Exception {
var ir = parse("""
run op =
op._.something
""");

assertSingleSyntaxError(ir, IR$Error$Syntax$InvalidUnderscore$.MODULE$, "Invalid use of _", 14, 15);
}

@Test
public void unfinishedLiteral2() throws Exception {
var ir = parse("""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static void initEnsoContext() {
RuntimeOptions.LANGUAGE_HOME_OVERRIDE,
Paths.get("../../distribution/component").toFile().getAbsolutePath()
)
.option(RuntimeOptions.STRICT_ERRORS, "false")
.logHandler(OutputStream.nullOutputStream())
.allowAllAccess(true)
.build();
Expand Down Expand Up @@ -114,8 +115,8 @@ public void dotUnderscore() throws Exception {
} catch (PolyglotException e) {
assertTrue("It is exception", e.getGuestObject().isException());
assertEquals("Panic", e.getGuestObject().getMetaObject().getMetaSimpleName());
if (!e.getMessage().contains("Compiler Internal Error")) {
fail("Expecting Compiler Internal Error, but was: " + e.getMessage());
if (!e.getMessage().contains("Invalid use of _")) {
fail("Expecting Invalid use of _, but was: " + e.getMessage());
}
}
}
Expand Down

0 comments on commit dd7ab1d

Please sign in to comment.