Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file compile.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234openOr_erroropenOdoc_model.Names(*
* Copyright (c) 2014 Leo White <leo@lpw25.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)typeparent_spec=|ExplicitofOdoc_model.Paths.Identifier.ContainerPage.t*Odoc_model.Paths.Reference.tlist|PackageofOdoc_model.Paths.Identifier.ContainerPage.t|Noparenttypeparent_cli_spec=|CliParentofstring|CliPackageofstring|CliNoparent(** Parse parent and child references. May print warnings. *)letparse_referencef=letopenOdoc_modelin(* This is a command-line error. *)letwarnings_options={Error.warn_error=true;print_warnings=true}inSemantics.parse_referencef|>Error.handle_errors_and_warnings~warnings_optionsletparentresolverparent_cli_spec=letfind_parent:Odoc_model.Paths.Reference.t->(Odoc_model.Lang.Page.t,[>`Msgofstring])Result.result=funr->matchrwith|`Root(p,`TPage)|`Root(p,`TUnknown)->(matchResolver.lookup_pageresolverpwith|Somer->Okr|None->Error(`Msg"Couldn't find specified parent page"))|_->Error(`Msg"Expecting page as parent")inletextract_parent=function|`Page_ascontainer->Okcontainer|_->Error(`Msg"Specified parent is not a parent of this file")inmatchparent_cli_specwith|CliParentf->parse_referencef>>=funr->find_parentr>>=funpage->extract_parentpage.name>>=funparent->Ok(Explicit(parent,page.children))|CliPackagepackage->Ok(Package(`Page(None,PageName.make_stdpackage)))|CliNoparent->OkNoparentletresolve_importsresolverimports=letopenOdoc_modelinList.map(function|Lang.Compilation_unit.Import.Resolved_asresolved->resolved|Unresolved(name,_)asunresolved->(matchResolver.resolve_importresolvernamewith|Someroot->Resolved(root,Names.ModuleName.make_stdname)|None->unresolved))imports(** Raises warnings and errors. *)letresolve_and_substitute~resolver(parent:Odoc_model.Paths.Identifier.ContainerPage.toption)input_fileread_file=letfilename=Fs.File.to_stringinput_fileinletunit=read_file~parent~filename|>Odoc_model.Error.raise_errors_and_warningsinifnotunit.Odoc_model.Lang.Compilation_unit.interfacethenPrintf.eprintf"WARNING: not processing the \"interface\" file.%s\n%!"(ifnot(Filename.check_suffixfilename"cmt")then""(* ? *)elsePrintf.sprintf" Using %S while you should use the .cmti file"filename);(* Resolve imports, used by the [link-deps] command. *)letunit={unitwithimports=resolve_importsresolverunit.imports}inletenv=Resolver.build_env_for_unitresolver~linking:falseunitinletcompiled=Odoc_xref2.Compile.compile~filenameenvunit|>Odoc_model.Error.raise_warningsin(* [expand unit] fetches [unit] from [env] to get the expansion of local, previously
defined, elements. We'd rather it got back the resolved bit so we rebuild an
environment with the resolved unit.
Note that this is bad and once rewritten expand should not fetch the unit it is
working on. *)(* let expand_env = Env.build env (`Unit resolved) in*)(* let expanded = Odoc_xref2.Expand.expand (Env.expander expand_env) resolved in *)compiledletroot_of_compilation_unit~parent_spec~hidden~output~module_name~digest=letopenOdoc_model.Rootinletfilename=Filename.chop_extensionFs.File.(to_string@@basenameoutput)inletresultparent=letfile=Odoc_file.create_unit~force_hidden:hiddenmodule_nameinOk{id=`Root(parent,ModuleName.make_stdmodule_name);file;digest}inletcheck_child:Odoc_model.Paths.Reference.t->bool=func->matchcwith|`Root(n,`TUnknown)|`Root(n,`TModule)->Astring.String.Ascii.(uncapitalizen=uncapitalizefilename)|_->falseinmatchparent_specwith|Noparent->resultNone|Explicit(parent,children)->ifList.existscheck_childchildrenthenresult(Someparent)elseError(`Msg"Specified parent is not a parent of this file")|Packageparent->result(Someparent)letmld~parent_spec~output~children~warnings_optionsinput=List.fold_left(funaccchild_str->match(acc,parse_referencechild_str)with|Okacc,Okr->Ok(r::acc)|Errorm,_->Errorm|_,Error(`Msgm)->Error(`Msg("Failed to parse child reference: "^m))|_,Error_->Error(`Msg"Unknown failure parsing child reference"))(Ok[])children>>=funchildren->letroot_name=letpage_dash_root=Filename.chop_extensionFs.File.(to_string@@basenameoutput)inString.subpage_dash_root(String.length"page-")(String.lengthpage_dash_root-String.length"page-")inletinput_s=Fs.File.to_stringinputinletdigest=Digest.fileinput_sinletpage_name=PageName.make_stdroot_nameinletcheck_child:Odoc_model.Paths.Reference.t->bool=func->matchcwith|`Root(n,`TUnknown)|`Root(n,`TPage)->root_name=n|_->falseinlet_=match(parent_spec,root_name)with|Explicit_,"index"->Format.eprintf"Warning: Potential name clash - child page named 'index'\n%!"|_->()inletname=letcheckparents_childrenv=ifList.existscheck_childparents_childrenthenOkvelseError(`Msg"Specified parent is not a parent of this file")inmatch(parent_spec,children)with|Explicit(p,cs),[]->checkcs@@`LeafPage(Somep,page_name)|Explicit(p,cs),_->checkcs@@`Page(Somep,page_name)|Packageparent,[]->Ok(`LeafPage(Someparent,page_name))|Packageparent,_->Ok(`Page(Someparent,page_name))(* This is a bit odd *)|Noparent,[]->Ok(`LeafPage(None,page_name))|Noparent,_->Ok(`Page(None,page_name))inname>>=funname->letroot=letfile=Odoc_model.Root.Odoc_file.create_pageroot_namein{Odoc_model.Root.id=(name:>Odoc_model.Paths.Identifier.OdocId.t);file;digest;}inletresolvecontent=letpage=Odoc_model.Lang.Page.{name;root;children;content;digest;linked=false}inOdoc_file.save_pageoutput~warnings:[]page;Ok()inFs.File.readinput>>=funstr->Odoc_loader.read_string(name:>Odoc_model.Paths.Identifier.LabelParent.t)input_sstr|>Odoc_model.Error.handle_errors_and_warnings~warnings_options>>=function|`Stop->resolve[](* TODO: Error? *)|`Docscontent->resolvecontentletcompile~resolver~parent_cli_spec~hidden~children~output~warnings_optionsinput=parentresolverparent_cli_spec>>=funparent_spec->letext=Fs.File.get_extinputinifext=".mld"thenmld~parent_spec~output~warnings_options~childreninputelse(matchextwith|".cmti"->OkOdoc_loader.read_cmti|".cmt"->OkOdoc_loader.read_cmt|".cmi"->OkOdoc_loader.read_cmi|_->Error(`Msg"Unknown extension, expected one of: cmti, cmt, cmi or mld."))>>=funloader->letparent=matchparent_specwith|Noparent->OkNone|Explicit(parent,_)->Ok(Someparent)|Packageparent->Ok(Someparent)inparent>>=funparent->letmake_root=root_of_compilation_unit~parent_spec~hidden~outputinletresult=Odoc_model.Error.catch_errors_and_warnings(fun()->resolve_and_substitute~resolverparentinput(loader~make_root))in(* Extract warnings to write them into the output file *)let_,warnings=Odoc_model.Error.unpack_warningsresultinOdoc_model.Error.handle_errors_and_warnings~warnings_optionsresult>>=fununit->Odoc_file.save_unitoutput~warningsunit;Ok()