Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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))
}
}
Expand Down
21 changes: 21 additions & 0 deletions tests/snippets/ints.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,24 @@ def __int__(self):
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)