Skip to content

Parsing syntax like Rust's raw string literals #441

Answered by Zij-IT
Cookie04DE asked this question in Q&A
Discussion options

You must be logged in to vote

What you are looking for is the Parser::then_with method, which allows you to define a second parser based on the result of the first. In this case, it allows us to get the amount of '#'s that the start parser was able to parse. Here is what I came up with based on what you described:

fn raw_str_lit() -> impl Parser<char, String, Error = Simple<char>> + Clone {
    just("r")
        .ignore_then(
            // This `Parser::map` saves us any allocations because `Vec` doesn't allocate for
            // ZSTs, and we only need the length anyhow.
            // This isn't necessary, and premature optimization is the root of all evil, so sue me for it ;D
            just('#').map(|_| ()).rep…

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@Cookie04DE
Comment options

@zesterer
Comment options

@mbund
Comment options

@zesterer
Comment options

@mbund
Comment options

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