From f6d0777fd00ddeaab46e5990de601510fd4ea0a3 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Mon, 5 Jul 2021 09:35:13 +0200 Subject: [PATCH] Save expected input type in ParseError --- .../typicalc/model/parser/ExpectedInput.java | 11 +++++++++++ .../typicalc/model/parser/LambdaParser.java | 2 +- .../kit/typicalc/model/parser/ParseError.java | 18 ++++++++++++++++++ .../model/parser/LambdaParserTest.java | 8 ++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/main/java/edu/kit/typicalc/model/parser/ExpectedInput.java diff --git a/src/main/java/edu/kit/typicalc/model/parser/ExpectedInput.java b/src/main/java/edu/kit/typicalc/model/parser/ExpectedInput.java new file mode 100644 index 0000000..db0ae5c --- /dev/null +++ b/src/main/java/edu/kit/typicalc/model/parser/ExpectedInput.java @@ -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 +} diff --git a/src/main/java/edu/kit/typicalc/model/parser/LambdaParser.java b/src/main/java/edu/kit/typicalc/model/parser/LambdaParser.java index 4669f25..8583cf1 100644 --- a/src/main/java/edu/kit/typicalc/model/parser/LambdaParser.java +++ b/src/main/java/edu/kit/typicalc/model/parser/LambdaParser.java @@ -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 term = parseTerm(false); error = expect(TokenType.RIGHT_PARENTHESIS); diff --git a/src/main/java/edu/kit/typicalc/model/parser/ParseError.java b/src/main/java/edu/kit/typicalc/model/parser/ParseError.java index 0678874..eaeeb91 100644 --- a/src/main/java/edu/kit/typicalc/model/parser/ParseError.java +++ b/src/main/java/edu/kit/typicalc/model/parser/ParseError.java @@ -29,6 +29,7 @@ public enum ParseError { private Optional cause = Optional.empty(); private Optional> needed = Optional.empty(); + private Optional 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 getExpectedInput() { + return this.expected; + } + /** * Attach an expected character to this error. * diff --git a/src/test/java/edu/kit/typicalc/model/parser/LambdaParserTest.java b/src/test/java/edu/kit/typicalc/model/parser/LambdaParserTest.java index 460d405..7e6bb64 100644 --- a/src/test/java/edu/kit/typicalc/model/parser/LambdaParserTest.java +++ b/src/test/java/edu/kit/typicalc/model/parser/LambdaParserTest.java @@ -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();