@@ -170,9 +170,7 @@ impl<'self> Iterator<Piece<'self>> for Parser<'self> {
170
170
Some ( ( _, '{' ) ) => {
171
171
self . cur . next ( ) ;
172
172
let ret = Some ( Argument ( self . argument ( ) ) ) ;
173
- if !self . consume ( '}' ) {
174
- self . err ( ~"unterminated format string") ;
175
- }
173
+ self . must_consume ( '}' ) ;
176
174
ret
177
175
}
178
176
Some ( ( pos, '\\' ) ) => {
@@ -223,6 +221,25 @@ impl<'self> Parser<'self> {
223
221
}
224
222
}
225
223
224
+ /// Forces consumption of the specified character. If the character is not
225
+ /// found, an error is emitted.
226
+ fn must_consume ( & mut self , c : char ) {
227
+ self . ws ( ) ;
228
+ match self . cur . clone ( ) . next ( ) {
229
+ Some ( ( _, maybe) ) if c == maybe => {
230
+ self . cur . next ( ) ;
231
+ }
232
+ Some ( ( _, other) ) => {
233
+ parse_error:: cond. raise (
234
+ format ! ( "expected `{}` but found `{}`" , c, other) ) ;
235
+ }
236
+ None => {
237
+ parse_error:: cond. raise (
238
+ format ! ( "expected `{}` but string was terminated" , c) ) ;
239
+ }
240
+ }
241
+ }
242
+
226
243
/// Attempts to consume any amount of whitespace followed by a character
227
244
fn wsconsume ( & mut self , c : char ) -> bool {
228
245
self . ws ( ) ; self . consume ( c)
@@ -386,15 +403,11 @@ impl<'self> Parser<'self> {
386
403
self . ws ( ) ;
387
404
match self . word ( ) {
388
405
"select" => {
389
- if !self . wsconsume ( ',' ) {
390
- self . err ( ~"`select` must be followed by `, `") ;
391
- }
406
+ self . must_consume ( ',' ) ;
392
407
Some ( self . select ( ) )
393
408
}
394
409
"plural" => {
395
- if !self . wsconsume ( ',' ) {
396
- self . err ( ~"`plural` must be followed by `, `") ;
397
- }
410
+ self . must_consume ( ',' ) ;
398
411
Some ( self . plural ( ) )
399
412
}
400
413
"" => {
@@ -420,15 +433,11 @@ impl<'self> Parser<'self> {
420
433
self . err ( ~"cannot have an empty selector") ;
421
434
break
422
435
}
423
- if !self . wsconsume ( '{' ) {
424
- self . err ( ~"selector must be followed by `{ `") ;
425
- }
436
+ self . must_consume ( '{' ) ;
426
437
self . depth += 1 ;
427
438
let pieces = self . collect ( ) ;
428
439
self . depth -= 1 ;
429
- if !self . wsconsume ( '}' ) {
430
- self . err ( ~"selector case must be terminated by `} `") ;
431
- }
440
+ self . must_consume ( '}' ) ;
432
441
if selector == "other" {
433
442
if !other. is_none ( ) {
434
443
self . err ( ~"multiple `other` statements in `select") ;
@@ -475,9 +484,7 @@ impl<'self> Parser<'self> {
475
484
self . err ( format ! ( "expected `offset`, found `{}`" ,
476
485
word) ) ;
477
486
} else {
478
- if !self . consume ( ':' ) {
479
- self . err ( ~"`offset` must be followed by `: `") ;
480
- }
487
+ self . must_consume ( ':' ) ;
481
488
match self . integer ( ) {
482
489
Some ( i) => { offset = Some ( i) ; }
483
490
None => {
@@ -524,15 +531,11 @@ impl<'self> Parser<'self> {
524
531
}
525
532
}
526
533
} ;
527
- if !self . wsconsume ( '{' ) {
528
- self . err ( ~"selector must be followed by `{ `") ;
529
- }
534
+ self . must_consume ( '{' ) ;
530
535
self . depth += 1 ;
531
536
let pieces = self . collect ( ) ;
532
537
self . depth -= 1 ;
533
- if !self . wsconsume ( '}' ) {
534
- self . err ( ~"selector case must be terminated by `} `") ;
535
- }
538
+ self . must_consume ( '}' ) ;
536
539
if isother {
537
540
if !other. is_none ( ) {
538
541
self . err ( ~"multiple `other` statements in `select") ;
0 commit comments