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 == '*') {
this.handleSimple(TokenType.MULT, line, column);
} else if (c == '/') {
if (this.content.peekNext().exists(n -> n == '/')) {
this.content.next();
this.handleLineComment();
} else {
this.handleSimple(TokenType.DIV, line, column);
}
this.handleForwardSlash(line, column);
} else if (c == '%') {
this.handleSimple(TokenType.MOD, line, column);
} else if (c == '^') {
@ -112,6 +107,20 @@ public class Lexer {
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() {
while (this.content.peekNext().exists(next -> next != '\n')) {
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) {
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