-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
multi_class.js
77 lines (69 loc) · 2.11 KB
/
multi_class.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
/* eslint-disable no-throw-literal */
import * as logger from "../../lib/logger.js";
import * as regex from "../regex.js";
/**
@typedef {import('highlight.js').CompiledMode} CompiledMode
*/
const MultiClassError = new Error();
/**
* Renumbers labeled scope names to account for additional inner match
* groups that otherwise would break everything.
*
* Lets say we 3 match scopes:
*
* { 1 => ..., 2 => ..., 3 => ... }
*
* So what we need is a clean match like this:
*
* (a)(b)(c) => [ "a", "b", "c" ]
*
* But this falls apart with inner match groups:
*
* (a)(((b)))(c) => ["a", "b", "b", "b", "c" ]
*
* Our scopes are now "out of alignment" and we're repeating `b` 3 times.
* What needs to happen is the numbers are remapped:
*
* { 1 => ..., 2 => ..., 5 => ... }
*
* We also need to know that the ONLY groups that should be output
* are 1, 2, and 5. This function handles this behavior.
*
* @param {CompiledMode} mode
* @param {Array<RegExp>} regexes
*/
function remapScopeNames(mode, regexes) {
let offset = 0;
const scopeNames = mode.scope;
/** @type Record<number,boolean> */
const emit = {};
/** @type Record<number,string> */
const positions = {};
for (let i = 1; i <= regexes.length; i++) {
positions[i + offset] = scopeNames[i];
emit[i + offset] = true;
offset += regex.countMatchGroups(regexes[i - 1]);
}
// we use _emit to keep track of which match groups are "top-level" to avoid double
// output from inside match groups
mode._emit = emit;
mode.scope = positions;
}
/**
* @param {CompiledMode} mode
*/
export function MultiClass(mode) {
if (!Array.isArray(mode.begin)) return;
if (mode.skip || mode.excludeBegin || mode.returnBegin) {
logger.error("skip, excludeBegin, returnBegin not compatible with multi-class");
throw MultiClassError;
}
if (typeof mode.scope !== "object" || mode.scope == null) {
logger.error("scope/className must be object");
throw MultiClassError;
}
const matchers = mode.begin;
remapScopeNames(mode, matchers);
mode.begin = regex._rewriteBackreferences(mode.begin, { joinWith: "" });
mode.isMultiClass = true;
}