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

Labelling has no effect on one_of()? #694

Open
cAttte opened this issue Oct 29, 2024 · 5 comments
Open

Labelling has no effect on one_of()? #694

cAttte opened this issue Oct 29, 2024 · 5 comments

Comments

@cAttte
Copy link

cAttte commented Oct 29, 2024

hey! i was trying to apply some labels on a .one_of(Range) parser but it didn't work, so i decided to try a few other cases:

type E = extra::Err<Rich<'static, char>>;
println!("one_of(range): {:?}", one_of::<_, _, E>('a'..='c').labelled("abc").parse("x"));
println!("one_of(str): {:?}", one_of::<_, _, E>("abc").labelled("abc").parse("x"));
println!("just().or(): {:?}", just::<_, _, E>('a').or(just('b').or(just('c'))).labelled("abc").parse("x"));
println!("choice(just()): {:?}", choice((just::<_, _, E>('a'), just('b'), just('c'))).labelled("abc").parse("x"));
println!("ident(): {:?}", text::unicode::ident::<_, _, E>().labelled("abc").parse("!"));
println!("none_of(): {:?}", none_of::<_, _, E>("def").labelled("abc").parse("e"));
one_of(range): ParseResult { output: None, errs: [found 'x' at 0..1 expected ''a'', ''b'', or ''c''] } // bad
one_of(str): ParseResult { output: None, errs: [found 'x' at 0..1 expected ''a'', ''b'', or ''c''] } // bad
just().or(): ParseResult { output: None, errs: [found 'x' at 0..1 expected "abc"] }
choice(just()): ParseResult { output: None, errs: [found 'x' at 0..1 expected "abc"] }
ident(): ParseResult { output: None, errs: [found '!' at 0..1 expected "abc"] }
none_of(): ParseResult { output: None, errs: [found 'e' at 0..1 expected something else] }

as you can see the label does not show up in cases where one_of() is used. let me know if i'm doing something wrong!

@zesterer
Copy link
Owner

Is this using the latest main? If so, I have an inkling...

@cAttte
Copy link
Author

cAttte commented Oct 29, 2024

hey, nope this is on alpha.7. i've tried main but i get a weird error, i can keep trying if you think it'd help!

@zesterer
Copy link
Owner

What's the error, out of interest?

I think I know what's causing this, but resolving it in the general case is... a little finickey, I think.

@cAttte
Copy link
Author

cAttte commented Oct 29, 2024

ha, i was trying to build with the nightly feature on stable, so that's what the error was.

sounds interesting, if you could explain it a little bit that would be cool, maybe someone can help

@zesterer
Copy link
Owner

So when a parser succeeds, it leaves the input pointing at the next token after whatever it parsed. But when a parser fails, the state it leaves the parser in is unspecified (as in: that's how chumsky behaves right now).

The problem is that labelled depends on the input being left in a specific place, since it determines whether to treat the label as a pattern (i.e: expected foo) or context (i.e: error occurred while parsing foo).

The reason this works with just and not one_of is because the former rewinds the input on failure, while the latter does not. Fixing this is as simple as making the two consistent, but to really fix it chumsky probably needs to take a clearer stance on what state the input should be left in on failure - which also means building that stance into the extension API and so on.

I can give more information tomorrow.

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

4 participants
@zesterer @cAttte and others