Source

ocaml-lib / ipp / printer.ml

open Ipp

class virtual cursor =
  object
    method virtual print_string : string -> cursor
    method print_flush : unit = ()
    method virtual at_init : bool
    method virtual init : cursor
  end

let print_string (v : string) = new operation (fun cursor -> cursor#print_string v)
let eof () = new operation (fun cursor -> cursor#init)


class cursor_buffer (buf : Buffer.t) (offset : int) (suf : string) (p : int) =
  object (self)
    inherit cursor

    method print_string s =
      new cursor_buffer buf offset (suf ^ s) (p + String.length s)

    method at_init = (p = offset)

    method init =
      Buffer.add_string buf suf;
      new cursor_buffer buf p "" p
  end
let cursor_of_buffer buf = new cursor_buffer buf 0 "" 0

class cursor_string_ref (text : string ref) (offset : int) (suf : string) (p : int) =
  object (self)
    inherit cursor

    method print_string s =
      new cursor_string_ref text offset (suf ^ s) (p + String.length s)

    method at_init = (p = offset)

    method init =
      text := !text ^ suf;
      new cursor_string_ref text p "" p
  end
let cursor_of_string_ref r = new cursor_string_ref r 0 "" 0

class cursor_formatter (fmt : Format.formatter) (offset : int) (suf : string) (p : int) =
  (* invariant : result string = buf ^ suf; offset + String.length suf = p *)
  object (self)
    inherit cursor
    method print_string (s : string) =
      new cursor_formatter fmt offset (suf ^ s) (p + String.length s)

    method print_flush = Format.pp_print_flush fmt ()

    method at_init : bool = (p = offset)

    method init =
      Format.pp_print_string fmt suf;
      new cursor_formatter fmt p "" p
  end
let cursor_of_formatter fmt = new cursor_formatter fmt 0 "" 0
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.