diff options
author | Alex Crichton <alex@alexcrichton.com> | 2015-01-15 15:36:54 -0800 |
---|---|---|
committer | Alex Crichton <alex@alexcrichton.com> | 2015-01-15 15:37:48 -0800 |
commit | f098d70145d6f2fe10855a74f5c70618e1ba88fe (patch) | |
tree | 51b33d01ed564a6c9de6c72fc53b7ab7207491d6 | |
parent | f4b2045de074488a4ded02a6321829300fb5f5d3 (diff) | |
download | milf-rs-f098d70145d6f2fe10855a74f5c70618e1ba88fe.tar.gz milf-rs-f098d70145d6f2fe10855a74f5c70618e1ba88fe.zip |
Disallow leading 0s in integers/floats
-rw-r--r-- | src/parser.rs | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/src/parser.rs b/src/parser.rs index e2e116f..e4dc7f1 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -466,9 +466,22 @@ impl<'a> Parser<'a> { fn number_or_datetime(&mut self, mut start: usize) -> Option<Value> { let sign = if self.eat('+') { start += 1; true } else {self.eat('-')}; let mut is_float = false; + if self.eat('0') { + match self.peek(0) { + Some((pos, c)) if '0' <= c && c <= '9' => { + self.errors.push(ParserError { + lo: start, + hi: pos, + desc: format!("leading zeroes are not allowed"), + }); + return None + } + _ => {} + } + } loop { match self.cur.clone().next() { - Some((_, ch)) if ch.is_digit(10) => { self.cur.next(); } + Some((_, ch)) if '0' <= ch && ch <= '9' => { self.cur.next(); } Some((_, '.')) if !is_float => { is_float = true; self.cur.next(); @@ -983,4 +996,16 @@ trimmed in raw strings. assert!(Parser::new("a = \"\n\"").parse().is_none()); assert!(Parser::new("a = '\n'").parse().is_none()); } + + #[test] + fn bad_leading_zeros() { + assert!(Parser::new("a = 00").parse().is_none()); + assert!(Parser::new("a = -00").parse().is_none()); + assert!(Parser::new("a = +00").parse().is_none()); + assert!(Parser::new("a = 00.0").parse().is_none()); + assert!(Parser::new("a = -00.0").parse().is_none()); + assert!(Parser::new("a = +00.0").parse().is_none()); + assert!(Parser::new("a = 9223372036854775808").parse().is_none()); + assert!(Parser::new("a = -9223372036854775809").parse().is_none()); + } } |