{"action":"created","issue":{"url":"https://api.github.com/repos/goccmack/gocc/issues/18","repository_url":"https://api.github.com/repos/goccmack/gocc","labels_url":"https://api.github.com/repos/goccmack/gocc/issues/18/labels{/name}","comments_url":"https://api.github.com/repos/goccmack/gocc/issues/18/comments","events_url":"https://api.github.com/repos/goccmack/gocc/issues/18/events","html_url":"https://github.com/goccmack/gocc/pull/18","id":147895784,"number":18,"title":"parser: enable custom error handling","user":{"login":"mewmew","id":1414531,"avatar_url":"https://avatars.githubusercontent.com/u/1414531?v=3","gravatar_id":"","url":"https://api.github.com/users/mewmew","html_url":"https://github.com/mewmew","followers_url":"https://api.github.com/users/mewmew/followers","following_url":"https://api.github.com/users/mewmew/following{/other_user}","gists_url":"https://api.github.com/users/mewmew/gists{/gist_id}","starred_url":"https://api.github.com/users/mewmew/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mewmew/subscriptions","organizations_url":"https://api.github.com/users/mewmew/orgs","repos_url":"https://api.github.com/users/mewmew/repos","events_url":"https://api.github.com/users/mewmew/events{/privacy}","received_events_url":"https://api.github.com/users/mewmew/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2016-04-12T22:49:08Z","updated_at":"2016-04-14T08:28:37Z","closed_at":"2016-04-13T20:50:57Z","pull_request":{"url":"https://api.github.com/repos/goccmack/gocc/pulls/18","html_url":"https://github.com/goccmack/gocc/pull/18","diff_url":"https://github.com/goccmack/gocc/pull/18.diff","patch_url":"https://github.com/goccmack/gocc/pull/18.patch"},"body":"While implementing a simple compiler for a subset of C, we wanted to add user-friendly error messages, and therefore started looking for ways of integrating them into the Gocc generated parser.\r\n\r\nAs Gocc has support for `error` production rules, this was the obvious mechanism to hook into. However, the errors reported were verbose and while helpful to a compiler developer, they were not user-friendly for end-users of the compiler.\r\n\r\nTo mitigate this issue, we implemented a way of creating custom error messages, using the builtin `error` interface of Go, to keep the current error messages as is, while enabling extensions (through type assertions).\r\n\r\nAn example using the updated code is provided below, using [uparse](https://github.com/mewmew/uc/blob/db976f881abc76f9806d71fca1eb3b2083627c12/cmd/uparse/uparse.go) to parse the incorrect input file [testdata/incorrect/parser/pe01.c ](https://github.com/mewmew/uc/blob/db976f881abc76f9806d71fca1eb3b2083627c12/testdata/incorrect/parser/pe01.c).\r\n\r\n### Before\r\n\r\n* uparse from commit [5f744176565bb4a797828b4ce981fbd16903af40](https://github.com/mewmew/uc/tree/5f744176565bb4a797828b4ce981fbd16903af40)\r\n\r\n```\r\nu@x1 ~/D/g/s/g/m/uc> uparse testdata/incorrect/parser/pe01.c \r\nParsing \"testdata/incorrect/parser/pe01.c\"\r\n2016/04/13 00:38:44 main.parseFile (uparse.go:85): error: Error in S157: )(6,)), Pos(offset=102, line=0, column=0)102: expected [\"ident\" \"(\" \"int_lit\" \"-\" \"!\"], got \")\"\r\n```\r\n\r\n### After\r\n\r\n* uparse from commit mewmew/uc@db976f881abc76f9806d71fca1eb3b2083627c12\r\n\r\n```\r\nu@x1 ~/D/g/s/g/m/uc> uparse testdata/incorrect/parser/pe01.c \r\nParsing \"testdata/incorrect/parser/pe01.c\"\r\n2016/04/13 00:39:37 main.parseFile (uparse.go:88): error: 102: expected [\"ident\" \"(\" \"int_lit\" \"-\" \"!\"], got \")\"\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/goccmack/gocc/issues/comments/209826419","html_url":"https://github.com/goccmack/gocc/pull/18#issuecomment-209826419","issue_url":"https://api.github.com/repos/goccmack/gocc/issues/18","id":209826419,"user":{"login":"mewmew","id":1414531,"avatar_url":"https://avatars.githubusercontent.com/u/1414531?v=3","gravatar_id":"","url":"https://api.github.com/users/mewmew","html_url":"https://github.com/mewmew","followers_url":"https://api.github.com/users/mewmew/followers","following_url":"https://api.github.com/users/mewmew/following{/other_user}","gists_url":"https://api.github.com/users/mewmew/gists{/gist_id}","starred_url":"https://api.github.com/users/mewmew/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mewmew/subscriptions","organizations_url":"https://api.github.com/users/mewmew/orgs","repos_url":"https://api.github.com/users/mewmew/repos","events_url":"https://api.github.com/users/mewmew/events{/privacy}","received_events_url":"https://api.github.com/users/mewmew/received_events","type":"User","site_admin":false},"created_at":"2016-04-14T08:28:37Z","updated_at":"2016-04-14T08:28:37Z","body":"> Sorry it took my so long to understand what you were trying to achieve here, thats my bad. This is really cool :)\r\n\r\nNo worries at all! I feel happy the solution turned out to be very light-weight in terms of restructuring of the Gocc code base, and fitted in naturally with how errors were already handed in Gocc.\r\n\r\nHave a great day!\r\n\r\nCheerful regards from a sunny Sweden :)"}}