limn-hl 0.1.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.
package/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  [![crates.io](https://img.shields.io/crates/v/limn-hl.svg)](https://crates.io/crates/limn-hl)
6
6
  [![npm](https://img.shields.io/npm/v/limn-hl.svg)](https://www.npmjs.com/package/limn-hl)
7
7
 
8
- An extremely fast, zero-dependency, synchronous syntax highlighter for multiple languages. Includes 59 languages, 25 built-in themes, language auto-detection, and HTML or 24-bit ANSI output.
8
+ Fast, lightweight, zero-dependency, synchronous syntax highlighter. Includes 59 languages, 26 built-in themes, language auto-detection, and HTML or 24-bit ANSI output.
9
9
 
10
10
  | Target | Package | Highlights |
11
11
  | --- | --- | --- |
@@ -280,7 +280,7 @@ Token CSS class suffixes (`--limn-*` / `.limn-*`):
280
280
 
281
281
  </details>
282
282
 
283
- ## Built-in Themes (25)
283
+ ## Built-in Themes (26)
284
284
 
285
285
  | Family | Light | Dark |
286
286
  | --- | --- | --- |
@@ -293,10 +293,40 @@ Token CSS class suffixes (`--limn-*` / `.limn-*`):
293
293
  | Gruvbox | gruvbox-light | gruvbox-dark |
294
294
  | Solarized | solarized-light | solarized-dark |
295
295
  | VS Code Modern | vscode-light-modern | vscode-dark-modern |
296
- | Dark-only | - | dracula, monokai, night-owl, nord, tokyo-night, vesper |
296
+ | Dark-only | - | dracula, monokai, night-owl, nord, tokyo-night, [undefined-theme](https://github.com/so1ve/nix-config/blob/main/dotfiles/nvim/colors/undefined.lua), vesper |
297
297
 
298
298
  Kebab-case theme names map mechanically to each target's naming convention (`tokyo-night` becomes `theme.TokyoNight` in Go, `theme::TOKYO_NIGHT` in Rust, `tokyoNight` in JS/TS, and `theme.tokyo_night` in Zig; `default` becomes `defaultTheme` in JS/TS and `theme.default_theme` in Zig; C uses kebab-case theme names directly).
299
299
 
300
+ ## Benchmarks
301
+
302
+ Measured on AMD Ryzen 7 PRO 8840HS (Linux `7.3.6` x86_64) across four ~3-4 KB source files in [`bench/corpus`](bench/corpus) (`sample.go` 3,343 B, `sample.js` 3,637 B, `sample.rs` 3,788 B, `sample.json` 2,965 B). Higher is better.
303
+
304
+ ### `limn` Tokenization Throughput
305
+
306
+ | Target | `sample.go` | `sample.js` | `sample.rs` | `sample.json` |
307
+ | --- | ---: | ---: | ---: | ---: |
308
+ | Zig (`0.17.1`, `ReleaseFast`) | 689.04 MB/s | 409.41 MB/s | 595.00 MB/s | 915.29 MB/s |
309
+ | Rust (`1.102.0-nightly`, `--release`) | 581.56 MB/s | 369.93 MB/s | 571.53 MB/s | 955.30 MB/s |
310
+ | Go (`go1.27.3`) | 501.12 MB/s | 350.58 MB/s | 451.02 MB/s | 769.45 MB/s |
311
+ | JS / TS (`Node.js v24.22.2`) | 112.90 MB/s | 53.18 MB/s | 87.56 MB/s | 168.45 MB/s |
312
+
313
+ ### End-to-End HTML Highlighting vs. Ecosystem Libraries
314
+
315
+ | Ecosystem | Library | `sample.go` | `sample.js` | `sample.rs` | `sample.json` | Speedup |
316
+ | --- | --- | ---: | ---: | ---: | ---: | --- |
317
+ | Go | **limn** | **192.70 MB/s** | **154.47 MB/s** | **171.46 MB/s** | **222.62 MB/s** | baseline |
318
+ | Go | Chroma (`v2.27.0`) | 0.91 MB/s | 1.03 MB/s | 0.82 MB/s | 2.59 MB/s | 86x to 212x slower |
319
+ | Rust | **limn** | **258.65 MB/s** | **186.03 MB/s** | **236.60 MB/s** | **330.63 MB/s** | baseline |
320
+ | Rust | syntect (`5.3.0`, `onig`) | 2.39 MB/s | 0.94 MB/s | 1.86 MB/s | 4.50 MB/s | 73x to 198x slower |
321
+ | Node.js | **limn** | **70.08 MB/s** | **28.76 MB/s** | **61.48 MB/s** | **90.32 MB/s** | baseline |
322
+ | Node.js | Prism (`1.30.0`) | 14.75 MB/s | 5.47 MB/s | 9.85 MB/s | 14.70 MB/s | 4.8x to 6.2x slower |
323
+ | Node.js | highlight.js (`11.12.0`) | 13.50 MB/s | 3.26 MB/s | 9.42 MB/s | 3.54 MB/s | 5.2x to 25.5x slower |
324
+ | Node.js | Shiki (`4.6.0`) | 0.58 MB/s | 0.43 MB/s | 0.63 MB/s | 1.40 MB/s | 65x to 121x slower |
325
+
326
+ - **Methodology:** All grammars, themes, and highlighters are initialized once before warmup (`8` iterations) and timed over at least `500 ms` per case (`bench/go`, `bench/rust`, `bench/js`, `bench/zig`). `limn`, Chroma, syntect, and Shiki emit inline styles (no line numbers); Prism and highlight.js emit CSS class spans.
327
+ - **Fairness note:** `limn` uses hand-written lexical scanners for speed and zero dependencies, whereas TextMate engines such as `syntect` and `Shiki` evaluate full TextMate scopes at higher startup and runtime cost.
328
+ - **Reproduce:** Install the JS comparison dependencies (`cd bench/js && npm install`, or `bun install`) and run `just bench-compare`. Run `just bench` for per-target microbenchmarks.
329
+
300
330
  ## Scope
301
331
 
302
332
  `limn` handles lexical highlighting accurately: strings, escapes, comments, regexes, template interpolations, and embedded languages such as `<script>`/`<style>` in HTML or fenced code blocks in Markdown. It is not an AST parser or semantic highlighter, so use Tree-sitter or Shiki when full syntactic or semantic analysis is required.
@@ -8,6 +8,18 @@ import {
8
8
  import { Grammar } from "../engine.js";
9
9
  import { TokenKind, WordTable } from "../index.js";
10
10
  import { onceGrammar } from "./code.js";
11
+ const BASH_CMD = 0;
12
+ const BASH_ASSIGN = 1;
13
+ const BASH_ARG = 2;
14
+ function isBashMetaOrSpace(b) {
15
+ return b === 32 || b === 9 || b === 10 || b === 13 || b === 59 || b === 124 || b === 38 || b === 60 || b === 62 || b === 40 || b === 41 || b === 123 || b === 125 || b === 34 || b === 39 || b === 35 || b === 61;
16
+ }
17
+ function isBashCmdIntroKeyword(w) {
18
+ return w === "if" || w === "elif" || w === "else" || w === "then" || w === "while" || w === "until" || w === "do" || w === "time" || w === "coproc";
19
+ }
20
+ function isBashExpectInKeyword(w) {
21
+ return w === "for" || w === "case" || w === "select";
22
+ }
11
23
  function scanBashDollar(src, pos, e) {
12
24
  const n = src.length;
13
25
  if (pos + 1 >= n)
@@ -74,13 +86,27 @@ function scanBashDoubleString(src, start, e) {
74
86
  e.emit(seg, n, TokenKind.String);
75
87
  return n;
76
88
  }
77
- function scanBash(src, words, e) {
89
+ function scanBash(src, words, startCmd, e) {
78
90
  const n = src.length;
91
+ let state = startCmd ? BASH_CMD : BASH_ARG;
92
+ let expectIn = false;
93
+ let inTest = false;
79
94
  let i = 0;
80
95
  while (i < n && !e.stopped()) {
81
96
  const ch = src.charCodeAt(i);
82
- if (ch === 32 || ch === 9 || ch === 10 || ch === 13) {
97
+ if (ch === 32 || ch === 9 || ch === 13) {
98
+ if (state === BASH_ASSIGN)
99
+ state = BASH_CMD;
100
+ i++;
101
+ } else if (ch === 10) {
102
+ state = BASH_CMD;
103
+ expectIn = false;
104
+ inTest = false;
83
105
  i++;
106
+ } else if (ch === 92 && i + 1 < n && (src.charCodeAt(i + 1) === 10 || src.charCodeAt(i + 1) === 13 && i + 2 < n && src.charCodeAt(i + 2) === 10)) {
107
+ if (state === BASH_ASSIGN)
108
+ state = BASH_CMD;
109
+ i += src.charCodeAt(i + 1) === 13 ? 3 : 2;
84
110
  } else if (ch === 35 && (i === 0 || src.charCodeAt(i - 1) === 32 || src.charCodeAt(i - 1) === 9 || src.charCodeAt(i - 1) === 10 || src.charCodeAt(i - 1) === 59)) {
85
111
  const nl = src.indexOf(`
86
112
  `, i + 1);
@@ -91,16 +117,24 @@ function scanBash(src, words, e) {
91
117
  const cur = new Cursor(src, i);
92
118
  cur.scanQuoted(39, true, true);
93
119
  e.emit(i, cur.pos, TokenKind.String);
120
+ if (state === BASH_CMD)
121
+ state = BASH_ARG;
94
122
  i = cur.pos;
95
123
  } else if (ch === 34) {
96
124
  i = scanBashDoubleString(src, i, e);
125
+ if (state === BASH_CMD)
126
+ state = BASH_ARG;
97
127
  } else if (ch === 36 && i + 1 < n && src.charCodeAt(i + 1) === 39) {
98
128
  const cur = new Cursor(src, i + 1);
99
129
  cur.scanQuoted(39, true, false);
100
130
  e.emit(i, cur.pos, TokenKind.String);
131
+ if (state === BASH_CMD)
132
+ state = BASH_ARG;
101
133
  i = cur.pos;
102
134
  } else if (ch === 36) {
103
135
  i = scanBashDollar(src, i, e);
136
+ if (state === BASH_CMD)
137
+ state = BASH_ARG;
104
138
  } else if (ch === 45 && i + 1 < n && (isIdentStart(src.charCodeAt(i + 1)) || src.charCodeAt(i + 1) === 45)) {
105
139
  if (i === 0 || src.charCodeAt(i - 1) === 32 || src.charCodeAt(i - 1) === 9) {
106
140
  const s = i;
@@ -108,6 +142,8 @@ function scanBash(src, words, e) {
108
142
  while (i < n && (isIdentCont(src.charCodeAt(i)) || src.charCodeAt(i) === 45))
109
143
  i++;
110
144
  e.emit(s, i, TokenKind.Property);
145
+ if (state === BASH_CMD)
146
+ state = BASH_ARG;
111
147
  continue;
112
148
  }
113
149
  e.emit(i, i + 1, TokenKind.Operator);
@@ -115,37 +151,118 @@ function scanBash(src, words, e) {
115
151
  } else if (ch === 124 || ch === 38 || ch === 62 || ch === 60 || ch === 61 || ch === 33) {
116
152
  const s = i;
117
153
  i++;
118
- while (i < n) {
119
- const b = src.charCodeAt(i);
120
- if (b === 124 || b === 38 || b === 62 || b === 60 || b === 61)
154
+ if (ch === 61) {
155
+ while (i < n && src.charCodeAt(i) === 61)
121
156
  i++;
122
- else
123
- break;
157
+ } else {
158
+ while (i < n) {
159
+ const b = src.charCodeAt(i);
160
+ if (b === 124 || b === 38 || b === 62 || b === 60 || b === 61)
161
+ i++;
162
+ else
163
+ break;
164
+ }
124
165
  }
125
166
  e.emit(s, i, TokenKind.Operator);
167
+ if ((ch === 124 || ch === 38) && !inTest) {
168
+ state = BASH_CMD;
169
+ expectIn = false;
170
+ } else if (ch === 33 && i === s + 1 && state === BASH_CMD) {} else if (ch === 62 || ch === 60) {
171
+ state = BASH_ARG;
172
+ }
126
173
  } else if (ch === 40 || ch === 41 || ch === 123 || ch === 125 || ch === 91 || ch === 93 || ch === 59) {
127
174
  e.emit(i, i + 1, TokenKind.Punctuation);
128
175
  i++;
176
+ if (ch === 59) {
177
+ state = BASH_CMD;
178
+ expectIn = false;
179
+ inTest = false;
180
+ } else if (ch === 123) {
181
+ state = BASH_CMD;
182
+ expectIn = false;
183
+ } else if (ch === 91) {
184
+ if (state === BASH_CMD)
185
+ inTest = true;
186
+ state = BASH_ARG;
187
+ } else if (ch === 93) {
188
+ inTest = false;
189
+ state = BASH_ARG;
190
+ } else if (ch === 40) {
191
+ if (state !== BASH_CMD)
192
+ state = BASH_ARG;
193
+ } else {
194
+ state = BASH_ARG;
195
+ }
129
196
  } else if (isDigit(ch) && (i === 0 || src.charCodeAt(i - 1) === 32 || src.charCodeAt(i - 1) === 9 || src.charCodeAt(i - 1) === 61)) {
130
197
  const s = i;
131
198
  while (i < n && isDigit(src.charCodeAt(i)))
132
199
  i++;
133
200
  if (i === n || !isIdentCont(src.charCodeAt(i))) {
134
201
  e.emit(s, i, TokenKind.Number);
202
+ if (state === BASH_CMD)
203
+ state = BASH_ARG;
135
204
  }
205
+ } else if (state === BASH_CMD && (ch === 46 || ch === 47)) {
206
+ const s = i;
207
+ i++;
208
+ while (i < n && !isBashMetaOrSpace(src.charCodeAt(i)))
209
+ i++;
210
+ e.emit(s, i, TokenKind.Func);
211
+ state = BASH_ARG;
136
212
  } else if (isIdentStart(ch)) {
137
213
  const s = i;
214
+ let hasHyphen = false;
138
215
  i++;
139
- while (i < n && (isIdentCont(src.charCodeAt(i)) || src.charCodeAt(i) === 45))
216
+ while (i < n && (isIdentCont(src.charCodeAt(i)) || src.charCodeAt(i) === 45)) {
217
+ if (src.charCodeAt(i) === 45)
218
+ hasHyphen = true;
140
219
  i++;
141
- if (i < n && src.charCodeAt(i) === 61) {
220
+ }
221
+ if (!hasHyphen && i < n && src.charCodeAt(i) === 61) {
142
222
  e.emit(s, i, TokenKind.Variable);
223
+ if (state === BASH_CMD)
224
+ state = BASH_ASSIGN;
225
+ } else if (state === BASH_CMD) {
226
+ if (i < n && (src.charCodeAt(i) === 47 || src.charCodeAt(i) === 46)) {
227
+ while (i < n && !isBashMetaOrSpace(src.charCodeAt(i)))
228
+ i++;
229
+ e.emit(s, i, TokenKind.Func);
230
+ state = BASH_ARG;
231
+ } else {
232
+ const w = src.slice(s, i);
233
+ const kind = words.lookup(w);
234
+ if (kind === TokenKind.Keyword) {
235
+ e.emit(s, i, TokenKind.Keyword);
236
+ if (isBashCmdIntroKeyword(w)) {
237
+ state = BASH_CMD;
238
+ } else if (isBashExpectInKeyword(w)) {
239
+ state = BASH_ARG;
240
+ expectIn = true;
241
+ } else {
242
+ state = BASH_ARG;
243
+ }
244
+ } else if (kind === TokenKind.Constant) {
245
+ e.emit(s, i, TokenKind.Constant);
246
+ state = BASH_ARG;
247
+ } else if (kind === TokenKind.Func) {
248
+ e.emit(s, i, TokenKind.Func);
249
+ } else {
250
+ e.emit(s, i, TokenKind.Func);
251
+ state = BASH_ARG;
252
+ }
253
+ }
143
254
  } else {
144
- const kind = words.lookupSpan(src, s, i);
145
- if (kind !== TokenKind.Plain)
146
- e.emit(s, i, kind);
255
+ const w = src.slice(s, i);
256
+ if (expectIn && w === "in") {
257
+ e.emit(s, i, TokenKind.Keyword);
258
+ expectIn = false;
259
+ } else if ((w === "true" || w === "false") && (s === 0 || src.charCodeAt(s - 1) === 32 || src.charCodeAt(s - 1) === 9 || src.charCodeAt(s - 1) === 61) && (i === n || isBashMetaOrSpace(src.charCodeAt(i)))) {
260
+ e.emit(s, i, TokenKind.Constant);
261
+ }
147
262
  }
148
263
  } else {
264
+ if (state === BASH_CMD && ch !== 64 && ch !== 92)
265
+ state = BASH_ARG;
149
266
  i++;
150
267
  }
151
268
  }
@@ -177,11 +294,13 @@ function scanDocker(src, instrs, e) {
177
294
  while (i < end && isIdentCont(src.charCodeAt(i)))
178
295
  i++;
179
296
  if (instrs.lookupSpan(src, ws, i) === TokenKind.Keyword) {
297
+ const instr = src.slice(ws, i).toLowerCase();
180
298
  e.emit(ws, i, TokenKind.Keyword);
181
299
  while (i < end && (src.charCodeAt(i) === 32 || src.charCodeAt(i) === 9))
182
300
  i++;
183
301
  if (i < end) {
184
- e.embed(src, i, end, bash(), TokenKind.Plain);
302
+ const sub = instr === "run" || instr === "cmd" || instr === "entrypoint" ? bash() : bashArgs();
303
+ e.embed(src, i, end, sub, TokenKind.Plain);
185
304
  }
186
305
  }
187
306
  }
@@ -252,15 +371,22 @@ function scanMake(src, dirs, e) {
252
371
  pos = end + 1;
253
372
  }
254
373
  }
255
- export const bash = /* @__PURE__ */ onceGrammar(() => {
256
- const words = new WordTable(false, {
374
+ function makeBashWordTable() {
375
+ return new WordTable(false, {
257
376
  kind: TokenKind.Keyword,
258
- words: "case coproc do done elif else esac exit export fi for function if in local readonly return select set then time until while"
377
+ words: "break case continue coproc declare do done elif else esac exit export fi for function if in local readonly return select set then time typeset unset until while"
259
378
  }, {
260
379
  kind: TokenKind.Func,
261
- words: "alias awk basename cat cd chmod chown cp curl cut dirname echo env eval exec find grep head jq kill ls mkdir mv printf ps pwd rm sed sleep sort source tail tar tee test touch tr xargs"
380
+ words: "builtin command doas env exec nohup sudo"
262
381
  }, { kind: TokenKind.Constant, words: "true false" });
263
- return new Grammar("bash", (src, e) => scanBash(src, words, e), "sh", "shell", "zsh", "fish", "console");
382
+ }
383
+ const bashArgs = /* @__PURE__ */ onceGrammar(() => {
384
+ const words = makeBashWordTable();
385
+ return new Grammar("bash", (src, e) => scanBash(src, words, false, e));
386
+ });
387
+ export const bash = /* @__PURE__ */ onceGrammar(() => {
388
+ const words = makeBashWordTable();
389
+ return new Grammar("bash", (src, e) => scanBash(src, words, true, e), "sh", "shell", "zsh", "fish", "console");
264
390
  });
265
391
  export const docker = /* @__PURE__ */ onceGrammar(() => {
266
392
  const instrs = new WordTable(true, {
@@ -49,6 +49,8 @@ export declare const atomDark: Theme;
49
49
  export declare const nightOwl: Theme;
50
50
  /** Vesper theme. */
51
51
  export declare const vesper: Theme;
52
+ /** Undefined dark theme. */
53
+ export declare const undefinedTheme: Theme;
52
54
  /** Sorted list of all built-in theme names. */
53
55
  export declare const NAMES: readonly string[];
54
56
  /** Returns all built-in theme names in sorted order. */
@@ -648,6 +648,32 @@ export const vesper = {
648
648
  "#ff8080"
649
649
  ]
650
650
  };
651
+ export const undefinedTheme = {
652
+ name: "undefined-theme",
653
+ scheme: "dark",
654
+ bg: "#262626",
655
+ fg: "#dbd7ca",
656
+ numbers: "#6a737d",
657
+ tokens: [
658
+ "",
659
+ "#4d9375",
660
+ "#5da994",
661
+ "#c99076",
662
+ "#4c9a91",
663
+ "#c98a7d",
664
+ "#e6cc77",
665
+ "#758575",
666
+ "#80a665",
667
+ "#bd976a",
668
+ "#b8a965",
669
+ "#cb7676",
670
+ "#929292",
671
+ "#4d9375",
672
+ "#85e89d",
673
+ "#fdaeb7",
674
+ "#fdaeb7"
675
+ ]
676
+ };
651
677
  export const NAMES = [
652
678
  "atom-dark",
653
679
  "catppuccin-latte",
@@ -671,6 +697,7 @@ export const NAMES = [
671
697
  "solarized-dark",
672
698
  "solarized-light",
673
699
  "tokyo-night",
700
+ "undefined-theme",
674
701
  "vesper",
675
702
  "vscode-dark-modern",
676
703
  "vscode-light-modern"
@@ -730,6 +757,8 @@ export function byName(name) {
730
757
  return nightOwl;
731
758
  case "vesper":
732
759
  return vesper;
760
+ case "undefined-theme":
761
+ return undefinedTheme;
733
762
  default:
734
763
  return;
735
764
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "limn-hl",
3
- "version": "0.1.0",
4
- "description": "Tiny, fast, zero-dependency, synchronous syntax highlighter",
3
+ "version": "0.1.1",
4
+ "description": "Fast, lightweight, zero-dependency, synchronous syntax highlighter",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",