forked from kaelzhang/node-json-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
258 lines (204 loc) · 4.28 KB
/
index.js
File metadata and controls
258 lines (204 loc) · 4.28 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
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
'use strict';
var esprima = require('esprima');
exports.tokenize = tokenize;
exports.parse = parse;
function tokenize (code) {
return esprima.tokenize(code, {
comment: true,
loc: true
});
}
var tokens;
var current;
var index;
var reviver;
function parse (code, rev) {
tokens = tokenize(code);
reviver = rev;
if (!tokens.length) {
unexpected_end();
}
sort_comment_tokens();
index = -1;
next();
var result = walk();
if (Object(result) === result) {
if (tokens.head_comments.length) {
result['//^'] = tokens.head_comments;
}
if (tokens.foot_comments.length) {
result['//$'] = tokens.foot_comments;
}
}
result = transform('', result);
reviver = null;
return result;
}
function transform (k, v) {
return reviver
? reviver(k, v)
: v;
}
function walk () {
switch (type()) {
case '{':
next();
return parse_object();
case '[':
next();
return parse_array();
case 'String':
case 'Boolean':
case 'Null':
case 'Numeric':
var value = current.value;
next();
return JSON.parse(value);
}
unexpected();
}
function next () {
return current = tokens[++ index];
}
function expect (a) {
if (!is(a)) {
unexpected();
}
}
function unexpected () {
throw new SyntaxError('Unexpected token ' + current.value.slice(0, 1));
}
function unexpected_end () {
throw new SyntaxError('Unexpected end of input');
}
function parse_object () {
var obj = {};
var comment;
var started;
var name;
while (!is('}')){
if (started) {
expect(',');
next();
}
started = true;
expect('String');
name = JSON.parse(current.value);
if (current.comments) {
obj['// ' + name] = current.comments;
}
next();
expect(':');
next();
obj[name] = transform(name, walk());
}
next();
return obj;
}
function parse_array () {
var array = [];
var started;
var i = 0;
while(!is(']')){
if (started) {
expect(',');
next();
}
started = true;
array[i] = transform(i, walk());
i ++;
}
next();
return array;
}
function type () {
if (!current) {
unexpected_end();
}
return current.type === 'Punctuator'
? current.value
: current.type;
}
function is (t) {
return type() === t;
}
function sort_comment_tokens () {
var ts = tokens;
var comments = ts.comments;
if (!comments) {
return;
}
function compare_to_then_push (condition, to, setup) {
var comment;
var first = true;
var host;
while((comment = comments[ci ++]) && condition(comment, to)){
if (first) {
host = setup();
}
first = false;
host.push(comment_content(comment));
}
ci --;
// Whether there are comments left.
return !!comment;
}
var head_comments = [];
var foot_comments = [];
var first = ts[0];
var ci = 0;
var comment = compare_to_then_push(left, first, function () {
return head_comments;
});
var i = 0;
var token;
var next;
for (; i < ts.length; i ++) {
if (!comment) {
break;
}
token = ts[i];
next = ts[i + 1];
if (token.type === 'String' && next && next.value === ':') {
comment = compare_to_then_push(left, token, function () {
token.comments || (token.comments = []);
return token.comments[0] || (token.comments[0] = []);
});
if (!comment) {
break;
}
comment = compare_to_then_push(right, token, function () {
token.comments || (token.comments = []);
return token.comments[1] || (token.comments[1] = []);
});
}
}
compare_to_then_push(function () {
return true
}, null, function () {
return foot_comments;
});
comments.length = 0;
delete ts.comments;
tokens.head_comments = head_comments;
tokens.foot_comments = foot_comments;
}
function left (a, b) {
return a
&& (
a.loc.start.line < b.loc.start.line
||
a.loc.start.line === b.loc.start.line
&& a.loc.start.column < b.loc.start.column
);
}
function right (a, b) {
return a
&& a.loc.start.line === b.loc.start.line
&& a.loc.start.column > b.loc.start.column;
}
function comment_content (comment) {
return comment.type === 'Block'
? '/*' + comment.value + '*/'
: '//' + comment.value;
}