package pla

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file pla.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
(* The MIT License (MIT)
   Copyright (c) 2016 Leonardo Laguna Ruiz

   Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
   associated documentation files (the "Software"), to deal in the Software without restriction, including
   without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to
   the following conditions:

   The above copyright notice and this permission notice shall be included in all copies or substantial
   portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
   LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
   SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*)

module PlaBuffer = struct
   type dest =
      | File   of out_channel
      | Buffer of Buffer.t


   let appendToBuff (d:dest) (s:string) : unit =
      match d with
      | File(c)   -> output_string c s
      | Buffer(b) -> Buffer.add_string b s


   (** Text buffer use by Pla *)
   type t =
      {
         buffer           : dest;
         mutable indent   : int;
         mutable space    : string;
         mutable indented : bool;
      }

   let newBuffer () =
      {
         buffer   = Buffer(Buffer.create 128);
         indent   = 0;
         space    = "";
         indented = false;
      }

   let newFile (file:string) =
      {
         buffer   = File(open_out file);
         indent   = 0;
         space    = "";
         indented = false;
      }

   let contents (t:t) : string =
      match t.buffer with
      | Buffer(b) -> Buffer.contents b
      | File _ -> ""

   let close (t:t) : unit =
      match t.buffer with
      | Buffer _ -> ()
      | File(c) -> close_out c

   let newline (t:t) =
      appendToBuff t.buffer "\n";
      t.indented <- false

   let indent (t:t) : unit =
      t.indent <- t.indent +1;
      t.space  <- String.make (t.indent * 3) ' ';
      newline t

   let outdent (t:t) : unit =
      t.indent <- t.indent - 1;
      if t.indent < 0 then
         failwith "Cannot outdent more";
      t.space <- String.make (t.indent * 3) ' '

   let append (t:t) (s:string) : unit =
      if not t.indented then begin
         appendToBuff t.buffer t.space;
         t.indented <- true;
      end;
      appendToBuff t.buffer s

end

type buffer = PlaBuffer.t

type t = buffer -> unit

(* Builtin templates *)

let unit : t = fun _ -> ()

let newline : t = fun buffer -> PlaBuffer.newline buffer

let comma : t = fun buffer -> PlaBuffer.append buffer ","

let commaspace : t = fun buffer -> PlaBuffer.append buffer ", "

let semi : t = fun buffer -> PlaBuffer.append buffer ";"

let space : t = fun buffer -> PlaBuffer.append buffer " "

(* Templates of basic types *)

let string (s:string) : t =
   fun buffer -> PlaBuffer.append buffer s

let string_quoted (s:string) : t =
   fun buffer ->
   PlaBuffer.append buffer "\"";
   PlaBuffer.append buffer s;
   PlaBuffer.append buffer "\""

let int (i:int) : t =
   fun buffer -> PlaBuffer.append buffer (string_of_int i)

let float (f:float) : t =
   fun buffer -> PlaBuffer.append buffer (string_of_float f)

(* Functions to wrap templates *)

let quote (t:t) : t =
   fun buffer ->
   PlaBuffer.append buffer "\"";
   t buffer;
   PlaBuffer.append buffer "\""

let parenthesize (t:t) : t =
   fun buffer ->
   PlaBuffer.append buffer "(";
   t buffer;
   PlaBuffer.append buffer ")"

let indent (t:t) : t =
   fun buffer ->
   PlaBuffer.indent buffer;
   t buffer;
   PlaBuffer.outdent buffer

let wrap (l:t) (r:t) (t:t) : t =
   fun buffer ->
   l buffer;
   t buffer;
   r buffer

(* Functions to append templates *)

let append (t1:t) (t2:t) : t =
   fun buffer ->
   t1 buffer;
   t2 buffer

let join (elems:t list) : t =
   fun buffer -> List.iter (fun a -> a buffer) elems

let join_sep (sep:t) (elems:'a list) : t =
   fun buffer ->
   let rec loop = function
      | []   -> ()
      | [h]  -> h buffer
      | h::t ->
         h buffer;
         sep buffer;
         loop t
   in loop elems

let join_sep_all (sep:t) (elems:'a list) : t =
   fun buffer -> List.iter (fun h -> h buffer; sep buffer) elems

let map_join (f:'a -> t) (elems:'a list) : t =
   fun buffer -> List.iter (fun a -> f a buffer) elems

let map_sep (sep:t) (f:'a -> t) (elems:'a list) : t =
   fun buffer ->
   let rec loop = function
      | []   -> ()
      | [h]  -> (f h) buffer
      | h::t ->
         (f h) buffer;
         sep buffer;
         loop t
   in loop elems

let map_sep_all (sep:t) (f:'a -> t) (elems:'a list) : t =
   fun buffer -> List.iter (fun h -> (f h) buffer; sep buffer) elems

let (++) (t1:t) (t2:t) : t =
   append t1 t2

let print (t:t) : string =
   let buffer = PlaBuffer.newBuffer () in
   t buffer;
   PlaBuffer.contents buffer

let write (file:string) (t:t) : unit =
   let buffer = PlaBuffer.newFile file in
   t buffer;
   PlaBuffer.close buffer