{"action":"created","issue":{"url":"https://api.github.com/repos/benjamin-hodgson/Pidgin/issues/13","repository_url":"https://api.github.com/repos/benjamin-hodgson/Pidgin","labels_url":"https://api.github.com/repos/benjamin-hodgson/Pidgin/issues/13/labels{/name}","comments_url":"https://api.github.com/repos/benjamin-hodgson/Pidgin/issues/13/comments","events_url":"https://api.github.com/repos/benjamin-hodgson/Pidgin/issues/13/events","html_url":"https://github.com/benjamin-hodgson/Pidgin/issues/13","id":340738001,"node_id":"MDU6SXNzdWUzNDA3MzgwMDE=","number":13,"title":"Error with sequencing parsers on .NET Core 2.1","user":{"login":"valxv","id":7983178,"node_id":"MDQ6VXNlcjc5ODMxNzg=","avatar_url":"https://avatars0.githubusercontent.com/u/7983178?v=4","gravatar_id":"","url":"https://api.github.com/users/valxv","html_url":"https://github.com/valxv","followers_url":"https://api.github.com/users/valxv/followers","following_url":"https://api.github.com/users/valxv/following{/other_user}","gists_url":"https://api.github.com/users/valxv/gists{/gist_id}","starred_url":"https://api.github.com/users/valxv/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/valxv/subscriptions","organizations_url":"https://api.github.com/users/valxv/orgs","repos_url":"https://api.github.com/users/valxv/repos","events_url":"https://api.github.com/users/valxv/events{/privacy}","received_events_url":"https://api.github.com/users/valxv/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-07-12T17:54:40Z","updated_at":"2018-07-13T07:32:06Z","closed_at":"2018-07-13T07:32:06Z","author_association":"NONE","body":"This test\r\n```csharp\r\n[Theory]\r\n[InlineData(\"<>\", 1)]\r\n[InlineData(\"<\", 2)]\r\n[InlineData(\">\", 3)]\r\n[InlineData(\"=\", 4)]\r\npublic void TryParse(string source, int expected)\r\n{\r\n    Parser<char, int> parser =\r\n        Parser.Char('<').Then(_ => Parser.Char('>')).ThenReturn(1)\r\n        .Or(Parser.Char('<').ThenReturn(2))\r\n        .Or(Parser.Char('>').ThenReturn(3))\r\n        .Or(Parser.Char('=').ThenReturn(4));\r\n     \r\n    Assert.Equal(expected, parser.ParseOrThrow(source));\r\n}\r\n```\r\nfails with the following error\r\n\r\n> Failed   PidginTests.PidginTests.TryParse(source: \"<\", expected: 2)\r\nError Message:\r\n Pidgin.ParseException : Parse error.\r\n    unexpected EOF\r\n    expected \"<\"\r\n    at line 1, col 2\r\nStack Trace:\r\n   at Pidgin.ParserExtensions.GetValueOrThrow[TToken,T](Result`2 result)\r\n   at Pidgin.ParserExtensions.ParseOrThrow[T](Parser`2 parser, String input, Func`3 calculatePos)\r\n   at PidginTests.PidginTests.TryParse(String source, Int32 expected)\r\n\r\nOther tests in theory passed.\r\nVerified on Windows 10 and MacOS."},"comment":{"url":"https://api.github.com/repos/benjamin-hodgson/Pidgin/issues/comments/404751317","html_url":"https://github.com/benjamin-hodgson/Pidgin/issues/13#issuecomment-404751317","issue_url":"https://api.github.com/repos/benjamin-hodgson/Pidgin/issues/13","id":404751317,"node_id":"MDEyOklzc3VlQ29tbWVudDQwNDc1MTMxNw==","user":{"login":"benjamin-hodgson","id":3857587,"node_id":"MDQ6VXNlcjM4NTc1ODc=","avatar_url":"https://avatars2.githubusercontent.com/u/3857587?v=4","gravatar_id":"","url":"https://api.github.com/users/benjamin-hodgson","html_url":"https://github.com/benjamin-hodgson","followers_url":"https://api.github.com/users/benjamin-hodgson/followers","following_url":"https://api.github.com/users/benjamin-hodgson/following{/other_user}","gists_url":"https://api.github.com/users/benjamin-hodgson/gists{/gist_id}","starred_url":"https://api.github.com/users/benjamin-hodgson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benjamin-hodgson/subscriptions","organizations_url":"https://api.github.com/users/benjamin-hodgson/orgs","repos_url":"https://api.github.com/users/benjamin-hodgson/repos","events_url":"https://api.github.com/users/benjamin-hodgson/events{/privacy}","received_events_url":"https://api.github.com/users/benjamin-hodgson/received_events","type":"User","site_admin":false},"created_at":"2018-07-13T07:32:06Z","updated_at":"2018-07-13T07:32:06Z","author_association":"OWNER","body":"Thanks for getting in touch! Your parser is of the form:\r\n\r\n```csharp\r\nChar('<').Then(Char('>))\r\n    .Or(...);\r\n```\r\n\r\n(I recommend putting `using static Pidgin.Parser; using static Pidgin.Parser<char>;` at the top of your file so you don't have to repeat the word `Parser`.)\r\n\r\n`Or` branches are evaluated by attempting to take each branch in sequence, entering the first branch which consumes some input. So when the first branch of the `Or` consumes the `<`, the parser commits to the first branch. Since the `<` is not followed by a `>` the parser then goes on to fail.\r\n\r\nYou have two options. One is to apply `Try`, to make the parser backtrack when it fails upon encountering the EOF:\r\n\r\n```csharp\r\nTry(Char('<').Then(Char('>')))\r\n    .Or(...)\r\n```\r\n\r\nYou can think of `Try` as dropping a bookmark in the input stream to which the parser can backtrack upon failure, somewhat like how `try`/`catch` in C# puts a bookmark in the call stack. Now, when the parser fails, it won't consume any input (because it backtracked), so the remaining branches of `Or` can be tried.\r\n\r\nThe other option is to combine the two branches of `Or` which begin with `<`, using `Optional` to distinguish inputs which continue with `>`.\r\n\r\n```csharp\r\nChar('<').Then(Char('>').Optional()).Select(mc => mc.HasValue ? 1 : 2)\r\n        .Or(Char('>').ThenReturn(3))\r\n        .Or(Char('=').ThenReturn(4));\r\n```"}}
{"actor":{"display_login":"benjamin-hodgson"}}