-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
json.ts
134 lines (105 loc) · 2.84 KB
/
json.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// https://www.json.org/json-en.html
import { createLexer, Token } from 'leac';
import { inspect } from 'util';
import * as p from '../src/core';
// Types
type JsonValue = null | true | false | number | string | JsonValue[] | JsonObject;
type JsonObject = { [name: string]: JsonValue };
type ObjectMember = { name: string; value: JsonValue; };
// Lexer / tokenizer
const lex = createLexer([
{ name: '{' },
{ name: '}' },
{ name: '[' },
{ name: ']' },
{ name: ',' },
{ name: ':' },
{ name: 'null' },
{ name: 'true' },
{ name: 'false' },
{
name: 'space',
regex: /\s+/,
discard: true
},
{ name: 'number', regex: /-?(?:[0-9]|[1-9][0-9]+)(?:\.[0-9]+)?(?:[eE][-+]?[0-9]+)?\b/ },
{ name: 'string', regex: /"(?:\\["bfnrt/\\]|\\u[a-fA-F0-9]{4}|[^"\\])*"/ }
]);
// Build up the JSON parser from smaller, simpler parsers
function literal (name: string): p.Parser<Token,unknown,true> {
return p.token((t) => t.name === name ? true : undefined);
}
const null_: p.Parser<Token,unknown,null>
= p.token((t) => t.name === 'null' ? null : undefined);
const true_: p.Parser<Token,unknown,true>
= p.token((t) => t.name === 'true' ? true : undefined);
const false_: p.Parser<Token,unknown,false>
= p.token((t) => t.name === 'false' ? false : undefined);
const number_: p.Parser<Token,unknown,number>
= p.token((t) => t.name === 'number' ? parseFloat(t.text) : undefined);
const string_: p.Parser<Token,unknown,string>
= p.token((t) => t.name === 'string' ? JSON.parse(t.text) as string : undefined);
const array_ = p.middle(
literal('['),
p.sepBy(
p.recursive(() => value_),
literal(',')
),
literal(']')
);
const member_: p.Parser<Token,unknown,ObjectMember> = p.ab(
string_,
p.right(
literal(':'),
p.recursive(() => value_)
),
(name,value) => ({ name: name, value: value })
);
const object_ = p.middle(
literal('{'),
p.map(
p.sepBy(
member_,
literal(',')
),
(pairs) => pairs.reduce(
(acc, pair) => { acc[pair.name] = pair.value; return acc; },
{} as JsonObject
)
),
literal('}')
);
const value_: p.Parser<Token,unknown,JsonValue> = p.choice(
null_ as p.Parser<Token,unknown,JsonValue>,
true_,
false_,
number_,
string_,
array_,
object_
);
// Complete parser
function parseJson (json: string): JsonValue {
const lexerResult = lex(json);
if (!lexerResult.complete) {
console.warn(
`Input string was only partially tokenized, stopped at offset ${lexerResult.offset}!`
);
}
return p.parse(value_, lexerResult.tokens, {});
}
// Usage
console.log(inspect(parseJson(`"Hello World!"`)));
console.log(inspect(parseJson(`
{
"foo": true, "bar": "baz",
"qux": [1, 2, [3], [ ], { "quux": "quuz" }]
}
`), { breakLength: 50 }));
// Output
// 'Hello World!'
// {
// foo: true,
// bar: 'baz',
// qux: [ 1, 2, [ 3 ], [], { quux: 'quuz' } ]
// }