BNFC / bnfc

BNF Converter
http://bnfc.digitalgrammars.com/
587 stars 165 forks source link

The Ocaml printer crashes if the indent level becomes negative #439

Closed Arm-Mike closed 1 year ago

Arm-Mike commented 1 year ago

The follow type of rule causes the ocaml printer to crash test. A ::= "{{" Id "}" "}" ; -- Note: should be "}}"

The error message is Fatal error: exception Invalid_argument("Bytes.create")

I have traced this to the indent function let indent (i: int) : string = "\n" ^ String.make i ' ' and I think that changed to let indent (i: int) : string = if i > 0 then "\n" ^ String.make i ' ' else "\n" ;; to match the behavior of other backends.

andreasabel commented 1 year ago

@Arm-Mike : Good catch, thanks for the report!

Maybe it is a bit silly by Ocaml to not truncate negative quantities passed to String.make. They seem to be obsessed with not a bounds check too much, living by the motto let it crash.

Anyway, I fixed this along the lines you suggested, and the fix should be on master soon. Is this fine with you or do you need a release of this fix urgently?

As a workaround, you can always patch the output of BNFC with the patch commandline tool.