aboutsummaryrefslogtreecommitdiff
path: root/crates/libsyntax2/src/lexer/strings.rs
blob: 795ea97b7b7c48e207444dec9cbf09801f351c78 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use SyntaxKind::{self, *};

use lexer::ptr::Ptr;

pub(crate) fn is_string_literal_start(c: char, c1: Option<char>, c2: Option<char>) -> bool {
    match (c, c1, c2) {
        ('r', Some('"'), _)
        | ('r', Some('#'), _)
        | ('b', Some('"'), _)
        | ('b', Some('\''), _)
        | ('b', Some('r'), Some('"'))
        | ('b', Some('r'), Some('#')) => true,
        _ => false,
    }
}

pub(crate) fn scan_char(ptr: &mut Ptr) {
    loop {
        if ptr.next_is('\\') {
            ptr.bump();
            if ptr.next_is('\\') || ptr.next_is('\'') {
                ptr.bump();
            }
            continue;
        }
        if ptr.next_is('\'') {
            ptr.bump();
            return;
        }
        if ptr.next_is('\n') {
            break;
        }
        ptr.bump();
    }
}

pub(crate) fn scan_byte_char_or_string(ptr: &mut Ptr) -> SyntaxKind {
    // unwrapping and not-exhaustive match are ok
    // because of string_literal_start
    let c = ptr.bump().unwrap();
    match c {
        '\'' => {
            scan_byte(ptr);
            BYTE
        }
        '"' => {
            scan_byte_string(ptr);
            BYTE_STRING
        }
        'r' => {
            scan_raw_byte_string(ptr);
            RAW_BYTE_STRING
        }
        _ => unreachable!(),
    }
}

pub(crate) fn scan_string(ptr: &mut Ptr) {
    while let Some(c) = ptr.bump() {
        if c == '"' {
            return;
        }
    }
}

pub(crate) fn scan_raw_string(ptr: &mut Ptr) {
    let mut hashes = 0;
    while ptr.next_is('#') {
        hashes += 1;
        ptr.bump();
    }
    if !ptr.next_is('"') {
        return;
    }
    ptr.bump();

    while let Some(c) = ptr.bump() {
        if c == '"' {
            let mut hashes_left = hashes;
            while ptr.next_is('#') && hashes_left > 0{
                hashes_left -= 1;
                ptr.bump();
            }
            if hashes_left == 0 {
                return;
            }
        }
    }
}

fn scan_byte(ptr: &mut Ptr) {
    if ptr.next_is('\'') {
        ptr.bump();
        return;
    }
    ptr.bump();
    if ptr.next_is('\'') {
        ptr.bump();
        return;
    }
}

fn scan_byte_string(ptr: &mut Ptr) {
    while let Some(c) = ptr.bump() {
        if c == '"' {
            return;
        }
    }
}

fn scan_raw_byte_string(ptr: &mut Ptr) {
    if !ptr.next_is('"') {
        return;
    }
    ptr.bump();

    while let Some(c) = ptr.bump() {
        if c == '"' {
            return;
        }
    }
}