mirror of
https://gitlab.kit.edu/uskyk/typicalc.git
synced 2024-11-08 18:30:42 +00:00
Save expected input type in ParseError
This commit is contained in:
parent
507d78e17f
commit
f6d0777fd0
@ -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
|
||||||
|
}
|
@ -217,7 +217,7 @@ public class LambdaParser {
|
|||||||
default:
|
default:
|
||||||
error = expect(TokenType.LEFT_PARENTHESIS);
|
error = expect(TokenType.LEFT_PARENTHESIS);
|
||||||
if (error.isPresent()) {
|
if (error.isPresent()) {
|
||||||
return new Result<>(null, error.get());
|
return new Result<>(null, error.get().expectedInput(ExpectedInput.TERM));
|
||||||
}
|
}
|
||||||
Result<LambdaTerm, ParseError> term = parseTerm(false);
|
Result<LambdaTerm, ParseError> term = parseTerm(false);
|
||||||
error = expect(TokenType.RIGHT_PARENTHESIS);
|
error = expect(TokenType.RIGHT_PARENTHESIS);
|
||||||
|
@ -29,6 +29,7 @@ public enum ParseError {
|
|||||||
|
|
||||||
private Optional<Token> cause = Optional.empty();
|
private Optional<Token> cause = Optional.empty();
|
||||||
private Optional<Collection<Token.TokenType>> needed = Optional.empty();
|
private Optional<Collection<Token.TokenType>> needed = Optional.empty();
|
||||||
|
private Optional<ExpectedInput> expected = Optional.empty();
|
||||||
private String term = "";
|
private String term = "";
|
||||||
private char wrongChar = '\0';
|
private char wrongChar = '\0';
|
||||||
private char correctChar = '\0';
|
private char correctChar = '\0';
|
||||||
@ -44,6 +45,7 @@ public enum ParseError {
|
|||||||
public ParseError withToken(Token cause, String term) {
|
public ParseError withToken(Token cause, String term) {
|
||||||
this.cause = Optional.of(cause);
|
this.cause = Optional.of(cause);
|
||||||
this.term = term;
|
this.term = term;
|
||||||
|
this.position = cause.getPos();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,6 +71,22 @@ public enum ParseError {
|
|||||||
return this;
|
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.
|
* Attach an expected character to this error.
|
||||||
*
|
*
|
||||||
|
@ -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());
|
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
|
@Test
|
||||||
void equality() {
|
void equality() {
|
||||||
EqualsVerifier.forClass(Token.class).usingGetClass().verify();
|
EqualsVerifier.forClass(Token.class).usingGetClass().verify();
|
||||||
|
Loading…
Reference in New Issue
Block a user