-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_parser.js
95 lines (86 loc) · 1.99 KB
/
json_parser.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
var lastString = "";
var addLevel;
var stack = [];
function parse(data, cb) {
var lead = true;
var inString = false;
var inEscape = false;
var inComment = false;
var line = 1;
addLevel = true;
for (var i = 0; i < data.length; i++) {
var ch = data.charAt(i);
if (ch === '\n') {
line ++;
}
if (lead && (ch != '{')) {
continue;
}
lead = false;
if ((inString !== false) && (ch === '\\')) {
inEscape = true;
continue;
}
if (inEscape) {
inEscape = false;
continue;
}
if (inComment && ch !== '\n') {
continue;
} else {
inComment = false;
}
if ((ch === '/') && (inString === false)) {
if (data.charAt(i+1) === '/') {
inComment = true;
}
}
if (ch === '\'') {
if (inString !== false) {
got_string(inString);
inString = false;
} else {
inString = "";
continue;
}
}
if ((ch === '{') && (inString === false)) {
got_in();
}
if ((ch === '}') && (inString === false)) {
got_out();
}
if ((ch === ':') && (inString === false)) {
got_colon(line, cb);
}
if ((ch === ',') && (inString === false)) {
got_coma();
}
if (inString !== false) {
inString += ch;
}
}
}
function got_in() {
addLevel = true;
}
function got_out() {
if (!addLevel) stack.pop();
addLevel = false;
}
function got_string(s) {
lastString = s;
}
function got_colon(line, cb) {
if (!addLevel) {
stack.pop();
}
stack.push(lastString);
addLevel = false;
if (typeof cb === "function") {
cb(stack.join("."), line);
}
}
function got_coma() {
}
module.exports = parse;