@@ -9,7 +9,7 @@ macro_rules! sequence {
9
9
$(
10
10
match $parser. next_byte( ) {
11
11
Some ( $ch) => { } ,
12
- Some ( ch) => return Err ( $parser. unexpected_character_error ( ch) ) ,
12
+ Some ( ch) => return Err ( $parser. unexpected_character ( ch) ) ,
13
13
None => return Err ( JsonError :: UnexpectedEndOfJson )
14
14
}
15
15
) *
@@ -62,7 +62,7 @@ macro_rules! expect {
62
62
63
63
match ch {
64
64
$byte => { } ,
65
- _ => return Err ( $parser. unexpected_character_error ( ch) )
65
+ _ => return Err ( $parser. unexpected_character ( ch) )
66
66
}
67
67
} )
68
68
}
@@ -77,7 +77,7 @@ macro_rules! expect_one_of {
77
77
$(
78
78
$byte => $then,
79
79
) *
80
- _ => return Err ( $parser. unexpected_character_error ( ch) )
80
+ _ => return Err ( $parser. unexpected_character ( ch) )
81
81
}
82
82
83
83
} )
@@ -106,7 +106,7 @@ macro_rules! expect_string {
106
106
b't' => b'\t' ,
107
107
b'r' => b'\r' ,
108
108
b'n' => b'\n' ,
109
- _ => return Err ( $parser. unexpected_character_error ( ch) )
109
+ _ => return Err ( $parser. unexpected_character ( ch) )
110
110
} ;
111
111
$parser. buffer. push( ch) ;
112
112
} ,
@@ -133,10 +133,22 @@ macro_rules! expect_value {
133
133
b'[' => JsonValue :: Array ( try!( $parser. read_array( ) ) ) ,
134
134
b'{' => JsonValue :: Object ( try!( $parser. read_object( ) ) ) ,
135
135
b'"' => JsonValue :: String ( expect_string!( $parser) ) ,
136
- b'0' ... b'9' => JsonValue :: Number ( try!( $parser. read_number( ch, false ) ) ) ,
136
+ b'0' => {
137
+ let num = try!( $parser. read_number_with_fraction( 0.0 , false ) ) ;
138
+ JsonValue :: Number ( num)
139
+ } ,
140
+ b'1' ... b'9' => {
141
+ let num = try!( $parser. read_number( ch, false ) ) ;
142
+ JsonValue :: Number ( num)
143
+ } ,
137
144
b'-' => {
138
145
let ch = try!( $parser. expect_byte( ) ) ;
139
- JsonValue :: Number ( try!( $parser. read_number( ch, true ) ) )
146
+ let num = match ch {
147
+ b'0' => try!( $parser. read_number_with_fraction( 0.0 , true ) ) ,
148
+ b'1' ... b'9' => try!( $parser. read_number( ch, true ) ) ,
149
+ _ => return Err ( $parser. unexpected_character( ch) )
150
+ } ;
151
+ JsonValue :: Number ( num)
140
152
}
141
153
b't' => {
142
154
sequence!( $parser, b'r' , b'u' , b'e' ) ;
@@ -150,7 +162,7 @@ macro_rules! expect_value {
150
162
sequence!( $parser, b'u' , b'l' , b'l' ) ;
151
163
JsonValue :: Null
152
164
} ,
153
- _ => return Err ( $parser. unexpected_character_error ( ch) )
165
+ _ => return Err ( $parser. unexpected_character ( ch) )
154
166
}
155
167
} )
156
168
}
@@ -190,7 +202,7 @@ impl<'a> Parser<'a> {
190
202
}
191
203
}
192
204
193
- fn unexpected_character_error ( & self , byte : u8 ) -> JsonError {
205
+ fn unexpected_character ( & self , byte : u8 ) -> JsonError {
194
206
let pos = self . source_position_from_index ( self . current_index ) ;
195
207
let ch = char:: from_u32 ( byte as u32 ) . unwrap_or ( '?' ) ;
196
208
@@ -237,7 +249,7 @@ impl<'a> Parser<'a> {
237
249
b'0' ... b'9' => ( ch - b'0' ) as u32 ,
238
250
b'a' ... b'f' => ( ch + 10 - b'a' ) as u32 ,
239
251
b'A' ... b'F' => ( ch + 10 - b'A' ) as u32 ,
240
- ch => return Err ( self . unexpected_character_error ( ch) ) ,
252
+ ch => return Err ( self . unexpected_character ( ch) ) ,
241
253
} )
242
254
}
243
255
@@ -296,6 +308,11 @@ impl<'a> Parser<'a> {
296
308
// u64 into freshly converted f64
297
309
read_num ! ( self , digit, num = num * 10.0 + digit as f64 ) ;
298
310
311
+ self . read_number_with_fraction ( num, is_negative)
312
+ }
313
+
314
+ fn read_number_with_fraction ( & mut self , mut num : f64 , is_negative : bool )
315
+ -> JsonResult < f64 > {
299
316
if let Some ( b'.' ) = self . peek_byte ( ) {
300
317
self . left_over = None ;
301
318
let mut precision = -1 ;
@@ -308,7 +325,6 @@ impl<'a> Parser<'a> {
308
325
309
326
match self . checked_next_byte ( ) {
310
327
Some ( b'e' ) | Some ( b'E' ) => {
311
- let mut e = 0 ;
312
328
let sign = match self . next_byte ( ) {
313
329
Some ( b'-' ) => -1 ,
314
330
Some ( b'+' ) => 1 ,
@@ -318,6 +334,12 @@ impl<'a> Parser<'a> {
318
334
} ,
319
335
} ;
320
336
337
+ let ch = try!( self . checked_expect_byte ( ) ) ;
338
+ let mut e = match ch {
339
+ b'0' ... b'9' => ( ch - b'0' ) as i32 ,
340
+ _ => return Err ( self . unexpected_character ( ch) ) ,
341
+ } ;
342
+
321
343
read_num ! ( self , digit, e = e * 10 + digit as i32 ) ;
322
344
323
345
num *= 10f64 . powi ( e * sign) ;
@@ -380,12 +402,18 @@ impl<'a> Parser<'a> {
380
402
}
381
403
382
404
fn ensure_end ( & mut self ) -> JsonResult < ( ) > {
383
- let ch = self . next_byte ( ) ;
405
+ let ch = self . checked_next_byte ( ) ;
406
+
384
407
if let Some ( ch) = ch {
385
408
match ch {
386
409
// whitespace
387
- 9 ... 13 | 32 => return self . ensure_end ( ) ,
388
- _ => return Err ( self . unexpected_character_error ( ch) )
410
+ 9 ... 13 | 32 => while let Some ( ch) = self . next_byte ( ) {
411
+ match ch {
412
+ 9 ... 13 | 32 => { } ,
413
+ _ => return Err ( self . unexpected_character ( ch) )
414
+ }
415
+ } ,
416
+ _ => return Err ( self . unexpected_character ( ch) )
389
417
}
390
418
}
391
419
0 commit comments