-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathjson-parser.ts
359 lines (312 loc) · 9.25 KB
/
json-parser.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
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
353
354
355
356
357
358
359
import { EscapeToken, NumberToken, Token } from './tokens';
import { JSONArray, JSONObject, JSONValue } from './types';
const FAILURE_EXIT_CODE = 1;
const SUCCESS_EXIT_CODE = 0;
const CONTROL_CHARACTERS_REGEX =
// eslint-disable-next-line no-control-regex
/[\u0000-\u001F\u007F-\u009F\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069]/g;
export class JsonParser {
private pos = 0;
private input: string;
constructor(input: string) {
this.input = input;
}
public parse(): JSONValue {
this.consumeWhitespace();
const value = this.parseValue();
this.consumeWhitespace();
if (this.hasNext()) {
console.log(
`Unexpected token ${this.currentToken()}at position ${this.pos}`
);
process.exit(FAILURE_EXIT_CODE);
}
console.log('Parsed successfully %s', value);
process.exit(SUCCESS_EXIT_CODE);
}
private parseValue(): JSONValue {
switch (this.currentToken()) {
case Token.BEGIN_OBJECT:
return this.parseObject();
case Token.BEGIN_ARRAY:
return this.parseArray();
case Token.QUOTE:
return this.parseString();
case Token.BEGIN_TRUE:
return this.parseTrue();
case Token.BEGIN_FALSE:
return this.parseFalse();
case Token.BEGIN_NULL:
return this.parseNull();
case NumberToken.ZERO:
case NumberToken.ONE:
case NumberToken.TWO:
case NumberToken.THREE:
case NumberToken.FOUR:
case NumberToken.FIVE:
case NumberToken.SIX:
case NumberToken.SEVEN:
case NumberToken.EIGHT:
case NumberToken.NINE:
case NumberToken.MINUS:
return this.parseNumber();
default:
console.log(
`Unexpected token ${this.currentToken()} at position ${this.pos}`
);
process.exit(FAILURE_EXIT_CODE);
}
}
private parseObject(): JSONObject {
const obj: JSONObject = {};
this.consume(Token.BEGIN_OBJECT);
// Used to check if there are more pairs.
// while loop will not end in this case : "{,}"
let morePairs = null;
while (this.currentToken() !== Token.END_OBJECT || morePairs) {
const pair = this.parsePair();
obj[pair.key] = pair.value;
// If there are more pairs
if (this.currentToken() === Token.COMMA) {
this.consume(Token.COMMA);
morePairs = true;
} else if (this.currentToken() !== Token.END_OBJECT) {
morePairs = false;
console.log(`Invalid object at position ${this.pos}`);
process.exit(FAILURE_EXIT_CODE);
} else {
morePairs = false;
}
}
this.consume(Token.END_OBJECT);
return obj;
}
private parsePair(): { key: string; value: JSONValue } {
const key = this.parseString();
this.consume(Token.SEMI_COLON);
const value = this.parseValue();
return { key, value };
}
private parseArray(): JSONArray {
const arr: JSONArray = [];
this.consume(Token.BEGIN_ARRAY);
// Used to check if there are more pairs.
// while loop will not end in this case : "[,]"
let morePairs = null;
while (this.currentToken() !== Token.END_ARRAY || morePairs) {
const value = this.parseValue();
arr.push(value);
// If there is another pair
if (this.currentToken() === Token.COMMA) {
this.consume(Token.COMMA);
morePairs = true;
} else if (this.currentToken() !== Token.END_ARRAY) {
morePairs = false;
console.log(
`Invalid array at position ${
this.pos
}, currentToken: ${this.input.substring(this.pos - 2, this.pos + 2)}`
);
process.exit(FAILURE_EXIT_CODE);
} else {
morePairs = false;
}
}
this.consume(Token.END_ARRAY);
return arr;
}
private parseString(): string {
let str = '';
this.consume(Token.QUOTE);
while (this.currentToken() !== Token.QUOTE) {
if (this.currentToken() === Token.ESCAPE) {
str += this.parseEscape();
} else {
if (this.isControlCode()) {
console.log(
`Invalid character at ${this.pos}. Control characters must be escaped`
);
process.exit(FAILURE_EXIT_CODE);
}
str += this.currentToken();
this.pos++;
}
}
this.consume(Token.QUOTE);
return str;
}
private parseEscape(): string {
// We are not skipping the white spaces after the consume.
// Since the next character matters in this case.
this.consume(Token.ESCAPE, false);
// Reject if a control character follows the escape token
if (this.isControlCode()) {
console.log(`Invalid escape character at ${this.pos}.`);
process.exit(FAILURE_EXIT_CODE);
}
switch (this.currentToken()) {
case EscapeToken.QUOTE:
case EscapeToken.REVERSE_SOLIDUS:
case EscapeToken.SOLIDUS: {
const c = this.currentToken();
this.consume();
return c;
}
case EscapeToken.BACKSPACE:
this.consume();
return Token.BACKSPACE;
case EscapeToken.FORM_FEED:
this.consume();
return Token.FORM_FEED;
case EscapeToken.LINE_FEED:
this.consume();
return Token.LINE_FEED;
case EscapeToken.CAR_RETURN:
this.consume();
return Token.CAR_RETURN;
case EscapeToken.HORIZONTAL_TAB:
this.consume();
return Token.HORIZONTAL_TAB;
case EscapeToken.HEX: {
this.consume();
const code = parseInt(this.input.substring(this.pos, this.pos + 4), 16);
if (isNaN(code)) {
console.log(`Invalid hex code at position ${this.pos}`);
process.exit(FAILURE_EXIT_CODE);
}
this.pos += 4;
return String.fromCharCode(code);
}
default:
console.log(`Invalid escape character at position ${this.pos}`);
process.exit(FAILURE_EXIT_CODE);
}
}
private parseTrue(): boolean {
this.consume('t');
this.consume('r');
this.consume('u');
this.consume('e');
return true;
}
private parseFalse(): boolean {
this.consume('f');
this.consume('a');
this.consume('l');
this.consume('s');
this.consume('e');
return false;
}
private parseNull(): null {
this.consume('n');
this.consume('u');
this.consume('l');
this.consume('l');
return null;
}
private parseNumber(): number {
let str = '';
// If the number if negative
if (this.currentToken() === NumberToken.MINUS) {
str += this.currentToken();
this.consume(NumberToken.MINUS);
}
// Parse the Integer part
str += this.parseDigits();
// If the number if a decimal
if (this.currentToken() === NumberToken.DOT) {
str += this.currentToken();
this.consume(NumberToken.DOT);
str += this.parseDigits(true);
}
// If the number as an exponent part
if (
this.currentToken() === NumberToken.SMALL_EXPONENT ||
this.currentToken() === NumberToken.CAPITAL_EXPONENT
) {
str += this.currentToken();
this.consume();
if (
this.currentToken() == NumberToken.PLUS ||
this.currentToken() == NumberToken.MINUS
) {
str += this.currentToken();
this.consume();
}
str += this.parseDigits(true);
}
return parseFloat(str);
}
private parseDigits(allowMultipleZerosAtPrefix: boolean = false): string {
let str = '';
if (this.currentToken() === NumberToken.ZERO) {
str += this.currentToken();
this.consume(NumberToken.ZERO);
// If the number has multiple zeros allowed at the prefix
// eg: 1e0001
if (allowMultipleZerosAtPrefix) {
while (this.currentToken() === NumberToken.ZERO) {
str += this.currentToken();
this.consume(NumberToken.ZERO);
}
}
} else if (
this.currentToken() >= NumberToken.ONE &&
this.currentToken() <= NumberToken.NINE
) {
str += this.currentToken();
this.consume();
while (
this.currentToken() >= NumberToken.ZERO &&
this.currentToken() <= NumberToken.NINE
) {
str += this.currentToken();
this.consume();
}
} else {
console.log(
`Invalid character ${this.currentToken()} at position ${this.pos}\n
parsed ${str} till now`
);
process.exit(FAILURE_EXIT_CODE);
}
return str;
}
private consumeWhitespace() {
while (/\s/.test(this.currentToken())) {
this.consume();
}
}
private consume(expected?: string, skip: boolean = true) {
if (expected && this.currentToken() !== expected) {
console.log(
`Expected ${expected} but found ${this.currentToken()} at position ${
this.pos
}`
);
process.exit(FAILURE_EXIT_CODE);
}
this.pos++;
if (skip) {
// Skip over any whitespace characters
while (
this.currentToken() === ' ' ||
this.currentToken() === '\t' ||
this.currentToken() === '\n' ||
this.currentToken() === '\r'
) {
this.pos++;
}
}
}
private hasNext(): boolean {
this.consumeWhitespace();
return this.input.codePointAt(this.pos) !== undefined;
}
private currentToken(): string {
return this.input.charAt(this.pos);
}
private isControlCode(): boolean {
return CONTROL_CHARACTERS_REGEX.test(this.currentToken());
}
}