Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of floats that start with a zero #272

Merged
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
20 changes: 10 additions & 10 deletions boa/src/syntax/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,17 +383,17 @@ impl<'a> Lexer<'a> {
return Ok(());
}
Some('x') | Some('X') => {
self.read_integer_in_base(16, buf)?
self.read_integer_in_base(16, buf)? as f64
}
Some('o') | Some('O') => {
self.read_integer_in_base(8, buf)?
self.read_integer_in_base(8, buf)? as f64
}
Some('b') | Some('B') => {
self.read_integer_in_base(2, buf)?
self.read_integer_in_base(2, buf)? as f64
}
Some(ch) if ch.is_ascii_digit() => {
Some(ch) if (ch.is_ascii_digit() || ch == '.') => {
// LEGACY OCTAL (ONLY FOR NON-STRICT MODE)
let mut gone_decimal = false;
let mut gone_decimal = ch == '.';
while let Some(next_ch) = self.preview_next() {
match next_ch {
c if next_ch.is_digit(8) => {
Expand All @@ -411,19 +411,19 @@ impl<'a> Lexer<'a> {
}
}
if gone_decimal {
u64::from_str(&buf).map_err(|_e| LexerError::new("Could not convert value to u64"))?
f64::from_str(&buf).map_err(|_e| LexerError::new("Could not convert value to f64"))?
} else if buf.is_empty() {
0
0.0
} else {
u64::from_str_radix(&buf, 8).map_err(|_e| LexerError::new("Could not convert value to u64"))?
(u64::from_str_radix(&buf, 8).map_err(|_e| LexerError::new("Could not convert value to u64"))?) as f64
}
}
Some(_) => {
0
0.0
}
};

self.push_token(TokenData::NumericLiteral(num as f64));
self.push_token(TokenData::NumericLiteral(num));

//11.8.3
if let Err(e) = self.check_after_numeric_literal() {
Expand Down
10 changes: 7 additions & 3 deletions boa/src/syntax/lexer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,10 @@ fn check_decrement_advances_lexer_2_places() {

#[test]
fn numbers() {
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");
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 0.0 0.12",
);

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));
Expand All @@ -401,7 +403,9 @@ fn numbers() {
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));
assert_eq!(lexer.tokens[15].data, TokenData::NumericLiteral(10.0));
assert_eq!(lexer.tokens[16].data, TokenData::NumericLiteral(0.0));
assert_eq!(lexer.tokens[17].data, TokenData::NumericLiteral(0.12));
}

#[test]
Expand Down