svelte-highlight 7.18.0 → 7.18.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/convert-language.d.ts +7 -1
- package/convert-language.js +59 -7
- package/engine.d.ts +2 -0
- package/engine.js +18 -2
- package/languages/gcode.js +6 -6
- package/languages/mathematica.js +7235 -1
- package/languages/nushell.js +2 -0
- package/languages/zig.js +0 -3
- package/package.json +1 -4
- package/preprocessor.js +162 -11
package/convert-language.d.ts
CHANGED
|
@@ -9,5 +9,11 @@ export interface ConvertResult {
|
|
|
9
9
|
* @param hljs instance (e.g. from `highlight.js/lib/core`) with the grammar
|
|
10
10
|
* (and any sublanguages it embeds) already registered
|
|
11
11
|
* @param name
|
|
12
|
+
* @param grammarSource raw source text of the grammar's own file, used only
|
|
13
|
+
* to recover data that the compiled mode tree doesn't expose
|
|
12
14
|
*/
|
|
13
|
-
export function convertLanguage(
|
|
15
|
+
export function convertLanguage(
|
|
16
|
+
hljs: unknown,
|
|
17
|
+
name: string,
|
|
18
|
+
grammarSource?: string,
|
|
19
|
+
): ConvertResult;
|
package/convert-language.js
CHANGED
|
@@ -37,13 +37,52 @@ const FLAGS = /** @type {const} */ ([
|
|
|
37
37
|
* @typedef {{ ir: GrammarIR, warnings: string[] }} ConvertResult
|
|
38
38
|
*/
|
|
39
39
|
|
|
40
|
+
const SET_MEMBERSHIP_GUARD_RE = /(\w+)\.has\(match\[0\]\)/;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Extracts hljs's array-membership `on:begin` filter (e.g. mathematica's
|
|
44
|
+
* `SYSTEM_SYMBOLS_SET.has(match[0])`) into a plain word list. The Set is
|
|
45
|
+
* built inside the grammar factory from a module-level array literal that
|
|
46
|
+
* isn't visible via the compiled callback's own `toString()`, so this greps
|
|
47
|
+
* the grammar's raw source file text instead - trusted, pinned highlight.js
|
|
48
|
+
* source, not user input.
|
|
49
|
+
* @param {string} onBegin
|
|
50
|
+
* @param {string | undefined} grammarSource
|
|
51
|
+
* @returns {string[] | null}
|
|
52
|
+
*/
|
|
53
|
+
function extractBeginWordSet(onBegin, grammarSource) {
|
|
54
|
+
if (!grammarSource) return null;
|
|
55
|
+
const setRef = onBegin.match(SET_MEMBERSHIP_GUARD_RE);
|
|
56
|
+
if (!setRef) return null;
|
|
57
|
+
const setDecl = grammarSource.match(
|
|
58
|
+
new RegExp(`${setRef[1]}\\s*=\\s*new Set\\(\\s*(\\w+)\\s*\\)`),
|
|
59
|
+
);
|
|
60
|
+
if (!setDecl) return null;
|
|
61
|
+
const arrayDecl = grammarSource.match(
|
|
62
|
+
new RegExp(`${setDecl[1]}\\s*=\\s*(\\[[^\\]]*\\])`),
|
|
63
|
+
);
|
|
64
|
+
if (!arrayDecl) return null;
|
|
65
|
+
try {
|
|
66
|
+
const words = new Function(`return (${arrayDecl[1]});`)();
|
|
67
|
+
if (Array.isArray(words) && words.every((w) => typeof w === "string")) {
|
|
68
|
+
return words;
|
|
69
|
+
}
|
|
70
|
+
} catch {
|
|
71
|
+
// fall through to null
|
|
72
|
+
}
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
|
|
40
76
|
/**
|
|
41
77
|
* @param {unknown} hljs instance with the grammar (and any sublanguages it
|
|
42
78
|
* embeds) already registered
|
|
43
79
|
* @param {string} name
|
|
80
|
+
* @param {string} [grammarSource] raw source text of the grammar's own file,
|
|
81
|
+
* used only to recover data (like `extractBeginWordSet`'s word list) that
|
|
82
|
+
* the compiled mode tree doesn't expose
|
|
44
83
|
* @returns {ConvertResult}
|
|
45
84
|
*/
|
|
46
|
-
export function convertLanguage(hljs, name) {
|
|
85
|
+
export function convertLanguage(hljs, name, grammarSource) {
|
|
47
86
|
// hljs's compiled mode type is not exported; read fields loosely.
|
|
48
87
|
const hl = /** @type {any} */ (hljs);
|
|
49
88
|
// Force compilation; the registered object is the compiled tree.
|
|
@@ -87,13 +126,26 @@ export function convertLanguage(hljs, name) {
|
|
|
87
126
|
} else if (onBegin.includes("hasClosingTag(")) {
|
|
88
127
|
// JSX opening tag vs generic/comparison. See Tokenizer#isTrulyOpeningTag.
|
|
89
128
|
state.xmlTagGuard = true;
|
|
129
|
+
} else if (onBegin.includes("charBeforeMatch")) {
|
|
130
|
+
// hljs gcode LETTER_BOUNDARY_CALLBACK: a manual negative-lookbehind
|
|
131
|
+
// stand-in (accept at input start or after a digit/underscore, reject
|
|
132
|
+
// otherwise), used until hljs can rely on native lookbehind.
|
|
133
|
+
state.letterBoundaryGuard = true;
|
|
90
134
|
} else {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
135
|
+
const wordSet = extractBeginWordSet(onBegin, grammarSource);
|
|
136
|
+
if (wordSet) {
|
|
137
|
+
// hljs word-list membership guard (e.g. mathematica's
|
|
138
|
+
// SYSTEM_SYMBOLS_SET), used to accept a begin match only for
|
|
139
|
+
// recognized identifiers rather than any syntactic shape.
|
|
140
|
+
state.beginWordSet = wordSet;
|
|
141
|
+
} else {
|
|
142
|
+
warnings.add(
|
|
143
|
+
`on:begin not convertible (scope: ${mode.scope ?? "<none>"}, begin: ${source(mode.begin)})`,
|
|
144
|
+
);
|
|
145
|
+
// Dropped guard callback: state now matches unconditionally.
|
|
146
|
+
// Zero relevance so auto-detection is not skewed.
|
|
147
|
+
state.relevance = 0;
|
|
148
|
+
}
|
|
97
149
|
}
|
|
98
150
|
}
|
|
99
151
|
if (beforeBegin) {
|
package/engine.d.ts
CHANGED
package/engine.js
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* from engine.d.ts. Tokenizer state is serializable so parses can
|
|
9
9
|
* checkpoint, resume, and stream. Callback behavior from hljs grammars
|
|
10
10
|
* becomes declarative IR flags (endSameAsBegin, onlyAtInputStart,
|
|
11
|
-
* notAfterDot).
|
|
11
|
+
* notAfterDot, letterBoundaryGuard, beginWordSet).
|
|
12
12
|
*
|
|
13
13
|
* A well-formed event stream is balanced: every OPEN has a matching later
|
|
14
14
|
* CLOSE, properly nested, and the TEXT values, concatenated in order, equal
|
|
@@ -43,13 +43,14 @@
|
|
|
43
43
|
* A grammar state after `compileProgram` has compiled its string patterns
|
|
44
44
|
* into RegExps (and normalized `keywords`/`relevance` - the IR omits
|
|
45
45
|
* `relevance` when it's the default 1, but the compiled form always has it).
|
|
46
|
-
* @typedef {Omit<GrammarState, "keywords" | "relevance"> & {
|
|
46
|
+
* @typedef {Omit<GrammarState, "keywords" | "relevance" | "beginWordSet"> & {
|
|
47
47
|
* relevance: number,
|
|
48
48
|
* keywords: Record<string, [string, number]> | null,
|
|
49
49
|
* beginRe: RegExp | null,
|
|
50
50
|
* endRe: RegExp | null,
|
|
51
51
|
* illegalRe: RegExp | null,
|
|
52
52
|
* keywordRe: RegExp | null,
|
|
53
|
+
* beginWordSet: Set<string> | null,
|
|
53
54
|
* }} CompiledState
|
|
54
55
|
*/
|
|
55
56
|
|
|
@@ -166,6 +167,7 @@ function compileProgram(ir) {
|
|
|
166
167
|
endRe: re(s.end),
|
|
167
168
|
illegalRe: re(s.illegal),
|
|
168
169
|
keywordRe: s.keywords ? re(s.keywordPattern || "\\w+") : null,
|
|
170
|
+
beginWordSet: s.beginWordSet ? new Set(s.beginWordSet) : null,
|
|
169
171
|
}));
|
|
170
172
|
return { ir, states };
|
|
171
173
|
}
|
|
@@ -455,6 +457,20 @@ class Tokenizer {
|
|
|
455
457
|
if (state.xmlTagGuard) {
|
|
456
458
|
return (m) => this.isTrulyOpeningTag(m);
|
|
457
459
|
}
|
|
460
|
+
if (state.letterBoundaryGuard) {
|
|
461
|
+
return (m) => {
|
|
462
|
+
if (m.index === 0) return true;
|
|
463
|
+
const charBeforeMatch = this.code[m.index - 1] ?? "";
|
|
464
|
+
return (
|
|
465
|
+
(charBeforeMatch >= "0" && charBeforeMatch <= "9") ||
|
|
466
|
+
charBeforeMatch === "_"
|
|
467
|
+
);
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
if (state.beginWordSet) {
|
|
471
|
+
const wordSet = state.beginWordSet;
|
|
472
|
+
return (m) => wordSet.has(m[0]);
|
|
473
|
+
}
|
|
458
474
|
return null;
|
|
459
475
|
}
|
|
460
476
|
|
package/languages/gcode.js
CHANGED
|
@@ -106,11 +106,11 @@ export const gcode =
|
|
|
106
106
|
end: "\\B|\\b",
|
|
107
107
|
},
|
|
108
108
|
{
|
|
109
|
-
relevance: 0,
|
|
110
109
|
rules: [],
|
|
111
110
|
scope: "title.function",
|
|
112
111
|
begin: "[GM]\\s*\\d+(\\.\\d+)?",
|
|
113
112
|
end: "\\B|\\b",
|
|
113
|
+
letterBoundaryGuard: true,
|
|
114
114
|
},
|
|
115
115
|
{
|
|
116
116
|
rules: [],
|
|
@@ -119,27 +119,27 @@ export const gcode =
|
|
|
119
119
|
end: "\\B|\\b",
|
|
120
120
|
},
|
|
121
121
|
{
|
|
122
|
-
relevance: 0,
|
|
123
122
|
rules: [],
|
|
124
123
|
scope: "title.function",
|
|
125
124
|
begin: "T\\s*\\d+",
|
|
126
125
|
end: "\\B|\\b",
|
|
126
|
+
letterBoundaryGuard: true,
|
|
127
127
|
},
|
|
128
128
|
{ rules: [], scope: "symbol", begin: "\\bO\\s*\\d+", end: "\\B|\\b" },
|
|
129
129
|
{
|
|
130
|
-
relevance: 0,
|
|
131
130
|
rules: [],
|
|
132
131
|
scope: "symbol",
|
|
133
132
|
begin: "O\\s*\\d+",
|
|
134
133
|
end: "\\B|\\b",
|
|
134
|
+
letterBoundaryGuard: true,
|
|
135
135
|
},
|
|
136
136
|
{ rules: [], scope: "symbol", begin: "\\bO<.+>", end: "\\B|\\b" },
|
|
137
137
|
{
|
|
138
|
-
relevance: 0,
|
|
139
138
|
rules: [],
|
|
140
139
|
scope: "symbol",
|
|
141
140
|
begin: "O<.+>",
|
|
142
141
|
end: "\\B|\\b",
|
|
142
|
+
letterBoundaryGuard: true,
|
|
143
143
|
},
|
|
144
144
|
{
|
|
145
145
|
rules: [],
|
|
@@ -156,11 +156,11 @@ export const gcode =
|
|
|
156
156
|
end: "\\B|\\b",
|
|
157
157
|
},
|
|
158
158
|
{
|
|
159
|
-
relevance: 0,
|
|
160
159
|
rules: [],
|
|
161
160
|
scope: "property",
|
|
162
161
|
begin: "[ABCUVWXYZ]\\s*[+-]?((\\.\\d+)|(\\d+)(\\.\\d*)?)",
|
|
163
162
|
end: "\\B|\\b",
|
|
163
|
+
letterBoundaryGuard: true,
|
|
164
164
|
},
|
|
165
165
|
{
|
|
166
166
|
rules: [],
|
|
@@ -169,11 +169,11 @@ export const gcode =
|
|
|
169
169
|
end: "\\B|\\b",
|
|
170
170
|
},
|
|
171
171
|
{
|
|
172
|
-
relevance: 0,
|
|
173
172
|
rules: [],
|
|
174
173
|
scope: "params",
|
|
175
174
|
begin: "[FHIJKPQRS]\\s*[+-]?((\\.\\d+)|(\\d+)(\\.\\d*)?)",
|
|
176
175
|
end: "\\B|\\b",
|
|
176
|
+
letterBoundaryGuard: true,
|
|
177
177
|
},
|
|
178
178
|
],
|
|
179
179
|
aliases: ["nc"],
|