Import Upstream version 20180207
[hcoop/debian/mlton.git] / lib / mlton / basic / tab.sml
CommitLineData
7f918cf1
CE
1(* Copyright (C) 1999-2006 Henry Cejtin, Matthew Fluet, Suresh
2 * Jagannathan, and Stephen Weeks.
3 *
4 * MLton is released under a BSD-style license.
5 * See the file MLton-LICENSE for details.
6 *)
7
8structure Tab: TAB =
9struct
10
11val initialSize: int = 40
12val growFactor: int = 2
13
14fun make(out, tab) =
15 let
16 fun makeTabs size =
17 Array.tabulate(size,
18 let val prev = ref ""
19 in fn _ =>
20 (let val cur = !prev
21 in (prev := tab ^ cur ;
22 cur)
23 end)
24 end)
25
26 val tabs = ref(makeTabs initialSize)
27
28 fun size() = Array.length(!tabs)
29
30 fun resize() = tabs := makeTabs(growFactor * size())
31
32 val index = ref 0
33
34 fun maybeResize() = if !index = size() then resize() else ()
35
36 fun reset() = index := 0
37
38 fun indent() = Out.output(out, Array.sub(!tabs, !index))
39
40 fun right() = (index := !index + 1 ;
41 maybeResize())
42
43 fun left() = if !index = 0 then Error.bug "Tab.left"
44 else index := !index - 1
45
46 fun output x =
47 (indent() ;
48 Out.output(out, x) ;
49 Out.output(out, "\n"))
50
51 in {reset = reset,
52 indent = indent,
53 left = left,
54 right = right}
55 end
56
57end