Source

ocaml-stringpainter / src / Engine.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
(*********************************************************************************************************************
 * Copyrights (C) by
 *  Pawel Wieczorek <wieczyk gmail>
 *
 * http://bitbucket.org/wieczyk/ocaml-stringpainter
 ********************************************************************************************************************)

(*********************************************************************************************************************
 * The core of whole system
 *   
 * +--LIBRARY--+           +---CORE---+               +---CORE---+  
 * |  various  |  encode   |   real   |  preprocess   |  simple  |  render
 * | painters  | ========> | painters | ============> | painters | =======> string
 * +-----------+           +----------+               +----------+
 *
 ********************************************************************************************************************)

module Core = struct

    type category
        = C_None
        | C_Keyword
        | C_Value
        | C_ValueKeyword
        | C_Operator
        | C_Syntax
        | C_Label
        | C_Special

    type simple
        = S_Text of string
        | S_TextC of string * category
        | S_Break
        | S_NewLine

        (* nested *)
        | S_Nested of simple list
        | S_Indent of simple list

    type painter = simple list * bool

    type t
        (* Syntactical categories, they are distinct due to color scheme *)
        = Text of string
        | Word of string
        | Keyword of string
        | Value of string
        | ValueKeyword of string
        | Operator of string
        | Label of string
        | Syntax of string
        | Special of string

        (* Line break *)
        | Break
        | NewLine

        (* Nested *)
        | Nested of t list
        | Indent of t list
        | Painter of painter

    (*----------------------------------------------------------------------------------------------------------------
     * Renderer
     *)

    module Renderer = struct

        open ColorScheme

        module Internal = struct 

            let eol = "\n"
            let tab = "    "

            type t =
                { buffer        : Buffer.t
                ; line          : Buffer.t
                ; indent        : int ref
                ; weak_break    : bool ref
                ; color_scheme  : color_scheme_functionalized
                }

            let empty_context cs =
                { buffer        = Buffer.create 1024
                ; line          = Buffer.create 120
                ; indent        = ref 0
                ; weak_break    = ref false
                ; color_scheme  = functionalize cs
                }

            let buffer_empty b = Buffer.length b == 0

            let indent internal = 
                for i = 1 to !(internal.indent) do
                    Buffer.add_string internal.buffer tab
                done

            let _break_line internal forced add_eol =
                if not (buffer_empty internal.line) || forced
                then begin
                    indent internal;
                    Buffer.add_string internal.buffer (Buffer.contents internal.line);
                    if add_eol then Buffer.add_string internal.buffer eol;
                    Buffer.reset internal.line
                end

            let break_line internal forced = _break_line internal forced true

            let add_text internal text =
                Buffer.add_string internal.line text

            let finalize internal cont =
                _break_line internal false false;
                cont internal.buffer

            let increment_indent internal =
                incr internal.indent

            let decrement_indent internal =
                decr internal.indent

            let set_weak_break internal =
                internal.weak_break := true

            let flush_weak_break internal =
                internal.weak_break := false;
                Buffer.add_string internal.buffer eol

            let apply_category internal = function
                | C_None            -> fun x -> x
                | C_Keyword         -> internal.color_scheme.fcs_keyword
                | C_Value           -> internal.color_scheme.fcs_value
                | C_ValueKeyword    -> internal.color_scheme.fcs_value_keyword
                | C_Operator        -> internal.color_scheme.fcs_operator
                | C_Syntax          -> internal.color_scheme.fcs_syntax
                | C_Label           -> internal.color_scheme.fcs_label
                | C_Special         -> internal.color_scheme.fcs_special


        end

        type driver =
            { internal          : Internal.t
            ; break_line        : bool -> unit 
            ; add_text          : string -> unit
            ; increment_indent  : unit -> unit
            ; decrement_indent  : unit -> unit
            ; set_weak_break    : unit -> unit
            ; flush_weak_break  : unit -> unit
            ; apply_category   : category -> string -> string
            }

        let empty_driver () =
            let internal = Internal.empty_context default_color_scheme in

            { internal          = internal
            ; break_line        = Internal.break_line internal
            ; add_text          = Internal.add_text internal
            ; increment_indent  = (fun () -> Internal.increment_indent internal)
            ; decrement_indent  = (fun () -> Internal.decrement_indent internal)
            ; set_weak_break    = (fun () -> Internal.set_weak_break internal)
            ; flush_weak_break  = (fun () -> Internal.flush_weak_break internal)
            ; apply_category   = Internal.apply_category internal
            }


        let rec process cont driver = function
            (* Text *)
            | (S_Text s :: rest) :: stack ->
                driver.add_text s;
                process cont driver (rest :: stack)

            | (S_TextC (s, a) :: rest) :: stack ->
                driver.add_text (driver.apply_category a s);
                process cont driver (rest :: stack)

            (* Breaking lines *)
            | (S_Break :: rest) :: stack ->
                driver.break_line false;
                process cont driver (rest :: stack)

            | (S_NewLine :: rest) :: stack ->
                driver.break_line true;
                process cont driver (rest :: stack)

            (* Nesting *)
            | (S_Nested xs :: rest) :: stack ->
                process cont driver (xs :: rest :: stack)

            | (S_Indent xs :: rest) :: stack ->
                indent_process cont driver xs (rest :: stack) 

            (* Rest *)
            | [] :: stack ->
                process cont driver stack

            | [] ->
                cont ()

        and indent_process cont driver xs stack =
            let subcont () =
                driver.break_line false;
                driver.decrement_indent (); 
                process cont driver stack
                in
            driver.break_line false;
            driver.increment_indent (); 
            process subcont driver [xs]


        let render_painters final_cont xs = 
            let driver  = empty_driver () in
            let cont () = Internal.finalize driver.internal final_cont in
            process cont driver [xs]

    end

    (*----------------------------------------------------------------------------------------------------------------
     * Preprocessor
     *)

    module Preprocessor = struct

        let nested multiline xs = (xs, multiline)

        let rec process multiline = function
            | Text txt :: rest ->
                process_text multiline rest 
                    [ S_Text txt
                    ]

            | Word txt :: rest ->
                process_text multiline rest 
                    [ S_TextC (txt, C_None)
                    ; S_Text " "
                    ]

            | Label txt :: rest ->
                process_text multiline rest 
                    [ S_TextC (txt, C_Label)
                    ; S_Text " "
                    ]

            | Operator txt :: rest ->
                process_text multiline rest 
                    [ S_TextC (txt, C_Operator)
                    ; S_Text " "
                    ]

            | Keyword txt :: rest ->
                process_text multiline rest 
                    [ S_TextC (txt, C_Keyword)
                    ; S_Text " "
                    ]

            | Syntax txt :: rest ->
                process_text multiline rest 
                    [ S_TextC (txt, C_Syntax)
                    ; S_Text " "
                    ]

            | Value txt :: rest ->
                process_text multiline rest 
                    [ S_TextC (txt, C_Value)
                    ; S_Text " "
                    ]

            | ValueKeyword txt :: rest ->
                process_text multiline rest 
                    [ S_TextC (txt, C_ValueKeyword)
                    ; S_Text " "
                    ]

            | Special txt :: rest ->
                process_text multiline rest 
                    [ S_TextC (txt, C_Special)
                    ; S_Text " "
                    ]

            | Indent xs :: rest ->
                let (simple', multiline')   = process multiline rest in
                let (simple'', multiline'') = process false xs in
                nested true
                    [ S_Indent simple''
                    ; S_Nested simple'
                    ]

            | Nested xs :: rest ->
                let (simple', multiline')   = process multiline rest in
                let (simple'', multiline'') = process false xs in
                nested (multiline' || multiline'')
                    [ S_Nested simple'
                    ; S_Nested simple''
                    ]

            | Break :: rest ->
                nested true [S_Break]

            | NewLine :: rest ->
                nested true [S_NewLine]

            | Painter (c_simple, c_multiline) :: rest ->
                let (simple', multiline') = process multiline rest in
                nested (multiline' || c_multiline)
                    [ S_Nested c_simple
                    ; S_Nested simple'
                    ]

            | [] ->
                ([], multiline)

        and process_text multiline rest res =
                let (simple', multiline') = process multiline rest in
                nested (multiline' || multiline)
                    [ S_Nested res
                    ; S_Nested simple'
                    ]

        let preprocess_painters finalcont xs =
            finalcont (process false xs)

    end


    (*----------------------------------------------------------------------------------------------------------------
     * Interface
     *)

    let preprocess xs = 
        let cont (simple, multiline) = (simple, multiline) in
        Preprocessor.preprocess_painters cont xs

    let from_painter p = Painter p

    let is_multiline (_, multiline) = multiline

    let any_multiline xs = List.exists is_multiline xs

    let render_painter (sp, _) = 
        let cont = Buffer.contents in
        Renderer.render_painters cont sp
        

end

open Core

type painter = Core.painter

let mk_painter xs = preprocess
    (List.map from_painter xs)

let is_multiline  = Core.is_multiline
let any_multiline = Core.any_multiline

let render_painter sp = Core.render_painter sp

let print_painter sp = print_string (render_painter sp)
    
let print_painter_nl sp = print_endline (render_painter sp)

(*********************************************************************************************************************
 * Basic painters
 ********************************************************************************************************************)

let sp_text txt = preprocess
    [ Text txt
    ]

let sp_word txt = preprocess
    [ Word txt
    ]

let sp_label txt = preprocess
    [ Label txt
    ]

let sp_special txt = preprocess
    [ Special txt
    ]

let sp_keyword txt = preprocess
    [ Keyword txt
    ]

let sp_value_keyword txt = preprocess
    [ ValueKeyword txt
    ]

let sp_value txt = preprocess
    [ Value txt
    ]

let sp_operator txt = preprocess
    [ Operator txt
    ]

let sp_syntax txt = preprocess
    [ Syntax txt
    ]

let sp_nested xs = mk_painter
    xs

let sp_indent xs = preprocess
    [ Indent (List.map from_painter xs)
    ]

let sp_break = preprocess
    [ Break
    ]

let sp_newline = preprocess
    [ NewLine
    ]