use core::marker::PhantomData;
use crate::ParseContext;
use super::*;
pub struct InputContext<E, C> {
emitter: E,
cache: C,
}
impl<E, C> InputContext<E, C> {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn new(emitter: E, cache: C) -> Self {
Self { emitter, cache }
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn into_components(self) -> (E, C) {
(self.emitter, self.cache)
}
}
pub(crate) struct Input<'inp, L, Ctx = DefaultCache<'inp, L>, Lang: ?Sized = ()>
where
L: Lexer<'inp>,
Ctx: ParseContext<'inp, L, Lang>,
{
input: &'inp L::Source,
state: L::State,
span: L::Span,
cursor: L::Offset,
cache: Ctx::Cache,
}
impl<'inp, L, Ctx, Lang: ?Sized> Clone for Input<'inp, L, Ctx, Lang>
where
L: Lexer<'inp>,
L::State: Clone,
Ctx: ParseContext<'inp, L, Lang>,
Ctx::Cache: Clone,
{
#[cfg_attr(not(tarpaulin), inline(always))]
fn clone(&self) -> Self {
Self {
input: self.input,
state: self.state.clone(),
span: self.span.clone(),
cursor: self.cursor.clone(),
cache: self.cache.clone(),
}
}
}
impl<'inp, L, Ctx, Lang: ?Sized> core::fmt::Debug for Input<'inp, L, Ctx, Lang>
where
L: Lexer<'inp>,
L::Source: core::fmt::Debug,
L::State: core::fmt::Debug,
Ctx: ParseContext<'inp, L, Lang>,
Ctx::Cache: core::fmt::Debug,
{
#[cfg_attr(not(tarpaulin), inline(always))]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Input")
.field("input", &self.input)
.field("state", &self.state)
.field("span", &self.span)
.field("cache", &self.cache)
.finish()
}
}
impl<'inp, L, Ctx, Lang: ?Sized> Input<'inp, L, Ctx, Lang>
where
L: Lexer<'inp>,
L::State: Default,
Ctx: ParseContext<'inp, L, Lang, Cache = DefaultCache<'inp, L>>,
{
#[cfg_attr(not(tarpaulin), inline(always))]
#[allow(dead_code)]
pub fn new(input: &'inp L::Source) -> Self {
Self::with_state(input, L::State::default())
}
}
impl<'inp, L, Ctx, Lang: ?Sized> Input<'inp, L, Ctx, Lang>
where
L: Lexer<'inp>,
Ctx: ParseContext<'inp, L, Lang, Cache = DefaultCache<'inp, L>>,
{
#[cfg_attr(not(tarpaulin), inline(always))]
#[allow(dead_code)]
pub fn with_state(input: &'inp L::Source, state: L::State) -> Self {
Self {
input,
state,
cursor: L::Offset::default(),
span: L::Span::new(L::Offset::default(), L::Offset::default()),
cache: DefaultCache::<'inp, L>::default(),
}
}
}
impl<'inp, L, Ctx, Lang: ?Sized> Input<'inp, L, Ctx, Lang>
where
L: Lexer<'inp>,
Ctx: ParseContext<'inp, L, Lang>,
{
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn with_state_and_cache(input: &'inp L::Source, state: L::State, cache: Ctx::Cache) -> Self
{
Self {
input,
state,
cursor: L::Offset::default(),
span: L::Span::new(L::Offset::default(), L::Offset::default()),
cache,
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn as_ref<'closure>(
&'closure mut self,
emitter: &'closure mut Ctx::Emitter,
) -> InputRef<'inp, 'closure, L, Ctx, Lang> {
InputRef {
input: &self.input,
state: &mut self.state,
cache: &mut self.cache,
span: &mut self.span,
emitter,
_marker: PhantomData,
}
}
}