Add decimal parsing support (#11640)
* docs: add DecimalLiteral to AST spec * add decimal support * fix: throw invalid decimal on start * add DecimalLiteral type definitions * update parser typings * add generator support * add syntax-decimal plugin * Add syntax-decimal to babel-standalone * add syntax-decimal to missing plugin helpers * fix incorrect test macro
This commit is contained in:
@@ -1095,6 +1095,8 @@ export default class Tokenizer extends ParserErrors {
|
||||
if (next === charCodes.lowercaseN) {
|
||||
++this.state.pos;
|
||||
isBigInt = true;
|
||||
} else if (next === charCodes.lowercaseM) {
|
||||
throw this.raise(start, Errors.InvalidDecimal);
|
||||
}
|
||||
|
||||
if (isIdentifierStart(this.input.codePointAt(this.state.pos))) {
|
||||
@@ -1116,6 +1118,8 @@ export default class Tokenizer extends ParserErrors {
|
||||
const start = this.state.pos;
|
||||
let isFloat = false;
|
||||
let isBigInt = false;
|
||||
let isDecimal = false;
|
||||
let hasExponent = false;
|
||||
let isOctal = false;
|
||||
|
||||
if (!startsWithDot && this.readInt(10) === null) {
|
||||
@@ -1157,6 +1161,7 @@ export default class Tokenizer extends ParserErrors {
|
||||
}
|
||||
if (this.readInt(10) === null) this.raise(start, Errors.InvalidNumber);
|
||||
isFloat = true;
|
||||
hasExponent = true;
|
||||
next = this.input.charCodeAt(this.state.pos);
|
||||
}
|
||||
|
||||
@@ -1174,18 +1179,32 @@ export default class Tokenizer extends ParserErrors {
|
||||
isBigInt = true;
|
||||
}
|
||||
|
||||
if (next === charCodes.lowercaseM) {
|
||||
this.expectPlugin("decimal", this.state.pos);
|
||||
if (hasExponent || hasLeadingZero) {
|
||||
this.raise(start, Errors.InvalidDecimal);
|
||||
}
|
||||
++this.state.pos;
|
||||
isDecimal = true;
|
||||
}
|
||||
|
||||
if (isIdentifierStart(this.input.codePointAt(this.state.pos))) {
|
||||
throw this.raise(this.state.pos, Errors.NumberIdentifier);
|
||||
}
|
||||
|
||||
// remove "_" for numeric literal separator, and "n" for BigInts
|
||||
const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, "");
|
||||
// remove "_" for numeric literal separator, and trailing `m` or `n`
|
||||
const str = this.input.slice(start, this.state.pos).replace(/[_mn]/g, "");
|
||||
|
||||
if (isBigInt) {
|
||||
this.finishToken(tt.bigint, str);
|
||||
return;
|
||||
}
|
||||
|
||||
if (isDecimal) {
|
||||
this.finishToken(tt.decimal, str);
|
||||
return;
|
||||
}
|
||||
|
||||
const val = isOctal ? parseInt(str, 8) : parseFloat(str);
|
||||
this.finishToken(tt.num, val);
|
||||
}
|
||||
|
||||
@@ -86,6 +86,7 @@ function createBinop(name: string, binop: number) {
|
||||
export const types: { [name: string]: TokenType } = {
|
||||
num: new TokenType("num", { startsExpr }),
|
||||
bigint: new TokenType("bigint", { startsExpr }),
|
||||
decimal: new TokenType("decimal", { startsExpr }),
|
||||
regexp: new TokenType("regexp", { startsExpr }),
|
||||
string: new TokenType("string", { startsExpr }),
|
||||
name: new TokenType("name", { startsExpr }),
|
||||
|
||||
Reference in New Issue
Block a user