chumsky icon indicating copy to clipboard operation
chumsky copied to clipboard

Add a State parameter to parsers

Open kythyria opened this issue 2 years ago • 2 comments

In some cases, it's useful to be able to pass something around to parsers, such as a tree builder, interner, arena, or similar gizmo. Something that can't just be created ex nihilo in a leaf and passed outward (an ego-tree document, in my case: the parent of a node has to exist before the children do).

zesterer: An extra type parameter on Parser with a default could work zesterer: trait Parser<I, O, State = ()> zesterer: Then an extra combinator like .map_with_state(|output, state: &mut State|) zesterer: And an extra Parser::parse_with_state function

Which is basically what Logos does modulo exactly how the state is retrieved.

kythyria avatar Dec 06 '21 02:12 kythyria

So I've begun adding this in #46, but it's sadly resulting in a lot of type inference problems. I've yet to determine whether these are inherent problems or just circumstantial though.

zesterer avatar Dec 06 '21 15:12 zesterer

I think I can get rid of the inference error with extra parameterisation...

zesterer avatar Dec 06 '21 15:12 zesterer

zero-copy now has a state type parameter and a .map_with_state combinator.

zesterer avatar Feb 20 '23 22:02 zesterer