limn-hl 0.1.1 → 0.2.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 (73) hide show
  1. package/README.md +105 -174
  2. package/dist/detect.d.ts +7 -1
  3. package/dist/detect.js +657 -499
  4. package/dist/engine.js +45 -1
  5. package/dist/generated/detect.d.ts +8 -0
  6. package/dist/generated/detect.js +31 -0
  7. package/dist/generated/grammars.d.ts +119 -0
  8. package/dist/generated/grammars.js +628 -0
  9. package/dist/generated/lang.d.ts +6 -0
  10. package/dist/{lang/index.js → generated/lang.js} +82 -56
  11. package/dist/{theme/generated.d.ts → generated/themes.d.ts} +1 -1
  12. package/dist/lang.d.ts +1 -0
  13. package/dist/lang.js +1 -0
  14. package/dist/render/ansi.d.ts +1 -1
  15. package/dist/render/html.d.ts +1 -1
  16. package/dist/{theme/index.d.ts → theme.d.ts} +1 -1
  17. package/dist/theme.js +1 -0
  18. package/dist/vm/compile.d.ts +4 -0
  19. package/dist/vm/compile.js +393 -0
  20. package/dist/vm/exec.d.ts +3 -0
  21. package/dist/vm/exec.js +218 -0
  22. package/dist/vm/format.d.ts +260 -0
  23. package/dist/vm/format.js +251 -0
  24. package/dist/vm/index.d.ts +2 -0
  25. package/dist/vm/index.js +2 -0
  26. package/dist/vm/pat.d.ts +4 -0
  27. package/dist/vm/pat.js +816 -0
  28. package/dist/vm/quoted.d.ts +8 -0
  29. package/dist/vm/quoted.js +232 -0
  30. package/dist/vm/scan.d.ts +3 -0
  31. package/dist/vm/scan.js +499 -0
  32. package/dist/wordtable.d.ts +8 -4
  33. package/dist/wordtable.js +107 -30
  34. package/package.json +6 -6
  35. package/dist/lang/clike.d.ts +0 -15
  36. package/dist/lang/clike.js +0 -35
  37. package/dist/lang/clike_helpers.d.ts +0 -8
  38. package/dist/lang/clike_helpers.js +0 -220
  39. package/dist/lang/code.d.ts +0 -45
  40. package/dist/lang/code.js +0 -779
  41. package/dist/lang/config.d.ts +0 -5
  42. package/dist/lang/config.js +0 -164
  43. package/dist/lang/css.d.ts +0 -5
  44. package/dist/lang/css.js +0 -125
  45. package/dist/lang/docs.d.ts +0 -12
  46. package/dist/lang/docs.js +0 -462
  47. package/dist/lang/formats.d.ts +0 -10
  48. package/dist/lang/formats.js +0 -295
  49. package/dist/lang/html.d.ts +0 -11
  50. package/dist/lang/html.js +0 -298
  51. package/dist/lang/index.d.ts +0 -20
  52. package/dist/lang/js.d.ts +0 -9
  53. package/dist/lang/js.js +0 -532
  54. package/dist/lang/js_helpers.d.ts +0 -7
  55. package/dist/lang/js_helpers.js +0 -308
  56. package/dist/lang/json.d.ts +0 -3
  57. package/dist/lang/json.js +0 -136
  58. package/dist/lang/markdown.d.ts +0 -3
  59. package/dist/lang/markdown.js +0 -249
  60. package/dist/lang/python.d.ts +0 -3
  61. package/dist/lang/python.js +0 -335
  62. package/dist/lang/scripting.d.ts +0 -7
  63. package/dist/lang/scripting.js +0 -216
  64. package/dist/lang/shell.d.ts +0 -7
  65. package/dist/lang/shell.js +0 -404
  66. package/dist/lang/specs_gen.d.ts +0 -66
  67. package/dist/lang/specs_gen.js +0 -381
  68. package/dist/lang/terminal.d.ts +0 -9
  69. package/dist/lang/terminal.js +0 -550
  70. package/dist/lang/yaml.d.ts +0 -4
  71. package/dist/lang/yaml.js +0 -176
  72. package/dist/theme/index.js +0 -1
  73. /package/dist/{theme/generated.js → generated/themes.js} +0 -0
package/README.md CHANGED
@@ -1,23 +1,35 @@
1
- # limn
1
+ <img width="2400" height="800" alt="header" src="assets/og.png" />
2
+
3
+ ### limn
4
+
5
+ Fast, lightweight, zero-dependency, synchronous syntax highlighter with native runtimes for Go, Rust, JS/TS, Zig, and C. Includes [59 languages and 26 themes](docs/languages.md), built-in language detection, HTML output, and 24-bit ANSI output.
2
6
 
3
7
  [![CI](https://github.com/ekesti/limn/actions/workflows/ci.yml/badge.svg)](https://github.com/ekesti/limn/actions/workflows/ci.yml)
4
8
  [![Go Reference](https://pkg.go.dev/badge/github.com/ekesti/limn.svg)](https://pkg.go.dev/github.com/ekesti/limn)
5
9
  [![crates.io](https://img.shields.io/crates/v/limn-hl.svg)](https://crates.io/crates/limn-hl)
6
10
  [![npm](https://img.shields.io/npm/v/limn-hl.svg)](https://www.npmjs.com/package/limn-hl)
7
11
 
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.
12
+ > [!NOTE]
13
+ > `limn` is a lexical highlighter. It does not build an AST or resolve symbols across scopes; use Tree-sitter or Shiki if you need semantic highlighting or TextMate scope parity.
14
+
15
+ #### Performance
9
16
 
10
- | Target | Package | Highlights |
11
- | --- | --- | --- |
12
- | Go | `github.com/ekesti/limn` | Pure Go stdlib, CLI included |
13
- | Rust | `limn-hl` (imported as `limn`) | Zero dependencies, modular Cargo features |
14
- | JavaScript / TypeScript | `limn-hl` | Tree-shakeable ESM with TypeScript types |
15
- | Zig | `github.com/ekesti/limn` (`limn`) | Zero-dependency Zig stdlib, comptime tables |
16
- | C | `zig/include/limn.h` (`liblimn`) | C99 ABI built from Zig (`liblimn.a` / `liblimn.so`) |
17
+ End-to-end HTML highlighting throughput on ~3-4 KB source files (higher is better; full tokenization numbers up to 1 GB/s and reproduction steps in [`docs/benchmarks.md`](docs/benchmarks.md)):
17
18
 
18
- ## Quick Start
19
+ | Ecosystem | Library | `sample.go` | `sample.js` | `sample.rs` | `sample.json` | vs. limn |
20
+ | --- | --- | ---: | ---: | ---: | ---: | --- |
21
+ | Go | **limn** | **195.81 MB/s** | **140.61 MB/s** | **182.04 MB/s** | **223.23 MB/s** | baseline |
22
+ | Go | Chroma (`v2.27.0`) | 1.02 MB/s | 1.20 MB/s | 0.95 MB/s | 2.99 MB/s | 75x to 192x slower |
23
+ | Rust | **limn** | **282.16 MB/s** | **192.72 MB/s** | **242.79 MB/s** | **331.27 MB/s** | baseline |
24
+ | Rust | syntect (`5.3.0`, `onig`) | 2.54 MB/s | 1.02 MB/s | 2.05 MB/s | 5.00 MB/s | 66x to 189x slower |
25
+ | Node.js | **limn** | **87.28 MB/s** | **48.08 MB/s** | **70.28 MB/s** | **91.01 MB/s** | baseline |
26
+ | Node.js | Prism (`1.30.0`) | 16.81 MB/s | 6.08 MB/s | 10.97 MB/s | 16.96 MB/s | 5.2x to 7.9x slower |
27
+ | Node.js | highlight.js (`11.12.0`) | 15.08 MB/s | 3.40 MB/s | 10.39 MB/s | 4.19 MB/s | 5.8x to 21.7x slower |
28
+ | Node.js | Shiki (`4.6.0`) | 0.68 MB/s | 0.49 MB/s | 0.72 MB/s | 1.56 MB/s | 58x to 128x slower |
29
+
30
+ #### Usage
19
31
 
20
- ### Go
32
+ ##### Go
21
33
 
22
34
  ```sh
23
35
  go get github.com/ekesti/limn
@@ -36,85 +48,102 @@ import (
36
48
  )
37
49
 
38
50
  func main() {
39
- src := "package main\nfunc main() { println(\"hi\") }\n"
51
+ src := "package main\nfunc main() { println(\"hello\") }\n"
40
52
  reg := lang.All()
41
- detected := limn.Detect(src)
53
+ detected := limn.Detect(src) // or limn.DetectIn(reg, src) for subset registries
42
54
 
55
+ // Inline-styled HTML and 24-bit ANSI output
43
56
  html := render.HTML(reg, src, render.HTMLOptions{
44
57
  Lang: detected,
45
58
  Theme: &theme.TokyoNight,
46
59
  })
47
60
  ansi := render.ANSI(reg, src, detected, &theme.TokyoNight)
48
61
 
49
- fmt.Println(html)
50
- fmt.Print(ansi)
62
+ // Class-based HTML paired with a generated stylesheet or CSS variables
63
+ classed := render.HTML(reg, src, render.HTMLOptions{Lang: detected, Classes: true})
64
+ sheet := render.CSS(&theme.TokyoNight, "limn")
65
+
66
+ // Direct zero-copy token streaming
67
+ limn.EachToken(src, reg.Lookup(detected), reg, func(tok limn.Token) {
68
+ fmt.Printf("%s: %q\n", tok.Kind, src[tok.Start:tok.End])
69
+ })
70
+ _, _, _ = html, ansi, sheet
71
+ _ = classed
51
72
  }
52
73
  ```
53
74
 
54
- ### Rust
75
+ ##### Rust
55
76
 
56
77
  ```sh
57
78
  cargo add limn-hl
58
79
  ```
59
80
 
60
81
  ```rust
61
- use limn::render::{self, HtmlOptions};
62
- use limn::{detect, lang, theme};
82
+ use limn::{lang, render, theme};
63
83
 
64
84
  fn main() {
65
- let src = "fn main() { println!(\"hi\"); }\n";
85
+ let src = "fn main() { println!(\"hello\"); }\n";
66
86
  let reg = lang::all();
67
- let detected = detect(src);
87
+ let detected = limn::detect(src); // or limn::detect_in(reg, src)
68
88
 
69
89
  let html = render::html(
70
90
  reg,
71
91
  src,
72
- HtmlOptions {
92
+ render::HtmlOptions {
73
93
  lang: detected,
74
94
  theme: Some(&theme::TOKYO_NIGHT),
75
- ..HtmlOptions::default()
95
+ ..Default::default()
76
96
  },
77
97
  );
78
98
  let ansi = render::ansi(reg, src, detected, &theme::TOKYO_NIGHT);
79
99
 
80
- println!("{html}");
81
- print!("{ansi}");
100
+ limn::each_token(src, reg.lookup(detected), Some(reg), |tok| {
101
+ println!("{:?}: {:?}", tok.kind, &src[tok.start as usize..tok.end as usize]);
102
+ });
103
+ let _ = (html, ansi);
82
104
  }
83
105
  ```
84
106
 
85
- To shrink binary size, disable default features and pick only the language group and output modules you need:
107
+ To shrink binary size, disable default features and select only what you need (`all-langs` or individual `lang-*` groups from [`docs/languages.md`](docs/languages.md), plus `detect`, `themes`, and `render`):
86
108
 
87
109
  ```toml
88
110
  [dependencies]
89
- limn-hl = { version = "0.1", default-features = false, features = ["lang-js", "render"] }
111
+ limn-hl = { version = "0.2", default-features = false, features = ["lang-js", "render"] }
90
112
  ```
91
113
 
92
- Available Cargo features: `all-langs` (enables all six `lang-*` groups), `detect`, `themes`, and `render` (implies `themes`).
93
-
94
- ### JavaScript / TypeScript
114
+ ##### JavaScript / TypeScript
95
115
 
96
116
  ```sh
97
117
  npm install limn-hl
98
118
  ```
99
119
 
100
120
  ```ts
101
- import { detect } from "limn-hl/detect";
102
- import { all } from "limn-hl/lang";
103
- import { ansi, html } from "limn-hl/render";
121
+ import { Registry, eachToken } from "limn-hl";
122
+ import { detect, detectIn } from "limn-hl/detect";
123
+ import { all, rs, ts } from "limn-hl/lang";
124
+ import { ansi, cssVars, html } from "limn-hl/render";
104
125
  import { githubDark, tokyoNight } from "limn-hl/theme";
105
126
 
106
- const src = "export function hello(name: string) { return `hi ${name}` }\n";
127
+ const src = `export const greet = (name: string) => \`hi \${name}\`;\n`;
128
+
129
+ // All 59 languages
107
130
  const reg = all();
108
131
  const detected = detect(src);
109
-
110
132
  const outHtml = html(reg, src, { lang: detected, theme: githubDark });
111
133
  const outAnsi = ansi(reg, src, detected, tokyoNight);
112
134
 
113
- console.log(outHtml);
114
- process.stdout.write(outAnsi);
135
+ // Tree-shaken subset registry + scoped detection + CSS variables
136
+ const subset = new Registry([ts(), rs()]);
137
+ const scopedLang = detectIn(subset, src);
138
+ const classed = html(subset, src, { lang: scopedLang, classes: true });
139
+ const vars = cssVars(githubDark, "limn");
140
+
141
+ eachToken(src, subset.lookup(scopedLang), subset, (tok) => {
142
+ console.log(tok.kind, src.slice(tok.start, tok.end));
143
+ });
115
144
  ```
116
145
 
117
- ### Zig
146
+ ##### Zig
118
147
 
119
148
  ```sh
120
149
  zig fetch --save git+https://github.com/ekesti/limn
@@ -124,11 +153,9 @@ zig fetch --save git+https://github.com/ekesti/limn
124
153
  const std = @import("std");
125
154
  const limn = @import("limn");
126
155
 
127
- pub fn main() !void {
128
- const alloc = std.heap.smp_allocator;
129
- const src = "const x = 42;\n";
156
+ pub fn highlight(alloc: std.mem.Allocator, src: []const u8) !void {
130
157
  const reg = limn.lang.all();
131
- const detected = limn.detect(src);
158
+ const detected = limn.detect(src); // or limn.detectIn(reg, src)
132
159
 
133
160
  const html = try limn.render.html(alloc, reg, src, .{
134
161
  .lang = detected,
@@ -141,7 +168,9 @@ pub fn main() !void {
141
168
  }
142
169
  ```
143
170
 
144
- ### C
171
+ ##### C
172
+
173
+ Build the C99 static and shared libraries with Zig:
145
174
 
146
175
  ```sh
147
176
  zig build -Doptimize=ReleaseFast
@@ -154,8 +183,9 @@ zig build -Doptimize=ReleaseFast
154
183
  #include <string.h>
155
184
 
156
185
  int main(void) {
157
- const char *src = "package main\nfunc main() {}\n";
186
+ const char *src = "int main(void) { return 0; }\n";
158
187
  size_t len = strlen(src);
188
+
159
189
  size_t lang_len = 0;
160
190
  const char *lang = limn_detect(src, len, &lang_len);
161
191
 
@@ -174,7 +204,7 @@ int main(void) {
174
204
  }
175
205
  ```
176
206
 
177
- ### CLI
207
+ ##### CLI
178
208
 
179
209
  ```sh
180
210
  go install github.com/ekesti/limn/cmd/limn@latest
@@ -187,11 +217,18 @@ limn -list-langs
187
217
  limn -list-themes
188
218
  ```
189
219
 
190
- ## Rendering
220
+ #### API and HTML Styling
191
221
 
192
- - **HTML:** Inline styles by default, or CSS classes (`Classes: true` / `classes: true`, prefix defaults to `limn`) paired with generated stylesheets (`CSS` / `css`) and CSS custom properties (`CSSVars` / `css_vars` / `cssVars`) for light/dark themes. Supports inline `<code>` snippets and toggling line numbers.
193
- - **ANSI:** 24-bit truecolor terminal output via `ANSI` / `WriteANSI` (`ansi` / `write_ansi` in Rust, `ansi` in JS/TS and Zig, `limn_render_ansi` in C).
194
- - **Tokens:** Use `EachToken` / `Tokenize` and `SplitLines` (matching `snake_case` in Rust, `camelCase` in JS/TS and Zig, and `limn_each_token` / `limn_tokenize` in C) when building custom renderers.
222
+ | Operation | Go | Rust | JS / TS | Zig | C |
223
+ | --- | --- | --- | --- | --- | --- |
224
+ | Detect | `limn.Detect` | `limn::detect` | `detect` (`limn-hl/detect`) | `limn.detect` | `limn_detect` |
225
+ | Detect (scoped) | `limn.DetectIn` | `limn::detect_in` | `detectIn` (`limn-hl/detect`) | `limn.detectIn` | - |
226
+ | Tokenize | `EachToken` / `Tokenize` | `each_token` / `tokenize` | `eachToken` / `tokenize` | `eachToken` / `tokenize` | `limn_each_token` / `limn_tokenize` |
227
+ | Split lines | `SplitLines` | `split_lines` | `splitLines` | `splitLines` | - |
228
+ | HTML | `render.HTML` | `render::html` | `html` (`limn-hl/render`) | `render.html` | `limn_render_html` |
229
+ | ANSI (24-bit) | `render.ANSI` / `WriteANSI` | `render::ansi` / `write_ansi` | `ansi` (`limn-hl/render`) | `render.ansi` | `limn_render_ansi` |
230
+
231
+ HTML emits inline styles by default. Enable `Classes` / `classes` (default prefix `limn`) to emit class spans, and generate stylesheets with `CSS` / `css` or CSS custom properties with `CSSVars` / `css_vars` / `cssVars` for light/dark themes. Inline `<code>` mode and line-number toggles are also supported.
195
232
 
196
233
  Token CSS class suffixes (`--limn-*` / `.limn-*`):
197
234
 
@@ -202,135 +239,29 @@ Token CSS class suffixes (`--limn-*` / `.limn-*`):
202
239
  | Identifiers | `func`, `var`, `prop` |
203
240
  | Diff and Status | `ins`, `del`, `err` |
204
241
 
205
- ## Supported Languages (59)
206
-
207
- | Group | Cargo Feature | Languages |
208
- | --- | --- | --- |
209
- | Systems and C-like | `lang-clike` | c, cpp, cs, dart, gleam, glsl, go, groovy, java, kt, php, rs, scala, solidity, sql, swift, wgsl, zig |
210
- | JavaScript and TypeScript | `lang-js` | js, jsx, ts, tsx |
211
- | Scripting | `lang-scripting` | lua, nix, py, r, rb |
212
- | Markup and Docs | `lang-markup` | astro, css, html, md, mermaid, scss, svelte, tex, typst, vue, xml |
213
- | Data and Config | `lang-data` | cmake, graphql, hcl, ini, json, nginx, prisma, proto, toml, yaml |
214
- | Shell and Formats | `lang-shell` | asm, bash, bat, diff, docker, http, log, make, plain, powershell, regex |
215
-
216
- <details>
217
- <summary>All 59 canonical names and aliases</summary>
218
-
219
- | Language | Aliases | Group |
220
- | --- | --- | --- |
221
- | `asm` | `s`, `nasm`, `assembly` | Shell and Formats |
222
- | `astro` | - | Markup and Docs |
223
- | `bash` | `sh`, `shell`, `zsh`, `fish`, `console` | Shell and Formats |
224
- | `bat` | `cmd`, `batch` | Shell and Formats |
225
- | `c` | `h` | Systems and C-like |
226
- | `cmake` | - | Data and Config |
227
- | `cpp` | `c++`, `cc`, `cxx`, `hpp` | Systems and C-like |
228
- | `cs` | `csharp`, `c#` | Systems and C-like |
229
- | `css` | - | Markup and Docs |
230
- | `dart` | - | Systems and C-like |
231
- | `diff` | `patch` | Shell and Formats |
232
- | `docker` | `dockerfile` | Shell and Formats |
233
- | `gleam` | - | Systems and C-like |
234
- | `glsl` | `vert`, `frag` | Systems and C-like |
235
- | `go` | `golang` | Systems and C-like |
236
- | `graphql` | `gql` | Data and Config |
237
- | `groovy` | `gradle` | Systems and C-like |
238
- | `hcl` | `terraform`, `tf` | Data and Config |
239
- | `html` | `htm` | Markup and Docs |
240
- | `http` | - | Shell and Formats |
241
- | `ini` | `conf`, `cfg`, `properties`, `dotenv`, `env` | Data and Config |
242
- | `java` | - | Systems and C-like |
243
- | `js` | `javascript`, `mjs`, `cjs` | JavaScript and TypeScript |
244
- | `json` | `jsonc`, `json5`, `jsonl`, `ndjson` | Data and Config |
245
- | `jsx` | - | JavaScript and TypeScript |
246
- | `kt` | `kotlin`, `kts` | Systems and C-like |
247
- | `log` | `logs` | Shell and Formats |
248
- | `lua` | - | Scripting |
249
- | `make` | `makefile`, `mk` | Shell and Formats |
250
- | `md` | `markdown` | Markup and Docs |
251
- | `mermaid` | `mmd` | Markup and Docs |
252
- | `nginx` | `nginxconf` | Data and Config |
253
- | `nix` | - | Scripting |
254
- | `php` | - | Systems and C-like |
255
- | `plain` | `text`, `txt` | Shell and Formats |
256
- | `powershell` | `ps1`, `pwsh` | Shell and Formats |
257
- | `prisma` | - | Data and Config |
258
- | `proto` | `protobuf` | Data and Config |
259
- | `py` | `python`, `python3` | Scripting |
260
- | `r` | - | Scripting |
261
- | `rb` | `ruby` | Scripting |
262
- | `regex` | `regexp` | Shell and Formats |
263
- | `rs` | `rust` | Systems and C-like |
264
- | `scala` | `sc` | Systems and C-like |
265
- | `scss` | `sass`, `less` | Markup and Docs |
266
- | `solidity` | `sol` | Systems and C-like |
267
- | `sql` | `postgres`, `postgresql`, `sqlite`, `mysql` | Systems and C-like |
268
- | `svelte` | - | Markup and Docs |
269
- | `swift` | - | Systems and C-like |
270
- | `tex` | `latex` | Markup and Docs |
271
- | `toml` | - | Data and Config |
272
- | `ts` | `typescript`, `mts`, `cts` | JavaScript and TypeScript |
273
- | `tsx` | - | JavaScript and TypeScript |
274
- | `typst` | `typ` | Markup and Docs |
275
- | `vue` | - | Markup and Docs |
276
- | `wgsl` | - | Systems and C-like |
277
- | `xml` | `svg`, `plist` | Markup and Docs |
278
- | `yaml` | `yml` | Data and Config |
279
- | `zig` | - | Systems and C-like |
280
-
281
- </details>
282
-
283
- ## Built-in Themes (26)
284
-
285
- | Family | Light | Dark |
286
- | --- | --- | --- |
287
- | Built-in | default | dark |
288
- | Atom One | one-light | atom-dark |
289
- | Catppuccin | catppuccin-latte | catppuccin-mocha |
290
- | Everforest | everforest-light | everforest-dark |
291
- | Geist | geist-light | geist-dark |
292
- | GitHub | github-light | github-dark, github-dim |
293
- | Gruvbox | gruvbox-light | gruvbox-dark |
294
- | Solarized | solarized-light | solarized-dark |
295
- | VS Code Modern | vscode-light-modern | vscode-dark-modern |
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
-
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
-
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 |
242
+ #### How It Works
243
+
244
+ ##### Grammar and VM
245
+
246
+ Multi-target support does not rely on FFI bindings (other than the C library built from Zig) or maintaining separate per-language parsers for each ecosystem. To keep maintenance low and output identical across targets, all 59 language specs are written once in a small TypeScript DSL (inspired by [monogram](https://github.com/johnsoncodehk/monogram)) under [`syntax/`](syntax), alongside color palettes in [`themes/`](themes). [`tools/compiler/`](tools/compiler) compiles them into bytecode tables and 256-byte dispatch tables for Go, Rust, JS/TS, and Zig. Each target runtime is a small scanner VM that executes those tables directly over UTF-8 bytes without a regex engine.
247
+
248
+ See [`docs/vm.md`](docs/vm.md) for the VM instruction set, bytecode layout, and steps to add a language.
249
+
250
+ ##### Detector
251
+
252
+ Instead of hand-written keyword/regex heuristics or trial-highlighting against every grammar (as in `highlight.js`'s `highlightAuto`), [`tools/detect/`](tools/detect) trains a sparse `int8` linear classifier against `limn`'s own tokenizers and exports [`tools/detect/model.json`](tools/detect/model.json) (62 KB binary). [`tools/compiler/`](tools/compiler) hashes feature keys with FNV-1a (`u32`) and emits sorted weight tables for every target.
253
+
254
+ At runtime, `Detect` checks shebangs and unambiguous headers first, then scores the first 4 KB in a single pass with 0 heap allocations in Go, Rust, and Zig. Its training objective is picking a language that highlights the snippet well rather than guaranteeing exact dialect classification on ambiguous code (on our 19k repo-disjoint test set it reaches 97.3% highlight equivalence and 89.3% exact accuracy across 59 languages). For example, `fn main() {}` may classify as `wgsl` instead of `rs`, while both produce identical highlight tokens.
325
255
 
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.
256
+ See [`tools/detect/README.md`](tools/detect/README.md) for the training pipeline and Go highlight scorer.
329
257
 
330
- ## Scope
258
+ #### Documentation
331
259
 
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.
260
+ - [`docs/languages.md`](docs/languages.md): Supported languages (59), aliases, Cargo groups, and built-in themes (26).
261
+ - [`docs/benchmarks.md`](docs/benchmarks.md): Tokenization throughput, HTML comparison, and reproduction steps.
262
+ - [`docs/vm.md`](docs/vm.md): Scanner VM instruction set, bytecode format, and adding a language.
263
+ - [`tools/detect/README.md`](tools/detect/README.md): Offline detector training toolchain and Go highlight scorer.
333
264
 
334
- ## License
265
+ ### License
335
266
 
336
267
  [MIT](LICENSE). Made by [Ray](https://github.com/so1ve) & [Ekesti Labs](https://github.com/ekesti) with ♥️
package/dist/detect.d.ts CHANGED
@@ -1,5 +1,11 @@
1
+ import type { Registry } from "./engine.js";
1
2
  /**
2
3
  * Guesses the canonical language name for src using O(1) prefix inspection
3
- * and a single-pass structural/keyword scorer. Returns "plain" if confidence is low.
4
+ * and a sparse int8 linear model. Returns "plain" for prose.
4
5
  */
5
6
  export declare function detect(src: string): string;
7
+ /**
8
+ * Guesses the canonical language name for src restricted to grammars
9
+ * registered in reg (plus "plain").
10
+ */
11
+ export declare function detectIn(src: string, reg: Registry): string;