Merge pull request #1430 from HyeockJinKim/issue1422

Generate SyntaxError at integer starting with 0
This commit is contained in:
Windel Bouwman authored and GitHub committed 2019-09-28 21:08:06 +02:00
commit 188ff0ea73
2 files changed
+29 -1

No files matched your search

+8 -1
View File
@@ -8,6 +8,7 @@ pub use super::token::Tok;
use crate::error::{LexicalError, LexicalErrorType};
use crate::location::Location;
use num_bigint::BigInt;
use num_traits::identities::Zero;
use num_traits::Num;
use std::cmp::Ordering;
use std::collections::HashMap;
@@ -352,7 +353,7 @@ where
/// Lex a normal number, that is, no octal, hex or binary number.
fn lex_normal_number(&mut self) -> LexResult {
let start_pos = self.get_pos();
let start_is_zero = self.chr0 == Some('0');
// Normal number:
let mut value_text = self.radix_run(10);
@@ -403,6 +404,12 @@ where
} else {
let end_pos = self.get_pos();
let value = value_text.parse::<BigInt>().unwrap();
if start_is_zero && !value.is_zero() {
return Err(LexicalError {
error: LexicalErrorType::OtherError("Invalid Token".to_string()),
location: self.get_pos(),
});
}
Ok((start_pos, Tok::Int { value }, end_pos))
}
}
+21
View File
@@ -232,3 +232,24 @@ assert_raises(TypeError, lambda: (0).__round__(None))
assert_raises(TypeError, lambda: (1).__round__(None))
assert_raises(TypeError, lambda: (0).__round__(0.0))
assert_raises(TypeError, lambda: (1).__round__(0.0))
assert 00 == 0
assert 0_0 == 0
assert 03.2 == 3.2
assert 3+02j == 3+2j
# Invalid syntax:
src = """
b = 02
"""
with assert_raises(SyntaxError):
exec(src)
# Invalid syntax:
src = """
b = 03 + 2j
"""
with assert_raises(SyntaxError):
exec(src)