fix(es/parser): Fix lexing of numbers (#1821)

swc_ecma_parser:
 - Fix lexing of long numeric literals.
This commit is contained in:
강동윤 2021-06-12 17:47:13 +09:00 committed by GitHub
parent d3944f5203
commit 001af8637d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 138 additions and 7 deletions

View File

@ -7,7 +7,7 @@ include = ["Cargo.toml", "src/**/*.rs", "examples/**/*.rs"]
license = "Apache-2.0/MIT"
name = "swc_ecma_parser"
repository = "https://github.com/swc-project/swc.git"
version = "0.59.0"
version = "0.59.1"
[features]
default = []

View File

@ -211,7 +211,7 @@ impl<'a, I: Input> Lexer<'a, I> {
let mut raw = Raw(Some(String::new()));
let val = self.read_digits(
self.read_digits(
radix,
|total, radix, v| {
read_any = true;
@ -230,9 +230,11 @@ impl<'a, I: Input> Lexer<'a, I> {
self.error(start, SyntaxError::ExpectedDigit { radix })?;
}
let raw_str = raw.0.take().unwrap();
Ok((
val,
BigIntValue::parse_bytes(&raw.0.take().unwrap().as_bytes(), radix as _)
lexical::parse_radix(raw_str.as_bytes(), radix as _)
.expect("failed to parse float using lexical"),
BigIntValue::parse_bytes(raw_str.as_bytes(), radix as _)
.expect("failed to parse string as a bigint"),
non_octal,
))
@ -454,7 +456,6 @@ mod tests {
}
#[test]
#[ignore]
fn num_big_many_zero() {
assert_eq!(
1_000_000_000_000_000_000_000_000_000_000f64,
@ -517,6 +518,17 @@ mod tests {
);
}
#[test]
fn large_float() {
const LONG: &str =
"0B11111111111111111111111111111111111111111111111101001010100000010111110001111111111";
assert_eq!(
lex(LONG, |l| l.read_radix_number(2).unwrap().left().unwrap()),
9.671406556917009e+24
);
}
/// Valid even on strict mode.
const VALID_CASES: &[&str] = &[".0", "0.e-1", "0e8", ".8e1", "0.8e1", "1.18e1"];
const INVALID_CASES_ON_STRICT: &[&str] = &["08e1", "08.1", "08.8e1", "08", "01"];

View File

@ -137,7 +137,7 @@ fn spec(file: PathBuf) {
};
// We are not debugging f64 parsing of serde.
if file_name.contains("issue-1803") {
if file_name.contains("issue-1803") || file_name.contains("stc") {
return Ok(());
}

View File

@ -20,7 +20,7 @@
"end": 33,
"ctxt": 0
},
"value": 1.0000002000000003e32
"value": 1.0000002e32
}
}
],

View File

@ -0,0 +1,4 @@
var obj2 = {
0B11111111111111111111111111111111111111111111111101001010100000010111110001111111111: false,
}
obj2["9.671406556917009e+24"]; // boolean

View File

@ -0,0 +1,115 @@
{
"type": "Script",
"span": {
"start": 0,
"end": 143,
"ctxt": 0
},
"body": [
{
"type": "VariableDeclaration",
"span": {
"start": 0,
"end": 112,
"ctxt": 0
},
"kind": "var",
"declare": false,
"declarations": [
{
"type": "VariableDeclarator",
"span": {
"start": 4,
"end": 112,
"ctxt": 0
},
"id": {
"type": "Identifier",
"span": {
"start": 4,
"end": 8,
"ctxt": 0
},
"value": "obj2",
"optional": false,
"typeAnnotation": null
},
"init": {
"type": "ObjectExpression",
"span": {
"start": 11,
"end": 112,
"ctxt": 0
},
"properties": [
{
"type": "KeyValueProperty",
"key": {
"type": "NumericLiteral",
"span": {
"start": 17,
"end": 102,
"ctxt": 0
},
"value": 9.671406556917009e24
},
"value": {
"type": "BooleanLiteral",
"span": {
"start": 104,
"end": 109,
"ctxt": 0
},
"value": false
}
}
]
},
"definite": false
}
]
},
{
"type": "ExpressionStatement",
"span": {
"start": 113,
"end": 143,
"ctxt": 0
},
"expression": {
"type": "MemberExpression",
"span": {
"start": 113,
"end": 142,
"ctxt": 0
},
"object": {
"type": "Identifier",
"span": {
"start": 113,
"end": 117,
"ctxt": 0
},
"value": "obj2",
"optional": false
},
"property": {
"type": "StringLiteral",
"span": {
"start": 118,
"end": 141,
"ctxt": 0
},
"value": "9.671406556917009e+24",
"hasEscape": false,
"kind": {
"type": "normal",
"containsQuote": true
}
},
"computed": true
}
}
],
"interpreter": null
}