limn-hl 0.0.0 → 0.1.1

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.
Files changed (62) hide show
  1. package/README.md +336 -0
  2. package/dist/cursor.d.ts +32 -0
  3. package/dist/cursor.js +322 -0
  4. package/dist/detect.d.ts +5 -0
  5. package/dist/detect.js +565 -0
  6. package/dist/engine.d.ts +78 -0
  7. package/dist/engine.js +268 -0
  8. package/dist/index.d.ts +3 -0
  9. package/dist/index.js +17 -0
  10. package/dist/lang/clike.d.ts +15 -0
  11. package/dist/lang/clike.js +35 -0
  12. package/dist/lang/clike_helpers.d.ts +8 -0
  13. package/dist/lang/clike_helpers.js +220 -0
  14. package/dist/lang/code.d.ts +45 -0
  15. package/dist/lang/code.js +779 -0
  16. package/dist/lang/config.d.ts +5 -0
  17. package/dist/lang/config.js +164 -0
  18. package/dist/lang/css.d.ts +5 -0
  19. package/dist/lang/css.js +125 -0
  20. package/dist/lang/docs.d.ts +12 -0
  21. package/dist/lang/docs.js +462 -0
  22. package/dist/lang/formats.d.ts +10 -0
  23. package/dist/lang/formats.js +295 -0
  24. package/dist/lang/html.d.ts +11 -0
  25. package/dist/lang/html.js +298 -0
  26. package/dist/lang/index.d.ts +20 -0
  27. package/dist/lang/index.js +167 -0
  28. package/dist/lang/js.d.ts +9 -0
  29. package/dist/lang/js.js +532 -0
  30. package/dist/lang/js_helpers.d.ts +7 -0
  31. package/dist/lang/js_helpers.js +308 -0
  32. package/dist/lang/json.d.ts +3 -0
  33. package/dist/lang/json.js +136 -0
  34. package/dist/lang/markdown.d.ts +3 -0
  35. package/dist/lang/markdown.js +249 -0
  36. package/dist/lang/python.d.ts +3 -0
  37. package/dist/lang/python.js +335 -0
  38. package/dist/lang/scripting.d.ts +7 -0
  39. package/dist/lang/scripting.js +216 -0
  40. package/dist/lang/shell.d.ts +7 -0
  41. package/dist/lang/shell.js +404 -0
  42. package/dist/lang/specs_gen.d.ts +66 -0
  43. package/dist/lang/specs_gen.js +381 -0
  44. package/dist/lang/terminal.d.ts +9 -0
  45. package/dist/lang/terminal.js +550 -0
  46. package/dist/lang/yaml.d.ts +4 -0
  47. package/dist/lang/yaml.js +176 -0
  48. package/dist/render/ansi.d.ts +7 -0
  49. package/dist/render/ansi.js +70 -0
  50. package/dist/render/html.d.ts +20 -0
  51. package/dist/render/html.js +212 -0
  52. package/dist/render/index.d.ts +2 -0
  53. package/dist/render/index.js +2 -0
  54. package/dist/theme/generated.d.ts +59 -0
  55. package/dist/theme/generated.js +765 -0
  56. package/dist/theme/index.d.ts +12 -0
  57. package/dist/theme/index.js +1 -0
  58. package/dist/token.d.ts +38 -0
  59. package/dist/token.js +67 -0
  60. package/dist/wordtable.d.ts +23 -0
  61. package/dist/wordtable.js +65 -0
  62. package/package.json +45 -10
@@ -0,0 +1,308 @@
1
+ import { Cursor, isIdentCont, isIdentStart } from "../cursor.js";
2
+ import { TokenKind } from "../token.js";
3
+ export function isJSControlFlowWithParen(w) {
4
+ switch (w) {
5
+ case "if":
6
+ case "for":
7
+ case "while":
8
+ case "switch":
9
+ case "catch":
10
+ case "with":
11
+ case "return":
12
+ case "throw":
13
+ case "typeof":
14
+ case "await":
15
+ case "yield":
16
+ case "new":
17
+ case "super":
18
+ case "import":
19
+ case "case":
20
+ return true;
21
+ default:
22
+ return false;
23
+ }
24
+ }
25
+ export function canJSKeywordPrecedeRegex(w) {
26
+ return w !== "this" && w !== "super" && w !== "debugger";
27
+ }
28
+ export function hasJSCallParen(src, pos) {
29
+ const n = src.length;
30
+ while (pos < n && (src.charCodeAt(pos) === 32 || src.charCodeAt(pos) === 9)) {
31
+ pos++;
32
+ }
33
+ if (pos + 1 < n && src.charCodeAt(pos) === 63 && src.charCodeAt(pos + 1) === 46) {
34
+ pos += 2;
35
+ while (pos < n && (src.charCodeAt(pos) === 32 || src.charCodeAt(pos) === 9)) {
36
+ pos++;
37
+ }
38
+ }
39
+ return pos < n && src.charCodeAt(pos) === 40;
40
+ }
41
+ export function hasJSPropColon(src, pos) {
42
+ const n = src.length;
43
+ while (pos < n && (src.charCodeAt(pos) === 32 || src.charCodeAt(pos) === 9)) {
44
+ pos++;
45
+ }
46
+ if (pos + 1 < n && src.charCodeAt(pos) === 63 && src.charCodeAt(pos + 1) === 58) {
47
+ return true;
48
+ }
49
+ return pos < n && src.charCodeAt(pos) === 58 && (pos + 1 >= n || src.charCodeAt(pos + 1) !== 58);
50
+ }
51
+ function skipJSNonCode(src, pos, limit) {
52
+ const ch = src.charCodeAt(pos);
53
+ if (ch === 34 || ch === 39 || ch === 96) {
54
+ pos++;
55
+ while (pos < limit) {
56
+ const b = src.charCodeAt(pos);
57
+ if (b === 92) {
58
+ pos += 2;
59
+ continue;
60
+ }
61
+ if (b === ch)
62
+ return pos + 1;
63
+ if (b === 10 && ch !== 96)
64
+ return pos;
65
+ pos++;
66
+ }
67
+ return limit;
68
+ }
69
+ if (ch === 47 && pos + 1 < limit) {
70
+ const next = src.charCodeAt(pos + 1);
71
+ if (next === 47) {
72
+ pos += 2;
73
+ while (pos < limit && src.charCodeAt(pos) !== 10)
74
+ pos++;
75
+ return pos;
76
+ }
77
+ if (next === 42) {
78
+ pos += 2;
79
+ while (pos + 1 < limit && (src.charCodeAt(pos) !== 42 || src.charCodeAt(pos + 1) !== 47)) {
80
+ pos++;
81
+ }
82
+ return pos + 1 < limit ? pos + 2 : limit;
83
+ }
84
+ }
85
+ return pos;
86
+ }
87
+ export function isJSFuncRHS(src, pos) {
88
+ const limit = Math.min(src.length, pos + 256);
89
+ while (pos < limit && (src.charCodeAt(pos) === 32 || src.charCodeAt(pos) === 9)) {
90
+ pos++;
91
+ }
92
+ if (pos + 1 >= limit || src.charCodeAt(pos) !== 61 || src.charCodeAt(pos + 1) === 61 || src.charCodeAt(pos + 1) === 62) {
93
+ return false;
94
+ }
95
+ pos++;
96
+ while (pos < limit && (src.charCodeAt(pos) === 32 || src.charCodeAt(pos) === 9)) {
97
+ pos++;
98
+ }
99
+ if (pos + 6 <= limit && src.startsWith("async", pos) && (src.charCodeAt(pos + 5) === 32 || src.charCodeAt(pos + 5) === 9)) {
100
+ pos += 5;
101
+ while (pos < limit && (src.charCodeAt(pos) === 32 || src.charCodeAt(pos) === 9)) {
102
+ pos++;
103
+ }
104
+ }
105
+ if (pos + 8 <= limit && src.startsWith("function", pos) && (pos + 8 === src.length || !isIdentCont(src.charCodeAt(pos + 8)) && src.charCodeAt(pos + 8) !== 36)) {
106
+ return true;
107
+ }
108
+ if (pos < limit && src.charCodeAt(pos) === 60) {
109
+ pos++;
110
+ let depth = 1;
111
+ while (pos < limit && depth > 0) {
112
+ const next = skipJSNonCode(src, pos, limit);
113
+ if (next !== pos) {
114
+ pos = next;
115
+ continue;
116
+ }
117
+ const b = src.charCodeAt(pos);
118
+ if (b === 10 || b === 59 || b === 123)
119
+ return false;
120
+ if (b === 60)
121
+ depth++;
122
+ else if (b === 62 && src.charCodeAt(pos - 1) !== 61)
123
+ depth--;
124
+ pos++;
125
+ }
126
+ if (depth !== 0)
127
+ return false;
128
+ while (pos < limit && (src.charCodeAt(pos) === 32 || src.charCodeAt(pos) === 9)) {
129
+ pos++;
130
+ }
131
+ }
132
+ if (pos < limit && src.charCodeAt(pos) === 40) {
133
+ pos++;
134
+ let depth = 1;
135
+ while (pos < limit && depth > 0) {
136
+ const next = skipJSNonCode(src, pos, limit);
137
+ if (next !== pos) {
138
+ pos = next;
139
+ continue;
140
+ }
141
+ const b = src.charCodeAt(pos);
142
+ if (b === 59)
143
+ return false;
144
+ if (b === 40)
145
+ depth++;
146
+ else if (b === 41)
147
+ depth--;
148
+ pos++;
149
+ }
150
+ if (depth !== 0)
151
+ return false;
152
+ while (pos < limit && (src.charCodeAt(pos) === 32 || src.charCodeAt(pos) === 9)) {
153
+ pos++;
154
+ }
155
+ if (pos + 1 < limit && src.charCodeAt(pos) === 61 && src.charCodeAt(pos + 1) === 62) {
156
+ return true;
157
+ }
158
+ if (pos < limit && src.charCodeAt(pos) === 58) {
159
+ pos++;
160
+ let retDepth = 0;
161
+ while (pos < limit) {
162
+ const next = skipJSNonCode(src, pos, limit);
163
+ if (next !== pos) {
164
+ pos = next;
165
+ continue;
166
+ }
167
+ const b = src.charCodeAt(pos);
168
+ if (b === 10 || b === 59)
169
+ return false;
170
+ if (b === 44 || b === 41) {
171
+ if (retDepth === 0)
172
+ return false;
173
+ if (b === 41)
174
+ retDepth--;
175
+ } else if (b === 40 || b === 91 || b === 123 || b === 60) {
176
+ retDepth++;
177
+ } else if (b === 93 || b === 125) {
178
+ if (retDepth > 0)
179
+ retDepth--;
180
+ } else if (b === 62) {
181
+ if (retDepth > 0 && src.charCodeAt(pos - 1) !== 61)
182
+ retDepth--;
183
+ } else if (b === 61 && pos + 1 < limit && src.charCodeAt(pos + 1) === 62) {
184
+ return true;
185
+ }
186
+ pos++;
187
+ }
188
+ }
189
+ return false;
190
+ }
191
+ if (pos < limit && (isIdentStart(src.charCodeAt(pos)) || src.charCodeAt(pos) === 36)) {
192
+ pos++;
193
+ while (pos < limit && (isIdentCont(src.charCodeAt(pos)) || src.charCodeAt(pos) === 36)) {
194
+ pos++;
195
+ }
196
+ while (pos < limit && (src.charCodeAt(pos) === 32 || src.charCodeAt(pos) === 9)) {
197
+ pos++;
198
+ }
199
+ return pos + 1 < limit && src.charCodeAt(pos) === 61 && src.charCodeAt(pos + 1) === 62;
200
+ }
201
+ return false;
202
+ }
203
+ export function tryJSXTag(src, pos, exprLang, e) {
204
+ const n = src.length;
205
+ if (pos + 1 >= n)
206
+ return 0;
207
+ const next = src.charCodeAt(pos + 1);
208
+ if (next !== 62 && next !== 47 && !isIdentStart(next) && next !== 36) {
209
+ return 0;
210
+ }
211
+ const cur = new Cursor(src, pos);
212
+ if (cur.hasPrefix("<>")) {
213
+ e.emit(pos, pos + 2, TokenKind.Punctuation);
214
+ return pos + 2;
215
+ }
216
+ if (cur.hasPrefix("</>")) {
217
+ e.emit(pos, pos + 3, TokenKind.Punctuation);
218
+ return pos + 3;
219
+ }
220
+ let isClose = false;
221
+ if (next === 47) {
222
+ isClose = true;
223
+ cur.pos += 2;
224
+ e.emit(pos, pos + 2, TokenKind.Punctuation);
225
+ } else {
226
+ cur.pos++;
227
+ e.emit(pos, pos + 1, TokenKind.Punctuation);
228
+ }
229
+ const nameStart = cur.pos;
230
+ while (cur.pos < n) {
231
+ const b = src.charCodeAt(cur.pos);
232
+ if (isIdentCont(b) || b === 46 || b === 45)
233
+ cur.pos++;
234
+ else
235
+ break;
236
+ }
237
+ if (cur.pos === nameStart)
238
+ return 0;
239
+ const firstCode = src.charCodeAt(nameStart);
240
+ e.emit(nameStart, cur.pos, firstCode >= 65 && firstCode <= 90 ? TokenKind.Type : TokenKind.Variable);
241
+ if (isClose) {
242
+ cur.skipWhitespace();
243
+ if (cur.peek() === 62) {
244
+ e.emit(cur.pos, cur.pos + 1, TokenKind.Punctuation);
245
+ return cur.pos + 1;
246
+ }
247
+ return cur.pos;
248
+ }
249
+ while (cur.pos < n) {
250
+ cur.skipWhitespace();
251
+ if (cur.eof())
252
+ return cur.pos;
253
+ if (cur.hasPrefix("/>")) {
254
+ e.emit(cur.pos, cur.pos + 2, TokenKind.Punctuation);
255
+ return cur.pos + 2;
256
+ }
257
+ if (cur.peek() === 62) {
258
+ e.emit(cur.pos, cur.pos + 1, TokenKind.Punctuation);
259
+ return cur.pos + 1;
260
+ }
261
+ if (cur.peek() === 123) {
262
+ const openPos = cur.pos;
263
+ e.emit(openPos, openPos + 1, TokenKind.Punctuation);
264
+ cur.pos++;
265
+ const closed = cur.scanBalanced(123, 125, true);
266
+ const exprEnd = closed ? cur.pos - 1 : cur.pos;
267
+ e.embed(src, openPos + 1, exprEnd, exprLang, TokenKind.Plain);
268
+ if (closed)
269
+ e.emit(exprEnd, cur.pos, TokenKind.Punctuation);
270
+ continue;
271
+ }
272
+ const attrStart = cur.pos;
273
+ while (cur.pos < n) {
274
+ const b = src.charCodeAt(cur.pos);
275
+ if (isIdentCont(b) || b === 45 || b === 58)
276
+ cur.pos++;
277
+ else
278
+ break;
279
+ }
280
+ if (cur.pos === attrStart) {
281
+ cur.pos++;
282
+ continue;
283
+ }
284
+ e.emit(attrStart, cur.pos, TokenKind.Property);
285
+ cur.skipWhitespace();
286
+ if (cur.peek() !== 61)
287
+ continue;
288
+ e.emit(cur.pos, cur.pos + 1, TokenKind.Operator);
289
+ cur.pos++;
290
+ cur.skipWhitespace();
291
+ const valCh = cur.peek();
292
+ if (valCh === 34 || valCh === 39) {
293
+ const vs = cur.pos;
294
+ cur.scanQuoted(valCh, false, false);
295
+ e.emit(vs, cur.pos, TokenKind.String);
296
+ } else if (valCh === 123) {
297
+ const openPos = cur.pos;
298
+ e.emit(openPos, openPos + 1, TokenKind.Punctuation);
299
+ cur.pos++;
300
+ const closed = cur.scanBalanced(123, 125, true);
301
+ const exprEnd = closed ? cur.pos - 1 : cur.pos;
302
+ e.embed(src, openPos + 1, exprEnd, exprLang, TokenKind.Plain);
303
+ if (closed)
304
+ e.emit(exprEnd, cur.pos, TokenKind.Punctuation);
305
+ }
306
+ }
307
+ return cur.pos;
308
+ }
@@ -0,0 +1,3 @@
1
+ import { Grammar } from "../engine.js";
2
+ /** Returns the JSON / JSONC / JSON5 grammar. */
3
+ export declare const json: () => Grammar;
@@ -0,0 +1,136 @@
1
+ import { Cursor, isIdentCont, isIdentStart } from "../cursor.js";
2
+ import { Grammar } from "../engine.js";
3
+ import { TokenKind } from "../token.js";
4
+ import { onceGrammar } from "./code.js";
5
+ function isHexOrNumByte(c) {
6
+ return c >= 48 && c <= 57 || c >= 97 && c <= 102 || c >= 65 && c <= 70 || c === 46 || c === 120 || c === 88 || c === 43 || c === 45 || c === 95;
7
+ }
8
+ function scanJSONString(src, start, quote, expectKey, e) {
9
+ const n = src.length;
10
+ let end = start + 1;
11
+ let hasEscape = false;
12
+ while (end < n) {
13
+ const c = src.charCodeAt(end);
14
+ if (c === 92) {
15
+ hasEscape = true;
16
+ end += 2;
17
+ continue;
18
+ }
19
+ if (c === quote) {
20
+ end++;
21
+ break;
22
+ }
23
+ if (c === 10) {
24
+ break;
25
+ }
26
+ end++;
27
+ }
28
+ if (end > n)
29
+ end = n;
30
+ let k = end;
31
+ while (k < n && (src.charCodeAt(k) === 32 || src.charCodeAt(k) === 9))
32
+ k++;
33
+ const kind = expectKey || k < n && src.charCodeAt(k) === 58 ? TokenKind.Property : TokenKind.String;
34
+ if (!hasEscape) {
35
+ e.emit(start, end, kind);
36
+ return end;
37
+ }
38
+ let seg = start;
39
+ let j = start + 1;
40
+ while (j < end - 1 || j < end && src.charCodeAt(end - 1) !== quote) {
41
+ if (src.charCodeAt(j) === 92) {
42
+ if (j > seg)
43
+ e.emit(seg, j, kind);
44
+ let escEnd = j + 2;
45
+ if (j + 1 < end && src.charCodeAt(j + 1) === 117) {
46
+ escEnd = Math.min(j + 6, end);
47
+ } else if (escEnd > end) {
48
+ escEnd = end;
49
+ }
50
+ e.emit(j, escEnd, TokenKind.Escape);
51
+ j = escEnd;
52
+ seg = j;
53
+ continue;
54
+ }
55
+ j++;
56
+ }
57
+ if (seg < end)
58
+ e.emit(seg, end, kind);
59
+ return end;
60
+ }
61
+ function scanJSON(src, e) {
62
+ const n = src.length;
63
+ let i = 0;
64
+ const objStack = [];
65
+ let expectKey = false;
66
+ while (i < n && !e.stopped()) {
67
+ const ch = src.charCodeAt(i);
68
+ if (ch === 32 || ch === 9 || ch === 10 || ch === 13) {
69
+ i++;
70
+ } else if (ch === 47 && i + 1 < n && src.charCodeAt(i + 1) === 47) {
71
+ const nl = src.indexOf(`
72
+ `, i + 2);
73
+ const end = nl >= 0 ? nl : n;
74
+ e.emit(i, end, TokenKind.Comment);
75
+ i = end;
76
+ } else if (ch === 47 && i + 1 < n && src.charCodeAt(i + 1) === 42) {
77
+ const cur = new Cursor(src, i + 2);
78
+ cur.scanUntil("*/");
79
+ e.emit(i, cur.pos, TokenKind.Comment);
80
+ i = cur.pos;
81
+ } else if (ch === 34 || ch === 39) {
82
+ i = scanJSONString(src, i, ch, expectKey, e);
83
+ expectKey = false;
84
+ } else if (ch === 45 || ch >= 48 && ch <= 57) {
85
+ const start = i;
86
+ if (ch === 45)
87
+ i++;
88
+ while (i < n && isHexOrNumByte(src.charCodeAt(i)))
89
+ i++;
90
+ if (i > start + 1 || ch >= 48 && ch <= 57) {
91
+ e.emit(start, i, TokenKind.Number);
92
+ } else {
93
+ e.emit(start, i, TokenKind.Operator);
94
+ }
95
+ expectKey = false;
96
+ } else if (ch === 123 || ch === 125 || ch === 91 || ch === 93 || ch === 58 || ch === 44) {
97
+ e.emit(i, i + 1, TokenKind.Punctuation);
98
+ if (ch === 123) {
99
+ objStack.push(true);
100
+ expectKey = true;
101
+ } else if (ch === 91) {
102
+ objStack.push(false);
103
+ expectKey = false;
104
+ } else if (ch === 125 || ch === 93) {
105
+ objStack.pop();
106
+ expectKey = false;
107
+ } else if (ch === 58) {
108
+ expectKey = false;
109
+ } else {
110
+ expectKey = objStack.length > 0 && objStack[objStack.length - 1];
111
+ }
112
+ i++;
113
+ } else if (isIdentStart(ch)) {
114
+ const start = i;
115
+ i++;
116
+ while (i < n && (isIdentCont(src.charCodeAt(i)) || src.charCodeAt(i) === 45)) {
117
+ i++;
118
+ }
119
+ const w = src.slice(start, i);
120
+ let k = i;
121
+ while (k < n && (src.charCodeAt(k) === 32 || src.charCodeAt(k) === 9))
122
+ k++;
123
+ if (k < n && src.charCodeAt(k) === 58) {
124
+ e.emit(start, i, TokenKind.Property);
125
+ } else if (w === "true" || w === "false" || w === "null" || w === "Infinity" || w === "NaN") {
126
+ e.emit(start, i, TokenKind.Constant);
127
+ } else if (expectKey) {
128
+ e.emit(start, i, TokenKind.Property);
129
+ }
130
+ expectKey = false;
131
+ } else {
132
+ i++;
133
+ }
134
+ }
135
+ }
136
+ export const json = /* @__PURE__ */ onceGrammar(() => new Grammar("json", scanJSON, "jsonc", "json5", "jsonl", "ndjson"));
@@ -0,0 +1,3 @@
1
+ import { Grammar } from "../engine.js";
2
+ /** Returns the Markdown grammar (including fenced code block embedding). */
3
+ export declare const md: () => Grammar;
@@ -0,0 +1,249 @@
1
+ import { isDigit } from "../cursor.js";
2
+ import { Grammar } from "../engine.js";
3
+ import { TokenKind } from "../token.js";
4
+ import { onceGrammar } from "./code.js";
5
+ function firstWord(s) {
6
+ let i = 0;
7
+ while (i < s.length && (s.charCodeAt(i) === 32 || s.charCodeAt(i) === 9))
8
+ i++;
9
+ const start = i;
10
+ while (i < s.length) {
11
+ const c = s.charCodeAt(i);
12
+ if (c === 32 || c === 9 || c === 13)
13
+ break;
14
+ i++;
15
+ }
16
+ return s.slice(start, i);
17
+ }
18
+ function matchFenceOpen(src, pos) {
19
+ const n = src.length;
20
+ let i = pos;
21
+ while (i < n && i < pos + 3 && src.charCodeAt(i) === 32)
22
+ i++;
23
+ if (i >= n)
24
+ return [0, 0, 0, 0, 0];
25
+ const fc = src.charCodeAt(i);
26
+ if (fc !== 96 && fc !== 126)
27
+ return [0, 0, 0, 0, 0];
28
+ const start = i;
29
+ while (i < n && src.charCodeAt(i) === fc)
30
+ i++;
31
+ if (i - start < 3)
32
+ return [0, 0, 0, 0, 0];
33
+ const nl = src.indexOf(`
34
+ `, i);
35
+ const le = nl >= 0 ? nl : n;
36
+ if (fc === 96) {
37
+ const bt = src.indexOf("`", i);
38
+ if (bt >= 0 && bt < le)
39
+ return [0, 0, 0, 0, 0];
40
+ }
41
+ return [fc, i - start, i, le, le];
42
+ }
43
+ function findFenceClose(src, start, fenceChar, minLen) {
44
+ const n = src.length;
45
+ let pos = start;
46
+ while (pos < n) {
47
+ const nl = src.indexOf(`
48
+ `, pos);
49
+ const le = nl >= 0 ? nl : n;
50
+ let i = pos;
51
+ while (i < le && i < pos + 3 && src.charCodeAt(i) === 32)
52
+ i++;
53
+ let cnt = 0;
54
+ while (i + cnt < le && src.charCodeAt(i + cnt) === fenceChar)
55
+ cnt++;
56
+ if (cnt >= minLen && src.slice(i + cnt, le).trim() === "") {
57
+ return [pos, pos, le];
58
+ }
59
+ if (le === n)
60
+ break;
61
+ pos = le + 1;
62
+ }
63
+ return [n, n, n];
64
+ }
65
+ function isThematicBreak(src, start, end) {
66
+ if (end - start < 3)
67
+ return false;
68
+ const ch = src.charCodeAt(start);
69
+ if (ch !== 45 && ch !== 42 && ch !== 95)
70
+ return false;
71
+ let count = 0;
72
+ for (let i = start;i < end; i++) {
73
+ const c = src.charCodeAt(i);
74
+ if (c === ch)
75
+ count++;
76
+ else if (c !== 32 && c !== 9)
77
+ return false;
78
+ }
79
+ return count >= 3;
80
+ }
81
+ function scanMarkdownLine(src, start, end, e) {
82
+ let i = start;
83
+ while (i < end && i < start + 3 && src.charCodeAt(i) === 32)
84
+ i++;
85
+ if (i >= end)
86
+ return;
87
+ if (src.charCodeAt(i) === 35) {
88
+ let h = i;
89
+ while (h < end && h < i + 6 && src.charCodeAt(h) === 35)
90
+ h++;
91
+ if (h === end || src.charCodeAt(h) === 32 || src.charCodeAt(h) === 9) {
92
+ e.emit(i, end, TokenKind.Section);
93
+ return;
94
+ }
95
+ }
96
+ if (isThematicBreak(src, i, end)) {
97
+ e.emit(i, end, TokenKind.Comment);
98
+ return;
99
+ }
100
+ if (src.charCodeAt(i) === 62) {
101
+ e.emit(i, end, TokenKind.Comment);
102
+ return;
103
+ }
104
+ const first = src.charCodeAt(i);
105
+ if ((first === 45 || first === 42 || first === 43) && i + 1 < end && src.charCodeAt(i + 1) === 32) {
106
+ e.emit(i, i + 1, TokenKind.Operator);
107
+ i += 2;
108
+ } else if (isDigit(first)) {
109
+ let d = i;
110
+ while (d < end && isDigit(src.charCodeAt(d)))
111
+ d++;
112
+ if (d + 1 < end && (src.charCodeAt(d) === 46 || src.charCodeAt(d) === 41) && src.charCodeAt(d + 1) === 32) {
113
+ e.emit(i, d + 1, TokenKind.Operator);
114
+ i = d + 2;
115
+ }
116
+ }
117
+ while (i < end && !e.stopped()) {
118
+ const ch = src.charCodeAt(i);
119
+ if (ch === 92) {
120
+ if (i + 1 < end) {
121
+ e.emit(i, i + 2, TokenKind.Escape);
122
+ i += 2;
123
+ } else {
124
+ i++;
125
+ }
126
+ } else if (ch === 96) {
127
+ let ticks = 0;
128
+ while (i + ticks < end && src.charCodeAt(i + ticks) === 96)
129
+ ticks++;
130
+ const closeSeq = src.slice(i, i + ticks);
131
+ const idx = src.indexOf(closeSeq, i + ticks);
132
+ if (idx >= 0 && idx + ticks <= end) {
133
+ const ce = idx + ticks;
134
+ e.emit(i, ce, TokenKind.String);
135
+ i = ce;
136
+ } else if (i + ticks < end) {
137
+ e.emit(i, end, TokenKind.String);
138
+ i = end;
139
+ } else {
140
+ i += ticks;
141
+ }
142
+ } else if (ch === 42) {
143
+ if (i + 1 < end && src.charCodeAt(i + 1) === 42) {
144
+ const idx = src.indexOf("**", i + 2);
145
+ if (idx > i + 2 && idx + 2 <= end) {
146
+ e.emit(i, idx + 2, TokenKind.Keyword);
147
+ i = idx + 2;
148
+ continue;
149
+ }
150
+ if (i + 2 < end) {
151
+ const next = src.charCodeAt(i + 2);
152
+ if (next !== 32 && next !== 9 && next !== 42) {
153
+ e.emit(i, end, TokenKind.Keyword);
154
+ i = end;
155
+ continue;
156
+ }
157
+ }
158
+ }
159
+ const idx = src.indexOf("*", i + 1);
160
+ if (idx > i + 1 && idx + 1 <= end) {
161
+ e.emit(i, idx + 1, TokenKind.Type);
162
+ i = idx + 1;
163
+ continue;
164
+ }
165
+ i++;
166
+ } else if (ch === 126) {
167
+ if (i + 1 < end && src.charCodeAt(i + 1) === 126) {
168
+ const idx = src.indexOf("~~", i + 2);
169
+ if (idx > i + 2 && idx + 2 <= end) {
170
+ e.emit(i, idx + 2, TokenKind.Deleted);
171
+ i = idx + 2;
172
+ continue;
173
+ }
174
+ if (i + 2 < end) {
175
+ const next = src.charCodeAt(i + 2);
176
+ if (next !== 32 && next !== 9 && next !== 126) {
177
+ e.emit(i, end, TokenKind.Deleted);
178
+ i = end;
179
+ continue;
180
+ }
181
+ }
182
+ }
183
+ i++;
184
+ } else if (ch === 91) {
185
+ const rb = src.indexOf("]", i + 1);
186
+ if (rb >= 0 && rb < end && rb + 1 < end && src.charCodeAt(rb + 1) === 40) {
187
+ const rp = src.indexOf(")", rb + 2);
188
+ if (rp >= 0 && rp < end) {
189
+ e.emit(i, rb + 1, TokenKind.Func);
190
+ e.emit(rb + 1, rb + 2, TokenKind.Punctuation);
191
+ e.emit(rb + 2, rp, TokenKind.String);
192
+ e.emit(rp, rp + 1, TokenKind.Punctuation);
193
+ i = rp + 1;
194
+ continue;
195
+ }
196
+ e.emit(i, rb + 1, TokenKind.Func);
197
+ e.emit(rb + 1, rb + 2, TokenKind.Punctuation);
198
+ if (rb + 2 < end)
199
+ e.emit(rb + 2, end, TokenKind.String);
200
+ i = end;
201
+ continue;
202
+ }
203
+ i++;
204
+ } else {
205
+ i++;
206
+ }
207
+ }
208
+ }
209
+ function scanMarkdown(src, e) {
210
+ const n = src.length;
211
+ let pos = 0;
212
+ while (pos < n && !e.stopped()) {
213
+ const [fenceChar, fenceLen, infoStart, infoEnd, lineEnd] = matchFenceOpen(src, pos);
214
+ if (fenceLen >= 3) {
215
+ e.emit(pos, infoStart, TokenKind.Keyword);
216
+ const langName = firstWord(src.slice(infoStart, infoEnd));
217
+ if (infoEnd > infoStart)
218
+ e.emit(infoStart, infoEnd, TokenKind.Type);
219
+ let bodyStart = lineEnd;
220
+ if (bodyStart < n && src.charCodeAt(bodyStart) === 10)
221
+ bodyStart++;
222
+ const [bodyEnd, closeStart, closeEnd] = findFenceClose(src, bodyStart, fenceChar, fenceLen);
223
+ if (bodyEnd > bodyStart) {
224
+ if (langName !== "") {
225
+ e.embed(src, bodyStart, bodyEnd, e.lookup(langName), TokenKind.Plain);
226
+ } else {
227
+ e.emit(bodyStart, bodyEnd, TokenKind.Plain);
228
+ }
229
+ }
230
+ if (closeEnd > closeStart) {
231
+ e.emit(closeStart, closeEnd, TokenKind.Keyword);
232
+ pos = closeEnd;
233
+ } else {
234
+ pos = n;
235
+ }
236
+ if (pos < n && src.charCodeAt(pos) === 10)
237
+ pos++;
238
+ continue;
239
+ }
240
+ const nl = src.indexOf(`
241
+ `, pos);
242
+ const le = nl >= 0 ? nl : n;
243
+ scanMarkdownLine(src, pos, le, e);
244
+ if (nl < 0)
245
+ break;
246
+ pos = le + 1;
247
+ }
248
+ }
249
+ export const md = /* @__PURE__ */ onceGrammar(() => new Grammar("md", scanMarkdown, "markdown"));
@@ -0,0 +1,3 @@
1
+ import { Grammar } from "../engine.js";
2
+ /** Returns the Python grammar. */
3
+ export declare const py: () => Grammar;