Block comments, and fix the line comments :)

This commit is contained in:
Manuel Friedli 2024-03-24 05:04:05 +01:00
parent ceb0d5aaba
commit 15b877a368
Signed by: manuel
GPG key ID: 41D08ABA75634DA1
2 changed files with 32 additions and 15 deletions

View file

@ -50,12 +50,7 @@ public class Lexer {
} else if (c == '*') { } else if (c == '*') {
this.handleSimple(TokenType.MULT, line, column); this.handleSimple(TokenType.MULT, line, column);
} else if (c == '/') { } else if (c == '/') {
if (this.content.peekNext().exists(n -> n == '/')) { this.handleForwardSlash(line, column);
this.content.next();
this.handleLineComment();
} else {
this.handleSimple(TokenType.DIV, line, column);
}
} else if (c == '%') { } else if (c == '%') {
this.handleSimple(TokenType.MOD, line, column); this.handleSimple(TokenType.MOD, line, column);
} else if (c == '^') { } else if (c == '^') {
@ -112,6 +107,20 @@ public class Lexer {
this.appendToken(new Token(tokenType, line, column)); this.appendToken(new Token(tokenType, line, column));
} }
private void handleForwardSlash(final int line, final int column) {
this.content.next();
if (this.content.peekNext().exists(n -> n == '/')) {
this.content.next();
this.handleLineComment();
}
if (this.content.peekNext().exists(n -> n == '*')) {
this.content.next();
this.handleBlockComment();
} else {
this.tokens.append(new Token(TokenType.DIV, line, column));
}
}
private void handleLineComment() { private void handleLineComment() {
while (this.content.peekNext().exists(next -> next != '\n')) { while (this.content.peekNext().exists(next -> next != '\n')) {
this.content.next(); this.content.next();
@ -121,6 +130,23 @@ public class Lexer {
} }
} }
private void handleBlockComment() {
// ...*/
while (this.content.peekNext().exists(next -> next != '*')) {
this.content.next();
}
if (this.content.peekNext().exists(n -> n == '*')) {
this.content.next();
if (this.content.peekNext().exists(n -> n == '/')) {
this.content.next();
return;
}
handleBlockComment();
} else {
return;
}
}
private void appendToken(@NotNull final Token token) { private void appendToken(@NotNull final Token token) {
this.tokens = this.tokens.append(token); this.tokens = this.tokens.append(token);
} }

View file

@ -1,9 +0,0 @@
let x = 1;
if (x) {
// x = 99;
} else {
x = 12;
}
// comment
exit(x);
// comment