{"action":"created","issue":{"url":"https://api.github.com/repos/pegjs/pegjs/issues/315","labels_url":"https://api.github.com/repos/pegjs/pegjs/issues/315/labels{/name}","comments_url":"https://api.github.com/repos/pegjs/pegjs/issues/315/comments","events_url":"https://api.github.com/repos/pegjs/pegjs/issues/315/events","html_url":"https://github.com/pegjs/pegjs/pull/315","id":56857892,"number":315,"title":"fix generated parsers not working in strict mode","user":{"login":"vith","id":3265539,"avatar_url":"https://avatars.githubusercontent.com/u/3265539?v=3","gravatar_id":"","url":"https://api.github.com/users/vith","html_url":"https://github.com/vith","followers_url":"https://api.github.com/users/vith/followers","following_url":"https://api.github.com/users/vith/following{/other_user}","gists_url":"https://api.github.com/users/vith/gists{/gist_id}","starred_url":"https://api.github.com/users/vith/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vith/subscriptions","organizations_url":"https://api.github.com/users/vith/orgs","repos_url":"https://api.github.com/users/vith/repos","events_url":"https://api.github.com/users/vith/events{/privacy}","received_events_url":"https://api.github.com/users/vith/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-02-06T19:41:10Z","updated_at":"2015-02-24T04:44:43Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/pegjs/pegjs/pulls/315","html_url":"https://github.com/pegjs/pegjs/pull/315","diff_url":"https://github.com/pegjs/pegjs/pull/315.diff","patch_url":"https://github.com/pegjs/pegjs/pull/315.patch"},"body":"I had written a PEG that was working in the online demo but not in my app. There were two problems.\r\n\r\nThe first error I got was `generate-bytecode.js:406 Uncaught ReferenceError: failedIndex is not defined`.\r\n\r\nTook me a while to figure out what was going on, because there's no such symbol `failedIndex` in either the latest commit to master or the 0.8.0 tagged commit.\r\n\r\nI had installed pegjs with `npm install --save-dev pegjs`, which reports the version as 0.8.0. However, the `failedIndex` error goes away if I install pegjs with `npm install --save-dev pegjs/pegjs`, which is also version 0.8.0. No idea where npm got the broken version of 0.8.0, maybe it has something to do with the repository being moved from one namespace to another on github. As you can see here https://www.npmjs.com/package/pegjs npm still lists the package source as github.com/dmajda/pegjs.\r\n\r\nAnyway, with `pegjs/pegjs` the `failedIndex` error is gone, but then I have the problem fixed in this pull request: `parser.js:4838 Uncaught SyntaxError: Unexpected strict mode reserved word`\r\n\r\nSo what's happening here is the javascript generator wraps the parser initializer in `{` `}`. This is invalid javascript code; it looks like a bare object but contains a block of code rather than object members.\r\n\r\nThe generated parsers don't include a `'use strict';` statement, so this normally just works anyway I guess. But I'm using webpack which is bundling a generated parser together with other modules and apparently strict mode is somehow applying to the parser, which causes the browser to bail out instead of executing the contents of the `{` `}` block.\r\n\r\nMy patch just stops wrapping the initializer in `{` `}`. It appears to work, but I'm wondering if the initializer and other code blocks should actually be more isolated from the rest of the generated code. I see most of the items defined inside `function peg$parse` are prefixed with `peg$` to prevent collisions, but there are a few names that a user might try to use: `input`, `options`, `text`, `offset`, `line`, `column`, `expected`, `error`. In fact, if I make an initializer like `{ input = null; }`, the parser breaks."},"comment":{"url":"https://api.github.com/repos/pegjs/pegjs/issues/comments/75698819","html_url":"https://github.com/pegjs/pegjs/pull/315#issuecomment-75698819","issue_url":"https://api.github.com/repos/pegjs/pegjs/issues/315","id":75698819,"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-02-24T04:44:42Z","updated_at":"2015-02-24T04:44:42Z","body":"> No idea where npm got the broken version of 0.8.0\r\n\r\nNot sure how you got the idea that PEG.js 0.8.0 package is broken in any way. I just moved the PEG.js repository since I released 0.8.0 and I didn’t bump the version in Git yet (will do that just before the 0.9.0 release).\r\n\r\n> Anyway, with `pegjs/pegjs` the `failedIndex` error is gone, but then I have the problem fixed in this pull request: `parser.js:4838 Uncaught SyntaxError: Unexpected strict mode reserved word`\r\n>\r\n> So what's happening here is the javascript generator wraps the parser initializer in `{` `}`. This is invalid javascript code; it looks like a bare object but contains a block of code rather than object members.\r\n\r\nThe initializer block is [perfectly valid JavaScript code](http://www.ecma-international.org/ecma-262/5.1/#sec-12.1). Your error message suggests that the problem with your parser was *inside* its initializer code.\r\n\r\nThe reason for wrapping the initializer code into `{` and `}` is mostly aesthetical — the output matches what was in the grammar exactly and the initializer code is visually separated from the parser code itself.\r\n\r\n> My patch just stops wrapping the initializer in `{` `}`. It appears to work, [...]\r\n\r\nBased on what I know and on the information you provided, there should be no difference.\r\n \r\n> I'm wondering if the initializer and other code blocks should actually be more isolated from the rest of the generated code. I see most of the items defined inside `function peg$parse` are prefixed with `peg$` to prevent collisions, but there are a few names that a user might try to use: `input`, `options`, `text`, `offset`, `line`, `column`, `expected`, `error`. In fact, if I make an initializer like `{ input = null; }`, the parser breaks.\r\n\r\nThe unprefixed identifiers are exactly the ones the parser code *should* see. The fact that the code can assign to some variables beside just reading them is unfortunate but can’t be easily prevented.\r\n\r\nAs for the isolation, I’d like to have more of it, but it’s not possible because of JavaScript lexical scoping and for performance reasons.\r\n\r\nAs for the strict mode, it’s definitely worthwhile to have both PEG.js and the parsers it generates in strict mode, but it’s not a priority for me ATM. I filed #323 and #324 to track this. "}}