Problem bundling compiled grammars with rollup
I'm trying to build a library that depends on a library that uses jison to generate a grammar.
Unfortunately while building with rollup I get the following error:
Error loading C:\Users\download\code\modules\swkit\node_modules\route-parser\lib\route\compiled-grammar.js: Invalid labeled declaration (163:8) in C:\Users\download\code\modules\swkit\node_modules\route-parser\lib\route\compiled-grammar.js
SyntaxError: Error loading C:\Users\download\code\modules\swkit\node_modules\route-parser\lib\route\compiled-grammar.js: Invalid labeled declaration (163:8) in C:\Users\download\code\modules\swkit\node_modules\route-parser\lib\route\compiled-grammar.js
at Parser.pp$4.raise (C:\Users\download\code\modules\swkit\node_modules\acorn\dist\acorn.js:2488:13)
at Parser.pp$1.parseLabeledStatement (C:\Users\download\code\modules\swkit\node_modules\acorn\dist\acorn.js:1017:10)
at Parser.pp$1.parseStatement (C:\Users\download\code\modules\swkit\node_modules\acorn\dist\acorn.js:779:19)
at Parser.pp$1.parseBlock (C:\Users\download\code\modules\swkit\node_modules\acorn\dist\acorn.js:1040:23)
at Parser.pp$3.parseFunctionBody (C:\Users\download\code\modules\swkit\node_modules\acorn\dist\acorn.js:2362:22)
at Parser.pp$1.parseFunction (C:\Users\download\code\modules\swkit\node_modules\acorn\dist\acorn.js:1132:8)
at Parser.pp$3.parseExprAtom (C:\Users\download\code\modules\swkit\node_modules\acorn\dist\acorn.js:1999:17)
at Parser.pp$3.parseExprSubscripts (C:\Users\download\code\modules\swkit\node_modules\acorn\dist\acorn.js:1872:19)
at Parser.pp$3.parseMaybeUnary (C:\Users\download\code\modules\swkit\node_modules\acorn\dist\acorn.js:1849:17)
at Parser.pp$3.parseExprOps (C:\Users\download\code\modules\swkit\node_modules\acorn\dist\acorn.js:1791:19)
Type rollup --help for help, or visit https://github.com/rollup/rollup/wiki
The files concerned can be seen here.
Do you know why this bit is considered to be invalid javascript? The whole file looks a little odd to me.
Is there something that can be changed to make the file pass validation?
I think I found the problem.
According to this, labeled function declarations are not allowed in strict mode, which is how all ES modules are treated. Since rollup only operates on ES modules the labeled function declaration is considered invalid.
I see that the label is used as a marker for where to add a function into the file.
Nevermind. I just realized that this is already fixed in #285. I'll close this.
Nevermind nevermind. Apparently #285 did not fix this issue.
For some reason addTokenStack is never called and so never has the chance to remove the _token_stack: label. It looks like addTokenStack only runs if the option token-stack has been provided, but the actual token stack label is in the code regardless of whether the option is present.
Is there some way to ensure that the _token_stack: line is always removed from the output?
#352 fixes this.
A more correct way to do it would be replacing the LabeledStatement in the AST with the contents of it's body property, but the string replacement method works fine for now.
I am experiencing this issue as well with https://github.com/knsv/mermaid (see https://github.com/knsv/mermaid/issues/277#issuecomment-304744162 ).
My solution was to remove all occurrences of _token_stack: from the generated code. It looks like you found the same problem and solution!
Why is https://github.com/zaach/jison/pull/352 not merged yet? I see the current workaround is the post-process the generated code and strip the _token_stack: parts out: https://github.com/rcs/route-parser/issues/26
This is laughable since the fix should be upstream in jison 😜 , however I am glad we at least this workaround.
Jison team, is there anything we can do to help you get this merged?