Skip to main content

pagesift_syntax/scanner/
mod.rs

1//! 外部扫描器:官方语法里手写的 `scanner.c` 用 Rust 重写(trait 形式,语义与 `TSLexer` 一致)。
2//!
3//! 约定与官方相同:`scan` 按 `valid` 判断哪些外部记号此刻可用,成功时用 `set_result` 给出外部记号序号;
4//! 状态通过 `serialize`/`deserialize` 随记号保存,解析器回退或分叉后会把状态恢复到最后一个外部记号之后。
5
6use crate::lexer::Lexer;
7use crate::table::EOF_CP;
8
9pub mod cpp;
10pub mod csharp;
11pub mod js;
12pub mod lua;
13pub mod php;
14pub mod python;
15pub mod rust;
16pub mod toml;
17pub mod ts;
18
19/// 序列化缓冲上限(与官方 `TREE_SITTER_SERIALIZATION_BUFFER_SIZE` 相同)。
20pub const MAX_SERIALIZED: usize = 1024;
21
22pub trait Scanner {
23    fn scan(&mut self, lx: &mut ScanLexer, valid: &[bool]) -> bool;
24    fn serialize(&self, out: &mut Vec<u8>);
25    /// 空切片表示回到初始状态。
26    fn deserialize(&mut self, data: &[u8]);
27}
28
29/// 按语法名创建扫描器,没有移植的语法返回 `None`。
30pub fn create(name: &str) -> Option<Box<dyn Scanner>> {
31    Some(match name {
32        "python" => Box::new(python::Python::default()),
33        "cpp" => Box::new(cpp::Cpp::default()),
34        "lua" => Box::new(lua::Lua::default()),
35        "toml" => Box::new(toml::Toml),
36        "php" | "php_only" => Box::new(php::Php::default()),
37        "csharp" | "c_sharp" => Box::new(csharp::CSharp::default()),
38        "rust" => Box::new(rust::Rust::default()),
39        "javascript" => Box::new(js::Js),
40        "typescript" | "tsx" => Box::new(ts::Ts),
41        _ => return None,
42    })
43}
44
45/// 交给扫描器的词法器视图。输入不可信:操作次数有上限,超限后表现为输入结束,保证扫描一定终止。
46pub struct ScanLexer<'l, 'a> {
47    lx: &'l mut Lexer<'a>,
48    ops: usize,
49    limit: usize,
50    pub result: u16,
51}
52
53impl<'l, 'a> ScanLexer<'l, 'a> {
54    pub fn new(lx: &'l mut Lexer<'a>) -> Self {
55        let limit = lx.input.len().saturating_sub(lx.pos.bytes as usize) + 4096;
56        ScanLexer {
57            lx,
58            ops: 0,
59            limit,
60            result: 0,
61        }
62    }
63    fn exhausted(&self) -> bool {
64        self.ops > self.limit
65    }
66    /// 当前前瞻字符,输入结束返回 '\0'。
67    pub fn lookahead(&self) -> char {
68        if self.exhausted() {
69            return '\0';
70        }
71        let c = self.lx.look();
72        if c == EOF_CP {
73            '\0'
74        } else {
75            char::from_u32(c).unwrap_or('\u{FFFD}')
76        }
77    }
78    pub fn eof(&self) -> bool {
79        self.exhausted() || self.lx.eof()
80    }
81    /// 前进一个字符;`skip` 为真时该字符算作记号前的空白。
82    pub fn advance(&mut self, skip: bool) {
83        self.ops += 1;
84        if !self.exhausted() {
85            self.lx.advance(skip);
86        }
87    }
88    pub fn skip(&mut self) {
89        self.advance(true);
90    }
91    pub fn next(&mut self) {
92        self.advance(false);
93    }
94    pub fn mark_end(&mut self) {
95        self.lx.mark_end();
96    }
97    pub fn set_result(&mut self, sym: u16) {
98        self.result = sym;
99    }
100    /// 当前位置在行内的列号(按字符计)。
101    pub fn column(&self) -> u32 {
102        self.lx.column()
103    }
104}
105
106/// 对应 C 的 `iswspace`(C.UTF-8 区域:不含不换行空格)。
107pub fn is_space(c: char) -> bool {
108    c.is_whitespace() && !matches!(c, '\u{a0}' | '\u{2007}' | '\u{202f}') && c != '\u{85}'
109}
110/// 对应 C 的 `iswalpha`。
111pub fn is_alpha(c: char) -> bool {
112    c.is_alphabetic()
113}