Skip to main content

pagesift_syntax/scanner/
erlang.rs

1//! Erlang 外部扫描器,移植自 tree-sitter-erlang 的 `src/scanner.c`
2//! (Apache-2.0,Copyright (c) Meta Platforms, Inc. and affiliates)。无状态:序列化为空。
3
4use super::{ScanLexer, Scanner};
5
6const TQ_STRING: usize = 0;
7const TQ_SIGIL_STRING: usize = 1;
8
9#[derive(Default)]
10pub struct Erlang;
11
12fn is_whitespace(lx: &ScanLexer) -> bool {
13    let c = lx.lookahead() as u32;
14    (0x01..=0x20).contains(&c) || (0x80..=0xA0).contains(&c)
15}
16
17impl Scanner for Erlang {
18    fn scan(&mut self, lx: &mut ScanLexer, v: &[bool]) -> bool {
19        if !(v[TQ_STRING] || v[TQ_SIGIL_STRING]) {
20            return false;
21        }
22        while is_whitespace(lx) {
23            lx.skip();
24        }
25        let mut sigil = false;
26        if v[TQ_SIGIL_STRING] && lx.lookahead() == '~' {
27            sigil = true;
28            lx.next();
29            match lx.lookahead() {
30                's' | 'S' | 'b' | 'B' => lx.next(),
31                '"' => {}
32                _ => return false,
33            }
34        }
35        // 三个或更多个引号开头,行内其余只能是空白;结尾引号个数必须相同
36        for _ in 0..3 {
37            if lx.lookahead() != '"' {
38                return false;
39            }
40            lx.next();
41        }
42        let mut count: u32 = 3;
43        while lx.lookahead() == '"' {
44            count += 1;
45            lx.next();
46        }
47        while lx.lookahead() != '\n' && is_whitespace(lx) {
48            lx.next();
49        }
50        if lx.lookahead() != '\n' {
51            return false;
52        }
53        lx.next();
54        loop {
55            if lx.lookahead() == '\n' {
56                lx.next();
57                while lx.lookahead() != '\n' && is_whitespace(lx) {
58                    lx.next();
59                }
60                let mut check = count;
61                while check > 0 && lx.lookahead() == '"' {
62                    lx.next();
63                    check -= 1;
64                }
65                if check == 0 {
66                    lx.mark_end();
67                    lx.set_result(if sigil { TQ_SIGIL_STRING } else { TQ_STRING } as u16);
68                    return true;
69                }
70            } else if lx.eof() {
71                return false;
72            } else {
73                lx.next();
74            }
75        }
76    }
77
78    fn serialize(&self, _out: &mut Vec<u8>) {}
79
80    fn deserialize(&mut self, _data: &[u8]) {}
81}