summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Tom Willemse2020-10-22 11:52:10 -0700
committerGravatar Tom Willemse2020-10-22 11:52:10 -0700
commitab75e7342152afab877f61580c454f623ce4826e (patch)
tree9a9bda109380bf411a3e65e56cdfb5f5da59c482
parent5fcfc5a01582c6159593eaaf23ec0109d1230130 (diff)
downloadcrafting-interpreters-ab75e7342152afab877f61580c454f623ce4826e.tar.gz
crafting-interpreters-ab75e7342152afab877f61580c454f623ce4826e.zip
Add support for nested multi-line commentsmultiline-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.
-rw-r--r--src/com/craftinginterpreters/lox/Scanner.java30
1 files changed, 30 insertions, 0 deletions
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;