chumsky icon indicating copy to clipboard operation
chumsky copied to clipboard

Possibility to use a different Span for `&str`

Open muja opened this issue 7 months ago • 3 comments

I see that type Span = SimpleSpan is hard-coded for the impl Input for &str.

I don't think it's possible to opt-out of SimpleSpan and into, say, std::range::Range, without implementing the trait yourself on a wrapper, right?

I'm not sure if the only way would be to put it behind a feature.

muja avatar Apr 26 '25 19:04 muja

Have you see Input::map_span?

zesterer avatar Apr 27 '25 17:04 zesterer

I hadn't, but I also couldn't quickly figure out how to use it. For instance:

fn number<'a, I>()
-> impl Parser<'a, I, Token, extra::Err<Rich<'a, char, Range<usize>>>> + Clone
where
    I: chumsky::input::ValueInput<'a, Token = char, Span = Range<usize>>,
{
    text::digits(10)
        .then(just('.').then(text::digits(10)).or_not())
        .to_span()
        .map(|span| Token::new(TokenKind::Number, span))
}

doesn't seem to work, it gives me a lot of errors

muja avatar Apr 27 '25 19:04 muja

Okay, this works, but wow is it verbose:

fn number<'a, I>()
-> impl Parser<'a, I, Token, extra::Err<Rich<'a, char, Range<usize>>>> + Clone
where
    I: chumsky::input::StrInput<'a, Token = char, Span = Range<usize>>
        + chumsky::input::SliceInput<'a, Slice = &'a str>,

And then call it with: parse("123".map_span(Into::into)) which is _okay_er

Maybe it's worth it to invest some time to facilitate an easier way?

muja avatar Apr 27 '25 19:04 muja