diff --git a/src/main/java/edu/kit/typicalc/model/parser/LambdaLexer.java b/src/main/java/edu/kit/typicalc/model/parser/LambdaLexer.java index 95eea30..6f52d49 100644 --- a/src/main/java/edu/kit/typicalc/model/parser/LambdaLexer.java +++ b/src/main/java/edu/kit/typicalc/model/parser/LambdaLexer.java @@ -4,9 +4,7 @@ import edu.kit.typicalc.model.parser.Token.TokenType; import edu.kit.typicalc.util.Result; import java.util.ArrayDeque; -import java.util.ArrayList; import java.util.Deque; -import java.util.List; /** * This class lexes a term given as String into tokens. @@ -71,13 +69,6 @@ public class LambdaLexer { } - public Result, ParseError> allTokens() { - if (result.isError()) { - return new Result<>(result); - } - return new Result<>(new ArrayList<>(result.unwrap())); - } - private Result parseNextToken() { while (pos < term.length() && Character.isWhitespace(term.charAt(pos))) { advance(); 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 67a06fd..2aab56f 100644 --- a/src/main/java/edu/kit/typicalc/model/parser/LambdaParser.java +++ b/src/main/java/edu/kit/typicalc/model/parser/LambdaParser.java @@ -186,10 +186,7 @@ public class LambdaParser { switch (token.getType()) { case VARIABLE: Result var = parseVar(); - if (var.isError()) { - return new Result<>(var); - } - return new Result<>(var.unwrap()); + return new Result<>(var.unwrap()); // variable token can always be parsed case NUMBER: String number = token.getText(); int n; 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 1d6bf29..5760f22 100644 --- a/src/test/java/edu/kit/typicalc/model/parser/LambdaParserTest.java +++ b/src/test/java/edu/kit/typicalc/model/parser/LambdaParserTest.java @@ -11,6 +11,7 @@ import edu.kit.typicalc.model.term.LetTerm; import edu.kit.typicalc.model.term.VarTerm; import edu.kit.typicalc.model.parser.Token.TokenType; import edu.kit.typicalc.util.Result; +import nl.jqno.equalsverifier.EqualsVerifier; import org.junit.jupiter.api.Test; class LambdaParserTest { @@ -82,7 +83,7 @@ class LambdaParserTest { ); } @Test - void miscellaneousTerms() { + void errorHandling() { LambdaParser parser = new LambdaParser(""); assertEquals(ParseError.TOO_FEW_TOKENS, parser.parse().unwrapError()); parser = new LambdaParser("x)"); @@ -97,5 +98,42 @@ class LambdaParserTest { error = parser.parse().unwrapError(); assertEquals(ParseError.UNEXPECTED_CHARACTER, error); assertEquals(new Token(TokenType.NUMBER, "123333333333333", 2), error.getCause()); + parser = new LambdaParser("λ)"); + error = parser.parse().unwrapError(); + assertEquals(ParseError.UNEXPECTED_TOKEN, error); + assertEquals(new Token(TokenType.RIGHT_PARENTHESIS, ")", 1), error.getCause()); + parser = new LambdaParser("λx="); + error = parser.parse().unwrapError(); + assertEquals(ParseError.UNEXPECTED_TOKEN, error); + assertEquals(new Token(TokenType.EQUALS, "=", 2), error.getCause()); + parser = new LambdaParser("λx.."); + error = parser.parse().unwrapError(); + assertEquals(ParseError.UNEXPECTED_TOKEN, error); + assertEquals(new Token(TokenType.DOT, ".", 3), error.getCause()); + parser = new LambdaParser("let ) ="); + error = parser.parse().unwrapError(); + assertEquals(ParseError.UNEXPECTED_TOKEN, error); + assertEquals(new Token(TokenType.RIGHT_PARENTHESIS, ")", 4), error.getCause()); + parser = new LambdaParser("let x ."); + error = parser.parse().unwrapError(); + assertEquals(ParseError.UNEXPECTED_TOKEN, error); + assertEquals(new Token(TokenType.DOT, ".", 6), error.getCause()); + parser = new LambdaParser("let x = )"); + error = parser.parse().unwrapError(); + assertEquals(ParseError.UNEXPECTED_TOKEN, error); + assertEquals(new Token(TokenType.RIGHT_PARENTHESIS, ")", 8), error.getCause()); + parser = new LambdaParser("let x = y )"); + error = parser.parse().unwrapError(); + assertEquals(ParseError.UNEXPECTED_TOKEN, error); + assertEquals(new Token(TokenType.RIGHT_PARENTHESIS, ")", 10), error.getCause()); + parser = new LambdaParser("let x = y in )"); + error = parser.parse().unwrapError(); + assertEquals(ParseError.UNEXPECTED_TOKEN, error); + assertEquals(new Token(TokenType.RIGHT_PARENTHESIS, ")", 13), error.getCause()); + } + + @Test + void equality() { + EqualsVerifier.forClass(Token.class).usingGetClass().verify(); } }