// ## Parser utilities
-var literal = /^(?:'((?:\\.|[^'])*?)'|"((?:\\.|[^"])*?)")/;
+var literal = /^(?:'((?:\\.|[^'\\])*?)'|"((?:\\.|[^"\\])*?)")/;
pp.strictDirective = function(start) {
for (;;) {
// Try to find string literal.
var node = this.startNode();
node.value = value;
node.raw = this.input.slice(this.start, this.end);
- if (node.raw.charCodeAt(node.raw.length - 1) === 110) { node.bigint = node.raw.slice(0, -1); }
+ if (node.raw.charCodeAt(node.raw.length - 1) === 110) { node.bigint = node.raw.slice(0, -1).replace(/_/g, ""); }
this.next();
return this.finishNode(node, "Literal")
};
pp$9.readToken_pipe_amp = function(code) { // '|&'
var next = this.input.charCodeAt(this.pos + 1);
- if (next === code) { return this.finishOp(code === 124 ? types.logicalOR : types.logicalAND, 2) }
+ if (next === code) {
+ if (this.options.ecmaVersion >= 12) {
+ var next2 = this.input.charCodeAt(this.pos + 2);
+ if (next2 === 61) { return this.finishOp(types.assign, 3) }
+ }
+ return this.finishOp(code === 124 ? types.logicalOR : types.logicalAND, 2)
+ }
if (next === 61) { return this.finishOp(types.assign, 2) }
return this.finishOp(code === 124 ? types.bitwiseOR : types.bitwiseAND, 1)
};
};
pp$9.readToken_question = function() { // '?'
- if (this.options.ecmaVersion >= 11) {
+ var ecmaVersion = this.options.ecmaVersion;
+ if (ecmaVersion >= 11) {
var next = this.input.charCodeAt(this.pos + 1);
if (next === 46) {
var next2 = this.input.charCodeAt(this.pos + 2);
if (next2 < 48 || next2 > 57) { return this.finishOp(types.questionDot, 2) }
}
- if (next === 63) { return this.finishOp(types.coalesce, 2) }
+ if (next === 63) {
+ if (ecmaVersion >= 12) {
+ var next2$1 = this.input.charCodeAt(this.pos + 2);
+ if (next2$1 === 61) { return this.finishOp(types.assign, 3) }
+ }
+ return this.finishOp(types.coalesce, 2)
+ }
}
return this.finishOp(types.question, 1)
};
// were read, the integer value otherwise. When `len` is given, this
// will return `null` unless the integer has exactly `len` digits.
-pp$9.readInt = function(radix, len) {
- var start = this.pos, total = 0;
- for (var i = 0, e = len == null ? Infinity : len; i < e; ++i) {
+pp$9.readInt = function(radix, len, maybeLegacyOctalNumericLiteral) {
+ // `len` is used for character escape sequences. In that case, disallow separators.
+ var allowSeparators = this.options.ecmaVersion >= 12 && len === undefined;
+
+ // `maybeLegacyOctalNumericLiteral` is true if it doesn't have prefix (0x,0o,0b)
+ // and isn't fraction part nor exponent part. In that case, if the first digit
+ // is zero then disallow separators.
+ var isLegacyOctalNumericLiteral = maybeLegacyOctalNumericLiteral && this.input.charCodeAt(this.pos) === 48;
+
+ var start = this.pos, total = 0, lastCode = 0;
+ for (var i = 0, e = len == null ? Infinity : len; i < e; ++i, ++this.pos) {
var code = this.input.charCodeAt(this.pos), val = (void 0);
+
+ if (allowSeparators && code === 95) {
+ if (isLegacyOctalNumericLiteral) { this.raiseRecoverable(this.pos, "Numeric separator is not allowed in legacy octal numeric literals"); }
+ if (lastCode === 95) { this.raiseRecoverable(this.pos, "Numeric separator must be exactly one underscore"); }
+ if (i === 0) { this.raiseRecoverable(this.pos, "Numeric separator is not allowed at the first of digits"); }
+ lastCode = code;
+ continue
+ }
+
if (code >= 97) { val = code - 97 + 10; } // a
else if (code >= 65) { val = code - 65 + 10; } // A
else if (code >= 48 && code <= 57) { val = code - 48; } // 0-9
else { val = Infinity; }
if (val >= radix) { break }
- ++this.pos;
+ lastCode = code;
total = total * radix + val;
}
+
+ if (allowSeparators && lastCode === 95) { this.raiseRecoverable(this.pos - 1, "Numeric separator is not allowed at the last of digits"); }
if (this.pos === start || len != null && this.pos - start !== len) { return null }
return total
};
+function stringToNumber(str, isLegacyOctalNumericLiteral) {
+ if (isLegacyOctalNumericLiteral) {
+ return parseInt(str, 8)
+ }
+
+ // `parseFloat(value)` stops parsing at the first numeric separator then returns a wrong value.
+ return parseFloat(str.replace(/_/g, ""))
+}
+
+function stringToBigInt(str) {
+ if (typeof BigInt !== "function") {
+ return null
+ }
+
+ // `BigInt(value)` throws syntax error if the string contains numeric separators.
+ return BigInt(str.replace(/_/g, ""))
+}
+
pp$9.readRadixNumber = function(radix) {
var start = this.pos;
this.pos += 2; // 0x
var val = this.readInt(radix);
if (val == null) { this.raise(this.start + 2, "Expected number in radix " + radix); }
if (this.options.ecmaVersion >= 11 && this.input.charCodeAt(this.pos) === 110) {
- val = typeof BigInt !== "undefined" ? BigInt(this.input.slice(start, this.pos)) : null;
+ val = stringToBigInt(this.input.slice(start, this.pos));
++this.pos;
} else if (isIdentifierStart(this.fullCharCodeAtPos())) { this.raise(this.pos, "Identifier directly after number"); }
return this.finishToken(types.num, val)
pp$9.readNumber = function(startsWithDot) {
var start = this.pos;
- if (!startsWithDot && this.readInt(10) === null) { this.raise(start, "Invalid number"); }
+ if (!startsWithDot && this.readInt(10, undefined, true) === null) { this.raise(start, "Invalid number"); }
var octal = this.pos - start >= 2 && this.input.charCodeAt(start) === 48;
if (octal && this.strict) { this.raise(start, "Invalid number"); }
var next = this.input.charCodeAt(this.pos);
if (!octal && !startsWithDot && this.options.ecmaVersion >= 11 && next === 110) {
- var str$1 = this.input.slice(start, this.pos);
- var val$1 = typeof BigInt !== "undefined" ? BigInt(str$1) : null;
+ var val$1 = stringToBigInt(this.input.slice(start, this.pos));
++this.pos;
if (isIdentifierStart(this.fullCharCodeAtPos())) { this.raise(this.pos, "Identifier directly after number"); }
return this.finishToken(types.num, val$1)
}
if (isIdentifierStart(this.fullCharCodeAtPos())) { this.raise(this.pos, "Identifier directly after number"); }
- var str = this.input.slice(start, this.pos);
- var val = octal ? parseInt(str, 8) : parseFloat(str);
+ var val = stringToNumber(this.input.slice(start, this.pos), octal);
return this.finishToken(types.num, val)
};
// Acorn is a tiny, fast JavaScript parser written in JavaScript.
-var version = "7.3.1";
+var version = "7.4.1";
Parser.acorn = {
Parser: Parser,