limn-hl 0.0.0 → 0.1.0
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/README.md +306 -0
- package/dist/cursor.d.ts +32 -0
- package/dist/cursor.js +322 -0
- package/dist/detect.d.ts +5 -0
- package/dist/detect.js +565 -0
- package/dist/engine.d.ts +78 -0
- package/dist/engine.js +268 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +17 -0
- package/dist/lang/clike.d.ts +15 -0
- package/dist/lang/clike.js +35 -0
- package/dist/lang/clike_helpers.d.ts +8 -0
- package/dist/lang/clike_helpers.js +220 -0
- package/dist/lang/code.d.ts +45 -0
- package/dist/lang/code.js +779 -0
- package/dist/lang/config.d.ts +5 -0
- package/dist/lang/config.js +164 -0
- package/dist/lang/css.d.ts +5 -0
- package/dist/lang/css.js +125 -0
- package/dist/lang/docs.d.ts +12 -0
- package/dist/lang/docs.js +462 -0
- package/dist/lang/formats.d.ts +10 -0
- package/dist/lang/formats.js +295 -0
- package/dist/lang/html.d.ts +11 -0
- package/dist/lang/html.js +298 -0
- package/dist/lang/index.d.ts +20 -0
- package/dist/lang/index.js +167 -0
- package/dist/lang/js.d.ts +9 -0
- package/dist/lang/js.js +532 -0
- package/dist/lang/js_helpers.d.ts +7 -0
- package/dist/lang/js_helpers.js +308 -0
- package/dist/lang/json.d.ts +3 -0
- package/dist/lang/json.js +136 -0
- package/dist/lang/markdown.d.ts +3 -0
- package/dist/lang/markdown.js +249 -0
- package/dist/lang/python.d.ts +3 -0
- package/dist/lang/python.js +335 -0
- package/dist/lang/scripting.d.ts +7 -0
- package/dist/lang/scripting.js +216 -0
- package/dist/lang/shell.d.ts +7 -0
- package/dist/lang/shell.js +278 -0
- package/dist/lang/specs_gen.d.ts +66 -0
- package/dist/lang/specs_gen.js +381 -0
- package/dist/lang/terminal.d.ts +9 -0
- package/dist/lang/terminal.js +550 -0
- package/dist/lang/yaml.d.ts +4 -0
- package/dist/lang/yaml.js +176 -0
- package/dist/render/ansi.d.ts +7 -0
- package/dist/render/ansi.js +70 -0
- package/dist/render/html.d.ts +20 -0
- package/dist/render/html.js +212 -0
- package/dist/render/index.d.ts +2 -0
- package/dist/render/index.js +2 -0
- package/dist/theme/generated.d.ts +57 -0
- package/dist/theme/generated.js +736 -0
- package/dist/theme/index.d.ts +12 -0
- package/dist/theme/index.js +1 -0
- package/dist/token.d.ts +38 -0
- package/dist/token.js +67 -0
- package/dist/wordtable.d.ts +23 -0
- package/dist/wordtable.js +65 -0
- package/package.json +44 -9
package/dist/engine.js
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { TokenKind } from "./token.js";
|
|
2
|
+
const MAX_EMBED_DEPTH = 16;
|
|
3
|
+
|
|
4
|
+
export class Grammar {
|
|
5
|
+
name;
|
|
6
|
+
aliases;
|
|
7
|
+
scan;
|
|
8
|
+
constructor(name, scan, ...aliases) {
|
|
9
|
+
this.name = name.toLowerCase();
|
|
10
|
+
this.aliases = [...aliases];
|
|
11
|
+
this.scan = scan;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class Registry {
|
|
16
|
+
byName = new Map;
|
|
17
|
+
grammars = [];
|
|
18
|
+
constructor(grammars = []) {
|
|
19
|
+
const seen = new Map;
|
|
20
|
+
for (const g of grammars) {
|
|
21
|
+
if (!g || g.name === "") {
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
const canon = g.name.toLowerCase();
|
|
25
|
+
const idx = seen.get(canon);
|
|
26
|
+
if (idx !== undefined) {
|
|
27
|
+
this.grammars[idx] = g;
|
|
28
|
+
} else {
|
|
29
|
+
seen.set(canon, this.grammars.length);
|
|
30
|
+
this.grammars.push(g);
|
|
31
|
+
}
|
|
32
|
+
this.byName.set(canon, g);
|
|
33
|
+
for (const alias of g.aliases) {
|
|
34
|
+
if (alias !== "") {
|
|
35
|
+
this.byName.set(alias.toLowerCase(), g);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
with(extra) {
|
|
41
|
+
return new Registry([...this.grammars, ...extra]);
|
|
42
|
+
}
|
|
43
|
+
lookup(name) {
|
|
44
|
+
if (name === "") {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
return this.byName.get(name) ?? this.byName.get(name.toLowerCase());
|
|
48
|
+
}
|
|
49
|
+
names() {
|
|
50
|
+
return this.grammars.map((g) => g.name).sort();
|
|
51
|
+
}
|
|
52
|
+
eachToken(src, lang, fn) {
|
|
53
|
+
eachToken(src, this.lookup(lang), this, fn);
|
|
54
|
+
}
|
|
55
|
+
eachTokenWhile(src, lang, fn) {
|
|
56
|
+
eachTokenWhile(src, this.lookup(lang), this, fn);
|
|
57
|
+
}
|
|
58
|
+
tokenizeInto(dst, src, lang) {
|
|
59
|
+
return tokenizeInto(dst, src, this.lookup(lang), this);
|
|
60
|
+
}
|
|
61
|
+
tokenize(src, lang) {
|
|
62
|
+
return tokenize(src, this.lookup(lang), this);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
class Sink {
|
|
67
|
+
reg;
|
|
68
|
+
srcLen;
|
|
69
|
+
lastEnd = 0;
|
|
70
|
+
depth = 0;
|
|
71
|
+
defaultKind = TokenKind.Plain;
|
|
72
|
+
hasPending = false;
|
|
73
|
+
pendingStart = 0;
|
|
74
|
+
pendingEnd = 0;
|
|
75
|
+
pendingKind = TokenKind.Plain;
|
|
76
|
+
stop = false;
|
|
77
|
+
dst;
|
|
78
|
+
fnEach;
|
|
79
|
+
fnWhile;
|
|
80
|
+
constructor(reg, srcLen, dst, fnEach, fnWhile) {
|
|
81
|
+
this.reg = reg;
|
|
82
|
+
this.srcLen = srcLen;
|
|
83
|
+
this.dst = dst;
|
|
84
|
+
this.fnEach = fnEach;
|
|
85
|
+
this.fnWhile = fnWhile;
|
|
86
|
+
}
|
|
87
|
+
emitBound(base, limit, start, end, kind) {
|
|
88
|
+
if (this.stop || start >= end) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const clampedStart = start < 0 ? 0 : start;
|
|
92
|
+
let absStart = base + clampedStart;
|
|
93
|
+
let absEnd = base + end;
|
|
94
|
+
if (absEnd > limit) {
|
|
95
|
+
absEnd = limit;
|
|
96
|
+
}
|
|
97
|
+
if (absStart < this.lastEnd) {
|
|
98
|
+
absStart = this.lastEnd;
|
|
99
|
+
}
|
|
100
|
+
if (absStart >= absEnd) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (absStart > this.lastEnd) {
|
|
104
|
+
this.pushToken(this.lastEnd, absStart, this.defaultKind);
|
|
105
|
+
if (this.stop) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
this.pushToken(absStart, absEnd, kind);
|
|
110
|
+
this.lastEnd = absEnd;
|
|
111
|
+
}
|
|
112
|
+
emitRaw(start, end, kind) {
|
|
113
|
+
if (start < end) {
|
|
114
|
+
this.pushToken(start, end, kind);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
fillGap(upTo) {
|
|
118
|
+
if (this.stop || upTo <= this.lastEnd) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const clamped = upTo > this.srcLen ? this.srcLen : upTo;
|
|
122
|
+
if (clamped > this.lastEnd) {
|
|
123
|
+
this.pushToken(this.lastEnd, clamped, this.defaultKind);
|
|
124
|
+
this.lastEnd = clamped;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
pushToken(start, end, kind) {
|
|
128
|
+
if (this.hasPending) {
|
|
129
|
+
if (this.pendingKind === kind && this.pendingEnd === start) {
|
|
130
|
+
this.pendingEnd = end;
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
this.flushPending();
|
|
134
|
+
if (this.stop) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
this.pendingStart = start;
|
|
139
|
+
this.pendingEnd = end;
|
|
140
|
+
this.pendingKind = kind;
|
|
141
|
+
this.hasPending = true;
|
|
142
|
+
}
|
|
143
|
+
flushPending() {
|
|
144
|
+
this.hasPending = false;
|
|
145
|
+
const tok = {
|
|
146
|
+
start: this.pendingStart,
|
|
147
|
+
end: this.pendingEnd,
|
|
148
|
+
kind: this.pendingKind
|
|
149
|
+
};
|
|
150
|
+
if (this.dst !== undefined) {
|
|
151
|
+
this.dst.push(tok);
|
|
152
|
+
} else if (this.fnEach !== undefined) {
|
|
153
|
+
this.fnEach(tok);
|
|
154
|
+
} else if (this.fnWhile !== undefined) {
|
|
155
|
+
if (!this.fnWhile(tok)) {
|
|
156
|
+
this.stop = true;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
finish() {
|
|
161
|
+
if (!this.stop && this.lastEnd < this.srcLen) {
|
|
162
|
+
this.pushToken(this.lastEnd, this.srcLen, this.defaultKind);
|
|
163
|
+
this.lastEnd = this.srcLen;
|
|
164
|
+
}
|
|
165
|
+
if (!this.stop && this.hasPending) {
|
|
166
|
+
this.flushPending();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export class Emitter {
|
|
172
|
+
s;
|
|
173
|
+
base;
|
|
174
|
+
limit;
|
|
175
|
+
constructor(s, base, limit) {
|
|
176
|
+
this.s = s;
|
|
177
|
+
this.base = base;
|
|
178
|
+
this.limit = limit;
|
|
179
|
+
}
|
|
180
|
+
stopped() {
|
|
181
|
+
return this.s.stop;
|
|
182
|
+
}
|
|
183
|
+
emit(start, end, kind) {
|
|
184
|
+
if (start < end) {
|
|
185
|
+
this.s.emitBound(this.base, this.limit, start, end, kind);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
lookup(name) {
|
|
189
|
+
if (this.s.reg === undefined || name === "") {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
return this.s.reg.lookup(name);
|
|
193
|
+
}
|
|
194
|
+
embed(src, start, end, grammar, fallbackKind) {
|
|
195
|
+
const s = this.s;
|
|
196
|
+
if (s.stop || start >= end) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
const clampedStart = start < 0 ? 0 : start;
|
|
200
|
+
const clampedEnd = end > src.length ? src.length : end;
|
|
201
|
+
if (clampedStart >= clampedEnd) {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
let absStart = this.base + clampedStart;
|
|
205
|
+
let absEnd = this.base + clampedEnd;
|
|
206
|
+
if (absEnd > this.limit) {
|
|
207
|
+
absEnd = this.limit;
|
|
208
|
+
}
|
|
209
|
+
if (absStart < s.lastEnd) {
|
|
210
|
+
absStart = s.lastEnd;
|
|
211
|
+
}
|
|
212
|
+
if (absStart >= absEnd) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
s.fillGap(absStart);
|
|
216
|
+
if (grammar === undefined || s.depth >= MAX_EMBED_DEPTH) {
|
|
217
|
+
s.emitRaw(absStart, absEnd, fallbackKind);
|
|
218
|
+
s.lastEnd = absEnd;
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
const prevKind = s.defaultKind;
|
|
222
|
+
s.defaultKind = fallbackKind;
|
|
223
|
+
s.depth++;
|
|
224
|
+
const child = new Emitter(s, absStart, absEnd);
|
|
225
|
+
grammar.scan(src.slice(clampedStart, clampedEnd), child);
|
|
226
|
+
s.depth--;
|
|
227
|
+
s.fillGap(absEnd);
|
|
228
|
+
s.defaultKind = prevKind;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
function runScan(src, grammar, reg, dst, fnEach, fnWhile) {
|
|
232
|
+
if (src.length === 0) {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const s = new Sink(reg, src.length, dst, fnEach, fnWhile);
|
|
236
|
+
if (grammar !== undefined) {
|
|
237
|
+
const root = new Emitter(s, 0, src.length);
|
|
238
|
+
grammar.scan(src, root);
|
|
239
|
+
}
|
|
240
|
+
s.finish();
|
|
241
|
+
}
|
|
242
|
+
export function eachToken(src, grammar, reg, fn) {
|
|
243
|
+
if (src.length === 0) {
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
runScan(src, grammar, reg, undefined, fn, undefined);
|
|
247
|
+
}
|
|
248
|
+
export function eachTokenWhile(src, grammar, reg, fn) {
|
|
249
|
+
if (src.length === 0) {
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
runScan(src, grammar, reg, undefined, undefined, fn);
|
|
253
|
+
}
|
|
254
|
+
export function tokenizeInto(dst, src, grammar, reg) {
|
|
255
|
+
if (src.length === 0) {
|
|
256
|
+
return dst;
|
|
257
|
+
}
|
|
258
|
+
runScan(src, grammar, reg, dst, undefined, undefined);
|
|
259
|
+
return dst;
|
|
260
|
+
}
|
|
261
|
+
export function tokenize(src, grammar, reg) {
|
|
262
|
+
if (src.length === 0) {
|
|
263
|
+
return [];
|
|
264
|
+
}
|
|
265
|
+
const dst = [];
|
|
266
|
+
runScan(src, grammar, reg, dst, undefined, undefined);
|
|
267
|
+
return dst;
|
|
268
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { type ScanFunc, Grammar, Registry, Emitter, eachToken, eachTokenWhile, tokenizeInto, tokenize, } from "./engine.js";
|
|
2
|
+
export { TokenKind, KIND_COUNT, KIND_NAMES, kindName, splitLines, type Token, } from "./token.js";
|
|
3
|
+
export { WordTable, type WordGroup } from "./wordtable.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export {
|
|
2
|
+
Grammar,
|
|
3
|
+
Registry,
|
|
4
|
+
Emitter,
|
|
5
|
+
eachToken,
|
|
6
|
+
eachTokenWhile,
|
|
7
|
+
tokenizeInto,
|
|
8
|
+
tokenize
|
|
9
|
+
} from "./engine.js";
|
|
10
|
+
export {
|
|
11
|
+
TokenKind,
|
|
12
|
+
KIND_COUNT,
|
|
13
|
+
KIND_NAMES,
|
|
14
|
+
kindName,
|
|
15
|
+
splitLines
|
|
16
|
+
} from "./token.js";
|
|
17
|
+
export { WordTable } from "./wordtable.js";
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Grammar } from "../engine.js";
|
|
2
|
+
/** Returns the C grammar. */
|
|
3
|
+
export declare const c: () => Grammar;
|
|
4
|
+
/** Returns the C++ grammar. */
|
|
5
|
+
export declare const cpp: () => Grammar;
|
|
6
|
+
/** Returns the C# grammar. */
|
|
7
|
+
export declare const cs: () => Grammar;
|
|
8
|
+
/** Returns the GLSL shader grammar. */
|
|
9
|
+
export declare const glsl: () => Grammar;
|
|
10
|
+
/** Returns the Rust grammar. */
|
|
11
|
+
export declare const rs: () => Grammar;
|
|
12
|
+
/** Returns the Zig grammar. */
|
|
13
|
+
export declare const zig: () => Grammar;
|
|
14
|
+
/** Returns the PHP grammar. */
|
|
15
|
+
export declare const php: () => Grammar;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {
|
|
2
|
+
scanCPreprocessor,
|
|
3
|
+
scanRustLifetimeOrChar,
|
|
4
|
+
tryCPPRawString,
|
|
5
|
+
tryPHPTag,
|
|
6
|
+
tryRustCustom,
|
|
7
|
+
tryRustString,
|
|
8
|
+
tryZigCustom
|
|
9
|
+
} from "./clike_helpers.js";
|
|
10
|
+
import { buildCodeGrammar, onceGrammar } from "./code.js";
|
|
11
|
+
import {
|
|
12
|
+
specC,
|
|
13
|
+
specCpp,
|
|
14
|
+
specCs,
|
|
15
|
+
specGlsl,
|
|
16
|
+
specPhp,
|
|
17
|
+
specRs,
|
|
18
|
+
specZig
|
|
19
|
+
} from "./specs_gen.js";
|
|
20
|
+
function cPreprocessorHook(self) {
|
|
21
|
+
return {
|
|
22
|
+
leadBytes: "#",
|
|
23
|
+
scan: (src, pos, lineStart, e) => lineStart ? scanCPreprocessor(src, pos, self(), e) : 0
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export const c = /* @__PURE__ */ onceGrammar(() => buildCodeGrammar(specC, cPreprocessorHook(c)));
|
|
27
|
+
export const cpp = /* @__PURE__ */ onceGrammar(() => buildCodeGrammar(specCpp, cPreprocessorHook(cpp), {
|
|
28
|
+
leadBytes: "R",
|
|
29
|
+
scan: tryCPPRawString
|
|
30
|
+
}));
|
|
31
|
+
export const cs = /* @__PURE__ */ onceGrammar(() => buildCodeGrammar(specCs, cPreprocessorHook(cs)));
|
|
32
|
+
export const glsl = /* @__PURE__ */ onceGrammar(() => buildCodeGrammar(specGlsl, cPreprocessorHook(glsl)));
|
|
33
|
+
export const rs = /* @__PURE__ */ onceGrammar(() => buildCodeGrammar(specRs, { leadBytes: "#", scan: tryRustCustom }, { leadBytes: "rbc", scan: tryRustString }, { leadBytes: "'", scan: scanRustLifetimeOrChar }));
|
|
34
|
+
export const zig = /* @__PURE__ */ onceGrammar(() => buildCodeGrammar(specZig, { leadBytes: "\\@", scan: tryZigCustom }));
|
|
35
|
+
export const php = /* @__PURE__ */ onceGrammar(() => buildCodeGrammar(specPhp, { leadBytes: "<?", scan: tryPHPTag }));
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Emitter, Grammar } from "../engine.js";
|
|
2
|
+
export declare function scanCPreprocessor(src: string, pos: number, self: Grammar, e: Emitter): number;
|
|
3
|
+
export declare function tryCPPRawString(src: string, pos: number, _ls: boolean, e: Emitter): number;
|
|
4
|
+
export declare function tryRustString(src: string, pos: number, _ls: boolean, e: Emitter): number;
|
|
5
|
+
export declare function scanRustLifetimeOrChar(src: string, pos: number, _ls: boolean, e: Emitter): number;
|
|
6
|
+
export declare function tryRustCustom(src: string, pos: number, _ls: boolean, e: Emitter): number;
|
|
7
|
+
export declare function tryZigCustom(src: string, pos: number, _ls: boolean, e: Emitter): number;
|
|
8
|
+
export declare function tryPHPTag(src: string, pos: number, _ls: boolean, e: Emitter): number;
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { Cursor, isIdentCont, isIdentStart, scanEscape } from "../cursor.js";
|
|
2
|
+
import { TokenKind } from "../token.js";
|
|
3
|
+
import { scanStringDelim } from "./code.js";
|
|
4
|
+
export function scanCPreprocessor(src, pos, self, e) {
|
|
5
|
+
const n = src.length;
|
|
6
|
+
const nl = src.indexOf(`
|
|
7
|
+
`, pos + 1);
|
|
8
|
+
let end = nl >= 0 ? nl : n;
|
|
9
|
+
while (end < n && end > pos && src.charCodeAt(end - 1) === 92) {
|
|
10
|
+
const nextNL = src.indexOf(`
|
|
11
|
+
`, end + 1);
|
|
12
|
+
if (nextNL < 0) {
|
|
13
|
+
end = n;
|
|
14
|
+
break;
|
|
15
|
+
}
|
|
16
|
+
end = nextNL;
|
|
17
|
+
}
|
|
18
|
+
let cur = pos + 1;
|
|
19
|
+
while (cur < end && (src.charCodeAt(cur) === 32 || src.charCodeAt(cur) === 9)) {
|
|
20
|
+
cur++;
|
|
21
|
+
}
|
|
22
|
+
const kwStart = cur;
|
|
23
|
+
while (cur < end && isIdentCont(src.charCodeAt(cur))) {
|
|
24
|
+
cur++;
|
|
25
|
+
}
|
|
26
|
+
if (cur > kwStart) {
|
|
27
|
+
e.emit(pos, cur, TokenKind.Keyword);
|
|
28
|
+
if (src.slice(kwStart, cur) === "include") {
|
|
29
|
+
let rest = cur;
|
|
30
|
+
while (rest < end && (src.charCodeAt(rest) === 32 || src.charCodeAt(rest) === 9)) {
|
|
31
|
+
rest++;
|
|
32
|
+
}
|
|
33
|
+
if (rest < end && src.charCodeAt(rest) === 60) {
|
|
34
|
+
const closeIdx = src.indexOf(">", rest);
|
|
35
|
+
if (closeIdx >= 0 && closeIdx < end) {
|
|
36
|
+
e.emit(rest, closeIdx + 1, TokenKind.String);
|
|
37
|
+
return end;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
} else {
|
|
42
|
+
e.emit(pos, pos + 1, TokenKind.Keyword);
|
|
43
|
+
}
|
|
44
|
+
if (cur < end) {
|
|
45
|
+
e.embed(src, cur, end, self, TokenKind.Plain);
|
|
46
|
+
}
|
|
47
|
+
return end;
|
|
48
|
+
}
|
|
49
|
+
export function tryCPPRawString(src, pos, _ls, e) {
|
|
50
|
+
const n = src.length;
|
|
51
|
+
if (src.charCodeAt(pos) !== 82 || pos + 1 >= n || src.charCodeAt(pos + 1) !== 34) {
|
|
52
|
+
return 0;
|
|
53
|
+
}
|
|
54
|
+
const paren = src.indexOf("(", pos + 2);
|
|
55
|
+
if (paren < 0 || paren - (pos + 2) > 16) {
|
|
56
|
+
return 0;
|
|
57
|
+
}
|
|
58
|
+
const delim = src.slice(pos + 2, paren);
|
|
59
|
+
for (let i = 0;i < delim.length; i++) {
|
|
60
|
+
const c = delim.charCodeAt(i);
|
|
61
|
+
if (c === 32 || c === 9 || c === 10 || c === 13 || c === 92 || c === 41) {
|
|
62
|
+
return 0;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const closeSeq = ")" + delim + '"';
|
|
66
|
+
const bodyStart = paren + 1;
|
|
67
|
+
const idx = src.indexOf(closeSeq, bodyStart);
|
|
68
|
+
if (idx >= 0) {
|
|
69
|
+
const end = idx + closeSeq.length;
|
|
70
|
+
e.emit(pos, end, TokenKind.String);
|
|
71
|
+
return end;
|
|
72
|
+
}
|
|
73
|
+
e.emit(pos, n, TokenKind.String);
|
|
74
|
+
return n;
|
|
75
|
+
}
|
|
76
|
+
export function tryRustString(src, pos, _ls, e) {
|
|
77
|
+
const n = src.length;
|
|
78
|
+
let p = pos;
|
|
79
|
+
const ch = src.charCodeAt(p);
|
|
80
|
+
if (ch === 98 || ch === 99) {
|
|
81
|
+
p++;
|
|
82
|
+
if (p < n && src.charCodeAt(p) === 114) {
|
|
83
|
+
p++;
|
|
84
|
+
}
|
|
85
|
+
} else if (ch === 114) {
|
|
86
|
+
p++;
|
|
87
|
+
}
|
|
88
|
+
if (p >= n) {
|
|
89
|
+
return 0;
|
|
90
|
+
}
|
|
91
|
+
if (src.charCodeAt(pos) !== 114 && p === pos + 1 && src.charCodeAt(p) === 34) {
|
|
92
|
+
return scanStringDelim(src, pos, p + 1, '"', true, false, true, e);
|
|
93
|
+
}
|
|
94
|
+
if (src.charCodeAt(p) === 34) {
|
|
95
|
+
return scanStringDelim(src, pos, p + 1, '"', true, true, false, e);
|
|
96
|
+
}
|
|
97
|
+
if (src.charCodeAt(p) === 35) {
|
|
98
|
+
let hashes = 0;
|
|
99
|
+
while (p < n && src.charCodeAt(p) === 35) {
|
|
100
|
+
hashes++;
|
|
101
|
+
p++;
|
|
102
|
+
}
|
|
103
|
+
if (p < n && src.charCodeAt(p) === 34) {
|
|
104
|
+
let i = p + 1;
|
|
105
|
+
while (i < n) {
|
|
106
|
+
const idx = src.indexOf('"', i);
|
|
107
|
+
if (idx >= 0) {
|
|
108
|
+
const qPos = idx + 1;
|
|
109
|
+
if (qPos + hashes <= n) {
|
|
110
|
+
let match = true;
|
|
111
|
+
for (let k = 0;k < hashes; k++) {
|
|
112
|
+
if (src.charCodeAt(qPos + k) !== 35) {
|
|
113
|
+
match = false;
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (match) {
|
|
118
|
+
const end = qPos + hashes;
|
|
119
|
+
e.emit(pos, end, TokenKind.String);
|
|
120
|
+
return end;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
i = qPos;
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
e.emit(pos, n, TokenKind.String);
|
|
129
|
+
return n;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return 0;
|
|
133
|
+
}
|
|
134
|
+
export function scanRustLifetimeOrChar(src, pos, _ls, e) {
|
|
135
|
+
const n = src.length;
|
|
136
|
+
if (pos + 1 >= n) {
|
|
137
|
+
e.emit(pos, n, TokenKind.Punctuation);
|
|
138
|
+
return n;
|
|
139
|
+
}
|
|
140
|
+
const next = src.charCodeAt(pos + 1);
|
|
141
|
+
if (next === 92) {
|
|
142
|
+
const escEnd = scanEscape(src, pos + 1);
|
|
143
|
+
if (escEnd < n && src.charCodeAt(escEnd) === 39) {
|
|
144
|
+
const end = escEnd + 1;
|
|
145
|
+
e.emit(pos, pos + 1, TokenKind.String);
|
|
146
|
+
e.emit(pos + 1, escEnd, TokenKind.Escape);
|
|
147
|
+
e.emit(escEnd, end, TokenKind.String);
|
|
148
|
+
return end;
|
|
149
|
+
}
|
|
150
|
+
return scanStringDelim(src, pos, pos + 1, "'", false, false, true, e);
|
|
151
|
+
}
|
|
152
|
+
for (let k = 1;k <= 4 && pos + 1 + k < n; k++) {
|
|
153
|
+
const c = src.charCodeAt(pos + 1 + k);
|
|
154
|
+
if (c === 10)
|
|
155
|
+
break;
|
|
156
|
+
if (c === 39) {
|
|
157
|
+
const end = pos + 2 + k;
|
|
158
|
+
e.emit(pos, end, TokenKind.String);
|
|
159
|
+
return end;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (isIdentStart(next)) {
|
|
163
|
+
let end = pos + 2;
|
|
164
|
+
while (end < n && isIdentCont(src.charCodeAt(end))) {
|
|
165
|
+
end++;
|
|
166
|
+
}
|
|
167
|
+
e.emit(pos, end, src.slice(pos + 1, end) === "static" ? TokenKind.Keyword : TokenKind.Type);
|
|
168
|
+
return end;
|
|
169
|
+
}
|
|
170
|
+
e.emit(pos, pos + 1, TokenKind.Punctuation);
|
|
171
|
+
return pos + 1;
|
|
172
|
+
}
|
|
173
|
+
export function tryRustCustom(src, pos, _ls, e) {
|
|
174
|
+
const n = src.length;
|
|
175
|
+
if (src.charCodeAt(pos) === 35 && pos + 1 < n && (src.charCodeAt(pos + 1) === 91 || src.charCodeAt(pos + 1) === 33 && pos + 2 < n && src.charCodeAt(pos + 2) === 91)) {
|
|
176
|
+
const cur = new Cursor(src, pos + 1);
|
|
177
|
+
if (cur.peek() === 33) {
|
|
178
|
+
cur.pos++;
|
|
179
|
+
}
|
|
180
|
+
cur.pos++;
|
|
181
|
+
cur.scanBalanced(91, 93, true);
|
|
182
|
+
e.emit(pos, cur.pos, TokenKind.Variable);
|
|
183
|
+
return cur.pos;
|
|
184
|
+
}
|
|
185
|
+
return 0;
|
|
186
|
+
}
|
|
187
|
+
export function tryZigCustom(src, pos, _ls, e) {
|
|
188
|
+
const n = src.length;
|
|
189
|
+
if (src.charCodeAt(pos) === 92 && pos + 1 < n && src.charCodeAt(pos + 1) === 92) {
|
|
190
|
+
const nl = src.indexOf(`
|
|
191
|
+
`, pos + 2);
|
|
192
|
+
const end = nl >= 0 ? nl : n;
|
|
193
|
+
e.emit(pos, end, TokenKind.String);
|
|
194
|
+
return end;
|
|
195
|
+
}
|
|
196
|
+
if (src.charCodeAt(pos) === 64 && pos + 1 < n && isIdentStart(src.charCodeAt(pos + 1))) {
|
|
197
|
+
let end = pos + 2;
|
|
198
|
+
while (end < n && isIdentCont(src.charCodeAt(end))) {
|
|
199
|
+
end++;
|
|
200
|
+
}
|
|
201
|
+
e.emit(pos, end, TokenKind.Func);
|
|
202
|
+
return end;
|
|
203
|
+
}
|
|
204
|
+
return 0;
|
|
205
|
+
}
|
|
206
|
+
export function tryPHPTag(src, pos, _ls, e) {
|
|
207
|
+
if (src.startsWith("<?php", pos)) {
|
|
208
|
+
e.emit(pos, pos + 5, TokenKind.Keyword);
|
|
209
|
+
return pos + 5;
|
|
210
|
+
}
|
|
211
|
+
if (src.startsWith("<?=", pos)) {
|
|
212
|
+
e.emit(pos, pos + 3, TokenKind.Keyword);
|
|
213
|
+
return pos + 3;
|
|
214
|
+
}
|
|
215
|
+
if (src.startsWith("?>", pos)) {
|
|
216
|
+
e.emit(pos, pos + 2, TokenKind.Keyword);
|
|
217
|
+
return pos + 2;
|
|
218
|
+
}
|
|
219
|
+
return 0;
|
|
220
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type Emitter, Grammar } from "../engine.js";
|
|
2
|
+
import { TokenKind, type WordTable } from "../index.js";
|
|
3
|
+
/** Creates a lazily initialized singleton getter for a Grammar. */
|
|
4
|
+
export declare function onceGrammar(build: () => Grammar): () => Grammar;
|
|
5
|
+
export type InterpolationKind = "dollarBrace" | "hashBrace" | "backslashParen";
|
|
6
|
+
export interface StringInterpolation {
|
|
7
|
+
readonly kind: InterpolationKind;
|
|
8
|
+
readonly quotes: string;
|
|
9
|
+
}
|
|
10
|
+
export interface PrefixIdentSpec {
|
|
11
|
+
readonly prefixes: string;
|
|
12
|
+
readonly contExtra?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface CustomHook {
|
|
15
|
+
readonly leadBytes: string;
|
|
16
|
+
readonly scan: (src: string, pos: number, lineStart: boolean, e: Emitter) => number;
|
|
17
|
+
}
|
|
18
|
+
export interface CodeConfig {
|
|
19
|
+
readonly name: string;
|
|
20
|
+
readonly aliases?: readonly string[];
|
|
21
|
+
readonly keywords?: string;
|
|
22
|
+
readonly types?: string;
|
|
23
|
+
readonly constants?: string;
|
|
24
|
+
readonly typeAfterKeywords?: string;
|
|
25
|
+
readonly caseInsensitive?: boolean;
|
|
26
|
+
readonly lineComments?: readonly string[];
|
|
27
|
+
readonly blockComment?: readonly [string, string];
|
|
28
|
+
readonly nestedBlockComments?: boolean;
|
|
29
|
+
readonly quotes?: string;
|
|
30
|
+
readonly multilineQuotes?: string;
|
|
31
|
+
readonly rawQuotes?: string;
|
|
32
|
+
readonly tripleQuotes?: boolean;
|
|
33
|
+
readonly stringEscapes?: boolean;
|
|
34
|
+
readonly interpolation?: StringInterpolation;
|
|
35
|
+
readonly typeAfterArrow?: boolean;
|
|
36
|
+
readonly bangMacros?: boolean;
|
|
37
|
+
readonly identStartExtra?: string;
|
|
38
|
+
readonly identContExtra?: string;
|
|
39
|
+
readonly prefixIdent?: PrefixIdentSpec;
|
|
40
|
+
}
|
|
41
|
+
export declare function buildWordTable(keywords: string, types: string, constants: string, caseInsensitive: boolean): WordTable;
|
|
42
|
+
export declare function scanStringDelim(src: string, start: number, bodyPos: number, closeDelim: string, multiline: boolean, raw: boolean, escapes: boolean, e: Emitter): number;
|
|
43
|
+
export declare function tryPrefixIdent(src: string, pos: number, extra: Uint8Array | undefined, kind: TokenKind, e: Emitter): number;
|
|
44
|
+
/** Builds an immutable Grammar from a declarative CodeConfig and optional custom hooks. */
|
|
45
|
+
export declare function buildCodeGrammar(cfg: CodeConfig, ...hooks: readonly CustomHook[]): Grammar;
|