Compare commits

...

1 commit

Author SHA1 Message Date
ab75e73421 Add support for nested multi-line comments
For example, this is a nested multi-line comment:

    /*
      Here's a multi-line comment
      /* It explains what this function does */
      It shouldn't appear in any output
    */

If the comment isn’t closed at the end of the file, that’s fine.
2020-10-22 11:52:10 -07:00

View file

@ -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;