Save expected input type in ParseError

This commit is contained in:
Arne Keller 2021-07-05 09:35:13 +02:00
parent 507d78e17f
commit f6d0777fd0
4 changed files with 38 additions and 1 deletions

View File

@ -0,0 +1,11 @@
package edu.kit.typicalc.model.parser;
/**
* Attached to a {@link ParseError} to signify what kind of input is expected.
*/
public enum ExpectedInput {
/**
* Any kind of lambda term.
*/
TERM
}

View File

@ -217,7 +217,7 @@ public class LambdaParser {
default:
error = expect(TokenType.LEFT_PARENTHESIS);
if (error.isPresent()) {
return new Result<>(null, error.get());
return new Result<>(null, error.get().expectedInput(ExpectedInput.TERM));
}
Result<LambdaTerm, ParseError> term = parseTerm(false);
error = expect(TokenType.RIGHT_PARENTHESIS);

View File

@ -29,6 +29,7 @@ public enum ParseError {
private Optional<Token> cause = Optional.empty();
private Optional<Collection<Token.TokenType>> needed = Optional.empty();
private Optional<ExpectedInput> expected = Optional.empty();
private String term = "";
private char wrongChar = '\0';
private char correctChar = '\0';
@ -44,6 +45,7 @@ public enum ParseError {
public ParseError withToken(Token cause, String term) {
this.cause = Optional.of(cause);
this.term = term;
this.position = cause.getPos();
return this;
}
@ -69,6 +71,22 @@ public enum ParseError {
return this;
}
/**
* Store which kind of input is expected. Clears expected tokens.
*
* @param input expected input
* @return this object
*/
public ParseError expectedInput(ExpectedInput input) {
this.needed = Optional.empty();
this.expected = Optional.of(input);
return this;
}
public Optional<ExpectedInput> getExpectedInput() {
return this.expected;
}
/**
* Attach an expected character to this error.
*

View File

@ -184,6 +184,14 @@ class LambdaParserTest {
assertEquals(new AppTerm(new AppTerm(new AbsTerm(X, X), new AbsTerm(X, X)), new AbsTerm(X, X)), term.unwrap());
}
@Test
void usefulErrors() {
LambdaParser parser = new LambdaParser("λx..");
ParseError error = parser.parse().unwrapError();
assertEquals(ExpectedInput.TERM, error.getExpectedInput().get());
assertEquals(3, error.getPosition());
}
@Test
void equality() {
EqualsVerifier.forClass(Token.class).usingGetClass().verify();