-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONParser.js
More file actions
132 lines (108 loc) · 3.01 KB
/
Copy pathJSONParser.js
File metadata and controls
132 lines (108 loc) · 3.01 KB
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
class JSONParser {
constructor() {
this.reset();
}
reset() {
this.index = 0;
this.length = 0;
this.data = null;
this.token = null;
}
error(message) {
this.setToken();
let msg = `Parsing error: ${message ? message : 'Not a valid JSON'}`;
showErrorModal(msg);
throw new Error(msg);
}
parse(lexerData) {
this.reset();
this.data = lexerData;
this.data = this.skip(TOKEN_TYPE.WHITESPACE);
if (this.data[this.index].value !== '{' && this.data[this.index].value !== '[') {
this.error('JSON should start with a "{" or "["');
}
return this.value();
}
next() {
this.index++;
}
value() {
this.setToken();
if (this.token && this.token.type == TOKEN_TYPE.SYMBOL) {
switch (this.token.value) {
case '{':
return this.getObject();
case '[':
return this.getArray();
}
} else if (this.token && (this.token.type == TOKEN_TYPE.STRING || this.token.type == TOKEN_TYPE.OTHER)) {
return this.token.value;
} else if (this.token && this.token.type == TOKEN_TYPE.NUMBER) {
return parseInt(this.token.value);
} else if (this.token && this.token.type == TOKEN_TYPE.LITERAL) {
switch (this.token.value) {
case 'true':
return true;
case 'false':
return false;
case 'null':
return null;
}
}else {
return this.error();
}
}
getObject() {
let obj = {}, key = null;
if (this.token.value !== '{') return this.error(`Not a valid object at ${this.index}`);
if (this.data[++this.index].value == '}') return obj;
do {
// key of an object
this.setToken();
console.log(this.token);
if (this.token.type == TOKEN_TYPE.STRING) {
key = this.token.value;
this.index++;
} else {
if (this.token.type == TOKEN_TYPE.SYMBOL && this.token.value == '}') {
this.error(`Unexpected token "${this.data[this.index - 1].value}"`);
}
this.error(`Key of an property must be a string`);
}
this.setToken();
// ":" of an object (delimeter) after key
if (!this.token || (this.token.type != TOKEN_TYPE.SYMBOL && this.token.value != ':')) {
this.error(`Expected ":" after key on property`);
}
// value of the object
this.index++;
obj[key] = this.value();
this.index++;
this.setToken();
if (this.token.type == TOKEN_TYPE.SYMBOL && this.token.value == '}') {
return obj;
}
} while (this.token.type == TOKEN_TYPE.SYMBOL && this.token.value == ',' && this.index++);
}
getArray() {
let arr = [];
if (this.token.value !== '[') return this.error(`Not a valid array at ${this.index}`);
if (this.data[++this.index].value == ']') return arr; // empty array
do {
this.setToken();
arr.push(this.value());
this.index++; this.setToken();
if (!this.token || (this.token.type == TOKEN_TYPE.SYMBOL && this.token.value == ']')) {
return arr;
}
} while (this.token.type == TOKEN_TYPE.SYMBOL && this.token.value == ',' && this.index++);
}
skip(type) {
return this.data.filter(token => {
return token.type != type;
});
}
setToken() {
this.token = this.data[this.index];
}
}