From c8d0f5c9569a8b5d63ef991a075149d920f1f68e Mon Sep 17 00:00:00 2001 From: HyeockJinKim Date: Sat, 28 Sep 2019 23:49:23 +0900 Subject: [PATCH 1/2] Generate SyntaxError at integer starting with 0 Generate syntaxError at integer starting with 0, not zero Fixes #1422 --- parser/src/lexer.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/parser/src/lexer.rs b/parser/src/lexer.rs index 0f2e28bd2..5d11514dc 100644 --- a/parser/src/lexer.rs +++ b/parser/src/lexer.rs @@ -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::().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)) } } From 831c883deb582da935b1fb5c3ccab59f1dc87464 Mon Sep 17 00:00:00 2001 From: HyeockJinKim Date: Sun, 29 Sep 2019 00:07:32 +0900 Subject: [PATCH 2/2] Add tests for integer --- tests/snippets/ints.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/snippets/ints.py b/tests/snippets/ints.py index fe3d046f9..86a848f20 100644 --- a/tests/snippets/ints.py +++ b/tests/snippets/ints.py @@ -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)