From 15b877a368376aed2b401dffe300e446a59929dd Mon Sep 17 00:00:00 2001 From: Manuel Friedli Date: Sun, 24 Mar 2024 05:04:05 +0100 Subject: [PATCH] Block comments, and fix the line comments :) --- .../fritteli/gombaila/domain/lexer/Lexer.java | 38 ++++++++++++++++--- src/main/resources/gombaila/simple.gb | 9 ----- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/main/java/ch/fritteli/gombaila/domain/lexer/Lexer.java b/src/main/java/ch/fritteli/gombaila/domain/lexer/Lexer.java index 8538bdc..d5bd107 100644 --- a/src/main/java/ch/fritteli/gombaila/domain/lexer/Lexer.java +++ b/src/main/java/ch/fritteli/gombaila/domain/lexer/Lexer.java @@ -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); } diff --git a/src/main/resources/gombaila/simple.gb b/src/main/resources/gombaila/simple.gb index 22df3fd..e69de29 100644 --- a/src/main/resources/gombaila/simple.gb +++ b/src/main/resources/gombaila/simple.gb @@ -1,9 +0,0 @@ -let x = 1; -if (x) { -// x = 99; -} else { - x = 12; -} -// comment -exit(x); -// comment \ No newline at end of file