123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899openReasonopenPpxlibtypet=Parsetree.structureleterr=Printer_maker.err(* Note: filename should only be used with .ml files. See reason_toolchain. *)letdefaultImplementationParserForuse_stdinfilename=letopenReason_toolchaininlettheParser,parsedAsML=ifFilename.check_suffixfilename".re"thenRE.implementation_with_comments,falseelseifFilename.check_suffixfilename".ml"thenML.implementation_with_comments,trueelseerr("Cannot determine default implementation parser for filename '"^filename^"'.")inletast,comments=theParser(setup_lexbuf~use_stdinfilename)in{Printer_maker.ast;comments;parsed_as_ml=parsedAsML;parsed_as_intf=false}letparse~use_stdinfiletypefilename=let{Printer_maker.ast;comments;parsed_as_ml=parsedAsML;parsed_as_intf=parsedAsInterface}=matchfiletypewith|`Auto->defaultImplementationParserForuse_stdinfilename|`BinaryReason->Printer_maker.reasonBinaryParseruse_stdinfilename|`Binary->Printer_maker.ocamlBinaryParseruse_stdinfilename|`ML->letlexbuf=Reason_toolchain.setup_lexbuf~use_stdinfilenameinletimpl=Reason_toolchain.ML.implementation_with_commentsinletast,comments=impllexbufin{ast;comments;parsed_as_ml=true;parsed_as_intf=false}|`Reason->letlexbuf=Reason_toolchain.setup_lexbuf~use_stdinfilenameinletimpl=Reason_toolchain.RE.implementation_with_commentsinletast,comments=impllexbufin{ast;comments;parsed_as_ml=false;parsed_as_intf=false}inifparsedAsInterfacethenerr"The file parsed does not appear to be an implementation file."else(ast,comments),parsedAsMLletprintprinttypefilenameparsedAsMLoutput_chanoutput_formatter=matchprinttypewith|`BinaryReason->fun(ast,comments)->(* Our special format for interchange between reason should keep the
* comments separate. This is not compatible for input into the
* ocaml compiler - only for input into another version of Reason. We
* also store whether or not the binary was originally *parsed* as an
* interface file.
*)output_valueoutput_chan(Ocaml_common.Config.ast_impl_magic_number,filename,ast,comments,parsedAsML,false)|`Binary->fun(ast,_)->letast=ast|>Reason_syntax_util.(apply_mapper_to_structureremove_stylistic_attrs_mapper)|>Reason_syntax_util.(apply_mapper_to_structurebackport_letopt_mapper)inPpxlib__.Utils.Ast_io.writeoutput_chan{Ppxlib__.Utils.Ast_io.input_name=filename;input_version=Obj.magic(modulePpxlib_ast.Compiler_version:Ppxlib_ast.OCaml_version);ast=Implast}~add_ppx_context:false|`AST->fun(ast,_)->Ocaml_common.Printast.implementationoutput_formatter(Reason_toolchain.To_current.copy_structureast)|`None->fun_->()|`ML->Reason_toolchain.ML.print_implementation_with_commentsoutput_formatter|`Reason->Reason_toolchain.RE.print_implementation_with_commentsoutput_formatter