@@ -129,6 +129,17 @@ pub mod ct {
129
129
CountImplied ,
130
130
}
131
131
132
+ struct Parsed < T > {
133
+ val : T ,
134
+ next : uint
135
+ }
136
+
137
+ impl < T > Parsed < T > {
138
+ static pure fn new( val: T , next : uint ) -> Parsed < T > {
139
+ Parsed { val : val , next : next }
140
+ }
141
+ }
142
+
132
143
// A formatted conversion from an expression to a string
133
144
pub struct Conv
134
145
{ param : Option < uint > ,
@@ -169,7 +180,7 @@ pub mod ct {
169
180
} else {
170
181
buf = flush_buf(move buf, &mut pieces);
171
182
let rs = parse_conversion(s, i, lim, err);
172
- pieces.push(copy rs.piece );
183
+ pieces.push(copy rs.val );
173
184
i = rs.next;
174
185
}
175
186
} else { buf += curr; i += size; }
@@ -178,7 +189,7 @@ pub mod ct {
178
189
move pieces
179
190
}
180
191
pub fn peek_num(s: &str, i: uint, lim: uint) ->
181
- Option<{num: uint, next: uint} > {
192
+ Option<Parsed< uint> > {
182
193
let mut j = i;
183
194
let mut accum = 0u;
184
195
let mut found = false;
@@ -194,54 +205,54 @@ pub mod ct {
194
205
}
195
206
}
196
207
if found {
197
- Some({num: accum, next: j} )
208
+ Some(Parsed::new( accum, j) )
198
209
} else {
199
210
None
200
211
}
201
212
}
202
213
pub fn parse_conversion(s: &str, i: uint, lim: uint,
203
214
err: ErrorFn) ->
204
- {piece: Piece, next: uint} {
215
+ Parsed< Piece> {
205
216
let parm = parse_parameter(s, i, lim);
206
217
let flags = parse_flags(s, parm.next, lim);
207
218
let width = parse_count(s, flags.next, lim);
208
219
let prec = parse_precision(s, width.next, lim);
209
220
let ty = parse_type(s, prec.next, lim, err);
210
- return {piece:
211
- PieceConv(Conv {param: parm.param ,
212
- flags: copy flags.flags ,
213
- width: width.count ,
214
- precision: prec.count ,
215
- ty: ty.ty }),
216
- next: ty.next} ;
221
+ return Parsed::new(
222
+ PieceConv(Conv {param: parm.val ,
223
+ flags: copy flags.val ,
224
+ width: width.val ,
225
+ precision: prec.val ,
226
+ ty: ty.val }),
227
+ ty.next) ;
217
228
}
218
229
pub fn parse_parameter(s: &str, i: uint, lim: uint) ->
219
- {param: Option<uint>, next: uint} {
220
- if i >= lim { return {param: None, next: i} ; }
230
+ Parsed< Option<uint>> {
231
+ if i >= lim { return Parsed::new( None, i) ; }
221
232
let num = peek_num(s, i, lim);
222
233
return match num {
223
- None => {param: None, next: i} ,
234
+ None => Parsed::new( None, i) ,
224
235
Some(t) => {
225
- let n = t.num ;
236
+ let n = t.val ;
226
237
let j = t.next;
227
238
if j < lim && s[j] == '$' as u8 {
228
- {param: Some(n), next: j + 1}
229
- } else { {param: None, next: i} }
239
+ Parsed::new( Some(n), j + 1)
240
+ } else { Parsed::new( None, i) }
230
241
}
231
242
};
232
243
}
233
244
pub fn parse_flags(s: &str, i: uint, lim: uint) ->
234
- {flags: ~[Flag], next: uint} {
245
+ Parsed< ~[Flag]> {
235
246
let noflags: ~[Flag] = ~[];
236
- if i >= lim { return {flags: move noflags, next: i} ; }
247
+ if i >= lim { return Parsed::new( move noflags, i) ; }
237
248
238
249
fn more(f: Flag, s: &str, i: uint, lim: uint) ->
239
- {flags: ~[Flag], next: uint} {
250
+ Parsed< ~[Flag]> {
240
251
let next = parse_flags(s, i + 1u, lim);
241
- let rest = copy next.flags ;
252
+ let rest = copy next.val ;
242
253
let j = next.next;
243
254
let curr: ~[Flag] = ~[f];
244
- return {flags: vec::append(move curr, rest), next: j} ;
255
+ return Parsed::new( vec::append(move curr, rest), j) ;
245
256
}
246
257
// Unfortunate, but because s is borrowed, can't use a closure
247
258
// fn more(f: Flag, s: &str) { more_(f, s, i, lim); }
@@ -256,48 +267,48 @@ pub mod ct {
256
267
more(FlagSignAlways, s, i, lim)
257
268
} else if f == '#' as u8 {
258
269
more(FlagAlternate, s, i, lim)
259
- } else { {flags: move noflags, next: i} };
270
+ } else { Parsed::new( move noflags, i) };
260
271
}
261
272
pub fn parse_count(s: &str, i: uint, lim: uint)
262
- -> {count: Count, next: uint} {
273
+ -> Parsed< Count> {
263
274
return if i >= lim {
264
- {count: CountImplied, next: i}
275
+ Parsed::new( CountImplied, i)
265
276
} else if s[i] == '*' as u8 {
266
277
let param = parse_parameter(s, i + 1, lim);
267
278
let j = param.next;
268
- match param.param {
269
- None => {count: CountIsNextParam, next: j} ,
270
- Some(n) => {count: CountIsParam(n), next: j}
279
+ match param.val {
280
+ None => Parsed::new( CountIsNextParam, j) ,
281
+ Some(n) => Parsed::new( CountIsParam(n), j)
271
282
}
272
283
} else {
273
284
let num = peek_num(s, i, lim);
274
285
match num {
275
- None => {count: CountImplied, next: i} ,
276
- Some(num) => {
277
- count: CountIs(num.num ),
278
- next: num.next
279
- }
286
+ None => Parsed::new( CountImplied, i) ,
287
+ Some(num) => Parsed::new(
288
+ CountIs(num.val ),
289
+ num.next
290
+ )
280
291
}
281
292
};
282
293
}
283
294
pub fn parse_precision(s: &str, i: uint, lim: uint) ->
284
- {count: Count, next: uint} {
295
+ Parsed< Count> {
285
296
return if i >= lim {
286
- {count: CountImplied, next: i}
297
+ Parsed::new( CountImplied, i)
287
298
} else if s[i] == '.' as u8 {
288
299
let count = parse_count(s, i + 1u, lim);
289
300
290
301
291
302
// If there were no digits specified, i.e. the precision
292
303
// was " . ", then the precision is 0
293
- match count . count {
294
- CountImplied => { count : CountIs ( 0 ) , next : count. next } ,
304
+ match count . val {
305
+ CountImplied => Parsed :: new ( CountIs ( 0 ) , count. next ) ,
295
306
_ => count
296
307
}
297
- } else { { count : CountImplied , next : i } } ;
308
+ } else { Parsed :: new ( CountImplied , i ) } ;
298
309
}
299
310
pub fn parse_type( s : & str , i : uint , lim : uint , err : ErrorFn ) ->
300
- { ty : Ty , next : uint } {
311
+ Parsed < Ty > {
301
312
if i >= lim { err ( ~"missing type in conversion") ; }
302
313
let tstr = str:: slice ( s, i, i+1 u) ;
303
314
// FIXME (#2249): Do we really want two signed types here?
@@ -326,7 +337,7 @@ pub mod ct {
326
337
} else if tstr == ~"?" {
327
338
TyPoly
328
339
} else { err ( ~"unknown type in conversion: " + tstr) };
329
- return {ty: t, next: i + 1u} ;
340
+ return Parsed::new( t, i + 1u) ;
330
341
}
331
342
}
332
343
0 commit comments