Comments (6)
@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.
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.
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.
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.
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.
You can do foo.separated_by(...).at_least(n)
. Is this what you mean?
from chumsky.
Related Issues (20)
- Indentation example: incorrect "end of input" HOT 2
- Idea: conditonal parser, which takes a parser and a condition HOT 4
- or combinator prefers output of second parser, even if first parser succeeds HOT 14
- when use tokio::spawn i meet problem anyone can help HOT 6
- `nested_in()` + `repeated()` produce incorrect error spans. HOT 5
- Failed to compile benches: no method named `slice` found
- `ContainerExactly` impls for `Rc` and `Arc` are unsound HOT 2
- Clone trait bounds not satisfied HOT 2
- Trait bound not satisfied on foldl HOT 2
- Input lifetimes HOT 4
- bug: couldn't build `examples/foo.rs` HOT 1
- Query : Create a parser that parses a vec of patterns HOT 7
- Crashed while parsing markup text HOT 4
- Fix SpannedInput HOT 1
- Trying to run the `nano_rust.rs` example using `chumsky = "1.0.0-alpha.6"` HOT 4
- Question: Using `map_with` to get spans HOT 2
- Question: Writing a Ternary Operator Parser HOT 5
- Can't build example HOT 1
- `/examples/pythonic.rs` is commented out HOT 1
- `/examples/foo.rs` is different from the tutorial HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chumsky.