-
Notifications
You must be signed in to change notification settings - Fork 282
Expand file tree
/
Copy pathparse.js
More file actions
executable file
·58 lines (57 loc) · 1.86 KB
/
parse.js
File metadata and controls
executable file
·58 lines (57 loc) · 1.86 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
var Parse = {
// 压缩JSON
compress: function (source) {
var index = 0, length = source.length, symbol, position, result = ""
while (index < length) {
symbol = source[index];
if ("\t\r\n ".indexOf(symbol) > -1) {
// Skip whitespace tokens.
index++;
} else if (symbol == "/") {
symbol = source[++index];
if (symbol == "/") {
// Line comment.
position = source.indexOf("\n", index);
if (position < 0) {
position = source.indexOf("\r", index);
}
index = position < 0 ? length : position;
} else if (symbol == "*") {
// Block comment.
position = source.indexOf("*/", index);
if (position < 0) {
throw SyntaxError("Unterminated block comment.");
}
// Advance the scanner position past the end of the comment.
index = position += 2;
} else {
throw SyntaxError("Invalid comment.");
}
} else if (symbol == '"') {
// Save the current scanner position.
position = index;
// Parse JavaScript strings separately to ensure that comment tokens
// within them are preserved correctly.
while (index < length) {
symbol = source[++index];
if (symbol == "\\") {
// Advance the scanner past escaped characters.
index++;
} else if (symbol == '"') {
// An unescaped double-quote character marks the end of the string.
break;
}
}
if (source[index] == '"') {
result += source.slice(position, ++index);
} else {
throw SyntaxError("Unterminated string.");
}
} else {
result += symbol;
index++;
}
}
return result;
}
}