PageRenderTime 115ms CodeModel.GetById 109ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/source/core/io/OcWriter.ooc

http://github.com/nddrylliog/oc
Unknown | 87 lines | 70 code | 17 blank | 0 comment | 0 complexity | ace705a08970fbad4b3824641997f1be MD5 | raw file
 1import io/Writer, structs/List
 2
 3OcWriter: class {
 4
 5    stream: Writer
 6    tabLevel := 0
 7    tabWidth := 2
 8
 9    init: func (=stream) { }
10
11    close: func {
12        app('\n')
13        stream close()
14    }
15
16    app: func ~chr (c: Char) {
17        stream write(c)
18    }
19
20    app: func ~str (s: String) {
21        stream write(s)
22    }
23
24    /**
25     * <left> elem0 <delim> elem1 <delim> elem2 <delim> elem3 <right>
26     * f is passed every elem of list and is responsible of writing them
27     */
28    writeEach: func <T> (list: List<T>, left, delim, right: String, f: Func(T)) {
29        app(left)
30        first := true
31        list each(|e|
32            if(!first) app(delim)
33            f(e)
34            first = false
35        )
36        app(right)
37    }
38    
39    /**
40     * {
41     *   elem0 <delim>
42     *   elem1 <delim>
43     *   elem2 <delim>
44     * }
45     * f is passed every elem of list and is responsible of writing them
46     */
47    writeBlock: func <T> (list: List<T>, delim: String, f: Func(T)) {
48        openBlock()
49        list each(|e|
50            nl()
51            f(e)
52            app(delim)
53        )
54        closeBlock()
55    }
56
57    writeTabs: func {
58        stream write(" " times (tabLevel * tabWidth), tabLevel * tabWidth)
59    }
60
61    newUntabbedLine: func {
62        stream write('\n')
63    }
64
65    nl: func {
66        newUntabbedLine()
67        writeTabs()
68    }
69
70    tab: func {
71        tabLevel += 1
72    }
73
74    untab: func {
75        tabLevel -= 1
76    }
77
78    openBlock: func {
79        this app("{"). tab()
80    }
81
82    closeBlock: func {
83        this untab(). nl(). app("}")
84    }
85
86}
87