Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Page
Library
Module
Module type
Parameter
Class
Class type
Source
print.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
open Easy_format open Ast let rlist = { list with wrap_body = `Force_breaks; indent_body = 0; align_closing = false; space_after_opening = false; space_before_closing = false } let plist = { list with align_closing = false; space_after_opening = false; space_before_closing = false } let hlist = { list with wrap_body = `No_breaks } let shlist = { hlist with stick_to_label = false; space_after_opening = false; space_before_closing = false } let shlist0 = { shlist with space_after_separator = false } let llist = { list with separators_stick_left = false; space_before_separator = true; space_after_separator = true } let lplist = { llist with space_after_opening = false; space_before_closing = false } let label0 = { label with space_after_label = false } let make_atom s = Atom (s, atom) let horizontal_sequence l = Easy_format.List (("", "", "", shlist), l) let horizontal_sequence0 l = Easy_format.List (("", "", "", shlist0), l) let quote_string s = Printf.sprintf "%S" s let format_prop (k, (_, opt)) = match opt with None -> make_atom k | Some s -> Label ( (make_atom (k ^ "="), label0), (make_atom (quote_string s)) ) let default_annot (s, (_, l)) = match l with [] -> make_atom ("<" ^ s ^ ">") | l -> List ( ("<", "", ">", plist), [ Label ( (make_atom s, label), List ( ("", "", "", plist), List.map format_prop l ) ) ] ) let string_of_field k = function Required -> k | Optional -> "?" ^ k | With_default -> "~" ^ k let make_closures format_annot = let append_annots (l : annot) x = match l with [] -> x | _ -> Label ( (x, label), List (("", "", "", plist), List.map format_annot l) ) in let prepend_colon_annots l x = match l with [] -> x | _ -> Label ( (Label ( (List (("", "", "", plist), List.map format_annot l), label0), make_atom ":" ), label), x ) in let rec format_module_item (x : module_item) = match x with Type (_, (s, param, a), t) -> let left = if a = [] then let l = make_atom "type" :: prepend_type_param param [ make_atom (s ^ " =") ] in horizontal_sequence l else let l = make_atom "type" :: prepend_type_param param [ make_atom s ] in let x = append_annots a (horizontal_sequence l) in horizontal_sequence [ x; make_atom "=" ] in Label ( (left, label), format_type_expr t ) and prepend_type_param l tl = match l with [] -> tl | _ -> let make_var s = make_atom ("'" ^ s) in let x = match l with [s] -> make_var s | l -> List (("(", ",", ")", plist), List.map make_var l) in x :: tl and prepend_type_args l tl = match l with [] -> tl | _ -> let x = match l with [t] -> format_type_expr t | l -> List (("(", ",", ")", plist), List.map format_type_expr l) in x :: tl and format_type_expr x = match x with Sum (_, l, a) -> append_annots a ( List ( ("[", "|", "]", llist), List.map format_variant l ) ) | Record (_, l, a) -> append_annots a ( List ( ("{", ";", "}", list), List.map format_field l ) ) | Tuple (_, l, a) -> append_annots a ( List ( ("(", "*", ")", lplist), List.map format_tuple_field l ) ) | List (_, t, a) -> format_type_name "list" [t] a | Option (_, t, a) -> format_type_name "option" [t] a | Nullable (_, t, a) -> format_type_name "nullable" [t] a | Shared (_, t, a) -> format_type_name "shared" [t] a | Wrap (_, t, a) -> format_type_name "wrap" [t] a | Name (_, (_, name, args), a) -> format_type_name name args a | Tvar (_, name) -> make_atom ("'" ^ name) and format_type_name name args a = append_annots a ( horizontal_sequence (prepend_type_args args [ make_atom name ]) ) and format_inherit t = horizontal_sequence [ make_atom "inherit"; format_type_expr t ] and format_tuple_field (_, x, a) = prepend_colon_annots a (format_type_expr x) and format_field x = match x with `Field (_, (k, fk, a), t) -> Label ( (horizontal_sequence0 [ append_annots a (make_atom (string_of_field k fk)); make_atom ":" ], label), format_type_expr t ) | `Inherit (_, t) -> format_inherit t and format_variant x = match x with Variant (_, (k, a), opt) -> let cons = append_annots a (make_atom k) in (match opt with None -> cons | Some t -> Label ( (cons, label), Label ( (make_atom "of", label), format_type_expr t ) ) ) | Inherit (_, t) -> format_inherit t in let format_full_module ((_, an), l) = Easy_format.List ( ("", "", "", rlist), List.map format_annot an @ List.map format_module_item l ) in format_full_module, format_type_name, format_type_expr let format ?(annot = default_annot) x = let f, _, _ = make_closures annot in f x let _default_format, default_format_type_name, default_format_type_expr = make_closures default_annot let string_of_type_name name args an = let x = default_format_type_name name args an in Easy_format.Pretty.to_string x let string_of_type_expr expr = let x = default_format_type_expr expr in Easy_format.Pretty.to_string x