Code style in util and parser

This commit is contained in:
Arne Keller 2021-03-08 11:40:05 +01:00
parent 8d6782793e
commit 1b642e84f9
5 changed files with 88 additions and 103 deletions

View File

@ -118,52 +118,56 @@ public class LambdaLexer {
advance();
return new Result<>(t);
default:
// only allow ascii characters in variable names
if (Character.isLetter(c) && (int) c < 128) {
int startPos = pos;
// identifier
StringBuilder sb = new StringBuilder();
do {
sb.append(term.charAt(pos));
advance();
} while (pos < term.length() && Character.isLetterOrDigit(term.charAt(pos))
&& (int) term.charAt(pos) < 128);
if (pos < term.length() && (int) term.charAt(pos) >= 128) {
return new Result<>(null, ParseError.UNEXPECTED_CHARACTER
.withCharacter(term.charAt(pos), pos));
}
String s = sb.toString();
TokenType type;
switch (s) {
case "let":
type = TokenType.LET;
break;
case "in":
type = TokenType.IN;
break;
case "true":
type = TokenType.TRUE;
break;
case "false":
type = TokenType.FALSE;
break;
default:
type = TokenType.VARIABLE;
break;
}
return new Result<>(new Token(type, sb.toString(), startPos));
} else if (Character.isDigit(c)) {
int startPos = pos;
// number literal
StringBuilder sb = new StringBuilder();
do {
sb.append(term.charAt(pos));
advance();
} while (pos < term.length() && Character.isDigit(term.charAt(pos)));
return new Result<>(new Token(TokenType.NUMBER, sb.toString(), startPos));
} else {
return new Result<>(null, ParseError.UNEXPECTED_CHARACTER.withCharacter(c, pos));
}
return parseAtomToken(c);
}
}
private Result<Token, ParseError> parseAtomToken(char c) {
// only allow ascii characters in variable names
if (Character.isLetter(c) && (int) c < 128) {
int startPos = pos;
// identifier or keyword
StringBuilder sb = new StringBuilder();
do {
sb.append(term.charAt(pos));
advance();
} while (pos < term.length() && Character.isLetterOrDigit(term.charAt(pos))
&& (int) term.charAt(pos) < 128);
if (pos < term.length() && (int) term.charAt(pos) >= 128) {
return new Result<>(null, ParseError.UNEXPECTED_CHARACTER
.withCharacter(term.charAt(pos), pos));
}
String s = sb.toString();
TokenType type;
switch (s) {
case "let":
type = TokenType.LET;
break;
case "in":
type = TokenType.IN;
break;
case "true":
type = TokenType.TRUE;
break;
case "false":
type = TokenType.FALSE;
break;
default:
type = TokenType.VARIABLE;
break;
}
return new Result<>(new Token(type, sb.toString(), startPos));
} else if (Character.isDigit(c)) {
int startPos = pos;
// number literal
StringBuilder sb = new StringBuilder();
do {
sb.append(term.charAt(pos));
advance();
} while (pos < term.length() && Character.isDigit(term.charAt(pos)));
return new Result<>(new Token(TokenType.NUMBER, sb.toString(), startPos));
} else {
return new Result<>(null, ParseError.UNEXPECTED_CHARACTER.withCharacter(c, pos));
}
}
}

View File

@ -34,7 +34,7 @@ public class LambdaParser {
private static final Set<TokenType> ATOM_START_TOKENS
= EnumSet.of(TokenType.VARIABLE, TokenType.NUMBER, TokenType.TRUE,
TokenType.FALSE, TokenType.LEFT_PARENTHESIS);
TokenType.FALSE, TokenType.LEFT_PARENTHESIS, TokenType.LAMBDA, TokenType.LET);
/**
* Constructs a parser with the specified String
@ -85,10 +85,10 @@ public class LambdaParser {
return t;
}
Optional<ParseError> next = expect(TokenType.EOF);
if (next.isPresent()) {
return new Result<>(null, next.get());
if (next.isEmpty()) {
return t;
}
return t;
return new Result<>(null, next.get());
}
/**
@ -103,26 +103,10 @@ public class LambdaParser {
return new Result<>(null, error.get());
}
}
switch (token.getType()) {
case LAMBDA:
Result<AbsTerm, ParseError> abs = parseAbstraction();
if (abs.isError()) {
return new Result<>(abs);
}
return new Result<>(abs.unwrap());
case LET:
Result<LetTerm, ParseError> let = parseLet();
if (let.isError()) {
return new Result<>(let);
}
return new Result<>(let.unwrap());
case VARIABLE:
return new Result<>(parseApplication());
case EOF:
return new Result<>(null, ParseError.TOO_FEW_TOKENS);
default:
return parseApplication();
if (token.getType() == TokenType.EOF) {
return new Result<>(null, ParseError.TOO_FEW_TOKENS);
}
return parseApplication();
}
private Result<AbsTerm, ParseError> parseAbstraction() {
@ -148,20 +132,12 @@ public class LambdaParser {
* @return the term, or an error
*/
private Result<LambdaTerm, ParseError> parseApplication() {
Result<LambdaTerm, ParseError> left = parseAtom();
Result<LambdaTerm, ParseError> left = parsePart();
if (left.isError()) {
return left;
}
while (ATOM_START_TOKENS.contains(token.getType())
|| token.getType() == TokenType.LAMBDA || token.getType() == TokenType.LET) {
Result<LambdaTerm, ParseError> atom;
if (token.getType() == TokenType.LAMBDA) {
atom = new Result<>(parseAbstraction());
} else if (token.getType() == TokenType.LET) {
atom = new Result<>(parseLet());
} else {
atom = parseAtom();
}
while (ATOM_START_TOKENS.contains(token.getType())) {
Result<LambdaTerm, ParseError> atom = parsePart();
if (atom.isError()) {
return atom;
}
@ -199,15 +175,28 @@ public class LambdaParser {
}
/**
* Parses an atom (variable or number) or a parenthesised expression.
* Parses a part of an expression (variable, constants, abstraction, let).
*
* @return the term
*/
private Result<LambdaTerm, ParseError> parseAtom() {
private Result<LambdaTerm, ParseError> parsePart() {
switch (token.getType()) {
case VARIABLE:
Result<VarTerm, ParseError> var = parseVar();
return new Result<>(var.unwrap()); // variable token can always be parsed
case LAMBDA:
return new Result<>(parseAbstraction());
case LET:
return new Result<>(parseLet());
case TRUE:
case FALSE:
String boolText = token.getText();
boolean b = Boolean.parseBoolean(boolText);
Optional<ParseError> error = nextToken();
if (error.isEmpty()) {
return new Result<>(new BooleanTerm(b));
}
return new Result<>(null, error.get());
case NUMBER:
String number = token.getText();
int n;
@ -216,20 +205,11 @@ public class LambdaParser {
} catch (NumberFormatException e) {
return new Result<>(null, ParseError.UNEXPECTED_CHARACTER.withToken(token));
}
Optional<ParseError> error = nextToken();
if (error.isPresent()) {
return new Result<>(null, error.get());
}
return new Result<>(new IntegerTerm(n));
case TRUE:
case FALSE:
String boolText = token.getText();
boolean b = Boolean.parseBoolean(boolText);
error = nextToken();
if (error.isPresent()) {
return new Result<>(null, error.get());
if (error.isEmpty()) {
return new Result<>(new IntegerTerm(n));
}
return new Result<>(new BooleanTerm(b));
return new Result<>(null, error.get());
default:
error = expect(TokenType.LEFT_PARENTHESIS);
if (error.isPresent()) {
@ -237,19 +217,19 @@ public class LambdaParser {
}
Result<LambdaTerm, ParseError> term = parseTerm(false);
error = expect(TokenType.RIGHT_PARENTHESIS);
if (error.isPresent()) {
return new Result<>(null, error.get());
if (error.isEmpty()) {
return term;
}
return term;
return new Result<>(null, error.get());
}
}
private Result<VarTerm, ParseError> parseVar() {
String s = token.getText();
Optional<ParseError> next = expect(TokenType.VARIABLE);
if (next.isPresent()) {
return new Result<>(null, next.get());
if (next.isEmpty()) {
return new Result<>(new VarTerm(s));
}
return new Result<>(new VarTerm(s));
return new Result<>(null, next.get());
}
}

View File

@ -24,7 +24,7 @@ public class Token {
RIGHT_PARENTHESIS, // )
DOT, // .
EQUALS, // =
ARROW, // -> TODO: document
ARROW, // ->
EOF // pseudo token if end of file is reached
}

View File

@ -27,6 +27,7 @@ public class Result<T, E> {
*
* @param other the result
*/
@SuppressWarnings("unchecked")
public Result(Result<?, ?> other) {
this.value = (T) other.value;
this.error = (E) other.error;
@ -42,7 +43,7 @@ public class Result<T, E> {
*/
public Result(T value, E error) {
if ((value != null) == (error != null)) {
throw new IllegalArgumentException("value xor error must be null in constructor!");
throw new IllegalArgumentException("value or error must be null in constructor!");
}
this.value = value;
this.error = error;

View File

@ -31,7 +31,7 @@ public class LambdaTermGenerator extends Generator<String> {
}
private LambdaTerm generateReal(SourceOfRandomness random) {
if (random.nextInt(1, 7) < 3) {
if (random.nextInt(1, 10) < 3) {
LambdaTerm one = generateReal(random);
LambdaTerm two = generateReal(random);
if (random.nextInt(1, 10) < 8) {