Skip to content

Commit 37684d5

Browse files
authored
Merge pull request #18 from spyoungtech/improve-invalid-literal-handling
Handle invalid number literals when document ends with decimal
2 parents c082d46 + d0f6416 commit 37684d5

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/de.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl<'de, 'a> Deserializer<'de> for JSONValueDeserializer<'a> {
7070
visitor.visit_u64(u)
7171
} else {
7272
// fallback: treat as a string or produce an error
73-
Err(de::Error::custom(format!("Invalid integer {}", s)))
73+
Err(de::Error::custom(format!("Invalid integer literal: {}", s)))
7474
}
7575
}
7676
}

src/tokenize.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,22 @@ impl <'input> Tokenizer<'input> {
224224

225225
fn process_number(&mut self) -> Result<TokenSpan, TokenizationError>{
226226
let (start_idx, start_char) = self.lookahead.expect("Unexpected end of input, was expecting numeric char");
227+
let mut last_index = start_idx;
228+
let mut decimal_seen: bool = false;
229+
let mut exponent_seen: bool = false;
230+
let mut unary_seen: bool = false;
231+
if start_char == '.' {
232+
decimal_seen = true
233+
}
227234

228235
let maybe_second_char = self.chars.peek();
229236
match maybe_second_char {
230-
None => return Ok((start_idx, TokType::Integer, start_idx + 1)),
237+
None => {
238+
if decimal_seen {
239+
return Err(self.make_error("Lone decimal is an invalid literal".to_string(), start_idx))
240+
}
241+
return Ok((start_idx, TokType::Integer, start_idx + 1))
242+
},
231243
Some((_second_idx, second_char)) if start_char == '0' => {
232244
match second_char {
233245
'x' | 'X' => {return self.process_hexadecimal()}
@@ -240,15 +252,6 @@ impl <'input> Tokenizer<'input> {
240252
_ => {}
241253
}
242254

243-
let mut last_index = start_idx;
244-
let mut decimal_seen: bool = false;
245-
let mut exponent_seen: bool = false;
246-
let mut unary_seen: bool = false;
247-
match start_char {
248-
'.' => {decimal_seen = true}
249-
'+' | '-' => {unary_seen = true}
250-
_ => {}
251-
}
252255
loop {
253256
match self.chars.peek() {
254257
None => {

0 commit comments

Comments
 (0)