package forester

  1. Overview
  2. Docs

Source file RenderMathMode.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
open Prelude
open Core

module Printer =
struct
  module P0 =
  struct
    type out = Format.formatter
    let text txt fmt =
      Format.fprintf fmt "%s" txt
  end

  include PrinterKit.Kit (P0)

  let contents (printer : t) : string =
    Format.asprintf "%a" (fun fmt _ -> printer fmt) ()
end

let rec render_node : Sem.node -> Printer.t =
  function
  | Sem.Text txt ->
    Printer.text txt
  | Sem.Math(_, xs) ->
    render xs
  | Sem.Tag (name, _, body) ->
    render_tag name body
  | node ->
    Format.eprintf "missing case: %a@." Sem.pp_node node;
    failwith "RenderMathMode.render_node"

and render_tag name body =
  Printer.seq
    [Printer.text "\\";
     Printer.text name;
     render_arg Braces body]

and render xs =
  Printer.iter ~sep:Printer.space render_node xs

and render_arg delim (arg : Sem.t) : Printer.t =
  match arg with
  | [] -> Printer.nil
  | _ ->
    let l, r =
      match delim with
      | Braces -> "{", "}"
      | Squares -> "[", "]"
      | Parens -> "(", ")"
    in
    Printer.seq
      [Printer.text l;
       render arg;
       Printer.text r]
OCaml

Innovation. Community. Security.