-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
48 lines (43 loc) · 909 Bytes
/
utils.js
File metadata and controls
48 lines (43 loc) · 909 Bytes
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
const isEmpty = (arr) => arr.length === 0;
const optional = (parser) => {
return (str) => {
var x = parser(str);
if (x) {
return x;
}
return [[], str];
};
};
const zeroOrMore = (parser) => {
return optional(oneOrMore(parser));
};
const oneOrMore = (parser) => {
return (str) => {
var nodes = [];
var x = parser(str);
if (!x) {
return;
}
tail = x[1];
nodes.push(x[0]);
while (tail.length && (x = parser(tail))) {
tail = x[1];
nodes.push(x[0]);
}
return [nodes, tail];
};
};
const combine = (...parsers) => {
return (str) => {
var nodes = [];
var tail = str;
for (const parser of parsers) {
var x = parser(tail);
if (!x) return undefined;
nodes.push(x[0]);
tail = x[1];
}
return [nodes, tail];
};
};
module.exports = { isEmpty, optional, zeroOrMore, oneOrMore, combine };