Skip to content

Commit f3ba2c5

Browse files
committed
Add bigint support
1 parent e7d6fd1 commit f3ba2c5

File tree

6 files changed

+97
-3
lines changed

6 files changed

+97
-3
lines changed

LICENSE.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,32 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616

17+
## acorn-bigint
18+
19+
The code in the `lib/bigint` folder is compiled from code licensed as MIT:
20+
21+
> Copyright (C) 2017-2018 by Adrian Heine
22+
>
23+
> Permission is hereby granted, free of charge, to any person obtaining a copy
24+
> of this software and associated documentation files (the "Software"), to deal
25+
> in the Software without restriction, including without limitation the rights
26+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27+
> copies of the Software, and to permit persons to whom the Software is
28+
> furnished to do so, subject to the following conditions:
29+
>
30+
> The above copyright notice and this permission notice shall be included in
31+
> all copies or substantial portions of the Software.
32+
>
33+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39+
> THE SOFTWARE.
40+
41+
Find the source code at https://github.com/acornjs/acorn-bigint.
42+
1743
## acorn-import-meta
1844

1945
The code in the `lib/import-meta` folder is compiled from code licensed as MIT:

build.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ function compile (name, output) { // eslint-disable-line no-unused-vars
1717
fs.writeFileSync(path.join(__dirname, output), HEADER + result.code, 'utf8')
1818
}
1919

20+
compile('acorn-bigint', './lib/bigint/index.js')
2021
compile('acorn-import-meta', './lib/import-meta/index.js')
2122
compile('./lib/dynamic-import/source', './lib/dynamic-import/index.js')

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ var acorn = require('acorn')
22
var xtend = require('xtend')
33

44
var CJSParser = acorn.Parser
5-
var ESModulesParser = acorn.Parser
5+
.extend(require('./lib/bigint'))
6+
var ESModulesParser = CJSParser
67
.extend(require('./lib/dynamic-import'))
78
.extend(require('./lib/import-meta'))
89

lib/bigint/index.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* Generated by `npm run build`, do not edit! */
2+
3+
"use strict"
4+
5+
var acorn = require("acorn")
6+
var tt = acorn.tokTypes
7+
var isIdentifierStart = acorn.isIdentifierStart
8+
9+
module.exports = function(Parser) {
10+
return (function (Parser) {
11+
function anonymous () {
12+
Parser.apply(this, arguments);
13+
}
14+
15+
if ( Parser ) anonymous.__proto__ = Parser;
16+
anonymous.prototype = Object.create( Parser && Parser.prototype );
17+
anonymous.prototype.constructor = anonymous;
18+
19+
anonymous.prototype.parseLiteral = function parseLiteral (value) {
20+
var node = Parser.prototype.parseLiteral.call(this, value)
21+
if (node.raw.charCodeAt(node.raw.length - 1) == 110) { node.bigint = node.raw }
22+
return node
23+
};
24+
25+
anonymous.prototype.readRadixNumber = function readRadixNumber (radix) {
26+
var start = this.pos
27+
this.pos += 2 // 0x
28+
var val = this.readInt(radix)
29+
if (val === null) { this.raise(this.start + 2, ("Expected number in radix " + radix)) }
30+
if (this.input.charCodeAt(this.pos) == 110) {
31+
var str = this.input.slice(start, this.pos)
32+
val = typeof BigInt !== "undefined" ? BigInt(str) : null
33+
++this.pos
34+
} else if (isIdentifierStart(this.fullCharCodeAtPos())) { this.raise(this.pos, "Identifier directly after number") }
35+
return this.finishToken(tt.num, val)
36+
};
37+
38+
anonymous.prototype.readNumber = function readNumber (startsWithDot) {
39+
var start = this.pos
40+
41+
// Not an int
42+
if (startsWithDot) { return Parser.prototype.readNumber.call(this, startsWithDot) }
43+
44+
// Legacy octal
45+
if (this.input.charCodeAt(start) === 48 && this.input.charCodeAt(start + 1) !== 110) {
46+
return Parser.prototype.readNumber.call(this, startsWithDot)
47+
}
48+
49+
if (this.readInt(10) === null) { this.raise(start, "Invalid number") }
50+
51+
// Not a BigInt, reset and parse again
52+
if (this.input.charCodeAt(this.pos) != 110) {
53+
this.pos = start
54+
return Parser.prototype.readNumber.call(this, startsWithDot)
55+
}
56+
57+
var str = this.input.slice(start, this.pos)
58+
var val = typeof BigInt !== "undefined" && BigInt.parseInt ? BigInt.parseInt(str) : null
59+
++this.pos
60+
return this.finishToken(tt.num, val)
61+
};
62+
63+
return anonymous;
64+
}(Parser))
65+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"xtend": "^4.0.1"
1313
},
1414
"devDependencies": {
15+
"acorn-bigint": "^0.3.0",
1516
"acorn-import-meta": "^0.3.0",
1617
"buble": "^0.19.3",
1718
"mkdirp": "^0.5.1",

test/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test('parses object spread syntax', function (t) {
1717

1818
test('does not change main acorn module', function (t) {
1919
t.throws(function () {
20-
baseAcorn.parse('var a = { ...b }')
20+
baseAcorn.parse('var a = 10n')
2121
})
2222
t.end()
2323
})
@@ -66,7 +66,7 @@ test('supports optional catch', function (t) {
6666
t.end()
6767
})
6868

69-
test.skip('supports bigint', function (t) {
69+
test('supports bigint', function (t) {
7070
t.doesNotThrow(function () {
7171
acorn.parse('50n ** 50n')
7272
})

0 commit comments

Comments
 (0)