use crate::lexer::Lexer;
use crate::table::EOF_CP;
pub mod bash;
pub mod cmake;
pub mod cpp;
pub mod csharp;
pub mod css;
pub mod d;
pub mod dart;
pub mod dockerfile;
pub mod elixir;
pub mod elm;
pub mod erlang;
pub mod gleam;
pub mod hcl;
pub mod html;
pub mod js;
pub mod julia;
pub mod kotlin;
pub mod lua;
pub mod nix;
pub mod ocaml;
pub mod php;
pub mod powershell;
pub mod python;
pub mod r;
pub mod ruby;
pub mod rust;
pub mod svelte;
pub mod toml;
pub mod ts;
pub mod vue;
pub const MAX_SERIALIZED: usize = 1024;
pub trait Scanner {
fn scan(&mut self, lx: &mut ScanLexer, valid: &[bool]) -> bool;
fn serialize(&self, out: &mut Vec<u8>);
fn deserialize(&mut self, data: &[u8]);
}
pub fn create(name: &str) -> Option<Box<dyn Scanner>> {
Some(match name {
"bash" => Box::new(bash::Bash::default()),
"ruby" => Box::new(ruby::Ruby::default()),
"kotlin" => Box::new(kotlin::Kotlin::default()),
"css" => Box::new(css::Css),
"elm" => Box::new(elm::Elm::default()),
"d" => Box::new(d::D),
"hcl" => Box::new(hcl::Hcl::default()),
"cmake" => Box::new(cmake::CMake::default()),
"erlang" => Box::new(erlang::Erlang),
"gleam" => Box::new(gleam::Gleam),
"powershell" => Box::new(powershell::PowerShell),
"r" => Box::new(r::R::default()),
"elixir" => Box::new(elixir::Elixir),
"ocaml" | "ocaml_interface" | "ocaml_type" => Box::new(ocaml::Ocaml::default()),
"dockerfile" => Box::new(dockerfile::Dockerfile::default()),
"nix" => Box::new(nix::Nix),
"julia" => Box::new(julia::Julia),
"dart" => Box::new(dart::Dart),
"html" => Box::new(html::Html::default()),
"svelte" => Box::new(html::Html::svelte()),
"vue" => Box::new(html::Html::vue()),
"python" => Box::new(python::Python::default()),
"cpp" => Box::new(cpp::Cpp::default()),
"lua" => Box::new(lua::Lua::default()),
"toml" => Box::new(toml::Toml),
"php" | "php_only" => Box::new(php::Php::default()),
"csharp" | "c_sharp" => Box::new(csharp::CSharp::default()),
"rust" => Box::new(rust::Rust::default()),
"javascript" => Box::new(js::Js),
"typescript" | "tsx" => Box::new(ts::Ts),
_ => return None,
})
}
pub struct ScanLexer<'l, 'a> {
lx: &'l mut Lexer<'a>,
ops: usize,
limit: usize,
pub result: u16,
}
impl<'l, 'a> ScanLexer<'l, 'a> {
pub fn new(lx: &'l mut Lexer<'a>) -> Self {
let limit = lx.input.len().saturating_sub(lx.pos.bytes as usize) + 4096;
ScanLexer {
lx,
ops: 0,
limit,
result: 0,
}
}
fn exhausted(&self) -> bool {
self.ops > self.limit || self.lx.timed_out
}
pub fn lookahead(&self) -> char {
if self.exhausted() {
return '\0';
}
let c = self.lx.look();
if c == EOF_CP {
'\0'
} else {
char::from_u32(c).unwrap_or('\u{FFFD}')
}
}
pub fn eof(&self) -> bool {
self.exhausted() || self.lx.eof()
}
pub fn advance(&mut self, skip: bool) {
self.ops += 1;
if self.ops.is_multiple_of(4096) {
self.lx.check_deadline();
}
if !self.exhausted() {
self.lx.advance(skip);
}
}
pub fn skip(&mut self) {
self.advance(true);
}
pub fn next(&mut self) {
self.advance(false);
}
pub fn mark_end(&mut self) {
self.lx.mark_end();
}
pub fn set_result(&mut self, sym: u16) {
self.result = sym;
}
pub fn column(&mut self) -> u32 {
self.lx.column()
}
}
pub fn is_space(c: char) -> bool {
c.is_whitespace() && !matches!(c, '\u{a0}' | '\u{2007}' | '\u{202f}') && c != '\u{85}'
}
pub fn is_alpha(c: char) -> bool {
c.is_alphabetic()
}