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:
William Horton
2017-06-27 23:35:57 -04:00
committed by Henry Zhu
parent fecdb6feeb
commit baa5f4dca7
22 changed files with 408 additions and 5 deletions

View File

@@ -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");

View File

@@ -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);

View File

@@ -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 }),

View File

@@ -96,6 +96,11 @@ export type NumericLiteral = NodeBase & {
value: number;
};
export type BigIntLiteral = NodeBase & {
type: "BigIntLiteral";
value: number;
}
// Programs
export type BlockStatementLike = Program | BlockStatement;