use super::*;
use crate::StringView;
#[derive(Clone, Copy, Debug)]
pub struct CommentLine {
head: &'static str,
}
impl CommentLine {
pub fn new(head: &'static str) -> Self {
Self { head }
}
}
impl<'i> FnOnce<(ParseState<'i>,)> for CommentLine {
type Output = ParseResult<'i, SurroundPair<'i>>;
#[inline]
extern "rust-call" fn call_once(self, (input,): (ParseState<'i>,)) -> Self::Output {
let (_, head) = input.match_str(self.head)?;
let offset = match input.residual.find(&['\r', '\n']) {
Some(s) => s,
None => input.residual.len(),
};
let body = unsafe { input.residual.get_unchecked(head.len()..offset) };
input.advance(offset).finish(SurroundPair {
head: StringView::new(head, input.start_offset),
body: StringView::new(body, input.start_offset + head.len()),
tail: StringView::new("", input.start_offset + offset),
})
}
}
#[derive(Clone, Copy, Debug)]
pub struct CommentBlock {
head: &'static str,
tail: &'static str,
nested: bool,
}
impl CommentBlock {
pub fn new(head: &'static str, tail: &'static str) -> Self {
Self { head, tail, nested: false }
}
pub fn with_nested(self, nested: bool) -> Self {
Self { nested, ..self }
}
}
impl<'i> FnOnce<(ParseState<'i>,)> for CommentBlock {
type Output = ParseResult<'i, SurroundPair<'i>>;
#[inline]
extern "rust-call" fn call_once(self, (input,): (ParseState<'i>,)) -> Self::Output {
let (state, _) = input.match_str(self.head)?;
match self.nested {
true => {
todo!()
}
false => {
match state.residual.find(self.tail) {
Some(s) => {
let offset = s + self.tail.len();
let body = unsafe { state.residual.get_unchecked(0..s) };
state.advance(offset).finish(SurroundPair {
head: StringView::new(self.head, input.start_offset),
body: StringView::new(body, input.start_offset + self.head.len()),
tail: StringView::new(self.tail, input.start_offset + offset),
})
}
None => StopBecause::missing_string(self.tail, state.start_offset)?,
}
}
}
}
}
impl<'i> FnMut<(ParseState<'i>,)> for CommentBlock {
extern "rust-call" fn call_mut(&mut self, args: (ParseState<'i>,)) -> Self::Output {
FnOnce::call_once(*self, args)
}
}
impl<'i> Fn<(ParseState<'i>,)> for CommentBlock {
extern "rust-call" fn call(&self, args: (ParseState<'i>,)) -> Self::Output {
FnOnce::call_once(*self, args)
}
}