{"action":"created","issue":{"url":"https://api.github.com/repos/pegjs/pegjs/issues/310","labels_url":"https://api.github.com/repos/pegjs/pegjs/issues/310/labels{/name}","comments_url":"https://api.github.com/repos/pegjs/pegjs/issues/310/comments","events_url":"https://api.github.com/repos/pegjs/pegjs/issues/310/events","html_url":"https://github.com/pegjs/pegjs/issues/310","id":54017692,"number":310,"title":"Parser doesn't recover if optional token steals match from a required token.","user":{"login":"DigiTec","id":1650078,"avatar_url":"https://avatars.githubusercontent.com/u/1650078?v=3","gravatar_id":"","url":"https://api.github.com/users/DigiTec","html_url":"https://github.com/DigiTec","followers_url":"https://api.github.com/users/DigiTec/followers","following_url":"https://api.github.com/users/DigiTec/following{/other_user}","gists_url":"https://api.github.com/users/DigiTec/gists{/gist_id}","starred_url":"https://api.github.com/users/DigiTec/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DigiTec/subscriptions","organizations_url":"https://api.github.com/users/DigiTec/orgs","repos_url":"https://api.github.com/users/DigiTec/repos","events_url":"https://api.github.com/users/DigiTec/events{/privacy}","received_events_url":"https://api.github.com/users/DigiTec/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-01-12T03:29:35Z","updated_at":"2015-01-12T13:02:16Z","closed_at":null,"body":"Note, I haven't fully debugged this and there are many workarounds that I have found to get around the bug. However, it would be interesting to understand why it fails to match and what might be wrong in the backtracking behavior.\r\n\r\nNote the parser in this case isn't very strong because of its use of _, its only to demonstrate the issue. By making mods and type more specific and by requiring trailing [ ]+ instead of [ ]* between tokens then things work out a lot better and the parser works.\r\n\r\n/*\r\nInput\r\nfinal LatLng(\r\n\r\nExpected:\r\nmods resolves 'final'\r\n_ resolves space\r\ntype? resolves LatLng then fails then _ and fails ident\r\ntype? advances then _ resolves nothing ident resolves LatLng...\r\n */\r\n\r\nstart = mods _ type? _ ident _ '('\r\n\r\nmods = 'public'? _ 'private'? _ 'final'?\r\n\r\ntype = IDENT\r\n\r\nident = IDENT\r\n\r\nIDENT = [A-Za-z_]+\r\n\r\n_ = [ ]*"},"comment":{"url":"https://api.github.com/repos/pegjs/pegjs/issues/comments/69566300","html_url":"https://github.com/pegjs/pegjs/issues/310#issuecomment-69566300","issue_url":"https://api.github.com/repos/pegjs/pegjs/issues/310","id":69566300,"user":{"login":"dmajda","id":18821,"avatar_url":"https://avatars.githubusercontent.com/u/18821?v=3","gravatar_id":"","url":"https://api.github.com/users/dmajda","html_url":"https://github.com/dmajda","followers_url":"https://api.github.com/users/dmajda/followers","following_url":"https://api.github.com/users/dmajda/following{/other_user}","gists_url":"https://api.github.com/users/dmajda/gists{/gist_id}","starred_url":"https://api.github.com/users/dmajda/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dmajda/subscriptions","organizations_url":"https://api.github.com/users/dmajda/orgs","repos_url":"https://api.github.com/users/dmajda/repos","events_url":"https://api.github.com/users/dmajda/events{/privacy}","received_events_url":"https://api.github.com/users/dmajda/received_events","type":"User","site_admin":false},"created_at":"2015-01-12T13:02:16Z","updated_at":"2015-01-12T13:02:16Z","body":"@DigiTec The `?` oeprator doesn’t backtrack. This means the failure of `ident` to match doesn’t magically cause `type?` (which already matched `LatLng`) to match nothing and retry. This is not a bug, this is by design.\r\n\r\nThe grammar can be easily fixed e.g. like this:\r\n\r\n    start = mods _ type _ ident _ '('\r\n          / mods _ ident _ '('\r\n\r\n@Mingun Please avoid unnecessary statements like your last one. Thanks."}}