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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
(* 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.
*)

type options = { indent_size : int }

type buffer =
  { append : string -> unit
  ; content : unit -> string
  ; close : unit -> unit
  ; mutable indent : int
  ; mutable space : string
  ; mutable indented : bool
  ; options : options
  }

type t = buffer -> unit

let default_options = { indent_size = 3 }

let buffer_new options =
  let b = Buffer.create 128 in
  { append = (fun s -> Buffer.add_string b s)
  ; content = (fun () -> Buffer.contents b)
  ; close = (fun () -> Buffer.clear b)
  ; indent = 0
  ; space = ""
  ; indented = false
  ; options
  }
;;

let buffer_new_file options (file : string) =
  let c = open_out file in
  { append = (fun s -> output_string c s)
  ; content = (fun () -> "")
  ; close = (fun () -> close_out c)
  ; indent = 0
  ; space = ""
  ; indented = false
  ; options
  }
;;

let buffer_contents t : string = t.content ()
let buffer_close t : unit = t.close ()

let buffer_newline t =
  t.append "\n";
  t.indented <- false
  [@@inline always]
;;

let buffer_indent t : unit =
  t.indent <- t.indent + 1;
  t.space <- String.make (t.indent * t.options.indent_size) ' ';
  buffer_newline t
  [@@inline always]
;;

let buffer_outdent t : unit =
  t.indent <- t.indent - 1;
  if t.indent < 0 then failwith "Cannot outdent more";
  t.space <- String.make (t.indent * t.options.indent_size) ' '
  [@@inline always]
;;

let buffer_append t (s : string) : unit =
  if not t.indented
  then (
    t.append t.space;
    t.indented <- true);
  t.append s
  [@@inline always]
;;

let buffer_apply (t : t) (buffer : buffer) : unit = t buffer [@@inline always]
let make (f : buffer -> unit) : t = f [@@inline always]

(* Builtin templates *)

let unit : t = fun _ -> ()
let newline : t = fun buffer -> buffer_newline buffer
let comma : t = fun buffer -> buffer_append buffer ","
let commaspace : t = fun buffer -> buffer_append buffer ", "
let semi : t = fun buffer -> buffer_append buffer ";"
let space : t = fun buffer -> buffer_append buffer " "

(* Templates of basic types *)

let string (s : string) : t = fun buffer -> buffer_append buffer s

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

let int (i : int) : t = fun buffer -> buffer_append buffer (string_of_int i)

let float (f : float) : t =
 fun buffer ->
  let s = string_of_float f in
  buffer_append buffer s;
  if s.[String.length s - 1] = '.' then buffer_append buffer "0"
;;

let bool (b : bool) : t =
 fun buffer ->
  let s = if b then "true" else "false" in
  buffer_append buffer s
;;

(* Functions to wrap templates *)

let quote (t : t) : t =
 fun buffer ->
  buffer_append buffer "\"";
  t buffer;
  buffer_append buffer "\""
;;

let parenthesize (t : t) : t =
 fun buffer ->
  buffer_append buffer "(";
  t buffer;
  buffer_append buffer ")"
;;

let indent (t : t) : t =
 fun buffer ->
  buffer_indent buffer;
  t buffer;
  buffer_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 ?(options = default_options) (t : t) : string =
  let buffer = buffer_new options in
  t buffer;
  buffer_contents buffer
;;

let write ?(options = default_options) (file : string) (t : t) : unit =
  let buffer = buffer_new_file options file in
  t buffer;
  buffer_close buffer
;;

type compiled =
  { tokens : Pla_tokens.s list
  ; slots : (string * Pla_tokens.vartype) list
  ; filled : (string * t) list
  }

module StringMap = Map.Make (String)

let collectSlots tokens =
  let rec loop acc t =
    match t with
    | Pla_tokens.V (name, type_, _) :: t -> loop (StringMap.add name type_ acc) t
    | _ :: t -> loop acc t
    | [] -> acc
  in
  loop StringMap.empty tokens
;;

let create text =
  let tokens = Pla_lex.tokenize text in
  let slots = StringMap.fold (fun key value acc -> (key, value) :: acc) (collectSlots tokens) [] in
  { tokens; slots; filled = [] }
;;

let set_generic name value type_ compiled =
  let rec loop acc slots =
    match slots with
    | (var, found_type_) :: t when var = name ->
      if type_ = found_type_ then (var, value), acc @ t else failwith ("The type for template does not match: " ^ name)
    | h :: t -> loop (h :: acc) t
    | [] ->
      let available = String.concat ", " (List.map (fun (s, _) -> s) compiled.slots) in
      failwith ("The slot '" ^ name ^ "' could not be filled. " ^ available)
  in
  let filled1, slots = loop [] compiled.slots in
  { compiled with filled = filled1 :: compiled.filled; slots }
;;

let set name value compiled = set_generic name value Pla_tokens.Template compiled
let seti name value compiled = set_generic name (int value) Pla_tokens.Int compiled
let setf name value compiled = set_generic name (float value) Pla_tokens.Float compiled
let sets name value compiled = set_generic name (string value) Pla_tokens.String compiled

let close compiled buffer =
  if compiled.slots <> []
  then (
    let available = String.concat ", " (List.map (fun (s, _) -> s) compiled.slots) in
    failwith ("The template is not filled. " ^ available))
  else
    let open Pla_tokens in
    let rec loop tokens =
      match tokens with
      | N :: t ->
        buffer_newline buffer;
        loop t
      | I :: t ->
        buffer_indent buffer;
        loop t
      | O :: t ->
        buffer_outdent buffer;
        loop t
      | T s :: t ->
        buffer_append buffer s;
        loop t
      | V (name, _, _) :: t ->
        let temp = List.assoc name compiled.filled in
        temp buffer;
        loop t
      | [] -> ()
    in
    loop compiled.tokens
;;