File: pretty.ml

package info (click to toggle)
camlp5 8.04.00-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,968 kB
  • sloc: ml: 137,918; makefile: 2,055; perl: 1,729; sh: 1,653; python: 38
file content (42 lines) | stat: -rw-r--r-- 910 bytes parent folder | download | duplicates (3)
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
(* camlp5r *)
(* pretty.ml,v *)
(* Copyright (c) INRIA 2007-2017 *)

#load "pa_macro.cmo";

open Versdep;

exception GiveUp;

value line_length = ref 78;
value horiz_ctx = ref False;

value utf8_string_length s =
  loop 0 0 where rec loop i len =
    if i = String.length s then len
    else
      let c = Char.code s.[i] in
      if c < 0b1000_0000 || c >= 0b1100_0000 then loop (i + 1) (len + 1)
      else loop (i + 1) len
;

value after_print s =
  if horiz_ctx.val then
    if string_contains s '\n' || utf8_string_length s > line_length.val then
      raise GiveUp
    else s
  else s
;

value sprintf fmt = Versdep.printf_ksprintf after_print fmt;

value horiz_vertic horiz vertic =
  try Ploc.call_with horiz_ctx True horiz () with
  [ GiveUp -> if horiz_ctx.val then raise GiveUp else vertic () ]
;

value vertic v =
  horiz_vertic (fun () -> raise GiveUp) v
;

value horizontally () = horiz_ctx.val;