aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/lexer/comments.rs
blob: afe6886a1face77bbb53fb7e086c91cb5feb7c76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use crate::lexer::ptr::Ptr;

use crate::SyntaxKind::{self, *};

pub(crate) fn scan_shebang(ptr: &mut Ptr) -> bool {
    if ptr.at_str("!/") {
        ptr.bump();
        ptr.bump();
        bump_until_eol(ptr);
        true
    } else {
        false
    }
}

fn scan_block_comment(ptr: &mut Ptr) -> Option<SyntaxKind> {
    if ptr.at('*') {
        ptr.bump();
        let mut depth: u32 = 1;
        while depth > 0 {
            if ptr.at_str("*/") {
                depth -= 1;
                ptr.bump();
                ptr.bump();
            } else if ptr.at_str("/*") {
                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.at('/') {
        bump_until_eol(ptr);
        Some(COMMENT)
    } else {
        scan_block_comment(ptr)
    }
}

fn bump_until_eol(ptr: &mut Ptr) {
    loop {
        if ptr.at('\n') || ptr.at_str("\r\n") {
            return;
        }
        if ptr.bump().is_none() {
            break;
        }
    }
}