diff --git a/src/com/craftinginterpreters/lox/Scanner.java b/src/com/craftinginterpreters/lox/Scanner.java index e21f701..9f95073 100644 --- a/src/com/craftinginterpreters/lox/Scanner.java +++ b/src/com/craftinginterpreters/lox/Scanner.java @@ -80,6 +80,8 @@ class Scanner { if (match('/')) { // A comment goes until the end of the line. while (peek() != '\n' && !isAtEnd()) advance(); + } else if (match('*')) { + multiline_comment(); } else { addToken(SLASH); } @@ -155,6 +157,34 @@ class Scanner { addToken(STRING, value); } + private void multiline_comment() { + int depth = 0; + + while (!isAtEnd() + && (peek() != '*' || peekNext() != '/' || depth != 0)) { + if (peek() == '\n') line++; + + if (peek() == '/' && peekNext() == '*') { + depth++; + advance(); + } + + if (peek() == '*' && peekNext() == '/') { + depth--; + advance(); + } + + advance(); + } + + if (isAtEnd()) { + return; + } + + advance(); + advance(); + } + private boolean match(char expected) { if (isAtEnd()) return false; if (source.charAt(current) != expected) return false;