| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var parseCst = require('./parse-cst.js');
|
|---|
| 4 | var Document = require('./Document-a8d0fbf9.js');
|
|---|
| 5 | require('./PlainValue-516d5bc2.js');
|
|---|
| 6 | require('./resolveSeq-95613e94.js');
|
|---|
| 7 | require('./Schema-bcc6c2d7.js');
|
|---|
| 8 | require('./warnings-793925ce.js');
|
|---|
| 9 |
|
|---|
| 10 | // test harness for yaml-test-suite event tests
|
|---|
| 11 | function testEvents(src, options) {
|
|---|
| 12 | const opt = Object.assign({
|
|---|
| 13 | keepCstNodes: true,
|
|---|
| 14 | keepNodeTypes: true,
|
|---|
| 15 | version: '1.2'
|
|---|
| 16 | }, options);
|
|---|
| 17 | const docs = parseCst.parse(src).map(cstDoc => new Document.Document(opt).parse(cstDoc));
|
|---|
| 18 | const errDoc = docs.find(doc => doc.errors.length > 0);
|
|---|
| 19 | const error = errDoc ? errDoc.errors[0].message : null;
|
|---|
| 20 | const events = ['+STR'];
|
|---|
| 21 | try {
|
|---|
| 22 | for (let i = 0; i < docs.length; ++i) {
|
|---|
| 23 | const doc = docs[i];
|
|---|
| 24 | let root = doc.contents;
|
|---|
| 25 | if (Array.isArray(root)) root = root[0];
|
|---|
| 26 | const [rootStart, rootEnd] = doc.range || [0, 0];
|
|---|
| 27 | let e = doc.errors[0] && doc.errors[0].source;
|
|---|
| 28 | if (e && e.type === 'SEQ_ITEM') e = e.node;
|
|---|
| 29 | if (e && (e.type === 'DOCUMENT' || e.range.start < rootStart)) throw new Error();
|
|---|
| 30 | let docStart = '+DOC';
|
|---|
| 31 | const pre = src.slice(0, rootStart);
|
|---|
| 32 | const explicitDoc = /---\s*$/.test(pre);
|
|---|
| 33 | if (explicitDoc) docStart += ' ---';else if (!doc.contents) continue;
|
|---|
| 34 | events.push(docStart);
|
|---|
| 35 | addEvents(events, doc, e, root);
|
|---|
| 36 | if (doc.contents && doc.contents.length > 1) throw new Error();
|
|---|
| 37 | let docEnd = '-DOC';
|
|---|
| 38 | if (rootEnd) {
|
|---|
| 39 | const post = src.slice(rootEnd);
|
|---|
| 40 | if (/^\.\.\./.test(post)) docEnd += ' ...';
|
|---|
| 41 | }
|
|---|
| 42 | events.push(docEnd);
|
|---|
| 43 | }
|
|---|
| 44 | } catch (e) {
|
|---|
| 45 | return {
|
|---|
| 46 | events,
|
|---|
| 47 | error: error || e
|
|---|
| 48 | };
|
|---|
| 49 | }
|
|---|
| 50 | events.push('-STR');
|
|---|
| 51 | return {
|
|---|
| 52 | events,
|
|---|
| 53 | error
|
|---|
| 54 | };
|
|---|
| 55 | }
|
|---|
| 56 | function addEvents(events, doc, e, node) {
|
|---|
| 57 | if (!node) {
|
|---|
| 58 | events.push('=VAL :');
|
|---|
| 59 | return;
|
|---|
| 60 | }
|
|---|
| 61 | if (e && node.cstNode === e) throw new Error();
|
|---|
| 62 | let props = '';
|
|---|
| 63 | let anchor = doc.anchors.getName(node);
|
|---|
| 64 | if (anchor) {
|
|---|
| 65 | if (/\d$/.test(anchor)) {
|
|---|
| 66 | const alt = anchor.replace(/\d$/, '');
|
|---|
| 67 | if (doc.anchors.getNode(alt)) anchor = alt;
|
|---|
| 68 | }
|
|---|
| 69 | props = ` &${anchor}`;
|
|---|
| 70 | }
|
|---|
| 71 | if (node.cstNode && node.cstNode.tag) {
|
|---|
| 72 | const {
|
|---|
| 73 | handle,
|
|---|
| 74 | suffix
|
|---|
| 75 | } = node.cstNode.tag;
|
|---|
| 76 | props += handle === '!' && !suffix ? ' <!>' : ` <${node.tag}>`;
|
|---|
| 77 | }
|
|---|
| 78 | let scalar = null;
|
|---|
| 79 | switch (node.type) {
|
|---|
| 80 | case 'ALIAS':
|
|---|
| 81 | {
|
|---|
| 82 | let alias = doc.anchors.getName(node.source);
|
|---|
| 83 | if (/\d$/.test(alias)) {
|
|---|
| 84 | const alt = alias.replace(/\d$/, '');
|
|---|
| 85 | if (doc.anchors.getNode(alt)) alias = alt;
|
|---|
| 86 | }
|
|---|
| 87 | events.push(`=ALI${props} *${alias}`);
|
|---|
| 88 | }
|
|---|
| 89 | break;
|
|---|
| 90 | case 'BLOCK_FOLDED':
|
|---|
| 91 | scalar = '>';
|
|---|
| 92 | break;
|
|---|
| 93 | case 'BLOCK_LITERAL':
|
|---|
| 94 | scalar = '|';
|
|---|
| 95 | break;
|
|---|
| 96 | case 'PLAIN':
|
|---|
| 97 | scalar = ':';
|
|---|
| 98 | break;
|
|---|
| 99 | case 'QUOTE_DOUBLE':
|
|---|
| 100 | scalar = '"';
|
|---|
| 101 | break;
|
|---|
| 102 | case 'QUOTE_SINGLE':
|
|---|
| 103 | scalar = "'";
|
|---|
| 104 | break;
|
|---|
| 105 | case 'PAIR':
|
|---|
| 106 | events.push(`+MAP${props}`);
|
|---|
| 107 | addEvents(events, doc, e, node.key);
|
|---|
| 108 | addEvents(events, doc, e, node.value);
|
|---|
| 109 | events.push('-MAP');
|
|---|
| 110 | break;
|
|---|
| 111 | case 'FLOW_SEQ':
|
|---|
| 112 | case 'SEQ':
|
|---|
| 113 | {
|
|---|
| 114 | const ev = node.type === 'FLOW_SEQ' ? '+SEQ []' : '+SEQ';
|
|---|
| 115 | events.push(`${ev}${props}`);
|
|---|
| 116 | node.items.forEach(item => {
|
|---|
| 117 | addEvents(events, doc, e, item);
|
|---|
| 118 | });
|
|---|
| 119 | events.push('-SEQ');
|
|---|
| 120 | break;
|
|---|
| 121 | }
|
|---|
| 122 | case 'FLOW_MAP':
|
|---|
| 123 | case 'MAP':
|
|---|
| 124 | {
|
|---|
| 125 | const ev = node.type === 'FLOW_SEQ' ? '+MAP {}' : '+MAP';
|
|---|
| 126 | events.push(`${ev}${props}`);
|
|---|
| 127 | node.items.forEach(({
|
|---|
| 128 | key,
|
|---|
| 129 | value
|
|---|
| 130 | }) => {
|
|---|
| 131 | addEvents(events, doc, e, key);
|
|---|
| 132 | addEvents(events, doc, e, value);
|
|---|
| 133 | });
|
|---|
| 134 | events.push('-MAP');
|
|---|
| 135 | break;
|
|---|
| 136 | }
|
|---|
| 137 | default:
|
|---|
| 138 | throw new Error(`Unexpected node type ${node.type}`);
|
|---|
| 139 | }
|
|---|
| 140 | if (scalar) {
|
|---|
| 141 | const value = node.cstNode.strValue.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');
|
|---|
| 142 | events.push(`=VAL${props} ${scalar}${value}`);
|
|---|
| 143 | }
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | exports.testEvents = testEvents;
|
|---|