Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when minified #7

Closed
stefan-leye opened this issue Apr 11, 2017 · 12 comments
Closed

Error when minified #7

stefan-leye opened this issue Apr 11, 2017 · 12 comments

Comments

@stefan-leye
Copy link

When minifying jison (and most sepecifically the regexp-lexer.js) an error is thrown when executing the parser generator.
Basically, there seems to be a problem with the minified lexer function, that is translated to a string and back.
The error happens here:
https://github.com/GerHobbelt/jison/blob/master/lib/util/regexp-lexer.js#L1043

I tested it with minification by uglify and goolge-closure.

It would be great, if there was a fix for that.

@GerHobbelt
Copy link
Owner

regexp-lexer.js is a compiler and not really meant to be 'uglified' as it should run in a developer environment only, so I wonder what your use case is, where you want/need to uglify/compress this particular file.

(The generated code on the other hand, i.e. the generated lexer, is meant to be minified as it would be running in any production environment.)

@stefan-leye
Copy link
Author

I'm currently working on a web-based project, where users need a highly dynamical filter language. They have to adapt the language (not just the filters themselves) to their needs/work environment.

To create such languages dynamically, they have to be generated on the client, and the code for that should be minified.

@GerHobbelt
Copy link
Owner

Got it. That's a bit of a challenge. ;-) Anyway, don't know when I get this fixed; for the time being, I would advise to find a way to make sure the critical code parts don't get obfuscated by the minifier -- my bet is that possibly the JisonLexerError 'class' (a function) is minified while it shouldn't as that is a 'global'. But that's just my guestimate... what is the actual error / crash report you get?

(Note to self: I could add a 'crash dump' functionality here a la 85e367d to help us diagnose what exactly is causing the new Function(...) code to barf a hairball.)

GerHobbelt added a commit to GerHobbelt/jison-lex that referenced this issue Apr 11, 2017
…jison@85e367d for the generated *lexer* source code.

**Note to self**: we define the utility function here, inside the jison-lex module, as that is the bottom = leaf dependency, while a *truly clean approach* would have `safe-code-exec-and-diag.js` sit in a (new) separate repository and `require()`-d into this module and the `jison` module instead, as `safe-code-exec-and-diag.js` is a generic module & API that is *unrelated* to the specific task of producing a **lexer** run-time source code!
GerHobbelt added a commit that referenced this issue Apr 11, 2017
GerHobbelt added a commit that referenced this issue Apr 11, 2017
…xer* and *parser* source code, using the `safe-code-exec-and-diag` module/API.

**Note to self**: we define the utility function inside the `jison-lex` module as that is the bottom = leaf dependency, while a *truly clean approach* would have `safe-code-exec-and-diag.js` sit in a (new) separate repository and `require()`-d into this module and the `jison` module instead, as `safe-code-exec-and-diag.js` is a generic module & API that is *unrelated* to the specific task of producing a **lexer** run-time source code!
@GerHobbelt
Copy link
Owner

GerHobbelt commented Apr 11, 2017

Please check the latest master; the generated code is dumped to file when a crash like that occurs, so that you can diagnose more precisely what went wrong during code generation.

(run node <named-dumpfile.js> to see where the generated code fails exactly. This helps diagnose what went wrong and how in the code generator.)

GerHobbelt added a commit to GerHobbelt/jison-lex that referenced this issue Apr 12, 2017
…n our situation(s)) is part of the crash dump file when the generated code crashes on compilation or execution within jison.
GerHobbelt added a commit to GerHobbelt/jison-lex that referenced this issue Apr 12, 2017
… set them via API, parser/lexer grammar input (`%options <camel-casing-option>=<value>`) and/or CLI.
GerHobbelt added a commit to GerHobbelt/jison-lex that referenced this issue Apr 12, 2017
…` and `throwErrorOnFailure` available in the jison API and CLI (`--dump-sourcecode-on-failure` and `--throw-on-compile-failure`), so that users can *disable* these features (both are *enabled* by default) when desired. (Also adjusted the naming of one of the options to `throwErrorOnCompileFailure` to make the option name itself more descriptive of what it's about.)
@stefan-leye
Copy link
Author

thank you, I will do that asap.

GerHobbelt added a commit that referenced this issue Apr 12, 2017
…s)) is part of the crash dump file when the generated code crashes on compilation or execution within jison.
GerHobbelt added a commit that referenced this issue Apr 12, 2017
…I, parser/lexer grammar input (`%options <camel-casing-option>=<value>`) and/or CLI.
GerHobbelt added a commit that referenced this issue Apr 12, 2017
…rOnFailure` available in the jison API and CLI (`--dump-sourcecode-on-failure` and `--throw-on-compile-failure`), so that users can *disable* these features (both are *enabled* by default) when desired. (Also adjusted the naming of one of the options to `throwErrorOnCompileFailure` to make the option name itself more descriptive of what it's about.)
@stefan-leye
Copy link
Author

stefan-leye commented Apr 12, 2017

After following your instructions I get the following error message:

function getRegExpLexerPrototype(){return{EOF:1,ERROR:2,currentRuleSet:null,__error_infos:[],__decompressed:!1,done:
!1,_backtrack:!1,_input:"",_more:!1,_signaled_error_token:!1,conditionStack:[],match:"",matched:"",matches:!1,yytext:"",
offset:0,yyleng:0,yylineno:0,yylloc:null,constructLexErrorInfo:function(e,r){var n={errStr:e,recoverable:!!r,text:this.m
atch,token:null,line:this.yylineno,loc:this.yylloc,yy:this.yy,lexer:this,destroy:function(){var e=!!this.recoverable;for
(var r in this)this.hasOwnProperty(r)&&"object"==typeof r&&(this[r]=void 0);this.recoverable=e}};return this.__error_inf
os.push(n),n},parseError:function(e,r,n){if(this.yy.parser&&"function"==typeof this.yy.parser.parseError)return this.yy.
parser.parseError(e,r,n)||this.ERROR;if("function"==typeof this.yy.parseError)return this.yy.parseError(e,r,n)||this.ERR
OR;throw new n(e,r)},cleanupAfterLex:function(e){if(this.setInput("",{}),!e){for(var r=this.__error_infos.length-1;r>=0;
r--){var n=this.__error_infos[r];n&
SyntaxError: Unexpected identifier
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3

This is the failing code (generated by a minified regexp-lexer.js):
dump.txt

GerHobbelt added a commit to GerHobbelt/jison-lex that referenced this issue Apr 14, 2017
…parser to lexer code generator. Also make sure these generator options are properly stripped from the generated source, since they won't be usable in/from there!
GerHobbelt added a commit to GerHobbelt/jison-lex that referenced this issue Apr 14, 2017
…them slightly more flexible in the light of code generator changes and/or minification processes.
GerHobbelt added a commit to GerHobbelt/jison-lex that referenced this issue Apr 14, 2017
…reCompiler markup to help compression. Also cleaning a few spots in the code to help the code optimizers (google closure compiler / uglifyJS).
GerHobbelt added a commit that referenced this issue Apr 14, 2017
…code generator. Also make sure these generator options are properly stripped from the generated source, since they won't be usable in/from there!
GerHobbelt added a commit that referenced this issue Apr 14, 2017
…re flexible in the light of code generator changes and/or minification processes.
@GerHobbelt
Copy link
Owner

Hi Stefan,
There's a new master, which should behave a wee bit better (though I don't expect it to pass without a hitch -- long story there); thanks for testing the previous round. Can you run another round with the latest master and tell me what you get (dumpfile + error report)?

BTW: are you using UglifyJS2 or Google Closure Compiler?

@stefan-leye
Copy link
Author

stefan-leye commented Apr 14, 2017

Here is the error with the new code:

dump.js:33
options: {},
^
SyntaxError: Unexpected token :
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3

This is the failing code: dump.txt

P.S. Currently I'm using a Webpack pipeline using the Uglify loader for minification, as only loaders allow me to exclude specific input files. But I'm very keen on switching to the Google Closure Compiler, which I can only do, when all files and libs are minifiable.

@GerHobbelt
Copy link
Owner

Hm, looks like maybe this can help resolve this issue: https://github.com/google/closure-compiler/wiki/Annotating-JavaScript-for-the-Closure-Compiler#user-content-nocompile

Will add that as a quick-fix band-aid to the jison lexer and parser engines as only the generated code (lexer and parser produced using user defined grammars) is meant to be used in production environments, i.e. minified form.

@GerHobbelt
Copy link
Owner

release v0.6.1-200 uses a different-enough approach now to code generation that this one might now be feasible. (haven't tested it yet, though!)

@GerHobbelt
Copy link
Owner

@stefan-leye: working on #32 now: this might be of interest to you too as it will make the jison tool itself more amenable to code compressors/rewriters and thus it is expected that once #32 is done, jison should cause minimal or even no trouble for gcc (google closure compiler) either.

GerHobbelt added a commit that referenced this issue Dec 22, 2017
…code components exported into generated parsers and/or lexers WILL NOT carry any undesirable code coverage or other code injection/rewriting/etc. artifacts: the code should be exported to the generated parser/lexer codebase as-is, without running the chance that another outside process (compressor/rewriter) modifies the code in unexpected ways). This should also help to reduce the troubles with #7 .

Note: now `actionInclude` section uses the same code extraction logic as any action code chunk, hence now also supports arrow functions, when you happen to have specified one of those via the API.
@GerHobbelt
Copy link
Owner

Closing due to age; please open a fresh issue, referencing this one for future ref on the same or similar subject.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants