Skip to content

Commit 77855d5

Browse files
committed
feat(parse): permit number and bigint underscore separators
1 parent 53019eb commit 77855d5

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

packages/parse/src/quasi-json.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,16 @@ hex <- digit / [a-fA-F];
116116
117117
NUMBER <- < int frac? exp? > _WSN;
118118
119-
int <- [1-9] digit+
119+
int <- [1-9] digits
120120
/ digit
121-
/ MINUS digit
122-
/ MINUS [1-9] digit+;
121+
/ MINUS [1-9] digits
122+
/ MINUS digit;
123123
124124
digit <- [0-9];
125+
digits <- digit*;
125126
126-
frac <- '.' digit+;
127-
exp <- [Ee] [+\-]? digit+;
127+
frac <- '.' digits;
128+
exp <- [Ee] [+\-]? digits;
128129
129130
# _WSN is whitespace or a non-ident character.
130131
_WSN <- ~[$A-Za-z_] _WS ${_ => SKIP};

packages/parse/src/quasi-justin.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// * undefined.
1111
// * includes all floating point values: NaN, Infinity, -Infinity
1212
// * includes BigInt literals: 123n
13+
// * numbers and bigints can have optional underscores: 1_000_000n
1314

1415
// Justin also includes most pure JavaScript expressions. Justin does not
1516
// include function expressions or variable or function
@@ -82,8 +83,15 @@ const makeJustin = peg => {
8283
RIGHT_PAREN <- ")" _WS;
8384
STARSTAR <- "**" _WS;
8485
86+
# Allow optional underscore digit separators.
87+
digits <- super.digit ** ("_"?);
88+
NUMBER <- super.NUMBER ${ns => ns.replaceAll('_', '')};
89+
8590
# BigInts are not supported in JSON, but they are in Justin.
86-
bigintLiteral <- < int > "n" _WSN ${int => ['data', BigInt(int)]};
91+
bigintLiteral <- < int > "n" _WSN ${ns => [
92+
'data',
93+
BigInt(ns.replaceAll('_', '')),
94+
]};
8795
8896
# Define Javascript-style comments.
8997
_WS <- super._WS (EOL_COMMENT / MULTILINE_COMMENT)? ${_ => SKIP};

packages/parse/test/test-justin.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ test('data', t => {
1010
t.assert(val, message),
1111
);
1212
t.deepEqual(parse(`12345`), ast(0, 'data', 12345));
13+
t.deepEqual(parse(`12_345`), ast(0, 'data', 12345));
14+
t.deepEqual(parse('9898n'), ast(0, 'data', BigInt(9898)));
15+
t.deepEqual(parse('98_9_8n'), ast(0, 'data', BigInt(9898)));
1316
t.deepEqual(parse(`{}`), ast(0, 'record', []));
1417
t.deepEqual(parse(`[]`), ast(0, 'array', []));
1518
t.deepEqual(

0 commit comments

Comments
 (0)