Skip to main content

pagesift_syntax/scanner/
rust.rs

1//! Rust 外部扫描器,移植自 tree-sitter-rust 的 `src/scanner.c`(MIT,Copyright (c) 2017 Maxim Sokolov)。
2
3use super::{ScanLexer, Scanner, is_alpha, is_space};
4
5const STRING_CONTENT: usize = 0;
6const STRING_CLOSE: usize = 1;
7const RAW_STRING_LITERAL_START: usize = 2;
8const RAW_STRING_LITERAL_CONTENT: usize = 3;
9const RAW_STRING_LITERAL_END: usize = 4;
10const FLOAT_LITERAL: usize = 5;
11const BLOCK_OUTER_DOC_MARKER: usize = 6;
12const BLOCK_INNER_DOC_MARKER: usize = 7;
13const BLOCK_COMMENT_CONTENT: usize = 8;
14const LINE_DOC_CONTENT: usize = 9;
15const ERROR_SENTINEL: usize = 10;
16
17#[derive(Default)]
18pub struct Rust {
19    opening_hash_count: u8,
20}
21
22fn is_num_char(c: char) -> bool {
23    c == '_' || c.is_ascii_digit()
24}
25
26fn process_string(lx: &mut ScanLexer) -> bool {
27    let mut has_content = false;
28    loop {
29        if lx.lookahead() == '"' || lx.lookahead() == '\\' {
30            break;
31        }
32        if lx.eof() {
33            return false;
34        }
35        has_content = true;
36        lx.next();
37    }
38    lx.set_result(STRING_CONTENT as u16);
39    lx.mark_end();
40    has_content
41}
42
43fn process_float(lx: &mut ScanLexer) -> bool {
44    lx.set_result(FLOAT_LITERAL as u16);
45    lx.next();
46    while is_num_char(lx.lookahead()) {
47        lx.next();
48    }
49    let (mut has_fraction, mut has_exponent) = (false, false);
50    if lx.lookahead() == '.' {
51        has_fraction = true;
52        lx.next();
53        if is_alpha(lx.lookahead()) || lx.lookahead() == '.' {
54            return false;
55        }
56        while is_num_char(lx.lookahead()) {
57            lx.next();
58        }
59    }
60    lx.mark_end();
61    if lx.lookahead() == 'e' || lx.lookahead() == 'E' {
62        has_exponent = true;
63        lx.next();
64        if lx.lookahead() == '+' || lx.lookahead() == '-' {
65            lx.next();
66        }
67        if !is_num_char(lx.lookahead()) {
68            return true;
69        }
70        lx.next();
71        while is_num_char(lx.lookahead()) {
72            lx.next();
73        }
74        lx.mark_end();
75    }
76    if !has_exponent && !has_fraction {
77        return false;
78    }
79    if !matches!(lx.lookahead(), 'u' | 'i' | 'f') {
80        return true;
81    }
82    lx.next();
83    if !lx.lookahead().is_ascii_digit() {
84        return true;
85    }
86    while lx.lookahead().is_ascii_digit() {
87        lx.next();
88    }
89    lx.mark_end();
90    true
91}
92
93fn line_doc_content(lx: &mut ScanLexer) -> bool {
94    lx.set_result(LINE_DOC_CONTENT as u16);
95    loop {
96        if lx.eof() {
97            return true;
98        }
99        if lx.lookahead() == '\n' {
100            lx.next();
101            return true;
102        }
103        lx.next();
104    }
105}
106
107#[derive(Clone, Copy, PartialEq)]
108enum Bc {
109    LeftSlash,
110    LeftAsterisk,
111    Continuing,
112}
113
114/// C 里是 `(char)lexer->lookahead`,只取低 8 位;保持同样的截断行为。
115fn low8(c: char) -> u8 {
116    (c as u32 & 0xFF) as u8
117}
118
119fn block_comment(lx: &mut ScanLexer, valid: &[bool]) -> bool {
120    let mut first = low8(lx.lookahead());
121    if valid[BLOCK_INNER_DOC_MARKER] && first == b'!' {
122        lx.set_result(BLOCK_INNER_DOC_MARKER as u16);
123        lx.next();
124        return true;
125    }
126    if valid[BLOCK_OUTER_DOC_MARKER] && first == b'*' {
127        lx.next();
128        lx.mark_end();
129        if lx.lookahead() == '/' {
130            return false;
131        }
132        if lx.lookahead() != '*' {
133            lx.set_result(BLOCK_OUTER_DOC_MARKER as u16);
134            return true;
135        }
136    } else {
137        lx.next();
138    }
139    if valid[BLOCK_COMMENT_CONTENT] {
140        let mut depth: u32 = 1;
141        let mut state = match first {
142            b'*' => {
143                if lx.lookahead() == '/' {
144                    return false;
145                }
146                Bc::LeftAsterisk
147            }
148            b'/' => Bc::LeftSlash,
149            _ => Bc::Continuing,
150        };
151        while !lx.eof() && depth != 0 {
152            first = low8(lx.lookahead());
153            match state {
154                Bc::LeftSlash => {
155                    if first == b'*' {
156                        depth += 1;
157                    }
158                    state = Bc::Continuing;
159                }
160                Bc::LeftAsterisk => {
161                    if first == b'*' {
162                        lx.mark_end();
163                        state = Bc::LeftAsterisk;
164                    } else {
165                        if first == b'/' {
166                            depth = depth.wrapping_sub(1);
167                        }
168                        state = Bc::Continuing;
169                    }
170                }
171                Bc::Continuing => {
172                    lx.mark_end();
173                    match first {
174                        b'/' => state = Bc::LeftSlash,
175                        b'*' => state = Bc::LeftAsterisk,
176                        _ => {}
177                    }
178                }
179            }
180            lx.next();
181            if first == b'/' && depth != 0 {
182                lx.mark_end();
183            }
184        }
185        lx.set_result(BLOCK_COMMENT_CONTENT as u16);
186        return true;
187    }
188    false
189}
190
191impl Scanner for Rust {
192    fn serialize(&self, out: &mut Vec<u8>) {
193        out.push(self.opening_hash_count);
194    }
195    fn deserialize(&mut self, d: &[u8]) {
196        self.opening_hash_count = if d.len() == 1 { d[0] } else { 0 };
197    }
198
199    fn scan(&mut self, lx: &mut ScanLexer, valid: &[bool]) -> bool {
200        if valid[ERROR_SENTINEL] {
201            return false;
202        }
203        if valid[BLOCK_COMMENT_CONTENT]
204            || valid[BLOCK_INNER_DOC_MARKER]
205            || valid[BLOCK_OUTER_DOC_MARKER]
206        {
207            return block_comment(lx, valid);
208        }
209        if valid[STRING_CONTENT] && !valid[FLOAT_LITERAL] && process_string(lx) {
210            return true;
211        }
212        if valid[STRING_CLOSE] && lx.lookahead() == '"' {
213            lx.next();
214            lx.set_result(STRING_CLOSE as u16);
215            lx.mark_end();
216            return true;
217        }
218        if valid[LINE_DOC_CONTENT] {
219            return line_doc_content(lx);
220        }
221        while is_space(lx.lookahead()) {
222            lx.skip();
223        }
224        if valid[RAW_STRING_LITERAL_START] && matches!(lx.lookahead(), 'r' | 'b' | 'c') {
225            // 原始字符串开头
226            if lx.lookahead() == 'b' || lx.lookahead() == 'c' {
227                lx.next();
228            }
229            if lx.lookahead() != 'r' {
230                return false;
231            }
232            lx.next();
233            let mut hashes: u8 = 0;
234            while lx.lookahead() == '#' {
235                lx.next();
236                hashes = hashes.wrapping_add(1);
237            }
238            if lx.lookahead() != '"' {
239                return false;
240            }
241            lx.next();
242            self.opening_hash_count = hashes;
243            lx.set_result(RAW_STRING_LITERAL_START as u16);
244            return true;
245        }
246        if valid[RAW_STRING_LITERAL_CONTENT] {
247            loop {
248                if lx.eof() {
249                    return false;
250                }
251                if lx.lookahead() == '"' {
252                    lx.mark_end();
253                    lx.next();
254                    let mut n = 0u32;
255                    while lx.lookahead() == '#' && n < self.opening_hash_count as u32 {
256                        lx.next();
257                        n += 1;
258                    }
259                    if n == self.opening_hash_count as u32 {
260                        lx.set_result(RAW_STRING_LITERAL_CONTENT as u16);
261                        return true;
262                    }
263                } else {
264                    lx.next();
265                }
266            }
267        }
268        if valid[RAW_STRING_LITERAL_END] && lx.lookahead() == '"' {
269            lx.next();
270            for _ in 0..self.opening_hash_count {
271                lx.next();
272            }
273            lx.set_result(RAW_STRING_LITERAL_END as u16);
274            return true;
275        }
276        if valid[FLOAT_LITERAL] && lx.lookahead().is_ascii_digit() {
277            return process_float(lx);
278        }
279        false
280    }
281}