-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjson-like.js
352 lines (331 loc) · 10.2 KB
/
json-like.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// *123
function Ref(id) {
this.id = id;
}
exports.Ref = Ref;
// ^123
function HeapRef(id) {
this.id = id;
}
exports.HeapRef = HeapRef;
function parse(input) {
const refDict = {};
let digit = characterMatching("0123456789");
let digits = oneOrMore(digit);
let oneNine = characterMatching("123456789");
let integer = choice([
sequence([literal("-"), oneNine, digits], (data) => "-" + data[1] + data[2]),
sequence([literal("-"), digit], (data) => "-" + data[1]),
sequence([oneNine, digits], (data) => data[0] + data[1]),
digit
], (data) => Number(data.join("")));
let sign = choice([literal("-"), nothing("")]);
let exponent = choice([
sequence([characterMatching("eE"), sign, digits], (data) => "e" + data[1] + data[2]),
nothing("")
]);
let fraction = choice([
sequence([literal("."), digits], (data) => "." + data[1]),
nothing("")
]);
let number = sequence([integer, fraction, exponent], (data) => Number(data.join("")));
let ws = zeroOrMore(characterMatching("\u0020\u000A\u000D\u0009"));
let hex = characterMatching("0123456789abcdefABCDEF");
let escape = choice([characterMatching('"\\\/bfnrt'), sequence([literal("u"), hex, hex, hex, hex])]);
let character = choice([sequence([literal("\\"), escape], (values) => values[1]), characterMatching((c) => c !== '"' && c !== '\\')]);
let characters = zeroOrMore(character);
let string = sequence([literal('"'), characters, literal('"')], (data) => {
return data[1].join("");
});
let identifier = sequence([characterMatching(/[a-zA-Z_]/), zeroOrMore(characterMatching(/[a-zA-Z_0-9.]/))],
(data) => data[0] + data[1].join(""));
let tag = sequence([literal("<"), identifier, literal(">")], (data) => data[1]);
let nonEmptyArray = sequence(() => [zeroOrOne(tag), literal("["), elements, literal("]")], (data) => {
if (data[0] === "[") {
// no tag
return data[1];
} else {
// has tag
const array = data[2];
array.__tag__ = data[0][0];
return array;
}
});
let emptyArray = sequence([zeroOrOne(tag), literal("["), ws, literal("]")], (data) => {
let array = [];
if (data[0] !== "[") {
// has tag
array.__tag__ = data[0][0];
}
return array;
});
let array = choice([emptyArray, nonEmptyArray], (data) => data[0]);
let value = choice(() => [
array,
object,
string,
number,
literal("true", true),
literal("false", false),
literal("null", null),
ref,
heapRef,
], (data) => data[0]);
let element = sequence([ws, value, ws], (data) => data[1]);
let elements = sequence(() => [
element,
zeroOrMore(sequence([literal(","), element], (data) => data[1]))
], (data) => [data[0], ...data[1]]);
let object = choice([
sequence([zeroOrOne(tag), literal("{"), ws, literal("}")], (data) => {
let map = new Map();
if (data[0] !== "{") {
// has tag
map.__tag__ = data[0][0];
}
return map;
}),
sequence(() => [zeroOrOne(tag), literal("{"), members, literal("}")], (data) => {
const map = new Map();
let entries;
if (data[0] === "{") {
// no tag
entries = data[1];
} else {
// has tag
map.__tag__ = data[0][0];
entries = data[2];
}
for (let pair of entries) {
map.set(pair[0], pair[1]);
}
return map;
})
]);
let ref = sequence([literal("*"), number], (data) => new Ref(data[1]));
let heapRef = sequence([literal("^"), number], (data) => new HeapRef(data[1]));
let member = sequence([element, literal(":"), element], (data) => [data[0], data[2]]);
let members = sequence([
member,
zeroOrMore(sequence([literal(","), member], (data) => data[1]))
], (data) => [data[0], ...data[1]]);
const result = element(input, 0);
if (result) {
return result.value;
} else {
throw new Error("No parse found");
}
}
exports.parse = parse;
function test() {
const testCases = [
// "-1.2",
// "5",
// "123",
// "-12",
// "-6",
// "12.50",
// "-5.6",
// "12.5e10",
// "4e5",
// "abc",
// "[1, 2.5, -0.5]",
// "[1,2.5,-0.5]",
// "[1,2.5-0.5]",
//`"abcde"`,
`"[\\"a\\", \\"b\\", \\"c\\"]"`,
// `"abc\tde"`,
// `"\\u1234"`,
// "{}",
// `{"a": 1}`,
// `{"a": 1, "b": 2}`,
// `{"hello": true, "world": false, "yo": null}`,
// `[{ "name": "jerry", "friends": [ {"name": "shanda"} ] }]`,
// `{ {"id": 1}: "brown" }`,
// `{ *123456: ^1234 }`,
// `{ "funCall": 2, "variables": *215}`,
// `<tuple>[1, 2]`,
// `<tuple>[]`,
// `<set>[]`,
// `<function>{}`,
// `<Point>{ "x": 1, "y": 2 }`,
// `^123`
];
for (let testCase of testCases) {
try {
const output = parse(testCase, 0);
if (output) {
console.log(`"${testCase}" =`, JSON.stringify(output));
} else {
console.log(`"${testCase}" no parse found.`);
}
} catch (e) {
console.log(`${e.message} for ${testCase}.`);
// console.log(e.message);
}
}
}
// test();
function characterMatching(choices) {
return function _characterMatching(buffer, cursor) {
const char = buffer[cursor];
if (typeof choices === "string") {
if (choices.indexOf(char) !== -1) {
return {
value: char,
cursor: cursor + 1
};
} else {
return null;
}
} else if (typeof choices === "function") {
if (choices(char)) {
return {
value: char,
cursor: cursor + 1
};
} else {
return null;
}
} else if (choices instanceof RegExp) {
if (choices.test(char)) {
return {
value: char,
cursor: cursor + 1
};
} else {
return null;
}
} else {
throw new Error("Unsupported argument type fo characterMatching: " + choices);
}
};
}
function literal(string, value) {
return function _literal(buffer, cursor) {
if (buffer.substr(cursor, string.length) === string) {
return {
value: value !== undefined ? value : string,
cursor: cursor + string.length
};
} else {
return null;
}
};
}
function sequence(parsers, converter) {
return function _sequence(buffer, cursor) {
if (typeof parsers === "function") {
parsers = parsers();
}
const values = [];
let currentCursor = cursor;
for (let parser of parsers) {
const result = parser(buffer, currentCursor);
if (result) {
values.push(result.value);
currentCursor = result.cursor;
} else {
return null;
}
}
return {
value: converter ? converter(values) : values,
cursor: currentCursor
};
};
}
function oneOrMore(parser) {
return function _oneOrMore(buffer, cursor) {
let values = [];
let currentCursor = cursor;
while (true) {
const result = parser(buffer, currentCursor);
if (result) {
values.push(result.value);
currentCursor = result.cursor;
} else {
if (values.length === 0) {
return null;
} else {
return {
value: values.join(""),
cursor: currentCursor
};
}
}
}
};
}
function zeroOrMore(parser) {
return function _zeroOrMore(buffer, cursor) {
let values = [];
let currentCursor = cursor;
while (true) {
const result = parser(buffer, currentCursor);
if (result) {
values.push(result.value);
currentCursor = result.cursor;
} else {
return {
value: values,
cursor: currentCursor
};
}
}
};
}
function zeroOrOne(parser) {
return zeroUpTo(parser, 1);
}
function zeroUpTo(parser, maxTimes) {
return function _zeroOrMore(buffer, cursor) {
let values = [];
let currentCursor = cursor;
let numMatches = 0;
while (true) {
const result = parser(buffer, currentCursor);
if (result) {
numMatches++;
values.push(result.value);
currentCursor = result.cursor;
if (numMatches === maxTimes) {
return {
value: values,
cursor: currentCursor
};
}
} else {
return {
value: values,
cursor: currentCursor
};
}
}
};
}
function choice(choices) {
return function _choice(buffer, cursor) {
if (typeof choices === "function") {
choices = choices();
}
for (let choice of choices) {
const result = choice(buffer, cursor);
if (result) {
return result;
}
}
return null;
};
}
function nothing(value) {
return function _nothing(buffer, cursor) {
return {
value: value,
cursor: cursor
};
}
}
function id(data) {
return data[0];
}