use crate::lexer::Lexer;
use crate::table::EOF_CP;
pub mod cpp;
pub mod csharp;
pub mod js;
pub mod lua;
pub mod php;
pub mod python;
pub mod rust;
pub mod toml;
pub mod ts;
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 {
"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
}
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.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(&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()
}