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.
Files changed (62) hide show
  1. package/README.md +306 -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 +278 -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 +57 -0
  55. package/dist/theme/generated.js +736 -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 +44 -9
@@ -0,0 +1,12 @@
1
+ /** Indicates whether a Theme targets light or dark backgrounds. */
2
+ export type ColorScheme = "light" | "dark";
3
+ /** Color palette for syntax highlighting. */
4
+ export interface Theme {
5
+ readonly name: string;
6
+ readonly scheme: ColorScheme;
7
+ readonly bg: string;
8
+ readonly fg: string;
9
+ readonly numbers: string;
10
+ readonly tokens: readonly string[];
11
+ }
12
+ export * from "./generated.js";
@@ -0,0 +1 @@
1
+ export * from "./generated.js";
@@ -0,0 +1,38 @@
1
+ /** Semantic token categories emitted by limn scanners. */
2
+ export declare const TokenKind: {
3
+ readonly Plain: 0;
4
+ readonly Keyword: 1;
5
+ readonly Type: 2;
6
+ readonly Constant: 3;
7
+ readonly Number: 4;
8
+ readonly String: 5;
9
+ readonly Escape: 6;
10
+ readonly Comment: 7;
11
+ readonly Func: 8;
12
+ readonly Variable: 9;
13
+ readonly Property: 10;
14
+ readonly Operator: 11;
15
+ readonly Punctuation: 12;
16
+ readonly Section: 13;
17
+ readonly Inserted: 14;
18
+ readonly Deleted: 15;
19
+ readonly Error: 16;
20
+ };
21
+ export type TokenKind = (typeof TokenKind)[keyof typeof TokenKind];
22
+ /** Total number of built-in TokenKind values. */
23
+ export declare const KIND_COUNT = 17;
24
+ /** Short lowercase names indexed by TokenKind. */
25
+ export declare const KIND_NAMES: readonly string[];
26
+ /** Returns the short lowercase name of kind (e.g. "kwd", "str"). */
27
+ export declare function kindName(kind: TokenKind): string;
28
+ /** Contiguous UTF-16 code-unit span [start, end) with a TokenKind. */
29
+ export interface Token {
30
+ readonly start: number;
31
+ readonly end: number;
32
+ readonly kind: TokenKind;
33
+ }
34
+ /**
35
+ * Groups tokens by '\n' line boundaries, splitting tokens that span newlines
36
+ * and omitting the '\n' characters while preserving exact offsets into src.
37
+ */
38
+ export declare function splitLines(src: string, tokens: readonly Token[]): Token[][];
package/dist/token.js ADDED
@@ -0,0 +1,67 @@
1
+ export const TokenKind = {
2
+ Plain: 0,
3
+ Keyword: 1,
4
+ Type: 2,
5
+ Constant: 3,
6
+ Number: 4,
7
+ String: 5,
8
+ Escape: 6,
9
+ Comment: 7,
10
+ Func: 8,
11
+ Variable: 9,
12
+ Property: 10,
13
+ Operator: 11,
14
+ Punctuation: 12,
15
+ Section: 13,
16
+ Inserted: 14,
17
+ Deleted: 15,
18
+ Error: 16
19
+ };
20
+ export const KIND_COUNT = 17;
21
+ export const KIND_NAMES = [
22
+ "plain",
23
+ "kwd",
24
+ "type",
25
+ "const",
26
+ "num",
27
+ "str",
28
+ "esc",
29
+ "cmnt",
30
+ "func",
31
+ "var",
32
+ "prop",
33
+ "oper",
34
+ "punct",
35
+ "section",
36
+ "ins",
37
+ "del",
38
+ "err"
39
+ ];
40
+ export function kindName(kind) {
41
+ return KIND_NAMES[kind] ?? "plain";
42
+ }
43
+ export function splitLines(src, tokens) {
44
+ if (src.length === 0) {
45
+ return [];
46
+ }
47
+ const lines = [[]];
48
+ for (let i = 0;i < tokens.length; i++) {
49
+ const tok = tokens[i];
50
+ let cur = tok.start < 0 ? 0 : tok.start;
51
+ const end = tok.end > src.length ? src.length : tok.end;
52
+ while (cur < end) {
53
+ const nl = src.indexOf(`
54
+ `, cur);
55
+ if (nl < 0 || nl >= end) {
56
+ lines[lines.length - 1].push({ start: cur, end, kind: tok.kind });
57
+ break;
58
+ }
59
+ if (nl > cur) {
60
+ lines[lines.length - 1].push({ start: cur, end: nl, kind: tok.kind });
61
+ }
62
+ lines.push([]);
63
+ cur = nl + 1;
64
+ }
65
+ }
66
+ return lines;
67
+ }
@@ -0,0 +1,23 @@
1
+ import { TokenKind } from "./token.js";
2
+ /** Associates a whitespace-separated list of words with a TokenKind. */
3
+ export interface WordGroup {
4
+ readonly kind: TokenKind;
5
+ readonly words: string;
6
+ }
7
+ /**
8
+ * Compact lookup table for language keywords, types, and constants,
9
+ * with an ASCII first-byte length bitmask to fast-reject non-keywords without slicing.
10
+ */
11
+ export declare class WordTable {
12
+ private readonly words;
13
+ private readonly lenMask;
14
+ private readonly maxLen;
15
+ private readonly foldCase;
16
+ constructor(foldCase: boolean, ...groups: readonly WordGroup[]);
17
+ /** Reports whether the table contains no words. */
18
+ isEmpty(): boolean;
19
+ /** Returns the TokenKind for word, or TokenKind.Plain if absent. */
20
+ lookup(word: string): TokenKind;
21
+ /** Returns the TokenKind for src.slice(start, end), avoiding allocation on length-mask miss. */
22
+ lookupSpan(src: string, start: number, end: number): TokenKind;
23
+ }
@@ -0,0 +1,65 @@
1
+ import { TokenKind } from "./token.js";
2
+
3
+ export class WordTable {
4
+ words = new Map;
5
+ lenMask = new Uint32Array(128);
6
+ maxLen = 0;
7
+ foldCase;
8
+ constructor(foldCase, ...groups) {
9
+ this.foldCase = foldCase;
10
+ let maxLen = 0;
11
+ for (const g of groups) {
12
+ if (g.kind === TokenKind.Plain || g.words === "") {
13
+ continue;
14
+ }
15
+ const parts = g.words.trim().split(/\s+/);
16
+ for (const raw of parts) {
17
+ if (raw === "")
18
+ continue;
19
+ const word = foldCase ? raw.toLowerCase() : raw;
20
+ this.words.set(word, g.kind);
21
+ const n = word.length;
22
+ if (n > maxLen) {
23
+ maxLen = n;
24
+ }
25
+ if (n < 32) {
26
+ const c0 = word.charCodeAt(0);
27
+ if (c0 < 128) {
28
+ this.lenMask[c0] |= 1 << n;
29
+ if (foldCase && c0 >= 97 && c0 <= 122) {
30
+ this.lenMask[c0 - 32] |= 1 << n;
31
+ }
32
+ }
33
+ }
34
+ }
35
+ }
36
+ this.maxLen = maxLen;
37
+ }
38
+ isEmpty() {
39
+ return this.words.size === 0;
40
+ }
41
+ lookup(word) {
42
+ const n = word.length;
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;
52
+ }
53
+ lookupSpan(src, start, end) {
54
+ const n = end - start;
55
+ if (n <= 0 || n > this.maxLen) {
56
+ return TokenKind.Plain;
57
+ }
58
+ const c0 = src.charCodeAt(start);
59
+ if (n < 32 && c0 < 128 && (this.lenMask[c0] & 1 << n) === 0) {
60
+ return TokenKind.Plain;
61
+ }
62
+ const word = src.slice(start, end);
63
+ return this.words.get(this.foldCase ? word.toLowerCase() : word) ?? TokenKind.Plain;
64
+ }
65
+ }
package/package.json CHANGED
@@ -1,17 +1,52 @@
1
1
  {
2
2
  "name": "limn-hl",
3
- "version": "0.0.0",
3
+ "version": "0.1.0",
4
4
  "description": "Tiny, fast, zero-dependency, synchronous syntax highlighter",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
8
- "url": "https://github.com/ekesti/limn"
8
+ "url": "git+https://github.com/ekesti/limn.git",
9
+ "directory": "js"
9
10
  },
10
- "keywords": [
11
- "ansi",
12
- "highlighter",
13
- "html",
14
- "lexer",
15
- "syntax-highlighting"
16
- ]
11
+ "type": "module",
12
+ "sideEffects": false,
13
+ "main": "./dist/index.js",
14
+ "types": "./dist/index.d.ts",
15
+ "exports": {
16
+ ".": {
17
+ "types": "./dist/index.d.ts",
18
+ "default": "./dist/index.js"
19
+ },
20
+ "./cursor": {
21
+ "types": "./dist/cursor.d.ts",
22
+ "default": "./dist/cursor.js"
23
+ },
24
+ "./detect": {
25
+ "types": "./dist/detect.d.ts",
26
+ "default": "./dist/detect.js"
27
+ },
28
+ "./lang": {
29
+ "types": "./dist/lang/index.d.ts",
30
+ "default": "./dist/lang/index.js"
31
+ },
32
+ "./render": {
33
+ "types": "./dist/render/index.d.ts",
34
+ "default": "./dist/render/index.js"
35
+ },
36
+ "./theme": {
37
+ "types": "./dist/theme/index.d.ts",
38
+ "default": "./dist/theme/index.js"
39
+ }
40
+ },
41
+ "files": [
42
+ "dist"
43
+ ],
44
+ "scripts": {
45
+ "build:js": "bun build src/*.ts src/*/*.ts --root src --outdir dist --format esm --no-bundle --target browser",
46
+ "build:types": "tsc -p tsconfig.build.json",
47
+ "build": "rm -rf dist && bun run build:js && bun run build:types",
48
+ "typecheck": "tsc -p tsconfig.json --noEmit",
49
+ "test": "bun test test",
50
+ "bench": "bun run bench/bench.ts"
51
+ }
17
52
  }