Stage 2: BigInt (#588)
* Stage 2: BigInt * Change plugin name to bigInt (camelcase). * Update based on PR review, add test cases. * Use hex for charCodes.
This commit is contained in:
parent
fecdb6feeb
commit
baa5f4dca7
@ -144,6 +144,7 @@ require("babylon").parse("code", {
|
||||
- `numericSeparator` ([proposal](https://github.com/samuelgoto/proposal-numeric-separator))
|
||||
- `optionalChaining` ([proposal](https://github.com/tc39/proposal-optional-chaining))
|
||||
- `importMeta` ([proposal](https://github.com/tc39/proposal-import-meta))
|
||||
- `bigInt` ([proposal](https://github.com/tc39/proposal-bigint))
|
||||
|
||||
### FAQ
|
||||
|
||||
|
||||
@ -554,6 +554,9 @@ export default class ExpressionParser extends LValParser {
|
||||
case tt.num:
|
||||
return this.parseLiteral(this.state.value, "NumericLiteral");
|
||||
|
||||
case tt.bigint:
|
||||
return this.parseLiteral(this.state.value, "BigIntLiteral");
|
||||
|
||||
case tt.string:
|
||||
return this.parseLiteral(this.state.value, "StringLiteral");
|
||||
|
||||
|
||||
@ -631,10 +631,27 @@ export default class Tokenizer extends LocationParser {
|
||||
}
|
||||
|
||||
readRadixNumber(radix: number): void {
|
||||
const start = this.state.pos;
|
||||
let isBigInt = false;
|
||||
|
||||
this.state.pos += 2; // 0x
|
||||
const val = this.readInt(radix);
|
||||
if (val == null) this.raise(this.state.start + 2, "Expected number in radix " + radix);
|
||||
|
||||
if (this.hasPlugin("bigInt")) {
|
||||
if (this.input.charCodeAt(this.state.pos) === 0x6E) { // 'n'
|
||||
++this.state.pos;
|
||||
isBigInt = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (isIdentifierStart(this.fullCharCodeAtPos())) this.raise(this.state.pos, "Identifier directly after number");
|
||||
|
||||
if (isBigInt) {
|
||||
const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, "");
|
||||
return this.finishToken(tt.bigint, str);
|
||||
}
|
||||
|
||||
return this.finishToken(tt.num, val);
|
||||
}
|
||||
|
||||
@ -642,30 +659,47 @@ export default class Tokenizer extends LocationParser {
|
||||
|
||||
readNumber(startsWithDot: boolean): void {
|
||||
const start = this.state.pos;
|
||||
let octal = this.input.charCodeAt(start) === 48; // '0'
|
||||
let octal = this.input.charCodeAt(start) === 0x30; // '0'
|
||||
let isFloat = false;
|
||||
let isBigInt = false;
|
||||
|
||||
if (!startsWithDot && this.readInt(10) === null) this.raise(start, "Invalid number");
|
||||
if (octal && this.state.pos == start + 1) octal = false; // number === 0
|
||||
|
||||
let next = this.input.charCodeAt(this.state.pos);
|
||||
if (next === 46 && !octal) { // '.'
|
||||
if (next === 0x2E && !octal) { // '.'
|
||||
++this.state.pos;
|
||||
this.readInt(10);
|
||||
isFloat = true;
|
||||
next = this.input.charCodeAt(this.state.pos);
|
||||
}
|
||||
|
||||
if ((next === 69 || next === 101) && !octal) { // 'eE'
|
||||
if ((next === 0x45 || next === 0x65) && !octal) { // 'Ee'
|
||||
next = this.input.charCodeAt(++this.state.pos);
|
||||
if (next === 43 || next === 45) ++this.state.pos; // '+-'
|
||||
if (next === 0x2B || next === 0x2D) ++this.state.pos; // '+-'
|
||||
if (this.readInt(10) === null) this.raise(start, "Invalid number");
|
||||
isFloat = true;
|
||||
next = this.input.charCodeAt(this.state.pos);
|
||||
}
|
||||
|
||||
if (this.hasPlugin("bigInt")) {
|
||||
if (next === 0x6E) { // 'n'
|
||||
// disallow floats and legacy octal syntax, new style octal ("0o") is handled in this.readRadixNumber
|
||||
if (isFloat || octal) this.raise(start, "Invalid BigIntLiteral");
|
||||
++this.state.pos;
|
||||
isBigInt = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (isIdentifierStart(this.fullCharCodeAtPos())) this.raise(this.state.pos, "Identifier directly after number");
|
||||
|
||||
const str = this.input.slice(start, this.state.pos).replace(/_/g, "");
|
||||
// remove "_" for numeric literal separator, and "n" for BigInts
|
||||
const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, "");
|
||||
|
||||
if (isBigInt) {
|
||||
return this.finishToken(tt.bigint, str);
|
||||
}
|
||||
|
||||
let val;
|
||||
if (isFloat) {
|
||||
val = parseFloat(str);
|
||||
|
||||
@ -82,6 +82,7 @@ export class BinopTokenType extends TokenType {
|
||||
|
||||
export const types: { [name: string]: TokenType } = {
|
||||
num: new TokenType("num", { startsExpr }),
|
||||
bigint: new TokenType("bigint", { startsExpr }),
|
||||
regexp: new TokenType("regexp", { startsExpr }),
|
||||
string: new TokenType("string", { startsExpr }),
|
||||
name: new TokenType("name", { startsExpr }),
|
||||
|
||||
@ -96,6 +96,11 @@ export type NumericLiteral = NodeBase & {
|
||||
value: number;
|
||||
};
|
||||
|
||||
export type BigIntLiteral = NodeBase & {
|
||||
type: "BigIntLiteral";
|
||||
value: number;
|
||||
}
|
||||
|
||||
// Programs
|
||||
|
||||
export type BlockStatementLike = Program | BlockStatement;
|
||||
|
||||
1
test/fixtures/experimental/bigint/invalid-decimal/actual.js
vendored
Normal file
1
test/fixtures/experimental/bigint/invalid-decimal/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
1.0n
|
||||
1
test/fixtures/experimental/bigint/invalid-decimal/options.json
vendored
Normal file
1
test/fixtures/experimental/bigint/invalid-decimal/options.json
vendored
Normal file
@ -0,0 +1 @@
|
||||
{ "throws": "Invalid BigIntLiteral (1:0)" }
|
||||
1
test/fixtures/experimental/bigint/invalid-e/actual.js
vendored
Normal file
1
test/fixtures/experimental/bigint/invalid-e/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
2e9n
|
||||
1
test/fixtures/experimental/bigint/invalid-e/options.json
vendored
Normal file
1
test/fixtures/experimental/bigint/invalid-e/options.json
vendored
Normal file
@ -0,0 +1 @@
|
||||
{ "throws": "Invalid BigIntLiteral (1:0)" }
|
||||
1
test/fixtures/experimental/bigint/invalid-octal-legacy/actual.js
vendored
Normal file
1
test/fixtures/experimental/bigint/invalid-octal-legacy/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
016432n
|
||||
1
test/fixtures/experimental/bigint/invalid-octal-legacy/options.json
vendored
Normal file
1
test/fixtures/experimental/bigint/invalid-octal-legacy/options.json
vendored
Normal file
@ -0,0 +1 @@
|
||||
{ "throws": "Invalid BigIntLiteral (1:0)" }
|
||||
3
test/fixtures/experimental/bigint/options.json
vendored
Normal file
3
test/fixtures/experimental/bigint/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["bigInt"]
|
||||
}
|
||||
1
test/fixtures/experimental/bigint/valid-binary/actual.js
vendored
Normal file
1
test/fixtures/experimental/bigint/valid-binary/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
0b101011101n
|
||||
69
test/fixtures/experimental/bigint/valid-binary/expected.json
vendored
Normal file
69
test/fixtures/experimental/bigint/valid-binary/expected.json
vendored
Normal file
@ -0,0 +1,69 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 12,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 12
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 12,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 12
|
||||
}
|
||||
},
|
||||
"sourceType": "script",
|
||||
"body": [
|
||||
{
|
||||
"type": "ExpressionStatement",
|
||||
"start": 0,
|
||||
"end": 12,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 12
|
||||
}
|
||||
},
|
||||
"expression": {
|
||||
"type": "BigIntLiteral",
|
||||
"start": 0,
|
||||
"end": 12,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 12
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"rawValue": "0b101011101",
|
||||
"raw": "0b101011101n"
|
||||
},
|
||||
"value": "0b101011101"
|
||||
}
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
1
test/fixtures/experimental/bigint/valid-hex/actual.js
vendored
Normal file
1
test/fixtures/experimental/bigint/valid-hex/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
0xFFF123n
|
||||
69
test/fixtures/experimental/bigint/valid-hex/expected.json
vendored
Normal file
69
test/fixtures/experimental/bigint/valid-hex/expected.json
vendored
Normal file
@ -0,0 +1,69 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 9,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 9
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 9,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 9
|
||||
}
|
||||
},
|
||||
"sourceType": "script",
|
||||
"body": [
|
||||
{
|
||||
"type": "ExpressionStatement",
|
||||
"start": 0,
|
||||
"end": 9,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 9
|
||||
}
|
||||
},
|
||||
"expression": {
|
||||
"type": "BigIntLiteral",
|
||||
"start": 0,
|
||||
"end": 9,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 9
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"rawValue": "0xFFF123",
|
||||
"raw": "0xFFF123n"
|
||||
},
|
||||
"value": "0xFFF123"
|
||||
}
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
1
test/fixtures/experimental/bigint/valid-large/actual.js
vendored
Normal file
1
test/fixtures/experimental/bigint/valid-large/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
9223372036854775807n
|
||||
69
test/fixtures/experimental/bigint/valid-large/expected.json
vendored
Normal file
69
test/fixtures/experimental/bigint/valid-large/expected.json
vendored
Normal file
@ -0,0 +1,69 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 20,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 20
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 20,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 20
|
||||
}
|
||||
},
|
||||
"sourceType": "script",
|
||||
"body": [
|
||||
{
|
||||
"type": "ExpressionStatement",
|
||||
"start": 0,
|
||||
"end": 20,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 20
|
||||
}
|
||||
},
|
||||
"expression": {
|
||||
"type": "BigIntLiteral",
|
||||
"start": 0,
|
||||
"end": 20,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 20
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"rawValue": "9223372036854775807",
|
||||
"raw": "9223372036854775807n"
|
||||
},
|
||||
"value": "9223372036854775807"
|
||||
}
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
1
test/fixtures/experimental/bigint/valid-octal-new/actual.js
vendored
Normal file
1
test/fixtures/experimental/bigint/valid-octal-new/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
0o16432n
|
||||
69
test/fixtures/experimental/bigint/valid-octal-new/expected.json
vendored
Normal file
69
test/fixtures/experimental/bigint/valid-octal-new/expected.json
vendored
Normal file
@ -0,0 +1,69 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 8,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 8
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 8,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 8
|
||||
}
|
||||
},
|
||||
"sourceType": "script",
|
||||
"body": [
|
||||
{
|
||||
"type": "ExpressionStatement",
|
||||
"start": 0,
|
||||
"end": 8,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 8
|
||||
}
|
||||
},
|
||||
"expression": {
|
||||
"type": "BigIntLiteral",
|
||||
"start": 0,
|
||||
"end": 8,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 8
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"rawValue": "0o16432",
|
||||
"raw": "0o16432n"
|
||||
},
|
||||
"value": "0o16432"
|
||||
}
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
1
test/fixtures/experimental/bigint/valid-small/actual.js
vendored
Normal file
1
test/fixtures/experimental/bigint/valid-small/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
100n
|
||||
69
test/fixtures/experimental/bigint/valid-small/expected.json
vendored
Normal file
69
test/fixtures/experimental/bigint/valid-small/expected.json
vendored
Normal file
@ -0,0 +1,69 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 4,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 4
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 4,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 4
|
||||
}
|
||||
},
|
||||
"sourceType": "script",
|
||||
"body": [
|
||||
{
|
||||
"type": "ExpressionStatement",
|
||||
"start": 0,
|
||||
"end": 4,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 4
|
||||
}
|
||||
},
|
||||
"expression": {
|
||||
"type": "BigIntLiteral",
|
||||
"start": 0,
|
||||
"end": 4,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 4
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"rawValue": "100",
|
||||
"raw": "100n"
|
||||
},
|
||||
"value": "100"
|
||||
}
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user