File size: 4,274 Bytes
31e8457 | 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 | 'use strict';
var identity = require('./nodes/identity.js');
var publicApi = require('./public-api.js');
var visit = require('./visit.js');
const scalarChar = {
BLOCK_FOLDED: '>',
BLOCK_LITERAL: '|',
PLAIN: ':',
QUOTE_DOUBLE: '"',
QUOTE_SINGLE: "'"
};
function anchorExists(doc, anchor) {
let found = false;
visit.visit(doc, {
Value(_key, node) {
if (node.anchor === anchor) {
found = true;
return visit.visit.BREAK;
}
}
});
return found;
}
// test harness for yaml-test-suite event tests
function testEvents(src) {
const docs = publicApi.parseAllDocuments(src);
const errDoc = docs.find(doc => doc.errors.length > 0);
const error = errDoc ? errDoc.errors[0].message : null;
const events = ['+STR'];
try {
for (let i = 0; i < docs.length; ++i) {
const doc = docs[i];
let root = doc.contents;
if (Array.isArray(root))
root = root[0];
const [rootStart] = doc.range || [0];
const error = doc.errors[0];
if (error && (!error.pos || error.pos[0] < rootStart))
throw new Error();
let docStart = '+DOC';
if (doc.directives.docStart)
docStart += ' ---';
else if (doc.contents &&
doc.contents.range[2] === doc.contents.range[0] &&
!doc.contents.anchor &&
!doc.contents.tag)
continue;
events.push(docStart);
addEvents(events, doc, error?.pos[0] ?? -1, root);
let docEnd = '-DOC';
if (doc.directives.docEnd)
docEnd += ' ...';
events.push(docEnd);
}
}
catch (e) {
return { events, error: error ?? e };
}
events.push('-STR');
return { events, error };
}
function addEvents(events, doc, errPos, node) {
if (!node) {
events.push('=VAL :');
return;
}
if (errPos !== -1 && identity.isNode(node) && node.range[0] >= errPos)
throw new Error();
let props = '';
let anchor = identity.isScalar(node) || identity.isCollection(node) ? node.anchor : undefined;
if (anchor) {
if (/\d$/.test(anchor)) {
const alt = anchor.replace(/\d$/, '');
if (anchorExists(doc, alt))
anchor = alt;
}
props = ` &${anchor}`;
}
if (identity.isNode(node) && node.tag)
props += ` <${node.tag}>`;
if (identity.isMap(node)) {
const ev = node.flow ? '+MAP {}' : '+MAP';
events.push(`${ev}${props}`);
node.items.forEach(({ key, value }) => {
addEvents(events, doc, errPos, key);
addEvents(events, doc, errPos, value);
});
events.push('-MAP');
}
else if (identity.isSeq(node)) {
const ev = node.flow ? '+SEQ []' : '+SEQ';
events.push(`${ev}${props}`);
node.items.forEach(item => {
addEvents(events, doc, errPos, item);
});
events.push('-SEQ');
}
else if (identity.isPair(node)) {
events.push(`+MAP${props}`);
addEvents(events, doc, errPos, node.key);
addEvents(events, doc, errPos, node.value);
events.push('-MAP');
}
else if (identity.isAlias(node)) {
let alias = node.source;
if (alias && /\d$/.test(alias)) {
const alt = alias.replace(/\d$/, '');
if (anchorExists(doc, alt))
alias = alt;
}
events.push(`=ALI${props} *${alias}`);
}
else {
const scalar = scalarChar[String(node.type)];
if (!scalar)
throw new Error(`Unexpected node type ${node.type}`);
const value = node.source
.replace(/\\/g, '\\\\')
.replace(/\0/g, '\\0')
.replace(/\x07/g, '\\a')
.replace(/\x08/g, '\\b')
.replace(/\t/g, '\\t')
.replace(/\n/g, '\\n')
.replace(/\v/g, '\\v')
.replace(/\f/g, '\\f')
.replace(/\r/g, '\\r')
.replace(/\x1b/g, '\\e');
events.push(`=VAL${props} ${scalar}${value}`);
}
}
exports.testEvents = testEvents;
|