pagesift_syntax/scanner/
mod.rs1use 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
19pub 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 fn deserialize(&mut self, data: &[u8]);
27}
28
29pub 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
45pub 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 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 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 pub fn column(&self) -> u32 {
102 self.lx.column()
103 }
104}
105
106pub fn is_space(c: char) -> bool {
108 c.is_whitespace() && !matches!(c, '\u{a0}' | '\u{2007}' | '\u{202f}') && c != '\u{85}'
109}
110pub fn is_alpha(c: char) -> bool {
112 c.is_alphabetic()
113}