limn-hl 0.1.0 → 0.2.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 +109 -148
- package/dist/detect.d.ts +7 -1
- package/dist/detect.js +592 -502
- package/dist/engine.js +45 -1
- package/dist/generated/detect.d.ts +11 -0
- package/dist/generated/detect.js +2434 -0
- package/dist/generated/grammars.d.ts +119 -0
- package/dist/generated/grammars.js +613 -0
- package/dist/generated/lang.d.ts +6 -0
- package/dist/{lang/index.js → generated/lang.js} +82 -56
- package/dist/{theme/generated.d.ts → generated/themes.d.ts} +3 -1
- package/dist/{theme/generated.js → generated/themes.js} +29 -0
- package/dist/lang.d.ts +1 -0
- package/dist/lang.js +1 -0
- package/dist/render/ansi.d.ts +1 -1
- package/dist/render/html.d.ts +1 -1
- package/dist/{theme/index.d.ts → theme.d.ts} +1 -1
- package/dist/theme.js +1 -0
- package/dist/vm/compile.d.ts +4 -0
- package/dist/vm/compile.js +390 -0
- package/dist/vm/exec.d.ts +3 -0
- package/dist/vm/exec.js +218 -0
- package/dist/vm/format.d.ts +255 -0
- package/dist/vm/format.js +248 -0
- package/dist/vm/index.d.ts +2 -0
- package/dist/vm/index.js +2 -0
- package/dist/vm/pat.d.ts +4 -0
- package/dist/vm/pat.js +701 -0
- package/dist/vm/quoted.d.ts +8 -0
- package/dist/vm/quoted.js +190 -0
- package/dist/vm/scan.d.ts +3 -0
- package/dist/vm/scan.js +499 -0
- package/dist/wordtable.d.ts +8 -4
- package/dist/wordtable.js +107 -30
- package/package.json +6 -6
- package/dist/lang/clike.d.ts +0 -15
- package/dist/lang/clike.js +0 -35
- package/dist/lang/clike_helpers.d.ts +0 -8
- package/dist/lang/clike_helpers.js +0 -220
- package/dist/lang/code.d.ts +0 -45
- package/dist/lang/code.js +0 -779
- package/dist/lang/config.d.ts +0 -5
- package/dist/lang/config.js +0 -164
- package/dist/lang/css.d.ts +0 -5
- package/dist/lang/css.js +0 -125
- package/dist/lang/docs.d.ts +0 -12
- package/dist/lang/docs.js +0 -462
- package/dist/lang/formats.d.ts +0 -10
- package/dist/lang/formats.js +0 -295
- package/dist/lang/html.d.ts +0 -11
- package/dist/lang/html.js +0 -298
- package/dist/lang/index.d.ts +0 -20
- package/dist/lang/js.d.ts +0 -9
- package/dist/lang/js.js +0 -532
- package/dist/lang/js_helpers.d.ts +0 -7
- package/dist/lang/js_helpers.js +0 -308
- package/dist/lang/json.d.ts +0 -3
- package/dist/lang/json.js +0 -136
- package/dist/lang/markdown.d.ts +0 -3
- package/dist/lang/markdown.js +0 -249
- package/dist/lang/python.d.ts +0 -3
- package/dist/lang/python.js +0 -335
- package/dist/lang/scripting.d.ts +0 -7
- package/dist/lang/scripting.js +0 -216
- package/dist/lang/shell.d.ts +0 -7
- package/dist/lang/shell.js +0 -278
- package/dist/lang/specs_gen.d.ts +0 -66
- package/dist/lang/specs_gen.js +0 -381
- package/dist/lang/terminal.d.ts +0 -9
- package/dist/lang/terminal.js +0 -550
- package/dist/lang/yaml.d.ts +0 -4
- package/dist/lang/yaml.js +0 -176
- package/dist/theme/index.js +0 -1
package/dist/wordtable.d.ts
CHANGED
|
@@ -5,19 +5,23 @@ export interface WordGroup {
|
|
|
5
5
|
readonly words: string;
|
|
6
6
|
}
|
|
7
7
|
/**
|
|
8
|
-
* Compact lookup table for language keywords, types, and constants,
|
|
9
|
-
*
|
|
8
|
+
* Compact open-addressed lookup table for language keywords, types, and constants,
|
|
9
|
+
* performing zero string allocations on both hits and misses.
|
|
10
10
|
*/
|
|
11
11
|
export declare class WordTable {
|
|
12
|
-
private readonly
|
|
12
|
+
private readonly slotTag;
|
|
13
|
+
private readonly slotKind;
|
|
14
|
+
private readonly slotWord;
|
|
15
|
+
private readonly mask;
|
|
13
16
|
private readonly lenMask;
|
|
14
17
|
private readonly maxLen;
|
|
15
18
|
private readonly foldCase;
|
|
19
|
+
private readonly emptyTable;
|
|
16
20
|
constructor(foldCase: boolean, ...groups: readonly WordGroup[]);
|
|
17
21
|
/** Reports whether the table contains no words. */
|
|
18
22
|
isEmpty(): boolean;
|
|
19
23
|
/** Returns the TokenKind for word, or TokenKind.Plain if absent. */
|
|
20
24
|
lookup(word: string): TokenKind;
|
|
21
|
-
/** Returns the TokenKind for src.slice(start, end)
|
|
25
|
+
/** Returns the TokenKind for src.slice(start, end) with zero string allocation. */
|
|
22
26
|
lookupSpan(src: string, start: number, end: number): TokenKind;
|
|
23
27
|
}
|
package/dist/wordtable.js
CHANGED
|
@@ -1,65 +1,142 @@
|
|
|
1
1
|
import { TokenKind } from "./token.js";
|
|
2
|
+
function toAsciiLower(c) {
|
|
3
|
+
return c >= 65 && c <= 90 ? c + 32 : c;
|
|
4
|
+
}
|
|
5
|
+
function mixWordTag(tag) {
|
|
6
|
+
let h = tag >>> 0;
|
|
7
|
+
h = (h ^ h >>> 16) >>> 0;
|
|
8
|
+
h = Math.imul(h, 2146121005) >>> 0;
|
|
9
|
+
return (h ^ h >>> 15) >>> 0;
|
|
10
|
+
}
|
|
11
|
+
function spanEquals(src, start, word, fold) {
|
|
12
|
+
const n = word.length;
|
|
13
|
+
if (!fold) {
|
|
14
|
+
for (let i = 0;i < n; i++) {
|
|
15
|
+
if (src.charCodeAt(start + i) !== word.charCodeAt(i))
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
for (let i = 0;i < n; i++) {
|
|
21
|
+
if (toAsciiLower(src.charCodeAt(start + i)) !== word.charCodeAt(i))
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
2
26
|
|
|
3
27
|
export class WordTable {
|
|
4
|
-
|
|
28
|
+
slotTag;
|
|
29
|
+
slotKind;
|
|
30
|
+
slotWord;
|
|
31
|
+
mask;
|
|
5
32
|
lenMask = new Uint32Array(128);
|
|
6
33
|
maxLen = 0;
|
|
7
34
|
foldCase;
|
|
35
|
+
emptyTable;
|
|
8
36
|
constructor(foldCase, ...groups) {
|
|
9
37
|
this.foldCase = foldCase;
|
|
10
|
-
|
|
38
|
+
const entries = new Map;
|
|
11
39
|
for (const g of groups) {
|
|
12
40
|
if (g.kind === TokenKind.Plain || g.words === "") {
|
|
13
41
|
continue;
|
|
14
42
|
}
|
|
15
43
|
const parts = g.words.trim().split(/\s+/);
|
|
16
44
|
for (const raw of parts) {
|
|
17
|
-
if (raw === "")
|
|
45
|
+
if (raw === "" || raw.length > 255)
|
|
18
46
|
continue;
|
|
19
47
|
const word = foldCase ? raw.toLowerCase() : raw;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
48
|
+
entries.set(word, g.kind);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (entries.size === 0) {
|
|
52
|
+
this.slotTag = new Int32Array(0);
|
|
53
|
+
this.slotKind = new Uint8Array(0);
|
|
54
|
+
this.slotWord = [];
|
|
55
|
+
this.mask = 0;
|
|
56
|
+
this.emptyTable = true;
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
let size = 16;
|
|
60
|
+
while (size < Math.floor(entries.size * 5 / 2)) {
|
|
61
|
+
size <<= 1;
|
|
62
|
+
}
|
|
63
|
+
const mask = size - 1;
|
|
64
|
+
const slotTag = new Int32Array(size);
|
|
65
|
+
const slotKind = new Uint8Array(size);
|
|
66
|
+
const slotWord = new Array(size);
|
|
67
|
+
let maxLen = 0;
|
|
68
|
+
for (const [word, kind] of entries) {
|
|
69
|
+
const n = word.length;
|
|
70
|
+
if (n > maxLen)
|
|
71
|
+
maxLen = n;
|
|
72
|
+
const c0 = word.charCodeAt(0);
|
|
73
|
+
if (n < 32 && c0 < 128) {
|
|
74
|
+
this.lenMask[c0] |= 1 << n;
|
|
75
|
+
if (foldCase && c0 >= 97 && c0 <= 122) {
|
|
76
|
+
this.lenMask[c0 - 32] |= 1 << n;
|
|
33
77
|
}
|
|
34
78
|
}
|
|
79
|
+
const cm = word.charCodeAt(n >> 1);
|
|
80
|
+
const cl = word.charCodeAt(n - 1);
|
|
81
|
+
const tag = c0 & 255 | (cm & 255) << 8 | (cl & 255) << 16 | n << 24 | 0;
|
|
82
|
+
let idx = mixWordTag(tag) & mask;
|
|
83
|
+
while (slotTag[idx] !== 0) {
|
|
84
|
+
idx = idx + 1 & mask;
|
|
85
|
+
}
|
|
86
|
+
slotTag[idx] = tag;
|
|
87
|
+
slotKind[idx] = kind;
|
|
88
|
+
slotWord[idx] = word;
|
|
35
89
|
}
|
|
90
|
+
this.slotTag = slotTag;
|
|
91
|
+
this.slotKind = slotKind;
|
|
92
|
+
this.slotWord = slotWord;
|
|
93
|
+
this.mask = mask;
|
|
36
94
|
this.maxLen = maxLen;
|
|
95
|
+
this.emptyTable = false;
|
|
37
96
|
}
|
|
38
97
|
isEmpty() {
|
|
39
|
-
return this.
|
|
98
|
+
return this.emptyTable;
|
|
40
99
|
}
|
|
41
100
|
lookup(word) {
|
|
42
|
-
|
|
43
|
-
if (n === 0 || n > this.maxLen) {
|
|
44
|
-
return TokenKind.Plain;
|
|
45
|
-
}
|
|
46
|
-
const c0 = word.charCodeAt(0);
|
|
47
|
-
if (n < 32 && c0 < 128 && (this.lenMask[c0] & 1 << n) === 0) {
|
|
48
|
-
return TokenKind.Plain;
|
|
49
|
-
}
|
|
50
|
-
const key = this.foldCase ? word.toLowerCase() : word;
|
|
51
|
-
return this.words.get(key) ?? TokenKind.Plain;
|
|
101
|
+
return this.lookupSpan(word, 0, word.length);
|
|
52
102
|
}
|
|
53
103
|
lookupSpan(src, start, end) {
|
|
54
104
|
const n = end - start;
|
|
55
105
|
if (n <= 0 || n > this.maxLen) {
|
|
56
106
|
return TokenKind.Plain;
|
|
57
107
|
}
|
|
58
|
-
|
|
108
|
+
let c0 = src.charCodeAt(start);
|
|
59
109
|
if (n < 32 && c0 < 128 && (this.lenMask[c0] & 1 << n) === 0) {
|
|
60
110
|
return TokenKind.Plain;
|
|
61
111
|
}
|
|
62
|
-
|
|
63
|
-
|
|
112
|
+
let cm = src.charCodeAt(start + (n >> 1));
|
|
113
|
+
let cl = src.charCodeAt(end - 1);
|
|
114
|
+
const fold = this.foldCase;
|
|
115
|
+
if (fold) {
|
|
116
|
+
if (c0 >= 65 && c0 <= 90)
|
|
117
|
+
c0 += 32;
|
|
118
|
+
if (cm >= 65 && cm <= 90)
|
|
119
|
+
cm += 32;
|
|
120
|
+
if (cl >= 65 && cl <= 90)
|
|
121
|
+
cl += 32;
|
|
122
|
+
}
|
|
123
|
+
const tag = c0 & 255 | (cm & 255) << 8 | (cl & 255) << 16 | n << 24 | 0;
|
|
124
|
+
const slotTag = this.slotTag;
|
|
125
|
+
const mask = this.mask;
|
|
126
|
+
let idx = mixWordTag(tag) & mask;
|
|
127
|
+
while (true) {
|
|
128
|
+
const st = slotTag[idx];
|
|
129
|
+
if (st === 0)
|
|
130
|
+
return TokenKind.Plain;
|
|
131
|
+
if (st === tag) {
|
|
132
|
+
if (n <= 3 && (c0 | cm | cl) < 128) {
|
|
133
|
+
return this.slotKind[idx];
|
|
134
|
+
}
|
|
135
|
+
if (spanEquals(src, start, this.slotWord[idx], fold)) {
|
|
136
|
+
return this.slotKind[idx];
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
idx = idx + 1 & mask;
|
|
140
|
+
}
|
|
64
141
|
}
|
|
65
142
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "limn-hl",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Fast, lightweight, zero-dependency, native multi-target syntax highlighter compiled from one shared grammar spec, with built-in language detection",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -26,16 +26,16 @@
|
|
|
26
26
|
"default": "./dist/detect.js"
|
|
27
27
|
},
|
|
28
28
|
"./lang": {
|
|
29
|
-
"types": "./dist/lang
|
|
30
|
-
"default": "./dist/lang
|
|
29
|
+
"types": "./dist/lang.d.ts",
|
|
30
|
+
"default": "./dist/lang.js"
|
|
31
31
|
},
|
|
32
32
|
"./render": {
|
|
33
33
|
"types": "./dist/render/index.d.ts",
|
|
34
34
|
"default": "./dist/render/index.js"
|
|
35
35
|
},
|
|
36
36
|
"./theme": {
|
|
37
|
-
"types": "./dist/theme
|
|
38
|
-
"default": "./dist/theme
|
|
37
|
+
"types": "./dist/theme.d.ts",
|
|
38
|
+
"default": "./dist/theme.js"
|
|
39
39
|
}
|
|
40
40
|
},
|
|
41
41
|
"files": [
|
package/dist/lang/clike.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
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;
|
package/dist/lang/clike.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
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 }));
|
|
@@ -1,8 +0,0 @@
|
|
|
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;
|
|
@@ -1,220 +0,0 @@
|
|
|
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
|
-
}
|
package/dist/lang/code.d.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
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;
|