Skip to content

separated_by but keep them joined #576

Answered by wackbyte
wyatt-herkamp asked this question in Q&A
Discussion options

You must be logged in to vote

To avoid allocation, you could write atext_seg as:

pub fn atext_seg<'a>() -> impl Parser<'a, &'a str, &'a str, ErrType<'a>>
{
    atext().repeated().at_least(1).to_slice()
}

And, if I'm understanding correctly, dot_atom_text can be written in a similar way:

pub fn dot_atom_text<'a>() -> impl Parser<'a, &'a str, &'a str, ErrType<'a>> {
    atext_seg()
        .separated_by(just('.'))
        .to_slice()
}

Right now, you are manually rebuilding the input you parse, char-by-char, using collect. However to_slice will return the slice of the input you parsed, for free, without any copying or allocation.
(This is the biggest strength of zero-copy parsing in chumsky 1.0!)

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@wyatt-herkamp
Comment options

Answer selected by wyatt-herkamp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants