From c408c253713d52d66d256522a1ff3c62b0693ed2 Mon Sep 17 00:00:00 2001 From: adumbidiot Date: Tue, 4 Feb 2020 19:35:25 -0800 Subject: [PATCH 1/2] Fixed parsing of scientific notation with floats --- src/lib/syntax/lexer.rs | 52 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/src/lib/syntax/lexer.rs b/src/lib/syntax/lexer.rs index f2cbb3c9941..aa857712e69 100644 --- a/src/lib/syntax/lexer.rs +++ b/src/lib/syntax/lexer.rs @@ -426,11 +426,25 @@ impl<'a> Lexer<'a> { None => break, }; - if !c.is_digit(10) { - break 'digitloop; + match c { + 'e' | 'E' => { + match self.preview_multiple_next(2).unwrap_or_default().to_digit(10) { + Some(0..=9) | None => { + buf.push(self.next()); + } + _ => { + break 'digitloop; + } + } + } + _ => { + if !c.is_digit(10) { + break 'digitloop; + } + } } }, - 'e' => { + 'e' | 'E' => { match self.preview_multiple_next(2).unwrap_or_default().to_digit(10) { Some(0..=9) | None => { buf.push(self.next()); @@ -1105,7 +1119,37 @@ mod tests { assert_eq!(lexer.tokens[1].data, TokenData::Punctuator(Punctuator::Add)); assert_eq!( lexer.tokens[2].data, - TokenData::NumericLiteral(100000000000.0) + TokenData::NumericLiteral(100_000_000_000.0) ); } + + #[test] + fn test_positive_float_positive_scientific_notaion() { + let mut lexer = Lexer::new("1.0e1"); + lexer.lex().expect("failed to lex"); + assert_eq!(lexer.tokens[0].data, TokenData::NumericLiteral(10.0)); + } + + #[test] + fn test_negative_float_positive_scientific_notaion() { + let mut lexer = Lexer::new("-1.0e1"); + lexer.lex().expect("failed to lex"); + assert_eq!(lexer.tokens[0].data, TokenData::Punctuator(Punctuator::Sub)); + assert_eq!(lexer.tokens[1].data, TokenData::NumericLiteral(10.0)); + } + + #[test] + fn test_positive_float_negative_scientific_notaion() { + let mut lexer = Lexer::new("1.0e-1"); + lexer.lex().expect("failed to lex"); + assert_eq!(lexer.tokens[0].data, TokenData::NumericLiteral(0.1)); + } + + #[test] + fn test_negative_float_negative_scientific_notaion() { + let mut lexer = Lexer::new("-1.0e-1"); + lexer.lex().expect("failed to lex"); + assert_eq!(lexer.tokens[0].data, TokenData::Punctuator(Punctuator::Sub)); + assert_eq!(lexer.tokens[1].data, TokenData::NumericLiteral(0.1)); + } } From ec91d6cd86da972be8df85bde9ee611a7fd1b781 Mon Sep 17 00:00:00 2001 From: adumbidiot Date: Tue, 4 Feb 2020 19:43:01 -0800 Subject: [PATCH 2/2] Reorganize tests --- src/lib/syntax/lexer.rs | 38 +++++++------------------------------- 1 file changed, 7 insertions(+), 31 deletions(-) diff --git a/src/lib/syntax/lexer.rs b/src/lib/syntax/lexer.rs index aa857712e69..04ee239b821 100644 --- a/src/lib/syntax/lexer.rs +++ b/src/lib/syntax/lexer.rs @@ -1016,7 +1016,9 @@ mod tests { #[test] fn numbers() { - let mut lexer = Lexer::new("1 2 0x34 056 7.89 42. 5e3 5e+3 5e-3 0b10 0O123 0999"); + let mut lexer = Lexer::new( + "1 2 0x34 056 7.89 42. 5e3 5e+3 5e-3 0b10 0O123 0999 1.0e1 1.0e-1 1.0E1 1E1", + ); lexer.lex().expect("failed to lex"); assert_eq!(lexer.tokens[0].data, TokenData::NumericLiteral(1.0)); assert_eq!(lexer.tokens[1].data, TokenData::NumericLiteral(2.0)); @@ -1030,6 +1032,10 @@ mod tests { assert_eq!(lexer.tokens[9].data, TokenData::NumericLiteral(2.0)); assert_eq!(lexer.tokens[10].data, TokenData::NumericLiteral(83.0)); assert_eq!(lexer.tokens[11].data, TokenData::NumericLiteral(999.0)); + assert_eq!(lexer.tokens[12].data, TokenData::NumericLiteral(10.0)); + assert_eq!(lexer.tokens[13].data, TokenData::NumericLiteral(0.1)); + assert_eq!(lexer.tokens[14].data, TokenData::NumericLiteral(10.0)); + assert_eq!(lexer.tokens[14].data, TokenData::NumericLiteral(10.0)); } #[test] @@ -1122,34 +1128,4 @@ mod tests { TokenData::NumericLiteral(100_000_000_000.0) ); } - - #[test] - fn test_positive_float_positive_scientific_notaion() { - let mut lexer = Lexer::new("1.0e1"); - lexer.lex().expect("failed to lex"); - assert_eq!(lexer.tokens[0].data, TokenData::NumericLiteral(10.0)); - } - - #[test] - fn test_negative_float_positive_scientific_notaion() { - let mut lexer = Lexer::new("-1.0e1"); - lexer.lex().expect("failed to lex"); - assert_eq!(lexer.tokens[0].data, TokenData::Punctuator(Punctuator::Sub)); - assert_eq!(lexer.tokens[1].data, TokenData::NumericLiteral(10.0)); - } - - #[test] - fn test_positive_float_negative_scientific_notaion() { - let mut lexer = Lexer::new("1.0e-1"); - lexer.lex().expect("failed to lex"); - assert_eq!(lexer.tokens[0].data, TokenData::NumericLiteral(0.1)); - } - - #[test] - fn test_negative_float_negative_scientific_notaion() { - let mut lexer = Lexer::new("-1.0e-1"); - lexer.lex().expect("failed to lex"); - assert_eq!(lexer.tokens[0].data, TokenData::Punctuator(Punctuator::Sub)); - assert_eq!(lexer.tokens[1].data, TokenData::NumericLiteral(0.1)); - } }