Suggest end of input in some error messages

see #15
This commit is contained in:
Arne Keller 2021-08-08 17:16:16 +02:00
parent 2817d9009f
commit e6d00f4561
3 changed files with 29 additions and 8 deletions

View File

@ -92,11 +92,12 @@ public class LambdaLexer {
return new Result<>(t); return new Result<>(t);
} else { } else {
return new Result<>(null, ParseError.unexpectedCharacter2( return new Result<>(null, ParseError.unexpectedCharacter2(
term.charAt(pos + 1), pos + 1, term, errorType)); term.charAt(pos + 1), pos + 1, term, errorType).expectedCharacter('>'));
} }
} else { } else {
return new Result<>(null, return new Result<>(null,
ParseError.unexpectedCharacter2(' ', term.length(), term, errorType)); ParseError.unexpectedCharacter2(' ', term.length(), term, errorType)
.expectedCharacter('>'));
} }
// bunch of single-character tokens // bunch of single-character tokens
case '.': case '.':

View File

@ -78,7 +78,8 @@ public class TypeAssumptionParser {
typeVariableUniqueIndex++; typeVariableUniqueIndex++;
if (currentToken.getType() != Token.TokenType.COMMA) { if (currentToken.getType() != Token.TokenType.COMMA) {
return new Result<>(null, ParseError.unexpectedToken(currentToken, return new Result<>(null, ParseError.unexpectedToken(currentToken,
ParseError.ErrorType.TYPE_ASSUMPTION_ERROR).expectedType(Token.TokenType.COMMA)); ParseError.ErrorType.TYPE_ASSUMPTION_ERROR)
.expectedTypes(List.of(Token.TokenType.COMMA, Token.TokenType.EOF)));
} }
} }
} }

View File

@ -281,10 +281,10 @@ class TypeAssumptionParserTest {
.expectedType(Token.TokenType.ARROW)); .expectedType(Token.TokenType.ARROW));
tests.put("x )", ParseError.unexpectedToken(new Token(Token.TokenType.RIGHT_PARENTHESIS, ")", "type1:x )", 8), tests.put("x )", ParseError.unexpectedToken(new Token(Token.TokenType.RIGHT_PARENTHESIS, ")", "type1:x )", 8),
ParseError.ErrorType.TYPE_ASSUMPTION_ERROR) ParseError.ErrorType.TYPE_ASSUMPTION_ERROR)
.expectedType(Token.TokenType.COMMA)); .expectedTypes(List.of(Token.TokenType.COMMA, Token.TokenType.EOF)));
tests.put("x -> (x) )", ParseError.unexpectedToken(new Token(Token.TokenType.RIGHT_PARENTHESIS, ")", "type1:x -> (x) )", 15), tests.put("x -> (x) )", ParseError.unexpectedToken(new Token(Token.TokenType.RIGHT_PARENTHESIS, ")", "type1:x -> (x) )", 15),
ParseError.ErrorType.TYPE_ASSUMPTION_ERROR) ParseError.ErrorType.TYPE_ASSUMPTION_ERROR)
.expectedType(Token.TokenType.COMMA)); .expectedTypes(List.of(Token.TokenType.COMMA, Token.TokenType.EOF)));
for (Map.Entry<String, ParseError> entry : tests.entrySet()) { for (Map.Entry<String, ParseError> entry : tests.entrySet()) {
TypeAssumptionParser parser = new TypeAssumptionParser(); TypeAssumptionParser parser = new TypeAssumptionParser();
Result<Map<VarTerm, TypeAbstraction>, ParseError> type = parser.parse("type1:" + entry.getKey()); Result<Map<VarTerm, TypeAbstraction>, ParseError> type = parser.parse("type1:" + entry.getKey());
@ -355,9 +355,9 @@ class TypeAssumptionParserTest {
void errorCase3() { void errorCase3() {
ParseError e = parse("s"); ParseError e = parse("s");
assertEquals(ParseError assertEquals(ParseError
.unexpectedToken(new Token(Token.TokenType.EOF, "", "s", 1), .unexpectedToken(new Token(Token.TokenType.EOF, "", "s", 1),
ParseError.ErrorType.TYPE_ASSUMPTION_ERROR) ParseError.ErrorType.TYPE_ASSUMPTION_ERROR)
.expectedType(Token.TokenType.COLON), .expectedType(Token.TokenType.COLON),
e); e);
} }
@ -371,6 +371,25 @@ class TypeAssumptionParserTest {
e); e);
} }
@Test
void errorCase5() {
ParseError e = parse("g: boolean-");
assertEquals(ParseError
.unexpectedCharacter2(' ', 11, "g: boolean-", ParseError.ErrorType.TYPE_ASSUMPTION_ERROR)
.expectedCharacter('>'),
e);
}
@Test
void errorCase6() {
ParseError e = parse("g: boolean:");
assertEquals(ParseError
.unexpectedToken(new Token(Token.TokenType.COLON, ":", "g: boolean:", 10),
ParseError.ErrorType.TYPE_ASSUMPTION_ERROR)
.expectedTypes(List.of(Token.TokenType.COMMA, Token.TokenType.EOF)),
e);
}
static ParseError parse(String input) { static ParseError parse(String input) {
return new TypeAssumptionParser().parse(input).unwrapError(); return new TypeAssumptionParser().parse(input).unwrapError();
} }