laurentlb / shader-minifier

Minify and obfuscate GLSL or HLSL code

Home Page:https://ctrl-alt-test.fr/minifier/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrect parsing behavior with `--preprocess` option enabled

Lutymane opened this issue · comments

For some reason if shader doesn't end with an empty line it throws:

System.Exception: Parse error: Error in ./test/s.glsl: Ln: 3 Col: 2
}
 ^
Note: The error occurred at the end of the input stream.
Expecting: any char

   at Microsoft.FSharp.Core.PrintfModule.PrintFormatToStringThenFail@1439.Invoke(String message)
   at Parse.ParseImpl.runParser(String streamName, String content)
   at ShaderMinifier.minify(Tuple`2[] files)
   at ShaderMinifier.run(String[] files)

Test code:

void main()  {
    gl_FragColor = vec4(1);
}

Notice there's no whitespace after last }

Test command:

./bin/shader_minifier-135.exe ./test/s.glsl -o - --format js --preprocess

Current workaround is just to add a new line at the end of the file

Thanks for the report!
The flag is very experimental (read: half implemented), so expect other issues. The bug reports will be useful whenever someone works on the feature again (it might take a few months).

@laurentlb I could work on it, but I don't know F# at all 🥲

@laurentlb Yeah, I could perhaps formulate multiple levels of support for preprocessor and case coverage by supplying test shader codes.

Also, I got a question. Does the preprocessor run during the parsing? Because I tried to minify this code:

#define BBB 1

void main() {

  vec4 frag = vec4(1);

  frag.rgb = 
#if BBB
    vec3(1)
#else
    vec3(0)
#endif
  ;

  gl_FragColor = frag;
}

And if failed with:

System.Exception: Parse error: Error in ./test/s.glsl: Ln: 8 Col: 1
#if BBB
^
Expecting: cast, expression or prefix operator

   at Microsoft.FSharp.Core.PrintfModule.PrintFormatToStringThenFail@1439.Invoke(String message)
   at ShaderMinifier.minify(Tuple`2[] files)
   at ShaderMinifier.run(String[] files)

It works with #ifdef, not with #if.

At the moment, the condition after #if is not evaluated (except if it's a literal 0 or 1).