use super::{ScanLexer, Scanner};
const STRING_FRAGMENT: usize = 0;
const INDENTED_STRING_FRAGMENT: usize = 1;
const PATH_START: usize = 2;
const PATH_FRAGMENT: usize = 3;
const DOLLAR_ESCAPE: usize = 4;
const INDENTED_DOLLAR_ESCAPE: usize = 5;
#[derive(Default)]
pub struct Nix;
fn scan_dollar_escape(lx: &mut ScanLexer) -> bool {
lx.set_result(DOLLAR_ESCAPE as u16);
lx.next();
lx.mark_end();
lx.lookahead() == '$'
}
fn scan_indented_dollar_escape(lx: &mut ScanLexer) -> bool {
lx.set_result(INDENTED_DOLLAR_ESCAPE as u16);
lx.next();
lx.mark_end();
if lx.lookahead() == '$' {
return true;
}
if lx.lookahead() == '\\' {
lx.next();
if lx.lookahead() == '$' {
lx.mark_end();
return true;
}
}
false
}
fn scan_string_fragment(lx: &mut ScanLexer) -> bool {
lx.set_result(STRING_FRAGMENT as u16);
let mut has_content = false;
loop {
lx.mark_end();
match lx.lookahead() {
'"' | '\\' => return has_content,
'$' => {
lx.next();
if lx.lookahead() == '{' {
return has_content;
} else if lx.lookahead() != '"' && lx.lookahead() != '\\' {
lx.next();
}
}
'\0' => return false,
_ => lx.next(),
}
has_content = true;
}
}
fn scan_indented_string_fragment(lx: &mut ScanLexer) -> bool {
lx.set_result(INDENTED_STRING_FRAGMENT as u16);
let mut has_content = false;
loop {
lx.mark_end();
match lx.lookahead() {
'$' => {
lx.next();
if lx.lookahead() == '{' {
return has_content;
} else if lx.lookahead() != '\'' {
lx.next();
}
}
'\'' => {
lx.next();
if lx.lookahead() == '\'' {
return has_content;
}
}
'\0' => return false,
_ => lx.next(),
}
has_content = true;
}
}
fn is_path_char(c: char) -> bool {
c.is_ascii_alphanumeric() || matches!(c, '-' | '+' | '_' | '.' | '/')
}
fn scan_path_start(lx: &mut ScanLexer) -> bool {
lx.set_result(PATH_START as u16);
let (mut have_sep, mut have_after_sep) = (false, false);
while matches!(lx.lookahead(), ' ' | '\n' | '\r' | '\t') {
lx.skip();
}
loop {
lx.mark_end();
let c = lx.lookahead();
if c == '/' {
have_sep = true;
} else if is_path_char(c) {
if have_sep {
have_after_sep = true;
}
} else if c == '$' {
return have_sep;
} else {
return have_after_sep;
}
lx.next();
}
}
fn scan_path_fragment(lx: &mut ScanLexer) -> bool {
lx.set_result(PATH_FRAGMENT as u16);
let mut has_content = false;
loop {
lx.mark_end();
if !is_path_char(lx.lookahead()) {
return has_content;
}
lx.next();
has_content = true;
}
}
impl Scanner for Nix {
fn scan(&mut self, lx: &mut ScanLexer, v: &[bool]) -> bool {
if v[STRING_FRAGMENT]
&& v[INDENTED_STRING_FRAGMENT]
&& v[PATH_START]
&& v[PATH_FRAGMENT]
&& v[DOLLAR_ESCAPE]
&& v[INDENTED_DOLLAR_ESCAPE]
{
return false;
}
if v[STRING_FRAGMENT] {
if lx.lookahead() == '\\' {
return scan_dollar_escape(lx);
}
return scan_string_fragment(lx);
}
if v[INDENTED_STRING_FRAGMENT] {
if lx.lookahead() == '\'' {
lx.mark_end();
lx.next();
if lx.lookahead() == '\'' {
return scan_indented_dollar_escape(lx);
}
}
return scan_indented_string_fragment(lx);
}
if v[PATH_FRAGMENT] && is_path_char(lx.lookahead()) {
return scan_path_fragment(lx);
}
if v[PATH_START] {
return scan_path_start(lx);
}
false
}
fn serialize(&self, _out: &mut Vec<u8>) {}
fn deserialize(&mut self, _data: &[u8]) {}
}