-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmessages.js
86 lines (76 loc) · 4.31 KB
/
messages.js
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
// Arbitrary value
const DEFAULT_MAX_DEPTH = 60;
function getRegexForId(id, rules, MAX_DEPTH = DEFAULT_MAX_DEPTH) {
let base_rules = Object.values(rules).filter(({ parts }) => typeof parts === 'string');
let computed_rules = {};
for (let { id, parts } of base_rules) {
computed_rules[id] = parts;
}
let computed_rules_for_id = buildUpIdFromRules(id, rules, computed_rules, MAX_DEPTH);
let regex_str = computed_rules_for_id.flat(Infinity).join('');
return new RegExp(`^${regex_str}$`);
}
/*
0: 4 1 5
1: 2 3 | 3 2
2: 4 4 | 5 5
3: 4 5 | 5 4
4: "a"
5: "b"
->
0: ['(', 4, 1, 5, ')']
1: ['(', 2, 3, '|', 3, 2, ')']
2: ['(', 4, 4, '|', 5, 5, ')']
3: ['(', 4, 5, '|', 5, 4, ')']
4: "a"
5: "b"
0: ['(', 4, 1, 5, ')']
0: ['(', "a", 1, 5, ')']
0: ['(', "a", ['(', 2, 3, '|', 3, 2, ')'], 5, ')']
0: ['(', "a", ['(', ['(', 4, 4, '|', 5, 5, ')'], 3, '|', 3, 2, ')'], 5, ')']
0: ['(', "a", ['(', ['(', "a", 4, '|', 5, 5, ')'], 3, '|', 3, 2, ')'], 5, ')']
0: ['(', "a", ['(', ['(', "a", "a", '|', 5, 5, ')'], 3, '|', 3, 2, ')'], 5, ')']
0: ['(', "a", ['(', ['(', "a", "a", '|', "b", 5, ')'], 3, '|', 3, 2, ')'], 5, ')']
0: ['(', "a", ['(', ['(', "a", "a", '|', "b", "b", ')'], 3, '|', 3, 2, ')'], 5, ')']
0: ['(', "a", ['(', ['(', "a", "a", '|', "b", "b", ')'], ['(', 4, 5, '|', 5, 4, ')'], '|', 3, 2, ')'], 5, ')']
0: ['(', "a", ['(', ['(', "a", "a", '|', "b", "b", ')'], ['(', "a", 5, '|', 5, 4, ')'], '|', 3, 2, ')'], 5, ')']
0: ['(', "a", ['(', ['(', "a", "a", '|', "b", "b", ')'], ['(', "a", "b", '|', 5, 4, ')'], '|', 3, 2, ')'], 5, ')']
0: ['(', "a", ['(', ['(', "a", "a", '|', "b", "b", ')'], ['(', "a", "b", '|', "a", 4, ')'], '|', 3, 2, ')'], 5, ')']
0: ['(', "a", ['(', ['(', "a", "a", '|', "b", "b", ')'], ['(', "a", "b", '|', "a", "b", ')'], '|', 3, 2, ')'], 5, ')']
0: ['(', "a", ['(', ['(', "a", "a", '|', "b", "b", ')'], ['(', "a", "b", '|', "a", "b", ')'], '|', ['(', 4, 5, '|', 5, 4, ')'], 2, ')'], 5, ')']
0: ['(', "a", ['(', ['(', "a", "a", '|', "b", "b", ')'], ['(', "a", "b", '|', "a", "b", ')'], '|', ['(', "a", 5, '|', 5, 4, ')'], 2, ')'], 5, ')']
0: ['(', "a", ['(', ['(', "a", "a", '|', "b", "b", ')'], ['(', "a", "b", '|', "a", "b", ')'], '|', ['(', "a", "b", '|', 5, 4, ')'], 2, ')'], 5, ')']
0: ['(', "a", ['(', ['(', "a", "a", '|', "b", "b", ')'], ['(', "a", "b", '|', "a", "b", ')'], '|', ['(', "a", "b", '|', "b", 4, ')'], 2, ')'], 5, ')']
0: ['(', "a", ['(', ['(', "a", "a", '|', "b", "b", ')'], ['(', "a", "b", '|', "a", "b", ')'], '|', ['(', "a", "b", '|', "b", "a", ')'], 2, ')'], 5, ')']
0: ['(', "a", ['(', ['(', "a", "a", '|', "b", "b", ')'], ['(', "a", "b", '|', "a", "b", ')'], '|', ['(', "a", "b", '|', "b", "a", ')'], ['(', 4, 4, '|', 5, 5, ')'], ')'], 5, ')']
0: ['(', "a", ['(', ['(', "a", "a", '|', "b", "b", ')'], ['(', "a", "b", '|', "a", "b", ')'], '|', ['(', "a", "b", '|', "b", "a", ')'], ['(', "a", 4, '|', 5, 5, ')'], ')'], 5, ')']
0: ['(', "a", ['(', ['(', "a", "a", '|', "b", "b", ')'], ['(', "a", "b", '|', "a", "b", ')'], '|', ['(', "a", "b", '|', "b", "a", ')'], ['(', "a", "a", '|', 5, 5, ')'], ')'], 5, ')']
0: ['(', "a", ['(', ['(', "a", "a", '|', "b", "b", ')'], ['(', "a", "b", '|', "a", "b", ')'], '|', ['(', "a", "b", '|', "b", "a", ')'], ['(', "a", "a", '|', "b", 5, ')'], ')'], 5, ')']
0: ['(', "a", ['(', ['(', "a", "a", '|', "b", "b", ')'], ['(', "a", "b", '|', "a", "b", ')'], '|', ['(', "a", "b", '|', "b", "a", ')'], ['(', "a", "a", '|', "b", "b", ')'], ')'], 5, ')']
0: ['(', "a", ['(', ['(', "a", "a", '|', "b", "b", ')'], ['(', "a", "b", '|', "a", "b", ')'], '|', ['(', "a", "b", '|', "b", "a", ')'], ['(', "a", "a", '|', "b", "b", ')'], ')'], "b", ')']
*/
function buildUpIdFromRules(id, rules, computed_rules, MAX_DEPTH = DEFAULT_MAX_DEPTH, depth = 0) {
if (computed_rules[id]) {
return computed_rules[id];
}
let { parts } = rules[id];
let expanded_parts = parts
.map((v) => {
if (depth > MAX_DEPTH) {
// Bail on an arbitrarily large depth
// This can probably be computed by the longest input code, but trial and error works well enough to determine the answer
return undefined;
} else if (typeof v === 'string') {
return v;
} else {
return buildUpIdFromRules(v, rules, computed_rules, MAX_DEPTH, depth + 1);
}
})
.filter((v) => v);
computed_rules[id] = expanded_parts;
return expanded_parts;
}
module.exports = {
getRegexForId,
DEFAULT_MAX_DEPTH,
};