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.
- package/README.md +306 -0
- package/dist/cursor.d.ts +32 -0
- package/dist/cursor.js +322 -0
- package/dist/detect.d.ts +5 -0
- package/dist/detect.js +565 -0
- package/dist/engine.d.ts +78 -0
- package/dist/engine.js +268 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +17 -0
- package/dist/lang/clike.d.ts +15 -0
- package/dist/lang/clike.js +35 -0
- package/dist/lang/clike_helpers.d.ts +8 -0
- package/dist/lang/clike_helpers.js +220 -0
- package/dist/lang/code.d.ts +45 -0
- package/dist/lang/code.js +779 -0
- package/dist/lang/config.d.ts +5 -0
- package/dist/lang/config.js +164 -0
- package/dist/lang/css.d.ts +5 -0
- package/dist/lang/css.js +125 -0
- package/dist/lang/docs.d.ts +12 -0
- package/dist/lang/docs.js +462 -0
- package/dist/lang/formats.d.ts +10 -0
- package/dist/lang/formats.js +295 -0
- package/dist/lang/html.d.ts +11 -0
- package/dist/lang/html.js +298 -0
- package/dist/lang/index.d.ts +20 -0
- package/dist/lang/index.js +167 -0
- package/dist/lang/js.d.ts +9 -0
- package/dist/lang/js.js +532 -0
- package/dist/lang/js_helpers.d.ts +7 -0
- package/dist/lang/js_helpers.js +308 -0
- package/dist/lang/json.d.ts +3 -0
- package/dist/lang/json.js +136 -0
- package/dist/lang/markdown.d.ts +3 -0
- package/dist/lang/markdown.js +249 -0
- package/dist/lang/python.d.ts +3 -0
- package/dist/lang/python.js +335 -0
- package/dist/lang/scripting.d.ts +7 -0
- package/dist/lang/scripting.js +216 -0
- package/dist/lang/shell.d.ts +7 -0
- package/dist/lang/shell.js +278 -0
- package/dist/lang/specs_gen.d.ts +66 -0
- package/dist/lang/specs_gen.js +381 -0
- package/dist/lang/terminal.d.ts +9 -0
- package/dist/lang/terminal.js +550 -0
- package/dist/lang/yaml.d.ts +4 -0
- package/dist/lang/yaml.js +176 -0
- package/dist/render/ansi.d.ts +7 -0
- package/dist/render/ansi.js +70 -0
- package/dist/render/html.d.ts +20 -0
- package/dist/render/html.js +212 -0
- package/dist/render/index.d.ts +2 -0
- package/dist/render/index.js +2 -0
- package/dist/theme/generated.d.ts +57 -0
- package/dist/theme/generated.js +736 -0
- package/dist/theme/index.d.ts +12 -0
- package/dist/theme/index.js +1 -0
- package/dist/token.d.ts +38 -0
- package/dist/token.js +67 -0
- package/dist/wordtable.d.ts +23 -0
- package/dist/wordtable.js +65 -0
- package/package.json +44 -9
package/README.md
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
# limn
|
|
2
|
+
|
|
3
|
+
[](https://github.com/ekesti/limn/actions/workflows/ci.yml)
|
|
4
|
+
[](https://pkg.go.dev/github.com/ekesti/limn)
|
|
5
|
+
[](https://crates.io/crates/limn-hl)
|
|
6
|
+
[](https://www.npmjs.com/package/limn-hl)
|
|
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.
|
|
9
|
+
|
|
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
|
+
|
|
18
|
+
## Quick Start
|
|
19
|
+
|
|
20
|
+
### Go
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
go get github.com/ekesti/limn
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```go
|
|
27
|
+
package main
|
|
28
|
+
|
|
29
|
+
import (
|
|
30
|
+
"fmt"
|
|
31
|
+
|
|
32
|
+
"github.com/ekesti/limn"
|
|
33
|
+
"github.com/ekesti/limn/lang"
|
|
34
|
+
"github.com/ekesti/limn/render"
|
|
35
|
+
"github.com/ekesti/limn/theme"
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
func main() {
|
|
39
|
+
src := "package main\nfunc main() { println(\"hi\") }\n"
|
|
40
|
+
reg := lang.All()
|
|
41
|
+
detected := limn.Detect(src)
|
|
42
|
+
|
|
43
|
+
html := render.HTML(reg, src, render.HTMLOptions{
|
|
44
|
+
Lang: detected,
|
|
45
|
+
Theme: &theme.TokyoNight,
|
|
46
|
+
})
|
|
47
|
+
ansi := render.ANSI(reg, src, detected, &theme.TokyoNight)
|
|
48
|
+
|
|
49
|
+
fmt.Println(html)
|
|
50
|
+
fmt.Print(ansi)
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Rust
|
|
55
|
+
|
|
56
|
+
```sh
|
|
57
|
+
cargo add limn-hl
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
```rust
|
|
61
|
+
use limn::render::{self, HtmlOptions};
|
|
62
|
+
use limn::{detect, lang, theme};
|
|
63
|
+
|
|
64
|
+
fn main() {
|
|
65
|
+
let src = "fn main() { println!(\"hi\"); }\n";
|
|
66
|
+
let reg = lang::all();
|
|
67
|
+
let detected = detect(src);
|
|
68
|
+
|
|
69
|
+
let html = render::html(
|
|
70
|
+
reg,
|
|
71
|
+
src,
|
|
72
|
+
HtmlOptions {
|
|
73
|
+
lang: detected,
|
|
74
|
+
theme: Some(&theme::TOKYO_NIGHT),
|
|
75
|
+
..HtmlOptions::default()
|
|
76
|
+
},
|
|
77
|
+
);
|
|
78
|
+
let ansi = render::ansi(reg, src, detected, &theme::TOKYO_NIGHT);
|
|
79
|
+
|
|
80
|
+
println!("{html}");
|
|
81
|
+
print!("{ansi}");
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
To shrink binary size, disable default features and pick only the language group and output modules you need:
|
|
86
|
+
|
|
87
|
+
```toml
|
|
88
|
+
[dependencies]
|
|
89
|
+
limn-hl = { version = "0.1", default-features = false, features = ["lang-js", "render"] }
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Available Cargo features: `all-langs` (enables all six `lang-*` groups), `detect`, `themes`, and `render` (implies `themes`).
|
|
93
|
+
|
|
94
|
+
### JavaScript / TypeScript
|
|
95
|
+
|
|
96
|
+
```sh
|
|
97
|
+
npm install limn-hl
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
import { detect } from "limn-hl/detect";
|
|
102
|
+
import { all } from "limn-hl/lang";
|
|
103
|
+
import { ansi, html } from "limn-hl/render";
|
|
104
|
+
import { githubDark, tokyoNight } from "limn-hl/theme";
|
|
105
|
+
|
|
106
|
+
const src = "export function hello(name: string) { return `hi ${name}` }\n";
|
|
107
|
+
const reg = all();
|
|
108
|
+
const detected = detect(src);
|
|
109
|
+
|
|
110
|
+
const outHtml = html(reg, src, { lang: detected, theme: githubDark });
|
|
111
|
+
const outAnsi = ansi(reg, src, detected, tokyoNight);
|
|
112
|
+
|
|
113
|
+
console.log(outHtml);
|
|
114
|
+
process.stdout.write(outAnsi);
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Zig
|
|
118
|
+
|
|
119
|
+
```sh
|
|
120
|
+
zig fetch --save git+https://github.com/ekesti/limn
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
```zig
|
|
124
|
+
const std = @import("std");
|
|
125
|
+
const limn = @import("limn");
|
|
126
|
+
|
|
127
|
+
pub fn main() !void {
|
|
128
|
+
const alloc = std.heap.smp_allocator;
|
|
129
|
+
const src = "const x = 42;\n";
|
|
130
|
+
const reg = limn.lang.all();
|
|
131
|
+
const detected = limn.detect(src);
|
|
132
|
+
|
|
133
|
+
const html = try limn.render.html(alloc, reg, src, .{
|
|
134
|
+
.lang = detected,
|
|
135
|
+
.theme = &limn.theme.tokyo_night,
|
|
136
|
+
});
|
|
137
|
+
defer alloc.free(html);
|
|
138
|
+
|
|
139
|
+
const ansi = try limn.render.ansi(alloc, reg, src, detected, &limn.theme.tokyo_night);
|
|
140
|
+
defer alloc.free(ansi);
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### C
|
|
145
|
+
|
|
146
|
+
```sh
|
|
147
|
+
zig build -Doptimize=ReleaseFast
|
|
148
|
+
# Produces zig-out/include/limn.h, zig-out/lib/liblimn.a, zig-out/lib/liblimn.so
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
```c
|
|
152
|
+
#include "limn.h"
|
|
153
|
+
#include <stdio.h>
|
|
154
|
+
#include <string.h>
|
|
155
|
+
|
|
156
|
+
int main(void) {
|
|
157
|
+
const char *src = "package main\nfunc main() {}\n";
|
|
158
|
+
size_t len = strlen(src);
|
|
159
|
+
size_t lang_len = 0;
|
|
160
|
+
const char *lang = limn_detect(src, len, &lang_len);
|
|
161
|
+
|
|
162
|
+
limn_html_options opts = {
|
|
163
|
+
.lang = lang,
|
|
164
|
+
.lang_len = lang_len,
|
|
165
|
+
.theme = "tokyo-night",
|
|
166
|
+
.theme_len = 11,
|
|
167
|
+
};
|
|
168
|
+
limn_string html = limn_render_html(src, len, &opts);
|
|
169
|
+
if (html.ptr) {
|
|
170
|
+
printf("%s\n", html.ptr);
|
|
171
|
+
limn_string_free(html);
|
|
172
|
+
}
|
|
173
|
+
return 0;
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### CLI
|
|
178
|
+
|
|
179
|
+
```sh
|
|
180
|
+
go install github.com/ekesti/limn/cmd/limn@latest
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
```sh
|
|
184
|
+
limn -theme tokyo-night path/to/file.go
|
|
185
|
+
limn -html -classes -lang ts < snippet.ts
|
|
186
|
+
limn -list-langs
|
|
187
|
+
limn -list-themes
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
## Rendering
|
|
191
|
+
|
|
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.
|
|
195
|
+
|
|
196
|
+
Token CSS class suffixes (`--limn-*` / `.limn-*`):
|
|
197
|
+
|
|
198
|
+
| Category | CSS Suffixes |
|
|
199
|
+
| --- | --- |
|
|
200
|
+
| Syntax | `kwd`, `type`, `oper`, `punct`, `section` |
|
|
201
|
+
| Literals | `const`, `num`, `str`, `esc`, `cmnt` |
|
|
202
|
+
| Identifiers | `func`, `var`, `prop` |
|
|
203
|
+
| Diff and Status | `ins`, `del`, `err` |
|
|
204
|
+
|
|
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 (25)
|
|
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, 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
|
+
## Scope
|
|
301
|
+
|
|
302
|
+
`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.
|
|
303
|
+
|
|
304
|
+
## License
|
|
305
|
+
|
|
306
|
+
[MIT](LICENSE). Made by [Ray](https://github.com/so1ve) & [Ekesti Labs](https://github.com/ekesti) with ♥️
|
package/dist/cursor.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Emitter } from "./engine.js";
|
|
2
|
+
/** Reports whether UTF-16 code unit c can start an ASCII or non-ASCII identifier. */
|
|
3
|
+
export declare function isIdentStart(c: number): boolean;
|
|
4
|
+
/** Reports whether UTF-16 code unit c can continue an ASCII or non-ASCII identifier. */
|
|
5
|
+
export declare function isIdentCont(c: number): boolean;
|
|
6
|
+
/** Reports whether code unit c is an ASCII decimal digit. */
|
|
7
|
+
export declare function isDigit(c: number): boolean;
|
|
8
|
+
/** Reports whether code unit c is an ASCII hexadecimal digit. */
|
|
9
|
+
export declare function isHexDigit(c: number): boolean;
|
|
10
|
+
/** Scanner cursor over a source string. */
|
|
11
|
+
export declare class Cursor {
|
|
12
|
+
readonly src: string;
|
|
13
|
+
pos: number;
|
|
14
|
+
constructor(src: string, pos?: number);
|
|
15
|
+
eof(): boolean;
|
|
16
|
+
peek(): number;
|
|
17
|
+
hasPrefix(prefix: string): boolean;
|
|
18
|
+
skipWhitespace(): void;
|
|
19
|
+
scanUntil(delim: string): boolean;
|
|
20
|
+
scanUntilByte(code: number): boolean;
|
|
21
|
+
scanQuoted(quoteCode: number, multiline: boolean, raw: boolean): void;
|
|
22
|
+
scanBalanced(openCode: number, closeCode: number, handleQuotes: boolean): boolean;
|
|
23
|
+
scanNestedComment(open: string, close: string): void;
|
|
24
|
+
}
|
|
25
|
+
/** Emits comment span [start, end) highlighting standalone TODO/FIXME/BUG/XXX/HACK markers as Error. */
|
|
26
|
+
export declare function emitCommentWithTodo(src: string, start: number, end: number, e: Emitter): void;
|
|
27
|
+
/** Scans a numeric literal (decimal, 0x/0b/0o, leading-dot float, exponent, suffix). */
|
|
28
|
+
export declare function scanNumber(src: string, pos: number): number;
|
|
29
|
+
/** Scans a backslash escape sequence starting at '\\' in src at pos. */
|
|
30
|
+
export declare function scanEscape(src: string, pos: number): number;
|
|
31
|
+
/** Reports whether the next non-horizontal-whitespace char at pos is '('. */
|
|
32
|
+
export declare function hasCallParen(src: string, pos: number): boolean;
|
package/dist/cursor.js
ADDED
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import { TokenKind } from "./token.js";
|
|
2
|
+
export function isIdentStart(c) {
|
|
3
|
+
return c >= 97 && c <= 122 || c >= 65 && c <= 90 || c === 95 || c >= 128;
|
|
4
|
+
}
|
|
5
|
+
export function isIdentCont(c) {
|
|
6
|
+
return c >= 97 && c <= 122 || c >= 65 && c <= 90 || c >= 48 && c <= 57 || c === 95 || c >= 128;
|
|
7
|
+
}
|
|
8
|
+
export function isDigit(c) {
|
|
9
|
+
return c >= 48 && c <= 57;
|
|
10
|
+
}
|
|
11
|
+
export function isHexDigit(c) {
|
|
12
|
+
return c >= 48 && c <= 57 || c >= 97 && c <= 102 || c >= 65 && c <= 70;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class Cursor {
|
|
16
|
+
src;
|
|
17
|
+
pos;
|
|
18
|
+
constructor(src, pos = 0) {
|
|
19
|
+
this.src = src;
|
|
20
|
+
this.pos = pos;
|
|
21
|
+
}
|
|
22
|
+
eof() {
|
|
23
|
+
return this.pos >= this.src.length;
|
|
24
|
+
}
|
|
25
|
+
peek() {
|
|
26
|
+
if (this.pos >= this.src.length) {
|
|
27
|
+
return 0;
|
|
28
|
+
}
|
|
29
|
+
return this.src.charCodeAt(this.pos);
|
|
30
|
+
}
|
|
31
|
+
hasPrefix(prefix) {
|
|
32
|
+
return this.src.startsWith(prefix, this.pos);
|
|
33
|
+
}
|
|
34
|
+
skipWhitespace() {
|
|
35
|
+
const s = this.src;
|
|
36
|
+
const n = s.length;
|
|
37
|
+
while (this.pos < n) {
|
|
38
|
+
const ch = s.charCodeAt(this.pos);
|
|
39
|
+
if (ch === 32 || ch === 9 || ch === 10 || ch === 13) {
|
|
40
|
+
this.pos++;
|
|
41
|
+
} else {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
scanUntil(delim) {
|
|
47
|
+
if (this.pos >= this.src.length) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
const idx = this.src.indexOf(delim, this.pos);
|
|
51
|
+
if (idx >= 0) {
|
|
52
|
+
this.pos = idx + delim.length;
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
this.pos = this.src.length;
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
scanUntilByte(code) {
|
|
59
|
+
const s = this.src;
|
|
60
|
+
const n = s.length;
|
|
61
|
+
for (let i = this.pos;i < n; i++) {
|
|
62
|
+
if (s.charCodeAt(i) === code) {
|
|
63
|
+
this.pos = i + 1;
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
this.pos = n;
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
scanQuoted(quoteCode, multiline, raw) {
|
|
71
|
+
const s = this.src;
|
|
72
|
+
const n = s.length;
|
|
73
|
+
if (this.pos >= n || s.charCodeAt(this.pos) !== quoteCode) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
this.pos++;
|
|
77
|
+
if (raw && multiline) {
|
|
78
|
+
this.scanUntilByte(quoteCode);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
while (this.pos < n) {
|
|
82
|
+
const ch = s.charCodeAt(this.pos);
|
|
83
|
+
if (ch === quoteCode) {
|
|
84
|
+
this.pos++;
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (ch === 10 && !multiline) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (ch === 92 && !raw) {
|
|
91
|
+
this.pos++;
|
|
92
|
+
if (this.pos < n) {
|
|
93
|
+
if (s.charCodeAt(this.pos) === 10 && !multiline) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
this.pos++;
|
|
97
|
+
}
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
this.pos++;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
scanBalanced(openCode, closeCode, handleQuotes) {
|
|
104
|
+
const s = this.src;
|
|
105
|
+
const n = s.length;
|
|
106
|
+
let depth = 1;
|
|
107
|
+
while (this.pos < n && depth > 0) {
|
|
108
|
+
const ch = s.charCodeAt(this.pos);
|
|
109
|
+
if (ch === openCode) {
|
|
110
|
+
depth++;
|
|
111
|
+
this.pos++;
|
|
112
|
+
} else if (ch === closeCode) {
|
|
113
|
+
depth--;
|
|
114
|
+
this.pos++;
|
|
115
|
+
if (depth === 0) {
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
} else if (handleQuotes && (ch === 34 || ch === 39 || ch === 96)) {
|
|
119
|
+
this.scanQuoted(ch, ch === 96, ch === 96);
|
|
120
|
+
} else {
|
|
121
|
+
this.pos++;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return depth === 0;
|
|
125
|
+
}
|
|
126
|
+
scanNestedComment(open, close) {
|
|
127
|
+
const s = this.src;
|
|
128
|
+
const n = s.length;
|
|
129
|
+
let depth = 1;
|
|
130
|
+
while (this.pos < n && depth > 0) {
|
|
131
|
+
const closeIdx = s.indexOf(close, this.pos);
|
|
132
|
+
if (closeIdx < 0) {
|
|
133
|
+
this.pos = n;
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const openIdx = s.indexOf(open, this.pos);
|
|
137
|
+
if (openIdx >= 0 && openIdx < closeIdx) {
|
|
138
|
+
depth++;
|
|
139
|
+
this.pos = openIdx + open.length;
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
depth--;
|
|
143
|
+
this.pos = closeIdx + close.length;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
export function emitCommentWithTodo(src, start, end, e) {
|
|
148
|
+
if (start >= end) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
let cur = start;
|
|
152
|
+
for (let i = start;i < end; i++) {
|
|
153
|
+
const ch = src.charCodeAt(i);
|
|
154
|
+
if (ch < 66 || ch > 88) {
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (i > start && isIdentCont(src.charCodeAt(i - 1))) {
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
let kwLen = 0;
|
|
161
|
+
if (ch === 84 && i + 4 <= end && src.startsWith("TODO", i)) {
|
|
162
|
+
kwLen = 4;
|
|
163
|
+
} else if (ch === 70 && i + 5 <= end && src.startsWith("FIXME", i)) {
|
|
164
|
+
kwLen = 5;
|
|
165
|
+
} else if (ch === 66 && i + 3 <= end && src.startsWith("BUG", i)) {
|
|
166
|
+
kwLen = 3;
|
|
167
|
+
} else if (ch === 88 && i + 3 <= end && src.startsWith("XXX", i)) {
|
|
168
|
+
kwLen = 3;
|
|
169
|
+
} else if (ch === 72 && i + 4 <= end && src.startsWith("HACK", i)) {
|
|
170
|
+
kwLen = 4;
|
|
171
|
+
}
|
|
172
|
+
if (kwLen === 0) {
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
const after = i + kwLen;
|
|
176
|
+
if (after < end && isIdentCont(src.charCodeAt(after))) {
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
if (i > cur) {
|
|
180
|
+
e.emit(cur, i, TokenKind.Comment);
|
|
181
|
+
}
|
|
182
|
+
e.emit(i, after, TokenKind.Error);
|
|
183
|
+
cur = after;
|
|
184
|
+
i = after - 1;
|
|
185
|
+
}
|
|
186
|
+
if (cur < end) {
|
|
187
|
+
e.emit(cur, end, TokenKind.Comment);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
function scanNumSuffix(src, pos) {
|
|
191
|
+
const n = src.length;
|
|
192
|
+
if (pos < n) {
|
|
193
|
+
const ch = src.charCodeAt(pos);
|
|
194
|
+
if (ch === 110 || ch === 105 || ch === 117 || ch === 102 || ch === 108 || ch === 76) {
|
|
195
|
+
pos++;
|
|
196
|
+
while (pos < n && isDigit(src.charCodeAt(pos))) {
|
|
197
|
+
pos++;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return pos;
|
|
202
|
+
}
|
|
203
|
+
export function scanNumber(src, pos) {
|
|
204
|
+
const n = src.length;
|
|
205
|
+
let i = pos;
|
|
206
|
+
if (i + 1 < n && src.charCodeAt(i) === 48) {
|
|
207
|
+
const next = src.charCodeAt(i + 1);
|
|
208
|
+
if (next === 120 || next === 88) {
|
|
209
|
+
i += 2;
|
|
210
|
+
while (i < n && (isHexDigit(src.charCodeAt(i)) || src.charCodeAt(i) === 95)) {
|
|
211
|
+
i++;
|
|
212
|
+
}
|
|
213
|
+
return scanNumSuffix(src, i);
|
|
214
|
+
}
|
|
215
|
+
if (next === 98 || next === 66) {
|
|
216
|
+
i += 2;
|
|
217
|
+
while (i < n) {
|
|
218
|
+
const ch = src.charCodeAt(i);
|
|
219
|
+
if (ch === 48 || ch === 49 || ch === 95) {
|
|
220
|
+
i++;
|
|
221
|
+
} else {
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return scanNumSuffix(src, i);
|
|
226
|
+
}
|
|
227
|
+
if (next === 111 || next === 79) {
|
|
228
|
+
i += 2;
|
|
229
|
+
while (i < n) {
|
|
230
|
+
const ch = src.charCodeAt(i);
|
|
231
|
+
if (ch >= 48 && ch <= 55 || ch === 95) {
|
|
232
|
+
i++;
|
|
233
|
+
} else {
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return scanNumSuffix(src, i);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
if (i < n && src.charCodeAt(i) === 46) {
|
|
241
|
+
i++;
|
|
242
|
+
while (i < n && (isDigit(src.charCodeAt(i)) || src.charCodeAt(i) === 95)) {
|
|
243
|
+
i++;
|
|
244
|
+
}
|
|
245
|
+
} else {
|
|
246
|
+
while (i < n && (isDigit(src.charCodeAt(i)) || src.charCodeAt(i) === 95)) {
|
|
247
|
+
i++;
|
|
248
|
+
}
|
|
249
|
+
if (i + 1 < n && src.charCodeAt(i) === 46 && src.charCodeAt(i + 1) !== 46 && isDigit(src.charCodeAt(i + 1))) {
|
|
250
|
+
i++;
|
|
251
|
+
while (i < n && (isDigit(src.charCodeAt(i)) || src.charCodeAt(i) === 95)) {
|
|
252
|
+
i++;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
if (i < n && (src.charCodeAt(i) === 101 || src.charCodeAt(i) === 69)) {
|
|
257
|
+
let j = i + 1;
|
|
258
|
+
if (j < n && (src.charCodeAt(j) === 43 || src.charCodeAt(j) === 45)) {
|
|
259
|
+
j++;
|
|
260
|
+
}
|
|
261
|
+
if (j < n && isDigit(src.charCodeAt(j))) {
|
|
262
|
+
i = j;
|
|
263
|
+
while (i < n && (isDigit(src.charCodeAt(i)) || src.charCodeAt(i) === 95)) {
|
|
264
|
+
i++;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return scanNumSuffix(src, i);
|
|
269
|
+
}
|
|
270
|
+
export function scanEscape(src, pos) {
|
|
271
|
+
const n = src.length;
|
|
272
|
+
if (pos + 1 >= n) {
|
|
273
|
+
return n;
|
|
274
|
+
}
|
|
275
|
+
const ch = src.charCodeAt(pos + 1);
|
|
276
|
+
if (ch === 120) {
|
|
277
|
+
let end = pos + 2;
|
|
278
|
+
while (end < n && end < pos + 4 && isHexDigit(src.charCodeAt(end))) {
|
|
279
|
+
end++;
|
|
280
|
+
}
|
|
281
|
+
return end;
|
|
282
|
+
}
|
|
283
|
+
if (ch === 117) {
|
|
284
|
+
let end = pos + 2;
|
|
285
|
+
if (end < n && src.charCodeAt(end) === 123) {
|
|
286
|
+
end++;
|
|
287
|
+
while (end < n && src.charCodeAt(end) !== 125 && src.charCodeAt(end) !== 10) {
|
|
288
|
+
end++;
|
|
289
|
+
}
|
|
290
|
+
if (end < n && src.charCodeAt(end) === 125) {
|
|
291
|
+
end++;
|
|
292
|
+
}
|
|
293
|
+
return end;
|
|
294
|
+
}
|
|
295
|
+
while (end < n && end < pos + 6 && isHexDigit(src.charCodeAt(end))) {
|
|
296
|
+
end++;
|
|
297
|
+
}
|
|
298
|
+
return end;
|
|
299
|
+
}
|
|
300
|
+
if (ch === 85) {
|
|
301
|
+
let end = pos + 2;
|
|
302
|
+
while (end < n && end < pos + 10 && isHexDigit(src.charCodeAt(end))) {
|
|
303
|
+
end++;
|
|
304
|
+
}
|
|
305
|
+
return end;
|
|
306
|
+
}
|
|
307
|
+
if (ch >= 48 && ch <= 55) {
|
|
308
|
+
let end = pos + 2;
|
|
309
|
+
while (end < n && end < pos + 4 && src.charCodeAt(end) >= 48 && src.charCodeAt(end) <= 55) {
|
|
310
|
+
end++;
|
|
311
|
+
}
|
|
312
|
+
return end;
|
|
313
|
+
}
|
|
314
|
+
return pos + 2;
|
|
315
|
+
}
|
|
316
|
+
export function hasCallParen(src, pos) {
|
|
317
|
+
const n = src.length;
|
|
318
|
+
while (pos < n && (src.charCodeAt(pos) === 32 || src.charCodeAt(pos) === 9)) {
|
|
319
|
+
pos++;
|
|
320
|
+
}
|
|
321
|
+
return pos < n && src.charCodeAt(pos) === 40;
|
|
322
|
+
}
|