pagesift_syntax/scanner/
mod.rs1use crate::lexer::Lexer;
7use crate::table::EOF_CP;
8
9pub mod bash;
10pub mod cmake;
11pub mod cpp;
12pub mod csharp;
13pub mod css;
14pub mod d;
15pub mod dart;
16pub mod dockerfile;
17pub mod elixir;
18pub mod elm;
19pub mod erlang;
20pub mod gleam;
21pub mod hcl;
22pub mod html;
23pub mod js;
24pub mod julia;
25pub mod kotlin;
26pub mod lua;
27pub mod nix;
28pub mod ocaml;
29pub mod php;
30pub mod powershell;
31pub mod python;
32pub mod r;
33pub mod ruby;
34pub mod rust;
35pub mod svelte;
36pub mod toml;
37pub mod ts;
38pub mod vue;
39
40pub const MAX_SERIALIZED: usize = 1024;
42
43pub trait Scanner {
44 fn scan(&mut self, lx: &mut ScanLexer, valid: &[bool]) -> bool;
45 fn serialize(&self, out: &mut Vec<u8>);
46 fn deserialize(&mut self, data: &[u8]);
48}
49
50pub fn create(name: &str) -> Option<Box<dyn Scanner>> {
52 Some(match name {
53 "bash" => Box::new(bash::Bash::default()),
54 "ruby" => Box::new(ruby::Ruby::default()),
55 "kotlin" => Box::new(kotlin::Kotlin::default()),
56 "css" => Box::new(css::Css),
57 "elm" => Box::new(elm::Elm::default()),
58 "d" => Box::new(d::D),
59 "hcl" => Box::new(hcl::Hcl::default()),
60 "cmake" => Box::new(cmake::CMake::default()),
61 "erlang" => Box::new(erlang::Erlang),
62 "gleam" => Box::new(gleam::Gleam),
63 "powershell" => Box::new(powershell::PowerShell),
64 "r" => Box::new(r::R::default()),
65 "elixir" => Box::new(elixir::Elixir),
66 "ocaml" | "ocaml_interface" | "ocaml_type" => Box::new(ocaml::Ocaml::default()),
67 "dockerfile" => Box::new(dockerfile::Dockerfile::default()),
68 "nix" => Box::new(nix::Nix),
69 "julia" => Box::new(julia::Julia),
70 "dart" => Box::new(dart::Dart),
71 "html" => Box::new(html::Html::default()),
72 "svelte" => Box::new(html::Html::svelte()),
73 "vue" => Box::new(html::Html::vue()),
74 "python" => Box::new(python::Python::default()),
75 "cpp" => Box::new(cpp::Cpp::default()),
76 "lua" => Box::new(lua::Lua::default()),
77 "toml" => Box::new(toml::Toml),
78 "php" | "php_only" => Box::new(php::Php::default()),
79 "csharp" | "c_sharp" => Box::new(csharp::CSharp::default()),
80 "rust" => Box::new(rust::Rust::default()),
81 "javascript" => Box::new(js::Js),
82 "typescript" | "tsx" => Box::new(ts::Ts),
83 _ => return None,
84 })
85}
86
87pub struct ScanLexer<'l, 'a> {
89 lx: &'l mut Lexer<'a>,
90 ops: usize,
91 limit: usize,
92 pub result: u16,
93}
94
95impl<'l, 'a> ScanLexer<'l, 'a> {
96 pub fn new(lx: &'l mut Lexer<'a>) -> Self {
97 let limit = lx.input.len().saturating_sub(lx.pos.bytes as usize) + 4096;
98 ScanLexer {
99 lx,
100 ops: 0,
101 limit,
102 result: 0,
103 }
104 }
105 fn exhausted(&self) -> bool {
106 self.ops > self.limit
107 }
108 pub fn lookahead(&self) -> char {
110 if self.exhausted() {
111 return '\0';
112 }
113 let c = self.lx.look();
114 if c == EOF_CP {
115 '\0'
116 } else {
117 char::from_u32(c).unwrap_or('\u{FFFD}')
118 }
119 }
120 pub fn eof(&self) -> bool {
121 self.exhausted() || self.lx.eof()
122 }
123 pub fn advance(&mut self, skip: bool) {
125 self.ops += 1;
126 if !self.exhausted() {
127 self.lx.advance(skip);
128 }
129 }
130 pub fn skip(&mut self) {
131 self.advance(true);
132 }
133 pub fn next(&mut self) {
134 self.advance(false);
135 }
136 pub fn mark_end(&mut self) {
137 self.lx.mark_end();
138 }
139 pub fn set_result(&mut self, sym: u16) {
140 self.result = sym;
141 }
142 pub fn column(&self) -> u32 {
144 self.lx.column()
145 }
146}
147
148pub fn is_space(c: char) -> bool {
150 c.is_whitespace() && !matches!(c, '\u{a0}' | '\u{2007}' | '\u{202f}') && c != '\u{85}'
151}
152pub fn is_alpha(c: char) -> bool {
154 c.is_alphabetic()
155}