Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

take_until equivalent combinator #436

Closed
rushmorem opened this issue May 31, 2018 · 2 comments
Closed

take_until equivalent combinator #436

rushmorem opened this issue May 31, 2018 · 2 comments

Comments

@rushmorem
Copy link

What's synom's equivalent combinator for take_until, if any? If none, how can it be achieved? I want a way to grab everything in a token stream until I reach a comma.

@rushmorem
Copy link
Author

rushmorem commented Jun 1, 2018

Here is what I have so far:-

impl Synom for ToComma {
    fn parse(mut rest: Cursor) -> PResult<Self> {
        let mut tokens = Vec::new();
        while let Some((tree, cursor)) = rest.token_tree() {
            rest = cursor;
            if let TokenTree::Punct(p) = tree.clone() {
                if p.as_char() == ',' {
                    break;
                }
            }
            tokens.push(tree);
        }
        let stream = TokenStream::from_iter(tokens);
        Ok((ToComma(stream), rest))
    }
    
    fn description() -> Option<&'static str> {
        Some("token stream to comma")
    }
}

However, for any input that has more tokens after a comma, this gives "failed to parse token stream to comma: failed to parse all tokens". Where am I going wrong?

Permalink to the playground
Direct link to the gist

EDIT: Never mind. This parser is actually working as expected. The final parser used in tests has to consume all tokens, otherwise it will be seen as a failure.

Permalink to the playground

Now I simply need to know if there is already a way to do this in synom besides manually implementing the trait like this.

@rushmorem
Copy link
Author

Here is the final version (in case someone is interested):-

impl Synom for ToComma {
    fn parse(mut rest: Cursor) -> PResult<Self> {
        let mut tokens = Vec::new();
        while let Some((tree, cursor)) = rest.token_tree() {
            if let TokenTree::Punct(p) = tree.clone() {
                if p.as_char() == ',' {
                    if !tokens.is_empty() {
                        break;
                    }
                }
            }
            rest = cursor;
            tokens.push(tree);
        }
        let stream = TokenStream::from_iter(tokens);
        Ok((ToComma(stream), rest))
    }
    
    fn description() -> Option<&'static str> {
        Some("token stream to comma")
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant