scratch – Blame information for rev 125

Subversion Repositories:
Rev:
Rev Author Line No. Line
125 office 1 // Generated by CoffeeScript 1.12.4
2 var DumpException, Escaper, Inline, ParseException, ParseMore, Pattern, Unescaper, Utils,
75 office 3 indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
4  
5 Pattern = require('./Pattern');
6  
7 Unescaper = require('./Unescaper');
8  
9 Escaper = require('./Escaper');
10  
11 Utils = require('./Utils');
12  
13 ParseException = require('./Exception/ParseException');
14  
125 office 15 ParseMore = require('./Exception/ParseMore');
16  
75 office 17 DumpException = require('./Exception/DumpException');
18  
19 Inline = (function() {
20 function Inline() {}
21  
22 Inline.REGEX_QUOTED_STRING = '(?:"(?:[^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'(?:[^\']*(?:\'\'[^\']*)*)\')';
23  
24 Inline.PATTERN_TRAILING_COMMENTS = new Pattern('^\\s*#.*$');
25  
26 Inline.PATTERN_QUOTED_SCALAR = new Pattern('^' + Inline.REGEX_QUOTED_STRING);
27  
28 Inline.PATTERN_THOUSAND_NUMERIC_SCALAR = new Pattern('^(-|\\+)?[0-9,]+(\\.[0-9]+)?$');
29  
30 Inline.PATTERN_SCALAR_BY_DELIMITERS = {};
31  
32 Inline.settings = {};
33  
34 Inline.configure = function(exceptionOnInvalidType, objectDecoder) {
35 if (exceptionOnInvalidType == null) {
36 exceptionOnInvalidType = null;
37 }
38 if (objectDecoder == null) {
39 objectDecoder = null;
40 }
41 this.settings.exceptionOnInvalidType = exceptionOnInvalidType;
42 this.settings.objectDecoder = objectDecoder;
43 };
44  
45 Inline.parse = function(value, exceptionOnInvalidType, objectDecoder) {
46 var context, result;
47 if (exceptionOnInvalidType == null) {
48 exceptionOnInvalidType = false;
49 }
50 if (objectDecoder == null) {
51 objectDecoder = null;
52 }
53 this.settings.exceptionOnInvalidType = exceptionOnInvalidType;
54 this.settings.objectDecoder = objectDecoder;
55 if (value == null) {
56 return '';
57 }
58 value = Utils.trim(value);
59 if (0 === value.length) {
60 return '';
61 }
62 context = {
63 exceptionOnInvalidType: exceptionOnInvalidType,
64 objectDecoder: objectDecoder,
65 i: 0
66 };
67 switch (value.charAt(0)) {
68 case '[':
69 result = this.parseSequence(value, context);
70 ++context.i;
71 break;
72 case '{':
73 result = this.parseMapping(value, context);
74 ++context.i;
75 break;
76 default:
77 result = this.parseScalar(value, null, ['"', "'"], context);
78 }
79 if (this.PATTERN_TRAILING_COMMENTS.replace(value.slice(context.i), '') !== '') {
80 throw new ParseException('Unexpected characters near "' + value.slice(context.i) + '".');
81 }
82 return result;
83 };
84  
85 Inline.dump = function(value, exceptionOnInvalidType, objectEncoder) {
86 var ref, result, type;
87 if (exceptionOnInvalidType == null) {
88 exceptionOnInvalidType = false;
89 }
90 if (objectEncoder == null) {
91 objectEncoder = null;
92 }
93 if (value == null) {
94 return 'null';
95 }
96 type = typeof value;
97 if (type === 'object') {
98 if (value instanceof Date) {
99 return value.toISOString();
100 } else if (objectEncoder != null) {
101 result = objectEncoder(value);
102 if (typeof result === 'string' || (result != null)) {
103 return result;
104 }
105 }
106 return this.dumpObject(value);
107 }
108 if (type === 'boolean') {
109 return (value ? 'true' : 'false');
110 }
111 if (Utils.isDigits(value)) {
112 return (type === 'string' ? "'" + value + "'" : String(parseInt(value)));
113 }
114 if (Utils.isNumeric(value)) {
115 return (type === 'string' ? "'" + value + "'" : String(parseFloat(value)));
116 }
117 if (type === 'number') {
125 office 118 return (value === 2e308 ? '.Inf' : (value === -2e308 ? '-.Inf' : (isNaN(value) ? '.NaN' : value)));
75 office 119 }
120 if (Escaper.requiresDoubleQuoting(value)) {
121 return Escaper.escapeWithDoubleQuotes(value);
122 }
123 if (Escaper.requiresSingleQuoting(value)) {
124 return Escaper.escapeWithSingleQuotes(value);
125 }
126 if ('' === value) {
127 return '""';
128 }
129 if (Utils.PATTERN_DATE.test(value)) {
130 return "'" + value + "'";
131 }
132 if ((ref = value.toLowerCase()) === 'null' || ref === '~' || ref === 'true' || ref === 'false') {
133 return "'" + value + "'";
134 }
135 return value;
136 };
137  
138 Inline.dumpObject = function(value, exceptionOnInvalidType, objectSupport) {
139 var j, key, len1, output, val;
140 if (objectSupport == null) {
141 objectSupport = null;
142 }
143 if (value instanceof Array) {
144 output = [];
145 for (j = 0, len1 = value.length; j < len1; j++) {
146 val = value[j];
147 output.push(this.dump(val));
148 }
149 return '[' + output.join(', ') + ']';
150 } else {
151 output = [];
152 for (key in value) {
153 val = value[key];
154 output.push(this.dump(key) + ': ' + this.dump(val));
155 }
156 return '{' + output.join(', ') + '}';
157 }
158 };
159  
160 Inline.parseScalar = function(scalar, delimiters, stringDelimiters, context, evaluate) {
161 var i, joinedDelimiters, match, output, pattern, ref, ref1, strpos, tmp;
162 if (delimiters == null) {
163 delimiters = null;
164 }
165 if (stringDelimiters == null) {
166 stringDelimiters = ['"', "'"];
167 }
168 if (context == null) {
169 context = null;
170 }
171 if (evaluate == null) {
172 evaluate = true;
173 }
174 if (context == null) {
175 context = {
176 exceptionOnInvalidType: this.settings.exceptionOnInvalidType,
177 objectDecoder: this.settings.objectDecoder,
178 i: 0
179 };
180 }
181 i = context.i;
182 if (ref = scalar.charAt(i), indexOf.call(stringDelimiters, ref) >= 0) {
183 output = this.parseQuotedScalar(scalar, context);
184 i = context.i;
185 if (delimiters != null) {
186 tmp = Utils.ltrim(scalar.slice(i), ' ');
187 if (!(ref1 = tmp.charAt(0), indexOf.call(delimiters, ref1) >= 0)) {
188 throw new ParseException('Unexpected characters (' + scalar.slice(i) + ').');
189 }
190 }
191 } else {
192 if (!delimiters) {
193 output = scalar.slice(i);
194 i += output.length;
195 strpos = output.indexOf(' #');
196 if (strpos !== -1) {
197 output = Utils.rtrim(output.slice(0, strpos));
198 }
199 } else {
200 joinedDelimiters = delimiters.join('|');
201 pattern = this.PATTERN_SCALAR_BY_DELIMITERS[joinedDelimiters];
202 if (pattern == null) {
203 pattern = new Pattern('^(.+?)(' + joinedDelimiters + ')');
204 this.PATTERN_SCALAR_BY_DELIMITERS[joinedDelimiters] = pattern;
205 }
206 if (match = pattern.exec(scalar.slice(i))) {
207 output = match[1];
208 i += output.length;
209 } else {
210 throw new ParseException('Malformed inline YAML string (' + scalar + ').');
211 }
212 }
213 if (evaluate) {
214 output = this.evaluateScalar(output, context);
215 }
216 }
217 context.i = i;
218 return output;
219 };
220  
221 Inline.parseQuotedScalar = function(scalar, context) {
222 var i, match, output;
223 i = context.i;
224 if (!(match = this.PATTERN_QUOTED_SCALAR.exec(scalar.slice(i)))) {
125 office 225 throw new ParseMore('Malformed inline YAML string (' + scalar.slice(i) + ').');
75 office 226 }
227 output = match[0].substr(1, match[0].length - 2);
228 if ('"' === scalar.charAt(i)) {
229 output = Unescaper.unescapeDoubleQuotedString(output);
230 } else {
231 output = Unescaper.unescapeSingleQuotedString(output);
232 }
233 i += match[0].length;
234 context.i = i;
235 return output;
236 };
237  
238 Inline.parseSequence = function(sequence, context) {
125 office 239 var e, i, isQuoted, len, output, ref, value;
75 office 240 output = [];
241 len = sequence.length;
242 i = context.i;
243 i += 1;
244 while (i < len) {
245 context.i = i;
246 switch (sequence.charAt(i)) {
247 case '[':
248 output.push(this.parseSequence(sequence, context));
249 i = context.i;
250 break;
251 case '{':
252 output.push(this.parseMapping(sequence, context));
253 i = context.i;
254 break;
255 case ']':
256 return output;
257 case ',':
258 case ' ':
259 case "\n":
260 break;
261 default:
262 isQuoted = ((ref = sequence.charAt(i)) === '"' || ref === "'");
263 value = this.parseScalar(sequence, [',', ']'], ['"', "'"], context);
264 i = context.i;
265 if (!isQuoted && typeof value === 'string' && (value.indexOf(': ') !== -1 || value.indexOf(":\n") !== -1)) {
266 try {
267 value = this.parseMapping('{' + value + '}');
268 } catch (error) {
269 e = error;
270 }
271 }
272 output.push(value);
273 --i;
274 }
275 ++i;
276 }
125 office 277 throw new ParseMore('Malformed inline YAML string ' + sequence);
75 office 278 };
279  
280 Inline.parseMapping = function(mapping, context) {
281 var done, i, key, len, output, shouldContinueWhileLoop, value;
282 output = {};
283 len = mapping.length;
284 i = context.i;
285 i += 1;
286 shouldContinueWhileLoop = false;
287 while (i < len) {
288 context.i = i;
289 switch (mapping.charAt(i)) {
290 case ' ':
291 case ',':
292 case "\n":
293 ++i;
294 context.i = i;
295 shouldContinueWhileLoop = true;
296 break;
297 case '}':
298 return output;
299 }
300 if (shouldContinueWhileLoop) {
301 shouldContinueWhileLoop = false;
302 continue;
303 }
304 key = this.parseScalar(mapping, [':', ' ', "\n"], ['"', "'"], context, false);
305 i = context.i;
306 done = false;
307 while (i < len) {
308 context.i = i;
309 switch (mapping.charAt(i)) {
310 case '[':
311 value = this.parseSequence(mapping, context);
312 i = context.i;
313 if (output[key] === void 0) {
314 output[key] = value;
315 }
316 done = true;
317 break;
318 case '{':
319 value = this.parseMapping(mapping, context);
320 i = context.i;
321 if (output[key] === void 0) {
322 output[key] = value;
323 }
324 done = true;
325 break;
326 case ':':
327 case ' ':
328 case "\n":
329 break;
330 default:
331 value = this.parseScalar(mapping, [',', '}'], ['"', "'"], context);
332 i = context.i;
333 if (output[key] === void 0) {
334 output[key] = value;
335 }
336 done = true;
337 --i;
338 }
339 ++i;
340 if (done) {
341 break;
342 }
343 }
344 }
125 office 345 throw new ParseMore('Malformed inline YAML string ' + mapping);
75 office 346 };
347  
348 Inline.evaluateScalar = function(scalar, context) {
349 var cast, date, exceptionOnInvalidType, firstChar, firstSpace, firstWord, objectDecoder, raw, scalarLower, subValue, trimmedScalar;
350 scalar = Utils.trim(scalar);
351 scalarLower = scalar.toLowerCase();
352 switch (scalarLower) {
353 case 'null':
354 case '':
355 case '~':
356 return null;
357 case 'true':
358 return true;
359 case 'false':
360 return false;
361 case '.inf':
125 office 362 return 2e308;
75 office 363 case '.nan':
125 office 364 return 0/0;
75 office 365 case '-.inf':
125 office 366 return 2e308;
75 office 367 default:
368 firstChar = scalarLower.charAt(0);
369 switch (firstChar) {
370 case '!':
371 firstSpace = scalar.indexOf(' ');
372 if (firstSpace === -1) {
373 firstWord = scalarLower;
374 } else {
375 firstWord = scalarLower.slice(0, firstSpace);
376 }
377 switch (firstWord) {
378 case '!':
379 if (firstSpace !== -1) {
380 return parseInt(this.parseScalar(scalar.slice(2)));
381 }
382 return null;
383 case '!str':
384 return Utils.ltrim(scalar.slice(4));
385 case '!!str':
386 return Utils.ltrim(scalar.slice(5));
387 case '!!int':
388 return parseInt(this.parseScalar(scalar.slice(5)));
389 case '!!bool':
390 return Utils.parseBoolean(this.parseScalar(scalar.slice(6)), false);
391 case '!!float':
392 return parseFloat(this.parseScalar(scalar.slice(7)));
393 case '!!timestamp':
394 return Utils.stringToDate(Utils.ltrim(scalar.slice(11)));
395 default:
396 if (context == null) {
397 context = {
398 exceptionOnInvalidType: this.settings.exceptionOnInvalidType,
399 objectDecoder: this.settings.objectDecoder,
400 i: 0
401 };
402 }
403 objectDecoder = context.objectDecoder, exceptionOnInvalidType = context.exceptionOnInvalidType;
404 if (objectDecoder) {
405 trimmedScalar = Utils.rtrim(scalar);
406 firstSpace = trimmedScalar.indexOf(' ');
407 if (firstSpace === -1) {
408 return objectDecoder(trimmedScalar, null);
409 } else {
410 subValue = Utils.ltrim(trimmedScalar.slice(firstSpace + 1));
411 if (!(subValue.length > 0)) {
412 subValue = null;
413 }
414 return objectDecoder(trimmedScalar.slice(0, firstSpace), subValue);
415 }
416 }
417 if (exceptionOnInvalidType) {
418 throw new ParseException('Custom object support when parsing a YAML file has been disabled.');
419 }
420 return null;
421 }
422 break;
423 case '0':
424 if ('0x' === scalar.slice(0, 2)) {
425 return Utils.hexDec(scalar);
426 } else if (Utils.isDigits(scalar)) {
427 return Utils.octDec(scalar);
428 } else if (Utils.isNumeric(scalar)) {
429 return parseFloat(scalar);
430 } else {
431 return scalar;
432 }
433 break;
434 case '+':
435 if (Utils.isDigits(scalar)) {
436 raw = scalar;
437 cast = parseInt(raw);
438 if (raw === String(cast)) {
439 return cast;
440 } else {
441 return raw;
442 }
443 } else if (Utils.isNumeric(scalar)) {
444 return parseFloat(scalar);
445 } else if (this.PATTERN_THOUSAND_NUMERIC_SCALAR.test(scalar)) {
446 return parseFloat(scalar.replace(',', ''));
447 }
448 return scalar;
449 case '-':
450 if (Utils.isDigits(scalar.slice(1))) {
451 if ('0' === scalar.charAt(1)) {
452 return -Utils.octDec(scalar.slice(1));
453 } else {
454 raw = scalar.slice(1);
455 cast = parseInt(raw);
456 if (raw === String(cast)) {
457 return -cast;
458 } else {
459 return -raw;
460 }
461 }
462 } else if (Utils.isNumeric(scalar)) {
463 return parseFloat(scalar);
464 } else if (this.PATTERN_THOUSAND_NUMERIC_SCALAR.test(scalar)) {
465 return parseFloat(scalar.replace(',', ''));
466 }
467 return scalar;
468 default:
469 if (date = Utils.stringToDate(scalar)) {
470 return date;
471 } else if (Utils.isNumeric(scalar)) {
472 return parseFloat(scalar);
473 } else if (this.PATTERN_THOUSAND_NUMERIC_SCALAR.test(scalar)) {
474 return parseFloat(scalar.replace(',', ''));
475 }
476 return scalar;
477 }
478 }
479 };
480  
481 return Inline;
482  
483 })();
484  
485 module.exports = Inline;