]> git.lizzy.rs Git - rust.git/commitdiff
Block Comments
authorChristopher Durham <cad97@cad97.com>
Sun, 28 Jan 2018 10:08:25 +0000 (05:08 -0500)
committerChristopher Durham <cad97@cad97.com>
Sun, 28 Jan 2018 10:08:25 +0000 (05:08 -0500)
closes #7

src/lexer/comments.rs
tests/data/lexer/00012_block_comment.rs [new file with mode: 0644]
tests/data/lexer/00012_block_comment.txt [new file with mode: 0644]

index b70f2c6c677bce4928ce2e756279bb89f03d174e..d388561bff2e84b76a9140c4a944fb3d7abbf1b6 100644 (file)
@@ -14,12 +14,35 @@ pub(crate) fn scan_shebang(ptr: &mut Ptr) -> bool {
     }
 }
 
+pub(crate) fn scan_block_comment(ptr: &mut Ptr) -> Option<SyntaxKind> {
+    if ptr.next_is('*') {
+        ptr.bump();
+        let mut depth: u32 = 1;
+        while depth > 0 {
+            if ptr.next_is('*') && ptr.nnext_is('/') {
+                depth -= 1;
+                ptr.bump();
+                ptr.bump();
+            } else if ptr.next_is('/') && ptr.nnext_is('*') {
+                depth += 1;
+                ptr.bump();
+                ptr.bump();
+            } else if ptr.bump().is_none() {
+                break;
+            }
+        }
+        Some(COMMENT)
+    } else {
+        None
+    }
+}
+
 pub(crate) fn scan_comment(ptr: &mut Ptr) -> Option<SyntaxKind> {
     if ptr.next_is('/') {
         bump_until_eol(ptr);
         Some(COMMENT)
     } else {
-        None
+        scan_block_comment(ptr)
     }
 }
 
diff --git a/tests/data/lexer/00012_block_comment.rs b/tests/data/lexer/00012_block_comment.rs
new file mode 100644 (file)
index 0000000..708aac1
--- /dev/null
@@ -0,0 +1,4 @@
+/* */
+/**/
+/* /* */ */
+/*
diff --git a/tests/data/lexer/00012_block_comment.txt b/tests/data/lexer/00012_block_comment.txt
new file mode 100644 (file)
index 0000000..9958b25
--- /dev/null
@@ -0,0 +1,7 @@
+COMMENT 5 "/* */"
+WHITESPACE 1 "\n"
+COMMENT 4 "/**/"
+WHITESPACE 1 "\n"
+COMMENT 11 "/* /* */ */"
+WHITESPACE 1 "\n"
+COMMENT 3 "/*\n"