Code Monkey home page Code Monkey logo

Comments (6)

shouya avatar shouya commented on May 20, 2024 1

@zesterer I didn't realized it was on master already! I was reading the doc on https://docs.rs, and that's why I didn't find it. Thanks a ton!

from chumsky.

shouya avatar shouya commented on May 20, 2024

Oh, just realized that it has been fixed in #25. I should have checked the master branch before posting it here. I'm closing this issue now.

from chumsky.

shouya avatar shouya commented on May 20, 2024

Sorry I have to re-open this issue. Although the problem I described in the OP has all worked as expected in @ryo33's fix, I found that separate_by still has a problem when combined with alternation.

Let's look at the code:

use chumsky::prelude::*;

fn main() {
  let a_list = just::<char, Simple<_>>('a').separated_by(just(','));
  let b_list = just('b').separated_by(just(','));

  let parser = a_list.or(b_list);

  // works as expected
  assert_eq!(parser.parse("a"), Ok(vec!['a']));

  // expected: ['b']
  // actual: []
  assert_eq!(parser.parse("b"), Ok(vec!['b']));
}

from chumsky.

zesterer avatar zesterer commented on May 20, 2024

Let's look at the code

This is correct behaviour. Parsers are lazy: they'll only parse the input you tell them to parse and no more. Some combinators (.repeated(), .separated_by(...), .or_not(), etc.) are able to parse no input at all and still produce a valid result.

For example, consider a parser that parses an array:

let array = expr
    .separated_by(just(','))
    .delimited_by('[', ']');

When given the input [] this parse succeeds! This only happens because the inner .separated_by(...) parser consumed no input (because there were no array items).

Obviously, when you're parsing a full program, you generally don't want the parser to consume only the first valid pattern and ignore the rest. To solve this, you can 'force' the parser to parse input right the way to the end using .then_ignore(end()).

For example, just('a').repeated() will succeed on the input "bbb" because it parses no characters and leaves the rest. However, just('a').repeated().then_ignore(end()) will not because it's expecting the end of the input afterwards.

from chumsky.

shouya avatar shouya commented on May 20, 2024

Thanks, your explanation makes total sense to me.

It seems to me what I need is an option that sort of act like Repeated::at_least. For now I'm going to use try_map at the end to enforce the number of repetition. But ideally I hope SeparatedBy can behave similarly to Repeated that has these options (at_least, at_most) to be play with.

I'll see if I can send a PR for that when I finished what I got on my hand :)

from chumsky.

zesterer avatar zesterer commented on May 20, 2024

You can do foo.separated_by(...).at_least(n). Is this what you mean?

from chumsky.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.