-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.js
More file actions
144 lines (126 loc) · 4 KB
/
Copy pathparser.js
File metadata and controls
144 lines (126 loc) · 4 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
const through = require('through2');
const yaml = require('js-yaml');
const re = {
ok: new RegExp([
'^(\\s\\s\\s\\s)?(not )?ok\\b(?:',
'(?:\\s+(\\d+))?(?:\\s+(?:(?:\\s*-\\s*)?(.*)))?',
')?',
].join('')),
plan: /^(\s\s\s\s)?(\d+)\.\.(\d+)/,
comment: /^#\s*(.+)/,
unbuffered_subtest: /^#\sSUBTEST:\s(.+)/i,
version: /^TAP\s+version\s+(\d+)/i,
label_todo: /^(.*?)\s*#\s*TODO\s*(.*)$/i,
label_skip: /^(.*?)\s*#\s*SKIP\s*(.*)$/i,
diag_open: /^\s+---$/,
diag_close: /^\s+\.\.\.$/,
bail_out: /^Bail out!.*$/,
};
function Parser() {
let linePart = '';
let lineNum = 0;
let state = 'normal';
let diagLines = [];
let bailing = false;
function parseLine(line) {
if (bailing) return null;
if (state === 'in-diag') {
const m = re.diag_close.exec(line);
if (!m) {
diagLines.push(line);
return null;
}
state = 'normal';
try {
const diagText = diagLines.join('\n').replace(/\\'/g, "''"); // note: js-yaml expects '' as escape for ' rather than \'
const value = yaml.load(diagText);
// Note: yaml does not support values specified as "undefined" (without the quotes)
// and js-yaml ends up parsing it as a string, which causes any failure message
// to erroneously indicate that a string was found.
// It would be possible to expand the yaml spec with a special !!js/undefined tag
// tag and parse that as actual undefined type (see: https://github.com/nodeca/js-yaml-js-types )
// but that would require TAP runners to output that and if they did that, then other
// formatters would probably choke on it. So it does not seem feasible to improve
// the situation without locking the runner to the formatter.
// so as a hack, just always convert the string "undefined" to the type undefined.
// and only do it for top level values (no "deep" hacking).
if (value.expected === 'undefined') value.expected = undefined;
if (value.actual === 'undefined') value.actual = undefined;
diagLines = [];
return { type: 'diag', value };
} catch (e) {
return {
type: 'parseError',
line: lineNum,
message: 'failed to parse yaml in diagnostic block',
reason: e,
};
}
}
let m = re.version.exec(line);
if (m) {
const ver = /^\d+(\.\d*)?$/.test(m[1]) ? Number(m[1]) : m[1];
return { type: 'version', value: ver };
}
m = re.comment.exec(line);
if (m) {
if (re.unbuffered_subtest.exec(line)) {
// unbuffered subtest starting
return null;
}
return { type: 'comment', value: m[1] };
}
m = re.ok.exec(line);
if (m) {
const isChild = !!m[1];
const ok = !m[2];
const number = m[3] && Number(m[3]);
const name = m[4];
const okObj = { type: 'assert', isChild, ok, number, name, skipReason: null, todoReason: null };
let m2 = re.label_skip.exec(name);
if (m2) {
okObj.name = m2[1];
okObj.skipReason = m2[2];
}
m2 = re.label_todo.exec(name);
if (m2) {
okObj.name = m2[1];
okObj.todoReason = m2[2];
}
return okObj;
}
m = re.plan.exec(line);
if (m) {
const isChild = !!m[1];
return { type: 'plan', isChild, start: Number(m[2]), end: Number(m[3]) };
}
m = re.diag_open.exec(line);
if (m) {
state = 'in-diag';
return null;
}
if (line.length === 0) {
// ignore empty lines
return null;
}
m = re.bail_out.exec(line);
if (m) {
bailing = true;
}
return { type: 'extra', value: line };
}
const parser = through({ objectMode: true }, function transform(chunk, enc, cb) {
const lines = (linePart + chunk).split('\n');
for (const line of lines) {
lineNum++;
const obj = parseLine(line);
if (obj) {
this.push(obj);
}
}
linePart = lines[lines.length - 1];
cb();
});
return parser;
}
module.exports = Parser;