/
parse.js
279 lines (250 loc) · 6.55 KB
/
parse.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
"use strict";
//The data doesn't need to be buffered because .properties files typically
//have a size less than a block (default is 16KB)
var hex = function (c){
switch (c){
case "0": return 0;
case "1": return 1;
case "2": return 2;
case "3": return 3;
case "4": return 4;
case "5": return 5;
case "6": return 6;
case "7": return 7;
case "8": return 8;
case "9": return 9;
case "a": case "A": return 10;
case "b": case "B": return 11;
case "c": case "C": return 12;
case "d": case "D": return 13;
case "e": case "E": return 14;
case "f": case "F": return 15;
}
};
module.exports = function (data, options, handlers, control){
var c;
var code;
var escape;
var skipSpace = true;
var isCommentLine;
var isSectionLine;
var newLine = true;
var multiLine;
var isKey = true;
var key = "";
var value = "";
var section;
var unicode;
var unicodeRemaining;
var escapingUnicode;
var keySpace;
var sep;
var ignoreLine;
var line = function (){
if (key || value || sep){
handlers.line (key, value);
key = "";
value = "";
sep = false;
}
};
var escapeString = function (key, c, code){
if (escapingUnicode && unicodeRemaining){
unicode = (unicode<<4) + hex (c);
if (--unicodeRemaining) return key;
escape = false;
escapingUnicode = false;
return key + String.fromCharCode (unicode);
}
//code 117: u
if (code === 117){
unicode = 0;
escapingUnicode = true;
unicodeRemaining = 4;
return key;
}
escape = false;
//code 116: t
//code 114: r
//code 110: n
//code 102: f
if (code === 116) return key + "\t";
else if (code === 114) return key + "\r";
else if (code === 110) return key + "\n";
else if (code === 102) return key + "\f";
return key + c;
};
var isComment;
var isSeparator;
if (options._strict){
isComment = function (c, code, options){
return options._comments[c];
};
isSeparator = function (c, code, options){
return options._separators[c];
};
}else{
isComment = function (c, code, options){
//code 35: #
//code 33: !
return code === 35 || code === 33 || options._comments[c];
};
isSeparator = function (c, code, options){
//code 61: =
//code 58: :
return code === 61 || code === 58 || options._separators[c];
};
}
for (var i=~~control.resume, ii=data.length; i<ii; i++){
if (control.abort) return;
if (control.pause){
//The next index is always the start of a new line, it's a like a fresh
//start, there's no need to save the current state
control.resume = i;
return;
}
c = data[i];
code = data.charCodeAt (i);
//code 13: \r
if (code === 13) continue;
if (isCommentLine){
//code 10: \n
if (code === 10){
isCommentLine = false;
newLine = true;
skipSpace = true;
}
continue;
}
//code 93: ]
if (isSectionLine && code === 93){
handlers.section (section);
//Ignore chars after the section in the same line
ignoreLine = true;
continue;
}
if (skipSpace){
//code 32: " " (space)
//code 9: \t
//code 12: \f
if (code === 32 || code === 9 || code === 12){
continue;
}
//code 10: \n
if (!multiLine && code === 10){
//Empty line or key w/ separator and w/o value
isKey = true;
keySpace = false;
newLine = true;
line ();
continue;
}
skipSpace = false;
multiLine = false;
}
if (newLine){
newLine = false;
if (isComment (c, code, options)){
isCommentLine = true;
continue;
}
//code 91: [
if (options.sections && code === 91){
section = "";
isSectionLine = true;
control.skipSection = false;
continue;
}
}
//code 10: \n
if (code !== 10){
if (control.skipSection || ignoreLine) continue;
if (!isSectionLine){
if (!escape && isKey && isSeparator (c, code, options)){
//sep is needed to detect empty key and empty value with a
//non-whitespace separator
sep = true;
isKey = false;
keySpace = false;
//Skip whitespace between separator and value
skipSpace = true;
continue;
}
}
//code 92: "\" (backslash)
if (code === 92){
if (escape){
if (escapingUnicode) continue;
if (keySpace){
//Line with whitespace separator
keySpace = false;
isKey = false;
}
if (isSectionLine) section += "\\";
else if (isKey) key += "\\";
else value += "\\";
}
escape = !escape;
}else{
if (keySpace){
//Line with whitespace separator
keySpace = false;
isKey = false;
}
if (isSectionLine){
if (escape) section = escapeString (section, c, code);
else section += c;
}else if (isKey){
if (escape){
key = escapeString (key, c, code);
}else{
//code 32: " " (space)
//code 9: \t
//code 12: \f
if (code === 32 || code === 9 || code === 12){
keySpace = true;
//Skip whitespace between key and separator
skipSpace = true;
continue;
}
key += c;
}
}else{
if (escape) value = escapeString (value, c, code);
else value += c;
}
}
}else{
if (escape){
if (!escapingUnicode){
escape = false;
}
skipSpace = true;
multiLine = true;
}else{
if (isSectionLine){
isSectionLine = false;
if (!ignoreLine){
//The section doesn't end with ], it's a key
control.error = new Error ("The section line \"" + section +
"\" must end with \"]\"");
return;
}
ignoreLine = false;
}
newLine = true;
skipSpace = true;
isKey = true;
line ();
}
}
}
control.parsed = true;
if (isSectionLine && !ignoreLine){
//The section doesn't end with ], it's a key
control.error = new Error ("The section line \"" + section + "\" must end" +
"with \"]\"");
return;
}
line ();
};