forked from s2online/s2online.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.js
249 lines (223 loc) · 7.27 KB
/
json.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
// Translation of src/util/JSON.as and src/util/ReadStream.as.
function scratchParseJSON(s) {
var src = (function(s) {
var src = s;
var i = 0;
return {
atEnd() {
return i >= src.length;
},
next() {
if (i >= src.length) return '';
return src.charAt(i++);
},
peek() {
return (i < src.length) ? src.charAt(i) : '';
},
peek2() {
return ((i + 1) < src.length) ? src.charAt(i + 1) : '';
},
peekString(n) {
return src.slice(i, i + n);
},
nextString(n) {
i += n;
return src.slice(i - n, i);
},
pos() {
return i;
},
setPos(newPos) {
i = newPos;
},
skip(count) {
i += count;
},
skipWhiteSpace() {
while ((i < src.length) && (src.charCodeAt(i) <= 32)) i++;
}
};
})(s);
function readValue() {
skipWhiteSpaceAndComments();
var ch = src.peek();
if (("0" <= ch) && (ch <= "9")) return readNumber(); // common case
switch(ch) {
case '"': return readString();
case "[": return readArray();
case "{": return readObject();
case "t":
if (src.nextString(4) == "true") return true;
else error("Expected 'true'");
case "f":
if (src.nextString(5) == "false") return false;
else error("Expected 'false'");
case "n":
if (src.nextString(4) == "null") return null;
else error("Expected 'null'");
case "-":
if (src.peekString(9) == "-Infinity") {
src.skip(9);
return Number.NEGATIVE_INFINITY;
} else return readNumber();
case "I":
if (src.nextString(8) == "Infinity") return Number.POSITIVE_INFINITY;
else error("Expected 'Infinity'");
case "N":
if (src.nextString(3) == "NaN") return NaN;
else error("Expected 'NaN'");
case "":
error("Incomplete JSON data");
default:
error("Bad character: " + ch);
}
return null;
}
function readArray() {
var result = [];
src.skip(1); // skip "["
while (true) {
if (src.atEnd()) return error("Incomplete array");
skipWhiteSpaceAndComments();
if (src.peek() == "]") break;
result.push(readValue());
skipWhiteSpaceAndComments();
if (src.peek() == ",") {
src.skip(1);
continue;
}
if (src.peek() == "]") break;
else error("Bad array syntax");
}
src.skip(1); // skip "]"
return result;
}
function readObject() {
var result = {};
src.skip(1); // skip "{"
while (true) {
if (src.atEnd()) return error("Incomplete object");
skipWhiteSpaceAndComments();
if (src.peek() == "}") break;
if (src.peek() != '"') error("Bad object syntax");
var key = readString();
skipWhiteSpaceAndComments();
if (src.next() != ":") error("Bad object syntax");
skipWhiteSpaceAndComments();
var value = readValue();
result[key] = value;
skipWhiteSpaceAndComments();
if (src.peek() == ",") {
src.skip(1);
continue;
}
if (src.peek() == "}") break;
else error("Bad object syntax");
}
src.skip(1); // skip "}"
return result;
}
function readNumber() {
var numStr = "";
var ch = src.peek();
if ((ch == "0") && (src.peek2() == "x")) { // hex number
numStr = src.nextString(2) + readHexDigits();
return Number(numStr);
}
if (ch == "-") numStr += src.next();
numStr += readDigits();
if ((numStr == "") || (numStr == "-")) error("At least one digit expected");
if (src.peek() == ".") numStr += src.next() + readDigits();
ch = src.peek();
if ((ch == "e") || (ch == "E")) {
numStr += src.next();
ch = src.peek();
if ((ch == "+") || (ch == "-")) numStr += src.next();
numStr += readDigits();
}
return Number(numStr);
}
function readDigits() {
var result = "";
while (true) {
var ch = src.next();
if (("0" <= ch) && (ch <= "9")) result += ch;
else {
if (ch != "") src.skip(-1);
break;
}
}
return result;
}
function readHexDigits() {
var result = "";
while (true) {
var ch = src.next();
if (("0" <= ch) && (ch <= "9")) result += ch;
else if (("a" <= ch) && (ch <= "f")) result += ch;
else if (("A" <= ch) && (ch <= "F")) result += ch;
else {
if (!src.atEnd()) src.skip(-1);
break;
}
}
return result;
}
function readString() {
var result = "";
src.skip(1); // skip opening quote
var ch;
while ((ch = src.next()) != '"') {
if (ch == "") return error("Incomplete string");
if (ch == "\\") result += readEscapedChar();
else result += ch;
}
return result;
}
function readEscapedChar() {
var ch = src.next();
switch(ch) {
case "b": return "\b";
case "f": return "\f";
case "n": return "\n";
case "r": return "\r";
case "t": return "\t";
case "u": return String.fromCharCode(Number("0x" + src.nextString(4)));
}
return ch;
}
function skipWhiteSpaceAndComments() {
while (true) {
// skip comments and white space until the stream position does not change
var lastPos = src.pos();
src.skipWhiteSpace();
skipComment();
if (src.pos() == lastPos) break; // done
}
}
function skipComment() {
var ch;
if ((src.peek() == "/") && (src.peek2() == "/")) {
src.skip(2);
while ((ch = src.next()) != "\n") { // comments goes until the end of the line
if (ch == "") return; // end of stream
}
}
if ((src.peek() == "/") && (src.peek2() == "*")) {
src.skip(2);
var lastWasAsterisk = false;
while (true) {
ch = src.next();
if (ch == "") return; // end of stream
if (lastWasAsterisk && (ch == "/")) return; // end of comment
if (ch == "*") lastWasAsterisk = true;
}
}
}
return readValue();
}
/*
if (typeof require === 'function' && typeof module === 'object' && require.main === module) {
console.log(scratchParseJSON('{"hi":"lol\nthere"}'));
}
*/