@@ -17,17 +17,19 @@ type context = rec(contexttype tp, uint indent);
17
17
18
18
type ps = @rec ( mutable vec[ context] context ,
19
19
uint width ,
20
+ io. writer out ,
20
21
mutable vec[ token] buffered ,
21
22
mutable uint scandepth ,
22
23
mutable uint bufferedcol ,
23
24
mutable uint col,
24
25
mutable bool start_of_line ) ;
25
26
26
- fn mkstate ( uint width ) -> ps {
27
+ fn mkstate ( io . writer out , uint width ) -> ps {
27
28
let vec[ context] stack = vec ( rec ( tp=cx_v, indent=0 u) ) ;
28
29
let vec[ token] buff = vec ( ) ;
29
30
ret @rec( mutable context=stack,
30
31
width=width,
32
+ out=out,
31
33
mutable buffered=buff,
32
34
mutable scandepth=0 u,
33
35
mutable bufferedcol=0 u,
@@ -46,10 +48,22 @@ impure fn pop_context(ps p) {
46
48
}
47
49
48
50
impure fn add_token ( ps p, token tok) {
49
- if ( p. scandepth == 0 u) { do_token ( p, tok) ; }
51
+ if ( p. width == 0 u) { direct_token ( p, tok) ; }
52
+ else if ( p. scandepth == 0 u) { do_token ( p, tok) ; }
50
53
else { buffer_token ( p, tok) ; }
51
54
}
52
55
56
+ impure fn direct_token ( ps p, token tok) {
57
+ alt ( tok) {
58
+ case ( brk ( ?sz) ) {
59
+ while ( sz > 0 u) { p. out . write_str ( " " ) ; sz -= 1 u; }
60
+ }
61
+ case ( word ( ?w) ) { p. out . write_str ( w) ; }
62
+ case ( cword ( ?w) ) { p. out . write_str ( w) ; }
63
+ case ( _) { }
64
+ }
65
+ }
66
+
53
67
impure fn buffer_token ( ps p, token tok) {
54
68
p. buffered += vec ( tok) ;
55
69
p. bufferedcol += token_size ( tok) ;
@@ -101,14 +115,13 @@ impure fn finish_block_scan(ps p, contexttype tp) {
101
115
102
116
impure fn finish_break_scan ( ps p) {
103
117
if ( p. bufferedcol > p. width ) {
104
- write_str ( "\n " ) ;
105
- p. col = 0 u;
118
+ line_break ( p) ;
106
119
}
107
120
else {
108
121
auto width;
109
122
alt ( p. buffered . ( 0 ) ) { case ( brk ( ?w) ) { width = w; } }
110
123
auto i = 0 u;
111
- while ( i < width) { write_str ( " " ) ; i+=1 u; }
124
+ while ( i < width) { p . out . write_str ( " " ) ; i+=1 u; }
112
125
p. col += width;
113
126
}
114
127
p. scandepth = 0 u;
@@ -142,20 +155,18 @@ impure fn do_token(ps p, token tok) {
142
155
start_scan ( p, tok) ;
143
156
}
144
157
case ( cx_v) {
145
- write_str ( "\n " ) ;
146
- p. col = 0 u;
147
- p. start_of_line = true ;
158
+ line_break ( p) ;
148
159
}
149
160
}
150
161
}
151
162
case ( word ( ?w) ) {
152
163
before_print ( p, false ) ;
153
- write_str ( w) ;
164
+ p . out . write_str ( w) ;
154
165
p. col += _str. byte_len ( w) ; // TODO char_len
155
166
}
156
167
case ( cword ( ?w) ) {
157
168
before_print ( p, true ) ;
158
- write_str ( w) ;
169
+ p . out . write_str ( w) ;
159
170
p. col += _str. byte_len ( w) ; // TODO char_len
160
171
}
161
172
case ( open ( ?tp, ?indent) ) {
@@ -170,21 +181,23 @@ impure fn do_token(ps p, token tok) {
170
181
}
171
182
}
172
183
184
+ impure fn line_break ( ps p) {
185
+ p. out . write_str ( "\n " ) ;
186
+ p. col = 0 u;
187
+ p. start_of_line = true ;
188
+ }
189
+
173
190
impure fn before_print ( ps p, bool closing ) {
174
191
if ( p. start_of_line ) {
175
192
p. start_of_line = false ;
176
193
auto ind;
177
194
if ( closing) { ind = base_indent ( p) ; }
178
195
else { ind = cur_context ( p) . indent ; }
179
196
p. col = ind;
180
- while ( ind > 0 u) { write_str ( " " ) ; ind -= 1 u; }
197
+ while ( ind > 0 u) { p . out . write_str ( " " ) ; ind -= 1 u; }
181
198
}
182
199
}
183
200
184
- fn write_str ( str s) {
185
- io. writefd ( 1 , _str. bytes ( s) ) ;
186
- }
187
-
188
201
fn token_size ( token tok) -> uint {
189
202
alt ( tok) {
190
203
case ( brk ( ?sz) ) { ret sz; }
0 commit comments