source: trip-planner-front/node_modules/yaml/dist/PlainValue-ec8e588e.js@ 8d391a1

Last change on this file since 8d391a1 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 20.0 KB
Line 
1'use strict';
2
3const Char = {
4 ANCHOR: '&',
5 COMMENT: '#',
6 TAG: '!',
7 DIRECTIVES_END: '-',
8 DOCUMENT_END: '.'
9};
10const Type = {
11 ALIAS: 'ALIAS',
12 BLANK_LINE: 'BLANK_LINE',
13 BLOCK_FOLDED: 'BLOCK_FOLDED',
14 BLOCK_LITERAL: 'BLOCK_LITERAL',
15 COMMENT: 'COMMENT',
16 DIRECTIVE: 'DIRECTIVE',
17 DOCUMENT: 'DOCUMENT',
18 FLOW_MAP: 'FLOW_MAP',
19 FLOW_SEQ: 'FLOW_SEQ',
20 MAP: 'MAP',
21 MAP_KEY: 'MAP_KEY',
22 MAP_VALUE: 'MAP_VALUE',
23 PLAIN: 'PLAIN',
24 QUOTE_DOUBLE: 'QUOTE_DOUBLE',
25 QUOTE_SINGLE: 'QUOTE_SINGLE',
26 SEQ: 'SEQ',
27 SEQ_ITEM: 'SEQ_ITEM'
28};
29const defaultTagPrefix = 'tag:yaml.org,2002:';
30const defaultTags = {
31 MAP: 'tag:yaml.org,2002:map',
32 SEQ: 'tag:yaml.org,2002:seq',
33 STR: 'tag:yaml.org,2002:str'
34};
35
36function findLineStarts(src) {
37 const ls = [0];
38 let offset = src.indexOf('\n');
39
40 while (offset !== -1) {
41 offset += 1;
42 ls.push(offset);
43 offset = src.indexOf('\n', offset);
44 }
45
46 return ls;
47}
48
49function getSrcInfo(cst) {
50 let lineStarts, src;
51
52 if (typeof cst === 'string') {
53 lineStarts = findLineStarts(cst);
54 src = cst;
55 } else {
56 if (Array.isArray(cst)) cst = cst[0];
57
58 if (cst && cst.context) {
59 if (!cst.lineStarts) cst.lineStarts = findLineStarts(cst.context.src);
60 lineStarts = cst.lineStarts;
61 src = cst.context.src;
62 }
63 }
64
65 return {
66 lineStarts,
67 src
68 };
69}
70/**
71 * @typedef {Object} LinePos - One-indexed position in the source
72 * @property {number} line
73 * @property {number} col
74 */
75
76/**
77 * Determine the line/col position matching a character offset.
78 *
79 * Accepts a source string or a CST document as the second parameter. With
80 * the latter, starting indices for lines are cached in the document as
81 * `lineStarts: number[]`.
82 *
83 * Returns a one-indexed `{ line, col }` location if found, or
84 * `undefined` otherwise.
85 *
86 * @param {number} offset
87 * @param {string|Document|Document[]} cst
88 * @returns {?LinePos}
89 */
90
91
92function getLinePos(offset, cst) {
93 if (typeof offset !== 'number' || offset < 0) return null;
94 const {
95 lineStarts,
96 src
97 } = getSrcInfo(cst);
98 if (!lineStarts || !src || offset > src.length) return null;
99
100 for (let i = 0; i < lineStarts.length; ++i) {
101 const start = lineStarts[i];
102
103 if (offset < start) {
104 return {
105 line: i,
106 col: offset - lineStarts[i - 1] + 1
107 };
108 }
109
110 if (offset === start) return {
111 line: i + 1,
112 col: 1
113 };
114 }
115
116 const line = lineStarts.length;
117 return {
118 line,
119 col: offset - lineStarts[line - 1] + 1
120 };
121}
122/**
123 * Get a specified line from the source.
124 *
125 * Accepts a source string or a CST document as the second parameter. With
126 * the latter, starting indices for lines are cached in the document as
127 * `lineStarts: number[]`.
128 *
129 * Returns the line as a string if found, or `null` otherwise.
130 *
131 * @param {number} line One-indexed line number
132 * @param {string|Document|Document[]} cst
133 * @returns {?string}
134 */
135
136function getLine(line, cst) {
137 const {
138 lineStarts,
139 src
140 } = getSrcInfo(cst);
141 if (!lineStarts || !(line >= 1) || line > lineStarts.length) return null;
142 const start = lineStarts[line - 1];
143 let end = lineStarts[line]; // undefined for last line; that's ok for slice()
144
145 while (end && end > start && src[end - 1] === '\n') --end;
146
147 return src.slice(start, end);
148}
149/**
150 * Pretty-print the starting line from the source indicated by the range `pos`
151 *
152 * Trims output to `maxWidth` chars while keeping the starting column visible,
153 * using `…` at either end to indicate dropped characters.
154 *
155 * Returns a two-line string (or `null`) with `\n` as separator; the second line
156 * will hold appropriately indented `^` marks indicating the column range.
157 *
158 * @param {Object} pos
159 * @param {LinePos} pos.start
160 * @param {LinePos} [pos.end]
161 * @param {string|Document|Document[]*} cst
162 * @param {number} [maxWidth=80]
163 * @returns {?string}
164 */
165
166function getPrettyContext({
167 start,
168 end
169}, cst, maxWidth = 80) {
170 let src = getLine(start.line, cst);
171 if (!src) return null;
172 let {
173 col
174 } = start;
175
176 if (src.length > maxWidth) {
177 if (col <= maxWidth - 10) {
178 src = src.substr(0, maxWidth - 1) + '…';
179 } else {
180 const halfWidth = Math.round(maxWidth / 2);
181 if (src.length > col + halfWidth) src = src.substr(0, col + halfWidth - 1) + '…';
182 col -= src.length - maxWidth;
183 src = '…' + src.substr(1 - maxWidth);
184 }
185 }
186
187 let errLen = 1;
188 let errEnd = '';
189
190 if (end) {
191 if (end.line === start.line && col + (end.col - start.col) <= maxWidth + 1) {
192 errLen = end.col - start.col;
193 } else {
194 errLen = Math.min(src.length + 1, maxWidth) - col;
195 errEnd = '…';
196 }
197 }
198
199 const offset = col > 1 ? ' '.repeat(col - 1) : '';
200 const err = '^'.repeat(errLen);
201 return `${src}\n${offset}${err}${errEnd}`;
202}
203
204class Range {
205 static copy(orig) {
206 return new Range(orig.start, orig.end);
207 }
208
209 constructor(start, end) {
210 this.start = start;
211 this.end = end || start;
212 }
213
214 isEmpty() {
215 return typeof this.start !== 'number' || !this.end || this.end <= this.start;
216 }
217 /**
218 * Set `origStart` and `origEnd` to point to the original source range for
219 * this node, which may differ due to dropped CR characters.
220 *
221 * @param {number[]} cr - Positions of dropped CR characters
222 * @param {number} offset - Starting index of `cr` from the last call
223 * @returns {number} - The next offset, matching the one found for `origStart`
224 */
225
226
227 setOrigRange(cr, offset) {
228 const {
229 start,
230 end
231 } = this;
232
233 if (cr.length === 0 || end <= cr[0]) {
234 this.origStart = start;
235 this.origEnd = end;
236 return offset;
237 }
238
239 let i = offset;
240
241 while (i < cr.length) {
242 if (cr[i] > start) break;else ++i;
243 }
244
245 this.origStart = start + i;
246 const nextOffset = i;
247
248 while (i < cr.length) {
249 // if end was at \n, it should now be at \r
250 if (cr[i] >= end) break;else ++i;
251 }
252
253 this.origEnd = end + i;
254 return nextOffset;
255 }
256
257}
258
259/** Root class of all nodes */
260
261class Node {
262 static addStringTerminator(src, offset, str) {
263 if (str[str.length - 1] === '\n') return str;
264 const next = Node.endOfWhiteSpace(src, offset);
265 return next >= src.length || src[next] === '\n' ? str + '\n' : str;
266 } // ^(---|...)
267
268
269 static atDocumentBoundary(src, offset, sep) {
270 const ch0 = src[offset];
271 if (!ch0) return true;
272 const prev = src[offset - 1];
273 if (prev && prev !== '\n') return false;
274
275 if (sep) {
276 if (ch0 !== sep) return false;
277 } else {
278 if (ch0 !== Char.DIRECTIVES_END && ch0 !== Char.DOCUMENT_END) return false;
279 }
280
281 const ch1 = src[offset + 1];
282 const ch2 = src[offset + 2];
283 if (ch1 !== ch0 || ch2 !== ch0) return false;
284 const ch3 = src[offset + 3];
285 return !ch3 || ch3 === '\n' || ch3 === '\t' || ch3 === ' ';
286 }
287
288 static endOfIdentifier(src, offset) {
289 let ch = src[offset];
290 const isVerbatim = ch === '<';
291 const notOk = isVerbatim ? ['\n', '\t', ' ', '>'] : ['\n', '\t', ' ', '[', ']', '{', '}', ','];
292
293 while (ch && notOk.indexOf(ch) === -1) ch = src[offset += 1];
294
295 if (isVerbatim && ch === '>') offset += 1;
296 return offset;
297 }
298
299 static endOfIndent(src, offset) {
300 let ch = src[offset];
301
302 while (ch === ' ') ch = src[offset += 1];
303
304 return offset;
305 }
306
307 static endOfLine(src, offset) {
308 let ch = src[offset];
309
310 while (ch && ch !== '\n') ch = src[offset += 1];
311
312 return offset;
313 }
314
315 static endOfWhiteSpace(src, offset) {
316 let ch = src[offset];
317
318 while (ch === '\t' || ch === ' ') ch = src[offset += 1];
319
320 return offset;
321 }
322
323 static startOfLine(src, offset) {
324 let ch = src[offset - 1];
325 if (ch === '\n') return offset;
326
327 while (ch && ch !== '\n') ch = src[offset -= 1];
328
329 return offset + 1;
330 }
331 /**
332 * End of indentation, or null if the line's indent level is not more
333 * than `indent`
334 *
335 * @param {string} src
336 * @param {number} indent
337 * @param {number} lineStart
338 * @returns {?number}
339 */
340
341
342 static endOfBlockIndent(src, indent, lineStart) {
343 const inEnd = Node.endOfIndent(src, lineStart);
344
345 if (inEnd > lineStart + indent) {
346 return inEnd;
347 } else {
348 const wsEnd = Node.endOfWhiteSpace(src, inEnd);
349 const ch = src[wsEnd];
350 if (!ch || ch === '\n') return wsEnd;
351 }
352
353 return null;
354 }
355
356 static atBlank(src, offset, endAsBlank) {
357 const ch = src[offset];
358 return ch === '\n' || ch === '\t' || ch === ' ' || endAsBlank && !ch;
359 }
360
361 static nextNodeIsIndented(ch, indentDiff, indicatorAsIndent) {
362 if (!ch || indentDiff < 0) return false;
363 if (indentDiff > 0) return true;
364 return indicatorAsIndent && ch === '-';
365 } // should be at line or string end, or at next non-whitespace char
366
367
368 static normalizeOffset(src, offset) {
369 const ch = src[offset];
370 return !ch ? offset : ch !== '\n' && src[offset - 1] === '\n' ? offset - 1 : Node.endOfWhiteSpace(src, offset);
371 } // fold single newline into space, multiple newlines to N - 1 newlines
372 // presumes src[offset] === '\n'
373
374
375 static foldNewline(src, offset, indent) {
376 let inCount = 0;
377 let error = false;
378 let fold = '';
379 let ch = src[offset + 1];
380
381 while (ch === ' ' || ch === '\t' || ch === '\n') {
382 switch (ch) {
383 case '\n':
384 inCount = 0;
385 offset += 1;
386 fold += '\n';
387 break;
388
389 case '\t':
390 if (inCount <= indent) error = true;
391 offset = Node.endOfWhiteSpace(src, offset + 2) - 1;
392 break;
393
394 case ' ':
395 inCount += 1;
396 offset += 1;
397 break;
398 }
399
400 ch = src[offset + 1];
401 }
402
403 if (!fold) fold = ' ';
404 if (ch && inCount <= indent) error = true;
405 return {
406 fold,
407 offset,
408 error
409 };
410 }
411
412 constructor(type, props, context) {
413 Object.defineProperty(this, 'context', {
414 value: context || null,
415 writable: true
416 });
417 this.error = null;
418 this.range = null;
419 this.valueRange = null;
420 this.props = props || [];
421 this.type = type;
422 this.value = null;
423 }
424
425 getPropValue(idx, key, skipKey) {
426 if (!this.context) return null;
427 const {
428 src
429 } = this.context;
430 const prop = this.props[idx];
431 return prop && src[prop.start] === key ? src.slice(prop.start + (skipKey ? 1 : 0), prop.end) : null;
432 }
433
434 get anchor() {
435 for (let i = 0; i < this.props.length; ++i) {
436 const anchor = this.getPropValue(i, Char.ANCHOR, true);
437 if (anchor != null) return anchor;
438 }
439
440 return null;
441 }
442
443 get comment() {
444 const comments = [];
445
446 for (let i = 0; i < this.props.length; ++i) {
447 const comment = this.getPropValue(i, Char.COMMENT, true);
448 if (comment != null) comments.push(comment);
449 }
450
451 return comments.length > 0 ? comments.join('\n') : null;
452 }
453
454 commentHasRequiredWhitespace(start) {
455 const {
456 src
457 } = this.context;
458 if (this.header && start === this.header.end) return false;
459 if (!this.valueRange) return false;
460 const {
461 end
462 } = this.valueRange;
463 return start !== end || Node.atBlank(src, end - 1);
464 }
465
466 get hasComment() {
467 if (this.context) {
468 const {
469 src
470 } = this.context;
471
472 for (let i = 0; i < this.props.length; ++i) {
473 if (src[this.props[i].start] === Char.COMMENT) return true;
474 }
475 }
476
477 return false;
478 }
479
480 get hasProps() {
481 if (this.context) {
482 const {
483 src
484 } = this.context;
485
486 for (let i = 0; i < this.props.length; ++i) {
487 if (src[this.props[i].start] !== Char.COMMENT) return true;
488 }
489 }
490
491 return false;
492 }
493
494 get includesTrailingLines() {
495 return false;
496 }
497
498 get jsonLike() {
499 const jsonLikeTypes = [Type.FLOW_MAP, Type.FLOW_SEQ, Type.QUOTE_DOUBLE, Type.QUOTE_SINGLE];
500 return jsonLikeTypes.indexOf(this.type) !== -1;
501 }
502
503 get rangeAsLinePos() {
504 if (!this.range || !this.context) return undefined;
505 const start = getLinePos(this.range.start, this.context.root);
506 if (!start) return undefined;
507 const end = getLinePos(this.range.end, this.context.root);
508 return {
509 start,
510 end
511 };
512 }
513
514 get rawValue() {
515 if (!this.valueRange || !this.context) return null;
516 const {
517 start,
518 end
519 } = this.valueRange;
520 return this.context.src.slice(start, end);
521 }
522
523 get tag() {
524 for (let i = 0; i < this.props.length; ++i) {
525 const tag = this.getPropValue(i, Char.TAG, false);
526
527 if (tag != null) {
528 if (tag[1] === '<') {
529 return {
530 verbatim: tag.slice(2, -1)
531 };
532 } else {
533 // eslint-disable-next-line no-unused-vars
534 const [_, handle, suffix] = tag.match(/^(.*!)([^!]*)$/);
535 return {
536 handle,
537 suffix
538 };
539 }
540 }
541 }
542
543 return null;
544 }
545
546 get valueRangeContainsNewline() {
547 if (!this.valueRange || !this.context) return false;
548 const {
549 start,
550 end
551 } = this.valueRange;
552 const {
553 src
554 } = this.context;
555
556 for (let i = start; i < end; ++i) {
557 if (src[i] === '\n') return true;
558 }
559
560 return false;
561 }
562
563 parseComment(start) {
564 const {
565 src
566 } = this.context;
567
568 if (src[start] === Char.COMMENT) {
569 const end = Node.endOfLine(src, start + 1);
570 const commentRange = new Range(start, end);
571 this.props.push(commentRange);
572 return end;
573 }
574
575 return start;
576 }
577 /**
578 * Populates the `origStart` and `origEnd` values of all ranges for this
579 * node. Extended by child classes to handle descendant nodes.
580 *
581 * @param {number[]} cr - Positions of dropped CR characters
582 * @param {number} offset - Starting index of `cr` from the last call
583 * @returns {number} - The next offset, matching the one found for `origStart`
584 */
585
586
587 setOrigRanges(cr, offset) {
588 if (this.range) offset = this.range.setOrigRange(cr, offset);
589 if (this.valueRange) this.valueRange.setOrigRange(cr, offset);
590 this.props.forEach(prop => prop.setOrigRange(cr, offset));
591 return offset;
592 }
593
594 toString() {
595 const {
596 context: {
597 src
598 },
599 range,
600 value
601 } = this;
602 if (value != null) return value;
603 const str = src.slice(range.start, range.end);
604 return Node.addStringTerminator(src, range.end, str);
605 }
606
607}
608
609class YAMLError extends Error {
610 constructor(name, source, message) {
611 if (!message || !(source instanceof Node)) throw new Error(`Invalid arguments for new ${name}`);
612 super();
613 this.name = name;
614 this.message = message;
615 this.source = source;
616 }
617
618 makePretty() {
619 if (!this.source) return;
620 this.nodeType = this.source.type;
621 const cst = this.source.context && this.source.context.root;
622
623 if (typeof this.offset === 'number') {
624 this.range = new Range(this.offset, this.offset + 1);
625 const start = cst && getLinePos(this.offset, cst);
626
627 if (start) {
628 const end = {
629 line: start.line,
630 col: start.col + 1
631 };
632 this.linePos = {
633 start,
634 end
635 };
636 }
637
638 delete this.offset;
639 } else {
640 this.range = this.source.range;
641 this.linePos = this.source.rangeAsLinePos;
642 }
643
644 if (this.linePos) {
645 const {
646 line,
647 col
648 } = this.linePos.start;
649 this.message += ` at line ${line}, column ${col}`;
650 const ctx = cst && getPrettyContext(this.linePos, cst);
651 if (ctx) this.message += `:\n\n${ctx}\n`;
652 }
653
654 delete this.source;
655 }
656
657}
658class YAMLReferenceError extends YAMLError {
659 constructor(source, message) {
660 super('YAMLReferenceError', source, message);
661 }
662
663}
664class YAMLSemanticError extends YAMLError {
665 constructor(source, message) {
666 super('YAMLSemanticError', source, message);
667 }
668
669}
670class YAMLSyntaxError extends YAMLError {
671 constructor(source, message) {
672 super('YAMLSyntaxError', source, message);
673 }
674
675}
676class YAMLWarning extends YAMLError {
677 constructor(source, message) {
678 super('YAMLWarning', source, message);
679 }
680
681}
682
683function _defineProperty(obj, key, value) {
684 if (key in obj) {
685 Object.defineProperty(obj, key, {
686 value: value,
687 enumerable: true,
688 configurable: true,
689 writable: true
690 });
691 } else {
692 obj[key] = value;
693 }
694
695 return obj;
696}
697
698class PlainValue extends Node {
699 static endOfLine(src, start, inFlow) {
700 let ch = src[start];
701 let offset = start;
702
703 while (ch && ch !== '\n') {
704 if (inFlow && (ch === '[' || ch === ']' || ch === '{' || ch === '}' || ch === ',')) break;
705 const next = src[offset + 1];
706 if (ch === ':' && (!next || next === '\n' || next === '\t' || next === ' ' || inFlow && next === ',')) break;
707 if ((ch === ' ' || ch === '\t') && next === '#') break;
708 offset += 1;
709 ch = next;
710 }
711
712 return offset;
713 }
714
715 get strValue() {
716 if (!this.valueRange || !this.context) return null;
717 let {
718 start,
719 end
720 } = this.valueRange;
721 const {
722 src
723 } = this.context;
724 let ch = src[end - 1];
725
726 while (start < end && (ch === '\n' || ch === '\t' || ch === ' ')) ch = src[--end - 1];
727
728 let str = '';
729
730 for (let i = start; i < end; ++i) {
731 const ch = src[i];
732
733 if (ch === '\n') {
734 const {
735 fold,
736 offset
737 } = Node.foldNewline(src, i, -1);
738 str += fold;
739 i = offset;
740 } else if (ch === ' ' || ch === '\t') {
741 // trim trailing whitespace
742 const wsStart = i;
743 let next = src[i + 1];
744
745 while (i < end && (next === ' ' || next === '\t')) {
746 i += 1;
747 next = src[i + 1];
748 }
749
750 if (next !== '\n') str += i > wsStart ? src.slice(wsStart, i + 1) : ch;
751 } else {
752 str += ch;
753 }
754 }
755
756 const ch0 = src[start];
757
758 switch (ch0) {
759 case '\t':
760 {
761 const msg = 'Plain value cannot start with a tab character';
762 const errors = [new YAMLSemanticError(this, msg)];
763 return {
764 errors,
765 str
766 };
767 }
768
769 case '@':
770 case '`':
771 {
772 const msg = `Plain value cannot start with reserved character ${ch0}`;
773 const errors = [new YAMLSemanticError(this, msg)];
774 return {
775 errors,
776 str
777 };
778 }
779
780 default:
781 return str;
782 }
783 }
784
785 parseBlockValue(start) {
786 const {
787 indent,
788 inFlow,
789 src
790 } = this.context;
791 let offset = start;
792 let valueEnd = start;
793
794 for (let ch = src[offset]; ch === '\n'; ch = src[offset]) {
795 if (Node.atDocumentBoundary(src, offset + 1)) break;
796 const end = Node.endOfBlockIndent(src, indent, offset + 1);
797 if (end === null || src[end] === '#') break;
798
799 if (src[end] === '\n') {
800 offset = end;
801 } else {
802 valueEnd = PlainValue.endOfLine(src, end, inFlow);
803 offset = valueEnd;
804 }
805 }
806
807 if (this.valueRange.isEmpty()) this.valueRange.start = start;
808 this.valueRange.end = valueEnd;
809 return valueEnd;
810 }
811 /**
812 * Parses a plain value from the source
813 *
814 * Accepted forms are:
815 * ```
816 * #comment
817 *
818 * first line
819 *
820 * first line #comment
821 *
822 * first line
823 * block
824 * lines
825 *
826 * #comment
827 * block
828 * lines
829 * ```
830 * where block lines are empty or have an indent level greater than `indent`.
831 *
832 * @param {ParseContext} context
833 * @param {number} start - Index of first character
834 * @returns {number} - Index of the character after this scalar, may be `\n`
835 */
836
837
838 parse(context, start) {
839 this.context = context;
840 const {
841 inFlow,
842 src
843 } = context;
844 let offset = start;
845 const ch = src[offset];
846
847 if (ch && ch !== '#' && ch !== '\n') {
848 offset = PlainValue.endOfLine(src, start, inFlow);
849 }
850
851 this.valueRange = new Range(start, offset);
852 offset = Node.endOfWhiteSpace(src, offset);
853 offset = this.parseComment(offset);
854
855 if (!this.hasComment || this.valueRange.isEmpty()) {
856 offset = this.parseBlockValue(offset);
857 }
858
859 return offset;
860 }
861
862}
863
864exports.Char = Char;
865exports.Node = Node;
866exports.PlainValue = PlainValue;
867exports.Range = Range;
868exports.Type = Type;
869exports.YAMLError = YAMLError;
870exports.YAMLReferenceError = YAMLReferenceError;
871exports.YAMLSemanticError = YAMLSemanticError;
872exports.YAMLSyntaxError = YAMLSyntaxError;
873exports.YAMLWarning = YAMLWarning;
874exports._defineProperty = _defineProperty;
875exports.defaultTagPrefix = defaultTagPrefix;
876exports.defaultTags = defaultTags;
Note: See TracBrowser for help on using the repository browser.