UNPKG

1.72 kBJavaScriptView Raw
1"use strict"
2
3const acorn = require("acorn")
4const tt = acorn.tokTypes
5const isIdentifierStart = acorn.isIdentifierStart
6
7module.exports = function(Parser) {
8 return class extends Parser {
9 parseLiteral(value) {
10 const node = super.parseLiteral(value)
11 if (node.raw.charCodeAt(node.raw.length - 1) == 110) node.bigint = node.raw
12 return node
13 }
14
15 readRadixNumber(radix) {
16 let start = this.pos
17 this.pos += 2 // 0x
18 let val = this.readInt(radix)
19 if (val === null) this.raise(this.start + 2, `Expected number in radix ${radix}`)
20 if (this.input.charCodeAt(this.pos) == 110) {
21 let str = this.input.slice(start, this.pos)
22 val = typeof BigInt !== "undefined" ? BigInt(str) : null
23 ++this.pos
24 } else if (isIdentifierStart(this.fullCharCodeAtPos())) this.raise(this.pos, "Identifier directly after number")
25 return this.finishToken(tt.num, val)
26 }
27
28 readNumber(startsWithDot) {
29 let start = this.pos
30
31 // Not an int
32 if (startsWithDot) return super.readNumber(startsWithDot)
33
34 // Legacy octal
35 if (this.input.charCodeAt(start) === 48 && this.input.charCodeAt(start + 1) !== 110) {
36 return super.readNumber(startsWithDot)
37 }
38
39 if (this.readInt(10) === null) this.raise(start, "Invalid number")
40
41 // Not a BigInt, reset and parse again
42 if (this.input.charCodeAt(this.pos) != 110) {
43 this.pos = start
44 return super.readNumber(startsWithDot)
45 }
46
47 let str = this.input.slice(start, this.pos)
48 let val = typeof BigInt !== "undefined" && BigInt.parseInt ? BigInt.parseInt(str) : null
49 ++this.pos
50 return this.finishToken(tt.num, val)
51 }
52 }
53}