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.
This commit is contained in:
parent
5fcfc5a015
commit
ab75e73421
1 changed files with 30 additions and 0 deletions
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue