Fix tokens after main function

This commit is contained in:
SirYwell 2025-05-07 14:23:45 +02:00
parent 2edeaaaee3
commit e1aefeba40
No known key found for this signature in database
2 changed files with 9 additions and 1 deletions

View file

@ -40,7 +40,11 @@ public class Parser {
} }
public ProgramTree parseProgram() { public ProgramTree parseProgram() {
return new ProgramTree(List.of(parseFunction())); ProgramTree programTree = new ProgramTree(List.of(parseFunction()));
if (this.tokenSource.hasMore()) {
throw new ParseException("expected end of input but got " + this.tokenSource.peek());
}
return programTree;
} }
private FunctionTree parseFunction() { private FunctionTree parseFunction() {

View file

@ -75,6 +75,10 @@ public class TokenSource {
return token; return token;
} }
public boolean hasMore() {
return this.idx < this.tokens.size();
}
private void expectHasMore() { private void expectHasMore() {
if (this.idx >= this.tokens.size()) { if (this.idx >= this.tokens.size()) {
throw new ParseException("reached end of file"); throw new ParseException("reached end of file");