diff --git a/opam b/opam index d0415bc..54ef8f9 100644 --- a/opam +++ b/opam @@ -13,7 +13,7 @@ tags: [ "org:ocamlpro" "org:typerex" ] -dev-repo: "https://github.com/OCamlPro/ocp-index.git#4.03" +dev-repo: "https://github.com/OCamlPro/ocp-index.git" build: [ ["./configure" "--prefix" prefix] [make] @@ -26,7 +26,7 @@ depends: [ ] depopts: "lambda-term" conflicts: "lambda-term" {< "1.7"} -available: [ocaml-version >= "4.03"] +available: [ocaml-version >= "4.01.0" ] messages: "For ocp-browser, please also install package lambda-term" {! lambda-term:installed} post-messages: "This package requires additional configuration for use in editors. Either install package 'user-setup', or manually: diff --git a/src/browserMain.ml b/src/browserMain.ml index 49065a9..8adea38 100644 --- a/src/browserMain.ml +++ b/src/browserMain.ml @@ -11,12 +11,15 @@ let rec eq l1 l2 = match l1, l2 with {LibIndex. path = path2 ; name = name2 } :: t2 -> path1 = path2 && name1 = name2 && eq t1 t2 -(** Provide an association LibIndex.kind -> tag (= string) -> style +(* * Provide an association LibIndex.kind -> tag (= string) -> style In order to encode styles in [Format.tag]. *) let kind_to_tag, tag_to_style, register_ressource = let h = Hashtbl.create 11 in let kind_to_tag = function - | LibIndex.Type | OpenType -> "Type" +#if OCAML_VERSION >= "4.03" + | LibIndex.OpenType +#endif + | LibIndex.Type -> "Type" | Value -> "Value" | Exception -> "Exception" | Field _ -> "Field" @@ -407,7 +410,7 @@ class virtual line_editor = object(self) end -(** Strip one path level. +(* * Strip one path level. Do the following transformation: "Foo.Bar." -> "Foo." @@ -553,7 +556,7 @@ let size (str : LTerm_text.t) = if fst str.(last) <> newline then incr rows ; {LTerm_geom. rows = !rows ; cols = !cols } -(** The show box shows the result of a research. +(* * The show box shows the result of a research. [content] is a list zipper positioned at the focused element. Left and right lists are elements before and after the focus. @@ -785,7 +788,7 @@ let event_handler (cbox : #completion_box) (sbox:#show_box) options show_help = true | _ -> false -(** Express the result as an event mapped on the content of the completion box. +(* * Express the result as an event mapped on the content of the completion box. *) let show_completion show_box input = let zipper n l = diff --git a/src/grepMain.ml b/src/grepMain.ml index c6a7367..af1eea7 100644 --- a/src/grepMain.ml +++ b/src/grepMain.ml @@ -14,12 +14,12 @@ module Grep : sig - (** [ident path, filename, input channel, [line, column, length]] + (* * [ident path, filename, input channel, [line, column, length]] Finds occurence of given ident in an opened file. Results are in reverse order. *) val ident: string list -> string -> in_channel -> (int * int * int) list - (** [regex, filename, input channel, [line, column, length]] + (* * [regex, filename, input channel, [line, column, length]] Finds matches of a regexp in the strings of an opened ocaml file. Results are in reverse order. *) val strings_re: Re.re -> string -> in_channel -> (int * int * int) list @@ -85,7 +85,14 @@ end = struct Filename.basename (try Filename.chop_extension f with Invalid_argument _ -> f) in +#if OCAML_VERSION >= "4.03" String.mapi (function 0 -> Char.uppercase_ascii | _ -> fun x -> x) s +#elif OCAML_VERSION >= "4.02" + String.mapi (function 0 -> Char.uppercase | _ -> fun x -> x) s +#else + s.[0] <- Char.uppercase s.[0]; + s +#endif in let f (curpath, lookfor, last_scope, acc) scope tok pos = let lookfor = @@ -319,4 +326,3 @@ let () = (* idea: single utility to color parts of source with syntactic context: pattern, expr, type, topexpr, module, record ... Could be used for better completion, analysis etc.*) - diff --git a/src/indexBuild.ml b/src/indexBuild.ml index d7a58ac..ab9893c 100644 --- a/src/indexBuild.ml +++ b/src/indexBuild.ml @@ -36,13 +36,19 @@ let orig_file_name = function | Cmt f | Cmti f | Cmi f -> f let equal_kind k1 k2 = match k1,k2 with - | Type,Type | Value,Value | Exception,Exception | OpenType,OpenType + | Type,Type | Value,Value | Exception,Exception +#if OCAML_VERSION >= "4.02" + | OpenType,OpenType +#endif | Field _,Field _ | Variant _,Variant _ | Method _,Method _ | Module,Module | ModuleType,ModuleType | Class,Class | ClassType,ClassType | Keyword,Keyword -> true - | Type,_ | Value,_ | Exception,_ | OpenType,_ + | Type,_ | Value,_ | Exception,_ +#if OCAML_VERSION >= "4.02" + | OpenType,_ +#endif | Field _,_ | Variant _,_ | Method _,_ | Module,_ | ModuleType,_ | Class,_ | ClassType,_ @@ -146,8 +152,13 @@ let ty_of_sig_item = function | Types.Sig_value(id, decl) -> tree_of_value_description id decl | Types.Sig_type(id, decl, rs) -> tree_of_type_declaration id decl rs +#if OCAML_VERSION >= "4.02" | Types.Sig_typext(id, decl, es) -> tree_of_extension_constructor id decl es | Types.Sig_module(id, { Types.md_type }, rs) -> tree_of_module id md_type rs +#else + | Types.Sig_exception(id, decl) -> tree_of_exception_declaration id decl + | Types.Sig_module(id, mty, rs) -> tree_of_module id mty rs +#endif | Types.Sig_modtype(id, decl) -> tree_of_modtype_declaration id decl | Types.Sig_class(id, decl, rs) -> tree_of_class_declaration id decl rs | Types.Sig_class_type(id, decl, rs) -> tree_of_cltype_declaration id decl rs @@ -220,8 +231,12 @@ let qualify_ty (parents:parents) ty = | Otyp_poly (str, ty) -> Otyp_poly (str, aux ty) | Otyp_module (str, strl, tylist) -> Otyp_module (str, strl, List.map aux tylist) +#if OCAML_VERSION >= "4.02" | Otyp_open -> Otyp_open +#endif +#if OCAML_VERSION >= "4.03" | Otyp_attribute (ty,attr) -> Otyp_attribute (aux ty, attr) +#endif in aux ty @@ -229,15 +244,33 @@ let qualify_ty_in_sig_item (parents:parents) = let qual = qualify_ty parents in let open Outcometree in function + +#if OCAML_VERSION >= "4.02" | Osig_type (out_type_decl, rc) -> Osig_type ({ out_type_decl with otype_type = qual out_type_decl.otype_type; otype_cstrs = List.map (fun (ty1,ty2) -> qual ty1, qual ty2) - out_type_decl.otype_cstrs }, rc) + out_type_decl.otype_cstrs }, rc) +#else + | Osig_type ((str, list, ty, priv, tylist2), rc) -> + Osig_type ((str, list, qual ty, priv, + List.map (fun (ty1,ty2) -> qual ty1, qual ty2) tylist2), rc) +#endif + +#if OCAML_VERSION >= "4.03" | Osig_value o -> Osig_value {o with oval_type = qual o.oval_type} +#else + | Osig_value (str, ty, str2) -> Osig_value (str, qual ty, str2) +#endif + +#if OCAML_VERSION >= "4.02" | Osig_typext (constr, es) -> Osig_typext ({ constr with - oext_args = List.map qual constr.oext_args }, es) + oext_args = List.map qual constr.oext_args }, es) +#else + | Osig_exception (str, tylist) -> Osig_exception (str, List.map qual tylist) +#endif + | out_sig -> out_sig (* don't get down in modules, classes and their types *) (* -- end -- *) @@ -263,16 +296,31 @@ let with_path_loc ?srcpath loc = let loc_of_sig_item = function | Types.Sig_value (_,descr) -> descr.Types.val_loc | Types.Sig_type (_,descr,_) -> descr.Types.type_loc +#if OCAML_VERSION >= "4.02" | Types.Sig_typext (_,descr,_) -> descr.Types.ext_loc | Types.Sig_module (_,descr,_) -> descr.Types.md_loc | Types.Sig_modtype (_,descr) -> descr.Types.mtd_loc | Types.Sig_class (_,descr,_) -> descr.Types.cty_loc | Types.Sig_class_type (_,descr,_) -> descr.Types.clty_loc +#else + | Types.Sig_exception (_,descr) -> descr.Types.exn_loc + (* Sadly the Types tree doesn't contain locations for those. This means we + won't associate comments easily either (todo...) *) + | Types.Sig_module _ + | Types.Sig_modtype _ + | Types.Sig_class _ + | Types.Sig_class_type _ + -> Location.none +#endif let id_of_sig_item = function | Types.Sig_value (id,_) | Types.Sig_type (id,_,_) +#if OCAML_VERSION >= "4.02" | Types.Sig_typext (id,_,_) +#else + | Types.Sig_exception (id,_) +#endif | Types.Sig_module (id,_,_) | Types.Sig_modtype (id,_) | Types.Sig_class (id,_,_) @@ -282,13 +330,18 @@ let id_of_sig_item = function let kind_of_sig_item = function | Types.Sig_value _ -> Value | Types.Sig_type _ -> Type +#if OCAML_VERSION >= "4.02" | Types.Sig_typext (_, _, Types.Text_exception) -> Exception | Types.Sig_typext _ -> OpenType +#else + | Types.Sig_exception _ -> Exception +#endif | Types.Sig_module _ -> Module | Types.Sig_modtype _ -> ModuleType | Types.Sig_class _ -> Class | Types.Sig_class_type _ -> ClassType +#if OCAML_VERSION >= "4.02" let attrs_of_sig_item = function | Types.Sig_value (_,descr) -> descr.Types.val_attributes | Types.Sig_type (_,descr,_) -> descr.Types.type_attributes @@ -301,32 +354,52 @@ let attrs_of_sig_item = function let doc_of_attributes attrs = let doc_loc_id = "ocaml.doc" in (* not exported ! *) let open Parsetree in + try match List.find (fun ({Location.txt},_) -> txt = doc_loc_id) attrs with | _, PStr [{pstr_desc = Pstr_eval ({pexp_desc},_)}] -> (match pexp_desc with +#if OCAML_VERSION >= "4.03" | Pexp_constant (Pconst_string (s,_)) -> Some s +#else + | Pexp_constant (Const_string (s,_)) -> Some s +#endif | _ -> debug "Unexpected ocaml.doc docstring format"; None) | _ -> None - | exception Not_found -> None + with Not_found -> None +#endif let trie_of_type_decl ?comments info ty_decl = match ty_decl.Types.type_kind with | Types.Type_abstract -> [], comments +#if OCAML_VERSION >= "4.02" | Types.Type_open -> [], comments +#endif | Types.Type_record (fields,_repr) -> List.map +#if OCAML_VERSION >= "4.02" (fun { Types.ld_id; ld_type; ld_attributes } -> let ty = Printtyp.tree_of_typexp false ld_type in let ty = - Outcometree.Osig_type (Outcometree.{ + Outcometree.Osig_type (Outcometree.({ otype_name = ""; otype_params = []; otype_type = ty; otype_private = Asttypes.Public; +#if OCAML_VERSION >= "4.03" otype_immediate = false ; - otype_cstrs = []; }, Outcometree.Orec_not) +#endif + otype_cstrs = []; }), Outcometree.Orec_not) in let doc = doc_of_attributes ld_attributes in +#else + (fun (ld_id, _mutable, ty_expr) -> + let ty = Printtyp.tree_of_typexp false ty_expr in + let ty = + Outcometree.Osig_type + (("", [], ty, Asttypes.Public, []), Outcometree.Orec_not) + in + let doc = None in +#endif string_to_key ld_id.Ident.name, Trie.create ~value:{ path = info.path; @@ -343,9 +416,14 @@ let trie_of_type_decl ?comments info ty_decl = comments | Types.Type_variant variants -> List.map +#if OCAML_VERSION >= "4.02" (fun { Types.cd_id; cd_args; cd_attributes } -> +#else + (fun (cd_id, cd_args, _constraints) -> +#endif let ty = let params = match cd_args with +#if OCAML_VERSION >= "4.03" | Cstr_tuple [] -> Outcometree.Otyp_sum [] | Cstr_tuple (param::_ as l) -> Printtyp.tree_of_typexp false @@ -361,16 +439,33 @@ let trie_of_type_decl ?comments info ty_decl = Printtyp.tree_of_typexp false l.ld_type) ) params) +#else + | [] -> Outcometree.Otyp_sum [] + | param::_ as l -> + Printtyp.tree_of_typexp false + { Types. desc = Types.Ttuple l; + level = param.Types.level; + id = param.Types.id } +#endif in - Outcometree.Osig_type (Outcometree.{ +#if OCAML_VERSION >= "4.02" + Outcometree.Osig_type (Outcometree.({ otype_name = ""; otype_params = []; otype_type = params; otype_private = Asttypes.Public; +#if OCAML_VERSION >= "4.03" otype_immediate = false ; - otype_cstrs = []; }, Outcometree.Orec_not) +#endif + otype_cstrs = []; }), Outcometree.Orec_not) in let doc = doc_of_attributes cd_attributes in +#else + Outcometree.Osig_type + (("", [], params, Asttypes.Public, []), Outcometree.Orec_not) + in + let doc = None in +#endif string_to_key cd_id.Ident.name, Trie.create ~value:{ path = info.path; @@ -427,10 +522,16 @@ let rec trie_of_sig_item | Some n -> with_path_loc ?srcpath (loc_of_sig_item n) in let doc, comments = +#if OCAML_VERSION >= "4.02" match doc_of_attributes (attrs_of_sig_item sig_item), comments with | Some s, _ -> lazy (Some s), comments | None, None -> lazy None, None | None, Some comments -> +#else + match comments with + | None -> lazy None, None + | Some comments -> +#endif let assoc = lazy ( associate_comment (Lazy.force comments) loc nextloc ) in @@ -463,6 +564,7 @@ let rec trie_of_sig_item in (* ignore functor arguments *) let rec sig_item_contents = function +#if OCAML_VERSION >= "4.02" | Types.Sig_module (id, ({Types.md_type = Types.Mty_functor (_,_,s)} as funct), is_rec) -> let funct = {funct with Types.md_type = s} in @@ -471,13 +573,26 @@ let rec trie_of_sig_item (id, ({Types.mtd_type = Some (Types.Mty_functor (_,_,s))} as funct)) -> let funct = {funct with Types.mtd_type = Some s} in sig_item_contents (Types.Sig_modtype (id, funct)) +#else + | Types.Sig_module (id, Types.Mty_functor (_,_,s), is_rec) -> + sig_item_contents (Types.Sig_module (id, s, is_rec)) + | Types.Sig_modtype + (id, Types.Modtype_manifest (Types.Mty_functor (_,_,s))) -> + sig_item_contents + (Types.Sig_modtype (id, Types.Modtype_manifest s)) +#endif | si -> si in (* read module / class contents *) let children, comments = match sig_item_contents sig_item with +#if OCAML_VERSION >= "4.02" | Types.Sig_module (id,{ Types.md_type = Types.Mty_signature sign },_) | Types.Sig_modtype (id,{ Types.mtd_type = Some (Types.Mty_signature sign) }) +#else + | Types.Sig_module (id,Types.Mty_signature sign,_) + | Types.Sig_modtype (id,Types.Modtype_manifest (Types.Mty_signature sign)) +#endif -> let path = path @ [id.Ident.name] in let children_comments = lazy ( @@ -497,12 +612,18 @@ let rec trie_of_sig_item | Some _, lazy (_, comments) -> comments in children, comments +#if OCAML_VERSION >= "4.02" | Types.Sig_module (_,{ Types.md_type = Types.Mty_ident sig_ident | Types.Mty_alias sig_ident},_) | Types.Sig_modtype (_,{ Types.mtd_type = Some ( Types.Mty_ident sig_ident | Types.Mty_alias sig_ident) }) -> +#else + | Types.Sig_module (_,Types.Mty_ident sig_ident,_) + | Types.Sig_modtype (_,Types.Modtype_manifest + (Types.Mty_ident sig_ident)) -> +#endif let sig_path = path_of_ocaml sig_ident in let children = lazy ( (* Only keep the children, don't override the module reference *) @@ -514,27 +635,42 @@ let rec trie_of_sig_item | Types.Sig_class_type (id,{Types.clty_type=cty},_) -> let rec get_clsig = function +#if OCAML_VERSION >= "4.02" | Types.Cty_constr (_,_,cty) | Types.Cty_arrow (_,_,cty) -> +#else + | Types.Cty_constr (_,_,cty) | Types.Cty_fun (_,_,cty) -> +#endif get_clsig cty | Types.Cty_signature clsig -> clsig in let clsig = get_clsig cty in let path = path@[id.Ident.name] in let (fields, _) = +#if OCAML_VERSION >= "4.02" Ctype.flatten_fields (Ctype.object_fields clsig.Types.csig_self) +#else + Ctype.flatten_fields (Ctype.object_fields clsig.Types.cty_self) +#endif in lazy (List.fold_left (fun t (lbl,_,ty_expr) -> if lbl = "*dummy method*" then t else let _ = Printtyp.reset_and_mark_loops ty_expr in let ty = Printtyp.tree_of_typexp false ty_expr in let ty = - Outcometree.Osig_type (Outcometree.{ +#if OCAML_VERSION >= "4.02" + Outcometree.Osig_type (Outcometree.({ otype_name = ""; otype_params = []; otype_type = ty; otype_private = Asttypes.Public; - otype_immediate = false ; - otype_cstrs = []; }, Outcometree.Orec_not) +#if OCAML_VERSION >= "4.03" + otype_immediate = false ; +#endif + otype_cstrs = []; }), Outcometree.Orec_not) +#else + Outcometree.Osig_type + (("", [], ty, Asttypes.Public, []), Outcometree.Orec_not) +#endif in Trie.add t (string_to_key lbl) { path = path; @@ -563,7 +699,7 @@ let rec trie_of_sig_item :: siblings, comments - +#if OCAML_VERSION >= "4.02" (* These four functions go through the typedtree to extract includes *) let rec lookup_trie_of_module_expr parents t path = function | Typedtree.Tmod_ident (incpath,{ Location.txt = _lid}) -> @@ -696,6 +832,15 @@ let add_locs ~locs t = } ) t +let cmt_includes parents t path cmt_contents = + match cmt_contents.Cmt_format.cmt_annots with + | Cmt_format.Implementation impl -> + get_includes_impl parents t path impl + | Cmt_format.Interface sign -> + get_includes_sig parents t path sign + | _ -> Trie.empty +#endif + (* Can work in a subtree (t doesn't have to be the root) *) let qualify_type_idents parents t = let qualify _key id = @@ -734,14 +879,6 @@ let cmt_sign cmt_contents = -> Some sign | _ -> None -let cmt_includes parents t path cmt_contents = - match cmt_contents.Cmt_format.cmt_annots with - | Cmt_format.Implementation impl -> - get_includes_impl parents t path impl - | Cmt_format.Interface sign -> - get_includes_sig parents t path sign - | _ -> Trie.empty - let protect_read reader f = try reader f with | Cmt_format.Error _ | Cmi_format.Error _ -> @@ -774,8 +911,10 @@ let load_loc_impl parents filename cmt_contents = sign in debug " %.3fs\n%!" (chrono()); +#if OCAML_VERSION >= "4.02" let includes = cmt_includes parents t [] cmt_contents in let t = add_locs ~locs:includes t in +#endif Some t | _ -> debug " %.3fs\n%!" (chrono()); @@ -902,6 +1041,7 @@ let load_cmt ?(qualify=false) root t modul orig_file = debug " %.3fs\n%!" (chrono()); t ) in +#if OCAML_VERSION >= "4.02" let children = lazy ( let includes = cmt_includes [[modul], children; [], root] @@ -909,6 +1049,7 @@ let load_cmt ?(qualify=false) root t modul orig_file = in add_locs ~locs:includes (Lazy.force children) ) in +#endif let loc_sig, loc_impl = let of_info i = match i.Cmt_format.cmt_sourcefile with | Some f -> Location.in_file f @@ -959,8 +1100,13 @@ let load_files t dirfiles = try let i = String.rindex file '.' in let len = String.length file in +#if OCAML_VERSION >= "4.03" let modul = String.capitalize_ascii (String.sub file 0 i) in let ext = String.lowercase_ascii (String.sub file (i+1) (len-i-1)) in +#else + let modul = String.capitalize (String.sub file 0 i) in + let ext = String.lowercase (String.sub file (i+1) (len-i-1)) in +#endif modul, ext with Not_found -> file, "" in diff --git a/src/indexMisc.ml b/src/indexMisc.ml index 8ba66f5..810b1c3 100644 --- a/src/indexMisc.ml +++ b/src/indexMisc.ml @@ -57,12 +57,28 @@ let string_to_key s = let key_to_string l = let rec aux n = function +#if OCAML_VERSION >= "4.02" | [] -> Bytes.create n +#else + | [] -> String.create n +#endif | c::r -> let s = aux (n+1) r in - Bytes.set s n (if c = dot then '.' else c); s +#if OCAML_VERSION >= "4.02" + Bytes.set s n +#else + s.[n] <- +#endif + (if c = dot then '.' else c); + s in - Bytes.to_string (aux 0 l) + let s = aux 0 l in +#if OCAML_VERSION >= "4.02" + Bytes.to_string s +#else + s +#endif + let modpath_to_key ?(enddot=true) path = List.fold_right (fun p acc -> @@ -74,15 +90,18 @@ let key_to_modpath l = | '\000'::r -> aux [] (acc1::acc2) r | c::r -> aux (c::acc1) acc2 r | [] -> if acc1 = [] then acc2 else acc1::acc2 - in - List.rev_map (fun l -> key_to_string (List.rev l)) (aux [] [] l) + in + List.rev_map (fun l -> key_to_string (List.rev l)) (aux [] [] l) let modpath_to_string path = String.concat "." path let parent_type id = match id.IndexTypes.kind with | Field parent | Variant parent | Method parent -> Some parent - | Type | Value | Exception | OpenType | Module | ModuleType | Class + | Type | Value | Exception | Module | ModuleType | Class +#if OCAML_VERSION >= "4.02" + | OpenType +#endif | ClassType | Keyword -> None diff --git a/src/indexMisc.mli b/src/indexMisc.mli index df74234..c0083d6 100644 --- a/src/indexMisc.mli +++ b/src/indexMisc.mli @@ -39,15 +39,15 @@ val modpath_to_string: string list -> string (** Returns the parent type for field records, variants and methods *) val parent_type: IndexTypes.info -> IndexTypes.info option -(** Returns the list of directories and all their recursive subdirectories. +(* * Returns the list of directories and all their recursive subdirectories. If directory basename verifies [skip], it is not descended nor returned. *) val unique_subdirs: ?skip:(string -> bool) -> string list -> string list -(** An heuristic to guess where the root directory of the current project. +(* * An heuristic to guess where the root directory of the current project. Returns (project_root, build_dir) *) val project_root: ?path:string -> unit -> string option * string option -(** Locates a build dir within a given directory, based on name ([_build], +(* * Locates a build dir within a given directory, based on name ([_build], [_obuild], etc.) *) val find_build_dir: string -> string option diff --git a/src/indexOptions.ml b/src/indexOptions.ml index a1e55c4..81f5ea4 100644 --- a/src/indexOptions.ml +++ b/src/indexOptions.ml @@ -37,7 +37,10 @@ let filter opt info = let open LibIndex in let kinds = opt.filter in match info.kind with - | Type | OpenType -> kinds.t +#if OCAML_VERSION >= "4.02" + | OpenType +#endif + | Type -> kinds.t | Value | Method _ -> kinds.v | Exception -> kinds.e | Field _ | Variant _ -> kinds.c diff --git a/src/indexOut.ml b/src/indexOut.ml index 1b0600d..dc59cfa 100644 --- a/src/indexOut.ml +++ b/src/indexOut.ml @@ -64,7 +64,10 @@ module IndexFormat = struct let color = let f kind fstr fmt = let colorcode = match kind with - | Type | OpenType -> "\027[36m" +#if OCAML_VERSION >= "4.02" + | OpenType +#endif + | Type -> "\027[36m" | Value -> "\027[1m" | Exception -> "\027[33m" | Field _ | Variant _ -> "\027[34m" @@ -92,10 +95,12 @@ module IndexFormat = struct let kind ?(colorise = no_color) fmt id = match id.kind with +#if OCAML_VERSION >= "4.02" + | OpenType -> Format.pp_print_string fmt "opentype" +#endif | Type -> Format.pp_print_string fmt "type" | Value -> Format.pp_print_string fmt "val" | Exception -> Format.pp_print_string fmt "exception" - | OpenType -> Format.pp_print_string fmt "opentype" | Field parentty -> Format.fprintf fmt "field(%a)" (colorise.f parentty.kind "%s") parentty.name @@ -167,9 +172,17 @@ module IndexFormat = struct | Osig_class (_,_,_,ctyp,_) | Osig_class_type (_,_,_,ctyp,_) -> !Oprint.out_class_type fmt ctyp +#if OCAML_VERSION >= "4.02" | Osig_typext ({ oext_args = [] }, _) -> +#else + | Osig_exception (_,[]) -> +#endif Format.pp_print_char fmt '-' +#if OCAML_VERSION >= "4.02" | Osig_typext ({ oext_args }, _) -> +#else + | Osig_exception (_,oext_args) -> +#endif list ~paren:true !Oprint.out_type (fun fmt () -> @@ -179,12 +192,24 @@ module IndexFormat = struct | Osig_modtype (_,mtyp) | Osig_module (_,mtyp,_) -> !Oprint.out_module_type fmt mtyp +#if OCAML_VERSION >= "4.03" | Osig_type ({ otype_type },_) -> tydecl fmt otype_type | Osig_value {oval_type} -> !Oprint.out_type fmt oval_type | Osig_ellipsis -> Format.fprintf fmt "..." +#elif OCAML_VERSION >= "4.02" + | Osig_type ({ otype_type },_) -> + tydecl fmt otype_type + | Osig_value (_,ty,_) -> + !Oprint.out_type fmt ty +#else + | Osig_type ((_,_,ty,_,_),_) -> + tydecl fmt ty + | Osig_value (_,ty,_) -> + !Oprint.out_type fmt ty +#endif let ty ?(colorise = no_color) fmt id = option_iter id.ty diff --git a/src/indexPredefined.ml b/src/indexPredefined.ml index a7276cd..6111250 100644 --- a/src/indexPredefined.ml +++ b/src/indexPredefined.ml @@ -24,13 +24,20 @@ let mktype name ?(params=[]) ?(def=Otyp_abstract) doc = { kind = Type; name = name; ty = Some (Osig_type ( +#if OCAML_VERSION >= "4.02" { otype_name = name; otype_params = List.map (fun v -> v,(true,true)) params; otype_type = def; otype_private = Asttypes.Public; - otype_immediate = false ; +#if OCAML_VERSION >= "4.03" + otype_immediate = false ; +#endif otype_cstrs = [] }, Orec_not)); - loc_sig = Lazy.from_val Location.none; +#else + (name,List.map (fun v -> v,(true,true)) params,def,Asttypes.Public,[]), + Orec_not)); +#endif +loc_sig = Lazy.from_val Location.none; loc_impl = Lazy.from_val Location.none; doc = Lazy.from_val (Some doc); file = Cmi "*built-in*"; @@ -41,14 +48,24 @@ let mkvariant name parent params = { orig_path = []; kind = Variant parent; name = name; +#if OCAML_VERSION >= "4.02" ty = Some (Osig_type ( { otype_name = ""; otype_params = []; otype_type = (match params with [] -> Otyp_sum [] | l -> Otyp_tuple l); otype_private = Asttypes.Public; +#if OCAML_VERSION >= "4.03" otype_immediate = false ; +#endif otype_cstrs = [] }, Orec_not)); +#else + ty = Some (Osig_type (("", [], + (match params with [] -> Otyp_sum [] + | l -> Otyp_tuple l), + Asttypes.Public, []), + Outcometree.Orec_not)); +#endif loc_sig = Lazy.from_val Location.none; loc_impl = Lazy.from_val Location.none; doc = Lazy.from_val None; @@ -60,6 +77,7 @@ let mkexn name params doc = { orig_path = []; kind = Exception; name = name; +#if OCAML_VERSION >= "4.02" ty = Some (Osig_typext ({ oext_name = name; oext_type_name = "exn"; @@ -67,6 +85,9 @@ let mkexn name params doc = { oext_args = params; oext_ret_type = None; oext_private = Asttypes.Public }, Oext_exception)); +#else + ty = Some (Osig_exception (name,params)); +#endif loc_sig = Lazy.from_val Location.none; loc_impl = Lazy.from_val Location.none; doc = Lazy.from_val (Some doc); diff --git a/src/indexTypes.ml b/src/indexTypes.ml index 342f86f..3743d2a 100644 --- a/src/indexTypes.ml +++ b/src/indexTypes.ml @@ -12,7 +12,7 @@ (* *) (**************************************************************************) -(** This module contains open types, for use within the library only. Its +(* * This module contains open types, for use within the library only. Its interface should only be exported through closed types in LibIndex. *) (** Internal representation of types *) @@ -35,7 +35,10 @@ type info = { path: string list; (** The kind of elements that can be stored in the trie *) and kind = - | Type | Value | Exception | OpenType + | Type | Value | Exception +#if OCAML_VERSION >= "4.02" + | OpenType +#endif | Field of info | Variant of info | Method of info | Module | ModuleType @@ -45,6 +48,6 @@ and kind = (** Lazy trie structure holding the info on all identifiers *) type t = (char, info) Trie.t -(** Raised when cmi/cmt/cmti files can't be loaded. Probably a different +(* * Raised when cmi/cmt/cmti files can't be loaded. Probably a different version of OCaml *) exception Bad_format of string diff --git a/src/libIndex.mli b/src/libIndex.mli index 9e27b1c..418c83a 100644 --- a/src/libIndex.mli +++ b/src/libIndex.mli @@ -12,7 +12,7 @@ (* *) (**************************************************************************) -(** {1 ocp-index} +(* * {1 ocp-index} Lightweight documentation extractor for installed OCaml libraries. This module contains the whole API. *) @@ -25,7 +25,7 @@ type t type orig_file = IndexTypes.orig_file = private Cmt of string | Cmti of string | Cmi of string -(** Raised when a file couldn't be loaded (generally due to a different +(* * Raised when a file couldn't be loaded (generally due to a different compiler version) *) exception Bad_format of string @@ -44,7 +44,10 @@ type info = IndexTypes.info = private { (** The kind of elements that can be stored in the trie *) and kind = IndexTypes.kind = private - | Type | Value | Exception | OpenType + | Type | Value | Exception +#if OCAML_VERSION >= "4.02" + | OpenType +#endif | Field of info | Variant of info | Method of info | Module | ModuleType @@ -55,7 +58,7 @@ and kind = IndexTypes.kind = private (** {2 Utility functions} *) module Misc: sig - (** Helper function, useful to lookup all subdirs of a given path before + (* * Helper function, useful to lookup all subdirs of a given path before calling [load] *) val unique_subdirs: ?skip:(string -> bool) -> string list -> string list end @@ -63,7 +66,7 @@ end (** {2 Building} *) -(** Build the trie from a list of include directories. They will be scanned for +(* * Build the trie from a list of include directories. They will be scanned for [.cmi] and [.cmt] files to complete on module names, and the contents of these files will be lazily read whenever needed. *) val load: string list -> t @@ -71,16 +74,16 @@ val load: string list -> t (** Load a single file into a trie *) val add_file: t -> string -> t -(** Consider the module at the given path as opened, i.e. rebind its contents at +(* * Consider the module at the given path as opened, i.e. rebind its contents at the root of the trie. If [cleanup_path], also change its contents to refer to the new path. *) val open_module: ?cleanup_path:bool -> t -> string list -> t -(** Same as [open_module], but tries to open even the elements that are not in +(* * Same as [open_module], but tries to open even the elements that are not in the external interface (this needs a cmt to be present) *) val fully_open_module: ?cleanup_path:bool -> t -> string list -> t -(** [alias t origin alias] binds at [alias] the contents found at [origin]. If +(* * [alias t origin alias] binds at [alias] the contents found at [origin]. If [~cleanup_path] is set, also change its contents to refer to the new path. *) val alias: ?cleanup_path:bool -> t -> string list -> string list -> t @@ -93,12 +96,12 @@ val all: t -> info list (** Lookup an identifier in a trie (eg. [option] or [List.map]) *) val get: t -> string -> info -(** Same as [get], but returns all existing bindings instead of only one. There +(* * Same as [get], but returns all existing bindings instead of only one. There can consistently be several if they are of different kinds (eg. a type and a value...) *) val get_all: t -> string -> info list -(** Lookup identifiers starting with the given string. Completion stops at +(* * Lookup identifiers starting with the given string. Completion stops at module boundaries (it wont unfold contents of modules) *) val complete: t -> ?filter:(info -> bool) -> string -> info list diff --git a/src/ocp-index.ocp b/src/ocp-index.ocp index 47b1a55..a4fb85d 100644 --- a/src/ocp-index.ocp +++ b/src/ocp-index.ocp @@ -1,29 +1,47 @@ -comp += [ "-g" "-w" "+1..39-4-9-37-40" "-safe-string" ] -link += [ "-g" "-w" "+1..39-4-9-37-40" ] +comp += [ "-g" "-w" "+1..39-4-9-37-40" ] + +if ocaml_version >= "4.02" then { + comp += [ "-safe-string" ] +} begin library "ocp-index-lib" sort = false files = [ "trie.ml" - "indexTypes.ml" "indexMisc.ml" "indexOut.ml" "indexPredefined" - "indexBuild.ml" "libIndex.ml" "indexScope.ml" ] + "indexTypes.ml" (pp = "ocp-pp") + "indexMisc.ml" (pp = "ocp-pp") + "indexOut.mli" + "indexOut.ml" (pp = "ocp-pp") + "indexPredefined.mli" + "indexPredefined.ml" (pp = "ocp-pp") + "indexBuild.mli" + "indexBuild.ml" (pp = "ocp-pp") + "libIndex.ml" (pp = "ocp-pp") + "indexScope.ml" ] requires = [ "compiler-libs" "compiler-libs.common" "ocp-indent.lexer" "ocp-indent.utils" ] end begin program "ocp-index" sort = false - files = [ "indexOptions.ml" "indexMain.ml" ] + files = [ + "indexOptions.mli" + "indexOptions.ml" (pp = "ocp-pp") + "indexMain.ml" ] requires = [ "ocp-index-lib" "unix" "cmdliner" ] end begin program "ocp-grep" sort = false - files = [ "grepMain.ml" ] + files = [ "grepMain.ml" (pp = "ocp-pp") ] requires = [ "unix" "cmdliner" "ocp-index-lib" "re" "re.posix" ] end begin program "ocp-browser" sort = false - files = [ "indexOptions.ml" "browserMain.ml" ] + files = [ + "indexOptions.mli" + "indexOptions.ml" (pp = "ocp-pp") + "browserMain.ml" (pp = "ocp-pp") + ] requires = [ "ocp-index-lib" "unix" "cmdliner" "lambda-term" ] end