test-events.js
3.92 KB
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
'use strict';
require('./PlainValue-ec8e588e.js');
var parseCst = require('./parse-cst.js');
require('./resolveSeq-4a68b39b.js');
var Document$1 = require('./Document-2cf6b08c.js');
require('./Schema-42e9705c.js');
require('./warnings-39684f17.js');
function testEvents(src, options) {
const opt = Object.assign({
keepCstNodes: true,
keepNodeTypes: true,
version: '1.2'
}, options);
const docs = parseCst.parse(src).map(cstDoc => new Document$1.Document(opt).parse(cstDoc));
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, rootEnd] = doc.range || [0, 0];
let e = doc.errors[0] && doc.errors[0].source;
if (e && e.type === 'SEQ_ITEM') e = e.node;
if (e && (e.type === 'DOCUMENT' || e.range.start < rootStart)) throw new Error();
let docStart = '+DOC';
const pre = src.slice(0, rootStart);
const explicitDoc = /---\s*$/.test(pre);
if (explicitDoc) docStart += ' ---';else if (!doc.contents) continue;
events.push(docStart);
addEvents(events, doc, e, root);
if (doc.contents && doc.contents.length > 1) throw new Error();
let docEnd = '-DOC';
if (rootEnd) {
const post = src.slice(rootEnd);
if (/^\.\.\./.test(post)) docEnd += ' ...';
}
events.push(docEnd);
}
} catch (e) {
return {
events,
error: error || e
};
}
events.push('-STR');
return {
events,
error
};
}
function addEvents(events, doc, e, node) {
if (!node) {
events.push('=VAL :');
return;
}
if (e && node.cstNode === e) throw new Error();
let props = '';
let anchor = doc.anchors.getName(node);
if (anchor) {
if (/\d$/.test(anchor)) {
const alt = anchor.replace(/\d$/, '');
if (doc.anchors.getNode(alt)) anchor = alt;
}
props = ` &${anchor}`;
}
if (node.cstNode && node.cstNode.tag) {
const {
handle,
suffix
} = node.cstNode.tag;
props += handle === '!' && !suffix ? ' <!>' : ` <${node.tag}>`;
}
let scalar = null;
switch (node.type) {
case 'ALIAS':
{
let alias = doc.anchors.getName(node.source);
if (/\d$/.test(alias)) {
const alt = alias.replace(/\d$/, '');
if (doc.anchors.getNode(alt)) alias = alt;
}
events.push(`=ALI${props} *${alias}`);
}
break;
case 'BLOCK_FOLDED':
scalar = '>';
break;
case 'BLOCK_LITERAL':
scalar = '|';
break;
case 'PLAIN':
scalar = ':';
break;
case 'QUOTE_DOUBLE':
scalar = '"';
break;
case 'QUOTE_SINGLE':
scalar = "'";
break;
case 'PAIR':
events.push(`+MAP${props}`);
addEvents(events, doc, e, node.key);
addEvents(events, doc, e, node.value);
events.push('-MAP');
break;
case 'FLOW_SEQ':
case 'SEQ':
events.push(`+SEQ${props}`);
node.items.forEach(item => {
addEvents(events, doc, e, item);
});
events.push('-SEQ');
break;
case 'FLOW_MAP':
case 'MAP':
events.push(`+MAP${props}`);
node.items.forEach(({
key,
value
}) => {
addEvents(events, doc, e, key);
addEvents(events, doc, e, value);
});
events.push('-MAP');
break;
default:
throw new Error(`Unexpected node type ${node.type}`);
}
if (scalar) {
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');
events.push(`=VAL${props} ${scalar}${value}`);
}
}
exports.testEvents = testEvents;