Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
Geal committed Nov 1, 2023
1 parent cc7f338 commit ec8fe15
Showing 1 changed file with 115 additions and 14 deletions.
129 changes: 115 additions & 14 deletions src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use core::{
};
use std::{collections::HashMap, thread_local};

Check failure on line 9 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features --features "alloc")

failed to resolve: use of undeclared crate or module `std`

Check failure on line 9 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features --features "alloc")

unresolved import `std`

Check failure on line 9 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features)

failed to resolve: use of undeclared crate or module `std`

Check failure on line 9 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features)

unresolved import `std`

Check failure on line 9 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --no-default-features --features "alloc")

failed to resolve: use of undeclared crate or module `std`

Check failure on line 9 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --no-default-features --features "alloc")

unresolved import `std`

Check failure on line 9 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --no-default-features)

failed to resolve: use of undeclared crate or module `std`

Check failure on line 9 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --no-default-features)

unresolved import `std`

Check failure on line 9 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features)

failed to resolve: use of undeclared crate or module `std`

Check failure on line 9 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features)

unresolved import `std`

Check failure on line 9 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features --features "alloc")

failed to resolve: use of undeclared crate or module `std`

Check failure on line 9 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features --features "alloc")

unresolved import `std`

use crate::{error::ParseError, internal::Err, IResult, OutputM, OutputMode, PResult, Parser};
use crate::{
error::ParseError, internal::Err, internal::Mode, IResult, OutputM, OutputMode, PResult, Parser,
};

thread_local!(

Check failure on line 15 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features --features "alloc")

cannot determine resolution for the macro `thread_local`

Check failure on line 15 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features)

cannot determine resolution for the macro `thread_local`

Check failure on line 15 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --no-default-features --features "alloc")

cannot determine resolution for the macro `thread_local`

Check failure on line 15 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --no-default-features)

cannot determine resolution for the macro `thread_local`

Check failure on line 15 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features)

cannot determine resolution for the macro `thread_local`

Check failure on line 15 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features --features "alloc")

cannot determine resolution for the macro `thread_local`
static STATES: RefCell<HashMap<TypeId, Box<dyn Any>>> = RefCell::new(HashMap::new());
Expand Down Expand Up @@ -54,6 +56,26 @@ where
}
}

/*
///a
pub fn on_output<T: Clone + 'static, I, F, G>(
mut parser: F,
mut map: G,
) -> impl Parser<I, Output = <F as Parser<I>>::Output, Error = <F as Parser<I>>::Error>
where
F: Parser<I>,
G: FnMut(
&mut T,
I,
) -> ControlFlow<IResult<I, <F as Parser<I>>::Output, <F as Parser<I>>::Error>, I>,
{
move |input: I| match InitialState::<T, F>::update_input(&mut map, input) {
ControlFlow::Continue(i) => parser.parse(i),
ControlFlow::Break(result) => result,
}
}*/

/*
fn st<State, Input, P>(input: Input, parser: P) -> StateParser<State, P>
//impl Parser<Input, Output = <P as Parser<Input>>::Output, Error = <P as Parser<Input>>::Error>
where
Expand Down Expand Up @@ -84,7 +106,7 @@ where
) -> PResult<OM, Input, Self::Output, Self::Error> {
todo!()
}
}
}*/

/*
fn st2<State, Input, F, O, E>(f: F) -> impl Parser<Input, Output = O, Error = E>
Expand All @@ -95,39 +117,51 @@ where
}
*/

/*
trait StateTrait<I>: Parser<I> {
fn on_input<T: Clone + 'static, G>(
map: G,
) -> impl Parser<I, Output = <Self as Parser<I>>::Output, Error = <Self as Parser<I>>::Error>
fn on_input<T: Clone + 'static, G>(self, map: G) -> StateInput<T, Self, G>
where
G: FnMut(
&mut T,
I,
)
-> ControlFlow<IResult<I, <Self as Parser<I>>::Output, <Self as Parser<I>>::Error>, I>;

fn on_output<T: Clone + 'static, G>(self, map: G) -> StateOutput<T, Self, G>
where
G: FnMut(&mut T, <Self as Parser<I>>::Output) -> <Self as Parser<I>>::Output;
}

impl<I, P> StateTrait<I> for P
where
P: Parser<I>,
{
fn on_input<T: Clone + 'static, G>(
mut map: G,
) -> impl Parser<I, Output = <Self as Parser<I>>::Output, Error = <Self as Parser<I>>::Error>
fn on_input<T: Clone + 'static, G>(self, mut map: G) -> StateInput<T, Self, G>
where
G: FnMut(
&mut T,
I,
)
-> ControlFlow<IResult<I, <Self as Parser<I>>::Output, <Self as Parser<I>>::Error>, I>,
{
move |input: I| match InitialState::<T, F>::update_input(&mut map, input) {
ControlFlow::Continue(i) => parser.parse(i),
ControlFlow::Break(result) => result,
StateInput {
parser: self,
f: map,
st: PhantomData,
}
}
}*/

fn on_output<T: Clone + 'static, G>(self, map: G) -> StateOutput<T, Self, G>
//impl Parser<I, Output = <Self as Parser<I>>::Output, Error = <Self as Parser<I>>::Error>
where
G: FnMut(&mut T, <Self as Parser<I>>::Output) -> <Self as Parser<I>>::Output,
{
StateInput {
parser: self,
f: map,
st: PhantomData,
}
}
}

struct StateInput<State, P, F> {
parser: P,
Expand All @@ -154,11 +188,43 @@ where
&mut self,
input: Input,
) -> PResult<OM, Input, Self::Output, Self::Error> {
match InitialState::<State, F>::update_input(&mut self.f, input) {
/*match InitialState::<State, F>::update_input(&mut self.f, input) {
ControlFlow::Continue(i) => self
.parser
.process::<OutputM<OM::Output, OM::Error, OM::Incomplete>>(i),
ControlFlow::Break(result) => result,
}*/
todo!()
}
}

struct StateOutput<State, P, F> {
parser: P,
f: F,
st: PhantomData<State>,
}

impl<State, Input, P, F> Parser<Input> for StateOutput<State, P, F>
where
State: Clone + 'static,
P: Parser<Input>,
F: FnMut(&mut State, <P as Parser<Input>>::Output) -> <P as Parser<Input>>::Output, //IResult<Input, <P as Parser<Input>>::Output, <P as Parser<Input>>::Error>,
{
type Output = <P as Parser<Input>>::Output;
type Error = <P as Parser<Input>>::Error;

fn process<OM: OutputMode>(
&mut self,
input: Input,
) -> PResult<OM, Input, Self::Output, Self::Error> {
match self.parser.process::<OM>(input) {
Err(e) => Err(e),
Ok((i, o)) => Ok((
i,
OM::Output::map(o, move |o| {
InitialState::<State, F>::update_input(&mut self.f, o)
}),
)),
}
}
}
Expand All @@ -177,6 +243,19 @@ impl<T: Clone + 'static, F> InitialState<T, F> {
})
}

pub(crate) fn update<G>(mut fun: G)
where
G: FnMut(&mut T),
{
STATES.with(|states| {

Check failure on line 250 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features --features "alloc")

cannot find value `STATES` in this scope

Check failure on line 250 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features)

cannot find value `STATES` in this scope

Check failure on line 250 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --no-default-features --features "alloc")

cannot find value `STATES` in this scope

Check failure on line 250 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --no-default-features)

cannot find value `STATES` in this scope

Check failure on line 250 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features)

cannot find value `STATES` in this scope

Check failure on line 250 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features --features "alloc")

cannot find value `STATES` in this scope
let mut h = states.borrow_mut();
let v = h.get_mut(&TypeId::of::<T>());
let value = v.unwrap();

fun(value.downcast_mut::<T>().unwrap())
})
}

fn reset(&self) {
STATES.with(|states| {

Check failure on line 260 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features --features "alloc")

cannot find value `STATES` in this scope

Check failure on line 260 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features)

cannot find value `STATES` in this scope

Check failure on line 260 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --no-default-features --features "alloc")

cannot find value `STATES` in this scope

Check failure on line 260 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --no-default-features)

cannot find value `STATES` in this scope

Check failure on line 260 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features)

cannot find value `STATES` in this scope

Check failure on line 260 in src/state/mod.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features --features "alloc")

cannot find value `STATES` in this scope
let mut h = states.borrow_mut();
Expand Down Expand Up @@ -292,5 +371,27 @@ mod tests {
})
.parse(i)*/
//}

fn a(i: &str) -> IResult<&str, &str, ()> {
on_input::<u8, &str, _, _>(preceded(tag("a"), b), |state, input| {
if *state == 3 {
ControlFlow::Break(Err(Err::Failure(())))
} else {
*state += 1;
ControlFlow::Continue(input)
}
})
.parse(i)
}

fn b(i: &str) -> IResult<&str, &str, ()> {
preceded(tag("b"), alt((a.map(|output| {}), tag(".")))).parse(i)
}

let mut parser = state(0u8, a);

assert_eq!(parser.parse("abababab"), Err(Err::Failure(())));

assert_eq!(parser.parse("abab."), Ok(("", ".")));
}
}

0 comments on commit ec8fe15

Please sign in to comment.