Skip to content

Commit

Permalink
fix(parser): throw a parse exception with the error token if token ki…
Browse files Browse the repository at this point in the history
…nd is error
  • Loading branch information
yusshu committed Nov 13, 2023
1 parent 4f07d21 commit de4230d
Showing 1 changed file with 56 additions and 54 deletions.
110 changes: 56 additions & 54 deletions src/main/java/team/unnamed/molang/parser/MolangParserImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,59 +51,6 @@ final class MolangParserImpl implements MolangParser {
this.lexer = requireNonNull(lexer, "lexer");
}

@Override
public @NotNull MolangLexer lexer() {
return lexer;
}

@Override
public @Nullable Expression current() {
if (current == UNSET_FLAG) {
throw new IllegalStateException("No current parsed expression, call next() at least once!");
}
return (Expression) current;
}

@Override
public @Nullable Expression next() throws IOException {
final Expression expr = next0();
current = expr;
return expr;
}

//
// Parses an expression until it finds an unexpected token,
// a semicolon, or an end-of-file token.
//
private @Nullable Expression next0() throws IOException {
Token token = lexer.next();

if (token.kind() == TokenKind.EOF) {
// reached end-of-file!
return null;
}

if (token.kind() == TokenKind.ERROR) {
// tokenization error!
throw new ParseException("Found an invalid token (error): " + token.value(), cursor());
}

final Expression expression = parseCompoundExpression(lexer, 0);

// check current token, should be a semicolon or an eof
token = lexer.current();
if (token.kind() != TokenKind.EOF && token.kind() != TokenKind.SEMICOLON) {
throw new ParseException("Expected a semicolon, but was " + token, lexer.cursor());
}

return expression;
}

@Override
public void close() throws IOException {
this.lexer.close();
}

//
// Parses a single expression.
// Single expressions don't require a left-hand expression
Expand Down Expand Up @@ -150,9 +97,11 @@ public void close() throws IOException {
"Found the end before the execution scope closing token",
null
);
} else if (token.kind() == TokenKind.ERROR) {
throw new ParseException("Found an invalid token (error): " + token.value(), lexer.cursor());
} else {
if (token.kind() != TokenKind.SEMICOLON) {
throw new ParseException("Missing semicolon", null);
throw new ParseException("Missing semicolon", lexer.cursor());
}
lexer.next();
}
Expand Down Expand Up @@ -295,4 +244,57 @@ public void close() throws IOException {
return new BinaryExpression(op, left, MolangParserImpl.parseCompoundExpression(lexer, precedence));
}

@Override
public @NotNull MolangLexer lexer() {
return lexer;
}

@Override
public @Nullable Expression current() {
if (current == UNSET_FLAG) {
throw new IllegalStateException("No current parsed expression, call next() at least once!");
}
return (Expression) current;
}

@Override
public @Nullable Expression next() throws IOException {
final Expression expr = next0();
current = expr;
return expr;
}

//
// Parses an expression until it finds an unexpected token,
// a semicolon, or an end-of-file token.
//
private @Nullable Expression next0() throws IOException {
Token token = lexer.next();

if (token.kind() == TokenKind.EOF) {
// reached end-of-file!
return null;
}

if (token.kind() == TokenKind.ERROR) {
// tokenization error!
throw new ParseException("Found an invalid token (error): " + token.value(), cursor());
}

final Expression expression = parseCompoundExpression(lexer, 0);

// check current token, should be a semicolon or an eof
token = lexer.current();
if (token.kind() != TokenKind.EOF && token.kind() != TokenKind.SEMICOLON) {
throw new ParseException("Expected a semicolon, but was " + token, lexer.cursor());
}

return expression;
}

@Override
public void close() throws IOException {
this.lexer.close();
}

}

0 comments on commit de4230d

Please sign in to comment.