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

Ref inside Or not working #166

Open
BorisTheBrave opened this issue Dec 19, 2020 · 1 comment
Open

Ref inside Or not working #166

BorisTheBrave opened this issue Dec 19, 2020 · 1 comment

Comments

@BorisTheBrave
Copy link

                var r = new Dictionary<int, Parser<bool>>();

                Parser<bool> R(int i) => Parse.Ref(() => r[i]);
                //Parser<bool> R(int i) => r[i];

                r[5] = Parse.Char('b').Return(true);
                r[4] = Parse.Char('a').Return(true);
                r[3] = R(4).Then(_ => R(5)).Or(R(5).Then(_ => R(4))); // ab or ba
                r[2] = R(4).Then(_ => R(4)).Or(R(5).Then(_ => R(5))); // aa or bb
                r[1] = R(2).Then(_ => R(3)).Or(R(3).Then(_ => R(2)));
                //r[1] = R(3).Then(_ => R(2));

                var top = R(1).End().Or(Parse.AnyChar.Return(false));

                Console.WriteLine(top.Parse("babb"));

Expected result: prints "true"
Actual result: Exception Parsing failure: unexpected 'b'; expected a (Line 1, Column 1); recently consumed:

This only occurs if Ref and Or are used - if you substitute either of the commented out lines for the ones above them, the error does not occur.

I presume #129 was not fixed properly.

I tested with Sprache 2.3.1 and 2.3.2-develop-00043

@tomtheisen
Copy link

tomtheisen commented Jan 4, 2021

Based on the timing, I suspect this was encountered during Advent of Code 2020 day 19. At least that's how I found it, although I'm running a few weeks late.

I'm getting the same behavior. Inspecting the current code in the repo here doesn't look like it would produce the error, although the binary on nuget certainly does.

If I get sufficiently motivated, I may try to repro using source. Depending how that turns out, I might even attempt a PR.

Anyway, for whatever it's worth, I reduced the test case a bit further.

var R4 = Parse.Char('a');
var F4 = Parse.Ref(() => R4);
var top = F4.Or(F4);

Console.WriteLine(top.TryParse("b").WasSuccessful);

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

2 participants