@scalar/highlight 0.2.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 (166) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.md +324 -0
  3. package/dist/all.d.ts +28 -0
  4. package/dist/all.d.ts.map +1 -0
  5. package/dist/all.js +105 -0
  6. package/dist/compat/hljs.d.ts +24 -0
  7. package/dist/compat/hljs.d.ts.map +1 -0
  8. package/dist/compat/hljs.js +95 -0
  9. package/dist/compat/index.d.ts +24 -0
  10. package/dist/compat/index.d.ts.map +1 -0
  11. package/dist/compat/index.js +21 -0
  12. package/dist/compat/languages.d.ts +46 -0
  13. package/dist/compat/languages.d.ts.map +1 -0
  14. package/dist/compat/languages.js +236 -0
  15. package/dist/compat/syntax-highlight.d.ts +37 -0
  16. package/dist/compat/syntax-highlight.d.ts.map +1 -0
  17. package/dist/compat/syntax-highlight.js +133 -0
  18. package/dist/core/compile.d.ts +3 -0
  19. package/dist/core/compile.d.ts.map +1 -0
  20. package/dist/core/compile.js +142 -0
  21. package/dist/core/index.d.ts +9 -0
  22. package/dist/core/index.d.ts.map +1 -0
  23. package/dist/core/index.js +5 -0
  24. package/dist/core/registry.d.ts +14 -0
  25. package/dist/core/registry.d.ts.map +1 -0
  26. package/dist/core/registry.js +77 -0
  27. package/dist/core/render.d.ts +67 -0
  28. package/dist/core/render.d.ts.map +1 -0
  29. package/dist/core/render.js +216 -0
  30. package/dist/core/scopes.d.ts +94 -0
  31. package/dist/core/scopes.d.ts.map +1 -0
  32. package/dist/core/scopes.js +113 -0
  33. package/dist/core/tokenize.d.ts +18 -0
  34. package/dist/core/tokenize.d.ts.map +1 -0
  35. package/dist/core/tokenize.js +131 -0
  36. package/dist/core/types.d.ts +99 -0
  37. package/dist/core/types.d.ts.map +1 -0
  38. package/dist/core/types.js +10 -0
  39. package/dist/index.d.ts +19 -0
  40. package/dist/index.d.ts.map +1 -0
  41. package/dist/index.js +20 -0
  42. package/dist/langs/bash.d.ts +4 -0
  43. package/dist/langs/bash.d.ts.map +1 -0
  44. package/dist/langs/bash.js +123 -0
  45. package/dist/langs/c.d.ts +4 -0
  46. package/dist/langs/c.d.ts.map +1 -0
  47. package/dist/langs/c.js +196 -0
  48. package/dist/langs/clojure.d.ts +4 -0
  49. package/dist/langs/clojure.d.ts.map +1 -0
  50. package/dist/langs/clojure.js +213 -0
  51. package/dist/langs/cpp.d.ts +4 -0
  52. package/dist/langs/cpp.d.ts.map +1 -0
  53. package/dist/langs/cpp.js +391 -0
  54. package/dist/langs/csharp.d.ts +4 -0
  55. package/dist/langs/csharp.d.ts.map +1 -0
  56. package/dist/langs/csharp.js +307 -0
  57. package/dist/langs/css.d.ts +4 -0
  58. package/dist/langs/css.d.ts.map +1 -0
  59. package/dist/langs/css.js +94 -0
  60. package/dist/langs/dart.d.ts +4 -0
  61. package/dist/langs/dart.d.ts.map +1 -0
  62. package/dist/langs/dart.js +262 -0
  63. package/dist/langs/diff.d.ts +10 -0
  64. package/dist/langs/diff.d.ts.map +1 -0
  65. package/dist/langs/diff.js +26 -0
  66. package/dist/langs/dockerfile.d.ts +4 -0
  67. package/dist/langs/dockerfile.d.ts.map +1 -0
  68. package/dist/langs/dockerfile.js +308 -0
  69. package/dist/langs/elixir.d.ts +4 -0
  70. package/dist/langs/elixir.d.ts.map +1 -0
  71. package/dist/langs/elixir.js +343 -0
  72. package/dist/langs/fsharp.d.ts +4 -0
  73. package/dist/langs/fsharp.d.ts.map +1 -0
  74. package/dist/langs/fsharp.js +330 -0
  75. package/dist/langs/go.d.ts +5 -0
  76. package/dist/langs/go.d.ts.map +1 -0
  77. package/dist/langs/go.js +90 -0
  78. package/dist/langs/graphql.d.ts +4 -0
  79. package/dist/langs/graphql.d.ts.map +1 -0
  80. package/dist/langs/graphql.js +185 -0
  81. package/dist/langs/haskell.d.ts +4 -0
  82. package/dist/langs/haskell.d.ts.map +1 -0
  83. package/dist/langs/haskell.js +375 -0
  84. package/dist/langs/html.d.ts +14 -0
  85. package/dist/langs/html.d.ts.map +1 -0
  86. package/dist/langs/html.js +71 -0
  87. package/dist/langs/http.d.ts +4 -0
  88. package/dist/langs/http.d.ts.map +1 -0
  89. package/dist/langs/http.js +185 -0
  90. package/dist/langs/ini.d.ts +4 -0
  91. package/dist/langs/ini.d.ts.map +1 -0
  92. package/dist/langs/ini.js +227 -0
  93. package/dist/langs/java.d.ts +4 -0
  94. package/dist/langs/java.d.ts.map +1 -0
  95. package/dist/langs/java.js +178 -0
  96. package/dist/langs/javascript.d.ts +4 -0
  97. package/dist/langs/javascript.d.ts.map +1 -0
  98. package/dist/langs/javascript.js +226 -0
  99. package/dist/langs/json.d.ts +10 -0
  100. package/dist/langs/json.d.ts.map +1 -0
  101. package/dist/langs/json.js +46 -0
  102. package/dist/langs/kotlin.d.ts +4 -0
  103. package/dist/langs/kotlin.d.ts.map +1 -0
  104. package/dist/langs/kotlin.js +220 -0
  105. package/dist/langs/lua.d.ts +4 -0
  106. package/dist/langs/lua.d.ts.map +1 -0
  107. package/dist/langs/lua.js +330 -0
  108. package/dist/langs/makefile.d.ts +4 -0
  109. package/dist/langs/makefile.d.ts.map +1 -0
  110. package/dist/langs/makefile.js +266 -0
  111. package/dist/langs/markdown.d.ts +11 -0
  112. package/dist/langs/markdown.d.ts.map +1 -0
  113. package/dist/langs/markdown.js +71 -0
  114. package/dist/langs/matlab.d.ts +4 -0
  115. package/dist/langs/matlab.d.ts.map +1 -0
  116. package/dist/langs/matlab.js +352 -0
  117. package/dist/langs/mojo.d.ts +4 -0
  118. package/dist/langs/mojo.d.ts.map +1 -0
  119. package/dist/langs/mojo.js +337 -0
  120. package/dist/langs/nginx.d.ts +4 -0
  121. package/dist/langs/nginx.d.ts.map +1 -0
  122. package/dist/langs/nginx.js +168 -0
  123. package/dist/langs/objectivec.d.ts +4 -0
  124. package/dist/langs/objectivec.d.ts.map +1 -0
  125. package/dist/langs/objectivec.js +287 -0
  126. package/dist/langs/ocaml.d.ts +4 -0
  127. package/dist/langs/ocaml.d.ts.map +1 -0
  128. package/dist/langs/ocaml.js +309 -0
  129. package/dist/langs/perl.d.ts +4 -0
  130. package/dist/langs/perl.d.ts.map +1 -0
  131. package/dist/langs/perl.js +435 -0
  132. package/dist/langs/php.d.ts +4 -0
  133. package/dist/langs/php.d.ts.map +1 -0
  134. package/dist/langs/php.js +325 -0
  135. package/dist/langs/powershell.d.ts +4 -0
  136. package/dist/langs/powershell.d.ts.map +1 -0
  137. package/dist/langs/powershell.js +308 -0
  138. package/dist/langs/python.d.ts +4 -0
  139. package/dist/langs/python.d.ts.map +1 -0
  140. package/dist/langs/python.js +350 -0
  141. package/dist/langs/r.d.ts +4 -0
  142. package/dist/langs/r.d.ts.map +1 -0
  143. package/dist/langs/r.js +265 -0
  144. package/dist/langs/ruby.d.ts +4 -0
  145. package/dist/langs/ruby.d.ts.map +1 -0
  146. package/dist/langs/ruby.js +365 -0
  147. package/dist/langs/rust.d.ts +10 -0
  148. package/dist/langs/rust.d.ts.map +1 -0
  149. package/dist/langs/rust.js +116 -0
  150. package/dist/langs/scala.d.ts +4 -0
  151. package/dist/langs/scala.d.ts.map +1 -0
  152. package/dist/langs/scala.js +308 -0
  153. package/dist/langs/sql.d.ts +5 -0
  154. package/dist/langs/sql.d.ts.map +1 -0
  155. package/dist/langs/sql.js +57 -0
  156. package/dist/langs/swift.d.ts +33 -0
  157. package/dist/langs/swift.d.ts.map +1 -0
  158. package/dist/langs/swift.js +256 -0
  159. package/dist/langs/yaml.d.ts +11 -0
  160. package/dist/langs/yaml.d.ts.map +1 -0
  161. package/dist/langs/yaml.js +123 -0
  162. package/dist/lazy.d.ts +69 -0
  163. package/dist/lazy.d.ts.map +1 -0
  164. package/dist/lazy.js +200 -0
  165. package/dist/style.css +308 -0
  166. package/package.json +86 -0
@@ -0,0 +1,131 @@
1
+ /**
2
+ * Runs the state machine over `code`, calling `emit` for every range.
3
+ *
4
+ * Ranges are emitted in order and cover the input exactly — concatenating
5
+ * every emitted slice reproduces the source. Nothing is allocated per token,
6
+ * which is why the HTML renderer can build its output in a single pass.
7
+ */
8
+ export const tokenize = (code, grammar, emit) => {
9
+ const len = code.length;
10
+ const stack = [grammar.root];
11
+ let top = grammar.root;
12
+ let pos = 0;
13
+ // Zero-length matches are legal (a pure state transition on a lookahead),
14
+ // but a rule that neither consumes nor eventually changes state would spin.
15
+ let spin = 0;
16
+ while (pos < len) {
17
+ const re = top.re;
18
+ re.lastIndex = pos;
19
+ const m = re.exec(code);
20
+ if (m === null)
21
+ break;
22
+ const start = m.index;
23
+ if (start > pos) {
24
+ emit(top.default, pos, start);
25
+ pos = start;
26
+ }
27
+ const rules = top.rules;
28
+ let rule;
29
+ for (let i = 0; i < rules.length; i++) {
30
+ const r = rules[i];
31
+ if (m[r.group] !== undefined) {
32
+ rule = r;
33
+ break;
34
+ }
35
+ }
36
+ if (rule === undefined)
37
+ break; // unreachable: some alternative matched
38
+ const end = start + m[0].length;
39
+ if (end > start) {
40
+ if (rule.captures !== null) {
41
+ emitCaptures(code, start, rule, top, emit);
42
+ }
43
+ else {
44
+ emit(rule.scope ?? top.default, start, end);
45
+ }
46
+ }
47
+ if (rule.pop > 0) {
48
+ // Never pop the root state off.
49
+ const n = Math.min(rule.pop, stack.length - 1);
50
+ stack.length -= n;
51
+ }
52
+ if (rule.set !== null) {
53
+ stack[stack.length - 1] = grammar.states[rule.set];
54
+ }
55
+ if (rule.push !== null) {
56
+ stack.push(grammar.states[rule.push]);
57
+ }
58
+ top = stack[stack.length - 1];
59
+ if (end > pos) {
60
+ pos = end;
61
+ spin = 0;
62
+ }
63
+ else if (++spin > 32) {
64
+ // Give up on the cycle and consume a whole code point so we always
65
+ // terminate — stepping by one UTF-16 unit could bisect a surrogate pair.
66
+ const step = code.codePointAt(pos) > 0xffff ? 2 : 1;
67
+ emit(top.default, pos, pos + step);
68
+ pos += step;
69
+ spin = 0;
70
+ }
71
+ }
72
+ if (pos < len)
73
+ emit(top.default, pos, len);
74
+ };
75
+ /**
76
+ * Re-runs the winning rule on its own to find where its capture groups sit.
77
+ *
78
+ * The rule's pattern is sticky and anchored at the position the merged regex
79
+ * already settled on, so it matches exactly the same text — this only
80
+ * recovers offsets, it never changes the outcome.
81
+ *
82
+ * Groups are walked left to right and clipped to the part of the match not yet
83
+ * emitted. Positional scoping assumes groups are siblings, but nothing stops a
84
+ * grammar from nesting one inside another — `((a)b)` — or capturing inside a
85
+ * lookahead, where the group can even reach past the end of the match. Emitting
86
+ * either verbatim would repeat text, or claim text the tokenizer is about to
87
+ * visit again, so a group only ever scopes what the match still has left.
88
+ */
89
+ const emitCaptures = (code, start, rule, state, emit) => {
90
+ const own = rule.own;
91
+ own.lastIndex = start;
92
+ const m = own.exec(code);
93
+ if (m === null)
94
+ return; // unreachable: the merged regex matched this rule here
95
+ const indices = m.indices;
96
+ const whole = indices[0];
97
+ const captures = rule.captures;
98
+ const fallback = rule.rest ?? state.default;
99
+ let cursor = whole[0];
100
+ const limit = whole[1];
101
+ for (let i = 0; i < captures.length; i++) {
102
+ const span = indices[i + 1];
103
+ if (span === undefined)
104
+ continue; // group did not participate
105
+ const to = span[1] > limit ? limit : span[1]; // a lookahead group can overrun
106
+ if (to <= cursor)
107
+ continue; // already covered by an enclosing group
108
+ const from = span[0] > cursor ? span[0] : cursor;
109
+ if (from > cursor)
110
+ emit(fallback, cursor, from);
111
+ if (to > from)
112
+ emit(captures[i] ?? fallback, from, to); // a group can match empty
113
+ cursor = to;
114
+ }
115
+ if (limit > cursor)
116
+ emit(fallback, cursor, limit);
117
+ };
118
+ /**
119
+ * Collects tokens into an array.
120
+ *
121
+ * Prefer `highlight()` when the destination is HTML — it skips this
122
+ * intermediate representation entirely. Use this to render into React
123
+ * elements, a canvas, or anything else.
124
+ */
125
+ export const tokenizeToArray = (code, grammar) => {
126
+ const tokens = [];
127
+ tokenize(code, grammar, (scope, start, end) => {
128
+ tokens.push({ scope, text: code.slice(start, end), start, end });
129
+ });
130
+ return tokens;
131
+ };
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Grammar authoring types.
3
+ *
4
+ * A grammar is a small state machine. Each state describes only the patterns
5
+ * that are *interesting* in that state; anything the state does not match is
6
+ * emitted with the state's `default` scope. That is what keeps grammars short:
7
+ * a string state needs three rules (escape, interpolation, closing quote), not
8
+ * a rule for "string content".
9
+ */
10
+ /** A scope name from `SCOPES`, e.g. `'keyword.control'`. */
11
+ export type ScopeName = string;
12
+ export type Rule = {
13
+ /**
14
+ * Pattern to match. Written without flags — the compiler merges every rule
15
+ * in a state into one alternation, so flags are set per state.
16
+ *
17
+ * Backreferences (`\1`) are not supported: group numbers shift during the
18
+ * merge. The compiler throws if it finds one.
19
+ */
20
+ match: string | RegExp;
21
+ /**
22
+ * How to scope the match.
23
+ *
24
+ * - a string scopes the whole match
25
+ * - an array scopes capture groups positionally (`[0]` is group 1); use
26
+ * `null` to leave a group unscoped
27
+ * - omitted leaves the match with the state's `default` scope
28
+ */
29
+ scope?: ScopeName | (ScopeName | null)[];
30
+ /** Scope for parts of the match not covered by capture groups. */
31
+ rest?: ScopeName;
32
+ /** Push a state onto the stack. */
33
+ push?: string;
34
+ /** Replace the current state (a push and a pop in one step). */
35
+ set?: string;
36
+ /** Pop `n` states (`true` means 1). */
37
+ pop?: number | true;
38
+ };
39
+ /** Splices another state's rules in at this position, keeping priority order. */
40
+ export type Include = {
41
+ include: string;
42
+ };
43
+ export type StateRule = Rule | Include;
44
+ export type State = {
45
+ /** Scope applied to text no rule matched. `undefined` renders unstyled. */
46
+ default?: ScopeName;
47
+ /** Compile this state's patterns case-insensitively. */
48
+ ignoreCase?: boolean;
49
+ rules: StateRule[];
50
+ };
51
+ export type Grammar = {
52
+ name: string;
53
+ aliases?: string[];
54
+ /** Must contain a `root` state — tokenizing starts there. */
55
+ states: Record<string, State>;
56
+ };
57
+ export type Token = {
58
+ /** `null` for text with no scope. */
59
+ scope: ScopeName | null;
60
+ text: string;
61
+ start: number;
62
+ end: number;
63
+ };
64
+ /** Streaming token sink. Ranges arrive in order and cover the input exactly. */
65
+ export type Emit = (scope: ScopeName | null, start: number, end: number) => void;
66
+ export type CompiledRule = {
67
+ /** Scope for the whole match, when `scope` was a string. */
68
+ scope: ScopeName | null;
69
+ /** Positional capture-group scopes, when `scope` was an array. */
70
+ captures: (ScopeName | null)[] | null;
71
+ /**
72
+ * This rule alone, sticky and with `hasIndices`, used to locate its capture
73
+ * groups after the merged regex has already picked it.
74
+ *
75
+ * Asking the merged regex for indices would make every match in the state
76
+ * pay to have offsets computed for every group of every rule; re-running one
77
+ * small anchored pattern for the few rules that need offsets is far cheaper.
78
+ */
79
+ own: RegExp | null;
80
+ rest: ScopeName | null;
81
+ /** Index into the merged regex of this rule's wrapper group. */
82
+ group: number;
83
+ push: string | null;
84
+ set: string | null;
85
+ pop: number;
86
+ };
87
+ export type CompiledState = {
88
+ name: string;
89
+ re: RegExp;
90
+ rules: CompiledRule[];
91
+ default: ScopeName | null;
92
+ };
93
+ export type CompiledGrammar = {
94
+ name: string;
95
+ aliases: string[];
96
+ states: Record<string, CompiledState>;
97
+ root: CompiledState;
98
+ };
99
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/core/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,4DAA4D;AAC5D,MAAM,MAAM,SAAS,GAAG,MAAM,CAAA;AAE9B,MAAM,MAAM,IAAI,GAAG;IACjB;;;;;;OAMG;IACH,KAAK,EAAE,MAAM,GAAG,MAAM,CAAA;IAEtB;;;;;;;OAOG;IACH,KAAK,CAAC,EAAE,SAAS,GAAG,CAAC,SAAS,GAAG,IAAI,CAAC,EAAE,CAAA;IAExC,kEAAkE;IAClE,IAAI,CAAC,EAAE,SAAS,CAAA;IAEhB,mCAAmC;IACnC,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,gEAAgE;IAChE,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,uCAAuC;IACvC,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;CACpB,CAAA;AAED,iFAAiF;AACjF,MAAM,MAAM,OAAO,GAAG;IACpB,OAAO,EAAE,MAAM,CAAA;CAChB,CAAA;AAED,MAAM,MAAM,SAAS,GAAG,IAAI,GAAG,OAAO,CAAA;AAEtC,MAAM,MAAM,KAAK,GAAG;IAClB,2EAA2E;IAC3E,OAAO,CAAC,EAAE,SAAS,CAAA;IACnB,wDAAwD;IACxD,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB,KAAK,EAAE,SAAS,EAAE,CAAA;CACnB,CAAA;AAED,MAAM,MAAM,OAAO,GAAG;IACpB,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,CAAC,EAAE,MAAM,EAAE,CAAA;IAClB,6DAA6D;IAC7D,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAA;CAC9B,CAAA;AAED,MAAM,MAAM,KAAK,GAAG;IAClB,qCAAqC;IACrC,KAAK,EAAE,SAAS,GAAG,IAAI,CAAA;IACvB,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,MAAM,CAAA;IACb,GAAG,EAAE,MAAM,CAAA;CACZ,CAAA;AAED,gFAAgF;AAChF,MAAM,MAAM,IAAI,GAAG,CAAC,KAAK,EAAE,SAAS,GAAG,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,KAAK,IAAI,CAAA;AAIhF,MAAM,MAAM,YAAY,GAAG;IACzB,4DAA4D;IAC5D,KAAK,EAAE,SAAS,GAAG,IAAI,CAAA;IACvB,kEAAkE;IAClE,QAAQ,EAAE,CAAC,SAAS,GAAG,IAAI,CAAC,EAAE,GAAG,IAAI,CAAA;IACrC;;;;;;;OAOG;IACH,GAAG,EAAE,MAAM,GAAG,IAAI,CAAA;IAClB,IAAI,EAAE,SAAS,GAAG,IAAI,CAAA;IACtB,gEAAgE;IAChE,KAAK,EAAE,MAAM,CAAA;IACb,IAAI,EAAE,MAAM,GAAG,IAAI,CAAA;IACnB,GAAG,EAAE,MAAM,GAAG,IAAI,CAAA;IAClB,GAAG,EAAE,MAAM,CAAA;CACZ,CAAA;AAED,MAAM,MAAM,aAAa,GAAG;IAC1B,IAAI,EAAE,MAAM,CAAA;IACZ,EAAE,EAAE,MAAM,CAAA;IACV,KAAK,EAAE,YAAY,EAAE,CAAA;IACrB,OAAO,EAAE,SAAS,GAAG,IAAI,CAAA;CAC1B,CAAA;AAED,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,MAAM,EAAE,CAAA;IACjB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAA;IACrC,IAAI,EAAE,aAAa,CAAA;CACpB,CAAA"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Grammar authoring types.
3
+ *
4
+ * A grammar is a small state machine. Each state describes only the patterns
5
+ * that are *interesting* in that state; anything the state does not match is
6
+ * emitted with the state's `default` scope. That is what keeps grammars short:
7
+ * a string state needs three rules (escape, interpolation, closing quote), not
8
+ * a rule for "string content".
9
+ */
10
+ export {};
@@ -0,0 +1,19 @@
1
+ import type { BlockOptions, RenderOptions } from './core/render.js';
2
+ import type { CompiledGrammar, Grammar, Token } from './core/types.js';
3
+ /** Anything that can name a language: a registered name, or a grammar itself. */
4
+ export type LanguageInput = string | Grammar | CompiledGrammar;
5
+ /** Highlights to an HTML fragment (the inside of a `<code>` element). */
6
+ export declare const highlight: (code: string, lang: LanguageInput, options?: RenderOptions) => string;
7
+ /** Highlights to a complete `<pre class="ch">` block. */
8
+ export declare const highlightBlock: (code: string, lang: LanguageInput, options?: BlockOptions) => string;
9
+ /** Tokenizes to an array — for rendering somewhere other than HTML. */
10
+ export declare const tokenize: (code: string, lang: LanguageInput) => Token[];
11
+ export { compile } from './core/compile.js';
12
+ export { getLanguage, isRegistered, listLanguages, registerLanguage, resolveGrammar, } from './core/registry.js';
13
+ export type { BlockOptions, RenderOptions } from './core/render.js';
14
+ export { DEFAULT_PREFIX, escapeAttribute, escapeHtml } from './core/render.js';
15
+ export type { Scope } from './core/scopes.js';
16
+ export { SCOPES, SCOPE_NAMES, scopeChain, scopeClass } from './core/scopes.js';
17
+ export { tokenize as tokenizeStream } from './core/tokenize.js';
18
+ export type { CompiledGrammar, Emit, Grammar, Include, Rule, ScopeName, State, StateRule, Token, } from './core/types.js';
19
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAGhE,OAAO,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAA;AAEnE,iFAAiF;AACjF,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,OAAO,GAAG,eAAe,CAAA;AAE9D,yEAAyE;AACzE,eAAO,MAAM,SAAS,GAAI,MAAM,MAAM,EAAE,MAAM,aAAa,EAAE,UAAU,aAAa,KAAG,MAEtF,CAAA;AAED,yDAAyD;AACzD,eAAO,MAAM,cAAc,GAAI,MAAM,MAAM,EAAE,MAAM,aAAa,EAAE,UAAU,YAAY,KAAG,MAE1F,CAAA;AAED,uEAAuE;AACvE,eAAO,MAAM,QAAQ,GAAI,MAAM,MAAM,EAAE,MAAM,aAAa,KAAG,KAAK,EAEjE,CAAA;AAED,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAA;AACxC,OAAO,EACL,WAAW,EACX,YAAY,EACZ,aAAa,EACb,gBAAgB,EAChB,cAAc,GACf,MAAM,iBAAiB,CAAA;AACxB,YAAY,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAChE,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAC3E,YAAY,EAAE,KAAK,EAAE,MAAM,eAAe,CAAA;AAC1C,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAC3E,OAAO,EAAE,QAAQ,IAAI,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAC5D,YAAY,EACV,eAAe,EACf,IAAI,EACJ,OAAO,EACP,OAAO,EACP,IAAI,EACJ,SAAS,EACT,KAAK,EACL,SAAS,EACT,KAAK,GACN,MAAM,cAAc,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,20 @@
1
+ import { resolveGrammar } from './core/registry.js';
2
+ import { highlight as render, highlightBlock as renderBlock } from './core/render.js';
3
+ import { tokenizeToArray } from './core/tokenize.js';
4
+ /** Highlights to an HTML fragment (the inside of a `<code>` element). */
5
+ export const highlight = (code, lang, options) => {
6
+ return render(code, resolveGrammar(lang), options);
7
+ };
8
+ /** Highlights to a complete `<pre class="ch">` block. */
9
+ export const highlightBlock = (code, lang, options) => {
10
+ return renderBlock(code, resolveGrammar(lang), options);
11
+ };
12
+ /** Tokenizes to an array — for rendering somewhere other than HTML. */
13
+ export const tokenize = (code, lang) => {
14
+ return tokenizeToArray(code, resolveGrammar(lang));
15
+ };
16
+ export { compile } from './core/compile.js';
17
+ export { getLanguage, isRegistered, listLanguages, registerLanguage, resolveGrammar, } from './core/registry.js';
18
+ export { DEFAULT_PREFIX, escapeAttribute, escapeHtml } from './core/render.js';
19
+ export { SCOPES, SCOPE_NAMES, scopeChain, scopeClass } from './core/scopes.js';
20
+ export { tokenize as tokenizeStream } from './core/tokenize.js';
@@ -0,0 +1,4 @@
1
+ import type { Grammar } from '../core/types.js';
2
+ declare const bash: Grammar;
3
+ export default bash;
4
+ //# sourceMappingURL=bash.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bash.d.ts","sourceRoot":"","sources":["../../src/langs/bash.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAY5C,QAAA,MAAM,IAAI,EAAE,OA0HX,CAAA;AAED,eAAe,IAAI,CAAA"}
@@ -0,0 +1,123 @@
1
+ /**
2
+ * Bash, sh and zsh.
3
+ *
4
+ * Shell scripts read badly when everything after the command name is one
5
+ * color. This separates the command from its flags, expands `$VAR`, `${...}`
6
+ * and `$(...)` inside double quotes, and leaves single-quoted strings literal
7
+ * the way the shell does.
8
+ */
9
+ const VAR = '\\$(?:[A-Za-z_]\\w*|[0-9!@#?*$-])';
10
+ const bash = {
11
+ name: 'bash',
12
+ aliases: ['sh', 'shell', 'zsh', 'console'],
13
+ states: {
14
+ root: {
15
+ rules: [
16
+ { match: '^#!.*', scope: 'comment' },
17
+ { match: '(?:^|[ \\t])#[^\\n]*', scope: 'comment' },
18
+ { include: 'expansion' },
19
+ { match: '"', scope: 'string', push: 'string-double' },
20
+ { match: "'", scope: 'string', push: 'string-single' },
21
+ { match: "\\$'", scope: 'string', push: 'string-ansi' },
22
+ { match: '`', scope: 'string', push: 'backtick' },
23
+ // Heredocs: the body is scoped as a string up to a line holding only
24
+ // the terminator. The terminator name is not tracked (that needs a
25
+ // backreference), so an inner line equal to another terminator ends it.
26
+ {
27
+ match: '(<<-?)(\\s*)([\'"]?)([A-Za-z_]\\w*)([\'"]?)',
28
+ scope: ['operator', null, 'string', 'string.special', 'string'],
29
+ push: 'heredoc',
30
+ },
31
+ {
32
+ match: '\\b(?:if|then|else|elif|fi|for|while|until|do|done|case|esac|select|break|continue|return|exit)\\b',
33
+ scope: 'keyword.control',
34
+ },
35
+ {
36
+ match: '\\b(?:function|local|declare|typeset|readonly|export|alias|unset)\\b',
37
+ scope: 'keyword.declaration',
38
+ },
39
+ { match: '\\b(?:in|then)\\b', scope: 'keyword.operator' },
40
+ {
41
+ match: '\\b(?:echo|printf|read|cd|pwd|source|eval|exec|test|trap|set|shift|wait|kill|jobs|getopts|command|type|hash|umask|ulimit)\\b',
42
+ scope: 'function.builtin',
43
+ },
44
+ {
45
+ match: '^([ \\t]*)([A-Za-z_]\\w*)(\\s*)(\\(\\))',
46
+ scope: [null, 'function', null, 'punctuation.bracket'],
47
+ },
48
+ { match: '^([ \\t]*)([A-Za-z_]\\w*)(?==)', scope: [null, 'variable'] },
49
+ // Flags are a large share of a shell line and benefit from their own color.
50
+ { match: '(?:^|\\s)(--?[A-Za-z][\\w-]*)', scope: ['constant'] },
51
+ { match: '\\b\\d+\\b', scope: 'number' },
52
+ // `/` is left out: at the top level it is almost always a path
53
+ // separator, and coloring every path segment boundary is noise.
54
+ { match: '&&|\\|\\||[|&;]|[<>]{1,2}|[-+*%!=]=?|=~', scope: 'operator' },
55
+ { match: '[()\\[\\]{}]', scope: 'punctuation.bracket' },
56
+ ],
57
+ },
58
+ expansion: {
59
+ rules: [
60
+ { match: '\\$\\{', scope: 'variable', push: 'brace-expansion' },
61
+ { match: '\\$\\(\\(', scope: 'operator', push: 'arithmetic' },
62
+ { match: '\\$\\(', scope: 'operator', push: 'subshell' },
63
+ { match: VAR, scope: 'variable' },
64
+ ],
65
+ },
66
+ 'brace-expansion': {
67
+ default: 'variable',
68
+ rules: [
69
+ { match: '\\}', scope: 'variable', pop: true },
70
+ { match: '[:#%/^,!*@-]+', scope: 'operator' },
71
+ ],
72
+ },
73
+ arithmetic: {
74
+ rules: [
75
+ { match: '\\)\\)', scope: 'operator', pop: true },
76
+ { include: 'expansion' },
77
+ { match: '\\b\\d+\\b', scope: 'number' },
78
+ { match: '[-+*/%<>=!&|^]+', scope: 'operator' },
79
+ ],
80
+ },
81
+ subshell: {
82
+ rules: [{ match: '\\)', scope: 'operator', pop: true }, { include: 'root' }],
83
+ },
84
+ 'string-double': {
85
+ default: 'string',
86
+ rules: [
87
+ { match: '\\\\[\\\\$`"\\n]', scope: 'string.escape' },
88
+ { include: 'expansion' },
89
+ { match: '`', scope: 'string', push: 'backtick' },
90
+ { match: '"', scope: 'string', pop: true },
91
+ ],
92
+ },
93
+ 'string-single': {
94
+ default: 'string',
95
+ rules: [{ match: "'", scope: 'string', pop: true }],
96
+ },
97
+ 'string-ansi': {
98
+ default: 'string',
99
+ rules: [
100
+ {
101
+ match: '\\\\(?:x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|[0-7]{1,3}|.)',
102
+ scope: 'string.escape',
103
+ },
104
+ { match: "'", scope: 'string', pop: true },
105
+ ],
106
+ },
107
+ backtick: {
108
+ rules: [{ match: '`', scope: 'string', pop: true }, { include: 'root' }],
109
+ },
110
+ heredoc: {
111
+ default: 'string',
112
+ rules: [
113
+ {
114
+ match: '^[ \\t]*[A-Za-z_]\\w*[ \\t]*$',
115
+ scope: 'string.special',
116
+ pop: true,
117
+ },
118
+ { include: 'expansion' },
119
+ ],
120
+ },
121
+ },
122
+ };
123
+ export default bash;
@@ -0,0 +1,4 @@
1
+ import type { Grammar } from '../core/types.js';
2
+ declare const c: Grammar;
3
+ export default c;
4
+ //# sourceMappingURL=c.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"c.d.ts","sourceRoot":"","sources":["../../src/langs/c.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAqC5C,QAAA,MAAM,CAAC,EAAE,OAqLR,CAAA;AAED,eAAe,CAAC,CAAA"}
@@ -0,0 +1,196 @@
1
+ /**
2
+ * C.
3
+ *
4
+ * Four things carry most of the weight:
5
+ *
6
+ * - The preprocessor is a second language living at the start of a line, so its
7
+ * directives are anchored to `^`. `#include` hands its header path to the
8
+ * string scope, and `#define` tells an object-like macro from a function-like
9
+ * one by the paren that follows the name.
10
+ * - A definition is told from a call by what comes after the parameter list: a
11
+ * body's `{`. Nothing local to `f(x)` distinguishes the two, so the lookahead
12
+ * is the whole heuristic — see the rule for what it gets wrong.
13
+ * - `'a'` is a character literal, not the start of a string, so it is matched
14
+ * whole. That is also what keeps `'"'` from opening one.
15
+ * - Reserved `__names__` read as compiler magic rather than as identifiers,
16
+ * which is what `__FILE__`, `__VA_ARGS__` and `__attribute__` all are.
17
+ *
18
+ * Ordinary identifiers deliberately match no rule and inherit the block's
19
+ * foreground, which keeps both the markup and the grammar small.
20
+ */
21
+ const ID = '[A-Za-z_]\\w*';
22
+ /** `\n`, `\0`, `\x41`, `\uXXXX`, `\033`, and a backslash-newline continuation. */
23
+ const ESCAPE = '\\\\(?:x[0-9a-fA-F]+|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}|[0-7]{1,3}|\\r?\\n|.)';
24
+ /** printf conversions, length modifiers included (`%zu`, `%#lx`, `%.3f`). */
25
+ const FORMAT = "%[-+ #0']*(?:\\d+|\\*)?(?:\\.(?:\\d+|\\*))?(?:hh|ll|[hljztL])?[diouxXeEfFgGaAcspn%]";
26
+ /** Literal suffixes: `1024u`, `42UL`, `3.14f`, `0xDEADBEEFULL`. */
27
+ const SUFFIX = '(?:[uU](?:ll|LL|[lL])?|(?:ll|LL|[lL])[uU]?|[fF])?';
28
+ /** The `u8`/`u`/`U`/`L` prefix on a string or character literal. */
29
+ const ENCODING = '(?:u8|[uUL])?';
30
+ const c = {
31
+ name: 'c',
32
+ aliases: ['h'],
33
+ states: {
34
+ root: {
35
+ rules: [
36
+ // ---- comments --------------------------------------------------------
37
+ // Doxygen opens with `/**` or `/*!`; the lookahead keeps `/**/` and a
38
+ // `/***` banner out of the doc scope.
39
+ // States, not a lazy `[\\s\\S]*?` scan: that form runs to EOF and fails
40
+ // once per opener, which is O(n2) on an unterminated `/*` — 123 KB of
41
+ // `/*a` measured 3.1 s. C block comments do not nest.
42
+ {
43
+ match: '/\\*[*!](?![/*])',
44
+ scope: 'comment.doc',
45
+ push: 'doc-comment',
46
+ },
47
+ { match: '/\\*', scope: 'comment', push: 'block-comment' },
48
+ { match: '//[/!][^\\n]*', scope: 'comment.doc' },
49
+ { match: '//[^\\n]*', scope: 'comment' },
50
+ // ---- preprocessor ----------------------------------------------------
51
+ // Indentation is captured rather than scoped, so `# if` inside a nest
52
+ // of conditionals keeps its leading whitespace unstyled.
53
+ {
54
+ match: '^([ \\t]*)(#[ \\t]*include(?:_next)?)([ \\t]*)(<[^>\\n]*>|"[^"\\n]*")',
55
+ scope: [null, 'keyword.import', null, 'string'],
56
+ },
57
+ {
58
+ match: `^([ \\t]*)(#[ \\t]*define)([ \\t]+)(${ID})(?=\\()`,
59
+ scope: [null, 'keyword.declaration', null, 'function'],
60
+ },
61
+ {
62
+ match: `^([ \\t]*)(#[ \\t]*(?:define|undef))([ \\t]+)(${ID})`,
63
+ scope: [null, 'keyword.declaration', null, 'constant'],
64
+ },
65
+ {
66
+ match: '^([ \\t]*)(#[ \\t]*(?:ifdef|ifndef|elifdef|elifndef|if|elif|else|endif))\\b',
67
+ scope: [null, 'keyword.control'],
68
+ },
69
+ // Everything else the preprocessor understands: pragma, error, line…
70
+ { match: '^([ \\t]*)(#[ \\t]*[a-z_]+)', scope: [null, 'keyword'] },
71
+ // The backslash that splices a macro onto the next line.
72
+ { match: '\\\\(?=\\r?\\n)', scope: 'operator' },
73
+ // ---- literals --------------------------------------------------------
74
+ {
75
+ match: `${ENCODING}(")`,
76
+ scope: ['string'],
77
+ rest: 'string.special',
78
+ push: 'string',
79
+ },
80
+ // Bounded repetition: a stray apostrophe must not scan to end of line,
81
+ // and multi-character constants (`'ab'`) are still legal C.
82
+ {
83
+ match: `${ENCODING}('(?:[^'\\\\\\n]|\\\\[^\\n]){1,8}')`,
84
+ scope: ['string'],
85
+ rest: 'string.special',
86
+ },
87
+ // ---- keywords --------------------------------------------------------
88
+ // The tag rule sits above the plain `struct` keyword so the two are one
89
+ // match and the tag name reads as a type.
90
+ {
91
+ match: `\\b(struct|union|enum)([ \\t]+)(${ID})`,
92
+ scope: ['keyword.declaration', null, 'type'],
93
+ },
94
+ {
95
+ match: '\\b(?:if|else|for|while|do|switch|case|default|break|continue|return|goto)\\b',
96
+ scope: 'keyword.control',
97
+ },
98
+ {
99
+ match: '\\b(?:struct|union|enum|typedef|static|extern|const|volatile|inline|register|auto|restrict|constexpr|_Atomic|_Noreturn|_Thread_local|thread_local)\\b',
100
+ scope: 'keyword.declaration',
101
+ },
102
+ {
103
+ match: '\\b(?:sizeof|alignof|_Alignof|typeof|typeof_unqual|_Generic|defined)\\b',
104
+ scope: 'keyword.operator',
105
+ },
106
+ { match: '\\b(?:true|false)\\b', scope: 'boolean' },
107
+ { match: '\\b(?:NULL|nullptr|EOF)\\b', scope: 'constant.builtin' },
108
+ {
109
+ match: '\\b(?:stdin|stdout|stderr|errno)\\b',
110
+ scope: 'variable.builtin',
111
+ },
112
+ {
113
+ match: '\\b(?:void|char|short|int|long|float|double|signed|unsigned|_Bool|bool|size_t|ssize_t|ptrdiff_t|wchar_t|char16_t|char32_t|FILE|va_list|u?int(?:8|16|32|64|ptr|max)_t)\\b',
114
+ scope: 'type.builtin',
115
+ },
116
+ // ---- names -----------------------------------------------------------
117
+ // A leading underscore pair is reserved for the implementation, so
118
+ // `__FILE__`, `__VA_ARGS__` and `__attribute__` are all compiler magic.
119
+ { match: '\\b__\\w+', scope: 'variable.special' },
120
+ {
121
+ match: `(->)(${ID})(?=[ \\t]*\\()`,
122
+ scope: ['operator', 'function.method'],
123
+ },
124
+ { match: `(->)(${ID})`, scope: ['operator', 'variable.member'] },
125
+ {
126
+ match: `(\\.)(${ID})(?=[ \\t]*\\()`,
127
+ scope: ['punctuation', 'function.method'],
128
+ },
129
+ { match: `(\\.)(${ID})`, scope: ['punctuation', 'variable.member'] },
130
+ // A definition is a parameter list followed by a body. The parameter
131
+ // scan excludes parens, so `if (ok(x)) {` and `while (next(&it)) {` stay
132
+ // calls; it also means a definition taking a function pointer, or one
133
+ // with more than 120 characters of parameters, falls back to
134
+ // `function.call`. A prototype ends in `;` and reads as a call too.
135
+ {
136
+ match: `\\b${ID}(?=[ \\t]*\\([^;(){}]{0,120}\\)[ \\t]*(?:\\r?\\n[ \\t]*)?\\{)`,
137
+ scope: 'function',
138
+ },
139
+ {
140
+ match: '\\b(?:printf|fprintf|sprintf|snprintf|scanf|sscanf|puts|fputs|putchar|getchar|perror|fflush|fopen|fclose|fread|fwrite|malloc|calloc|realloc|free|memcpy|memmove|memset|memcmp|strlen|strcmp|strncmp|strcpy|strncpy|strcat|strchr|strstr|strtol|strtod|atoi|abort|exit|assert|qsort|bsearch|va_start|va_arg|va_end)\\b(?=[ \\t]*\\()',
141
+ scope: 'function.builtin',
142
+ },
143
+ // Before the constant and type rules, so a SCREAMING_CASE macro used as
144
+ // `MIN(a, b)` reads as the call it expands to.
145
+ { match: `\\b${ID}(?=[ \\t]*\\()`, scope: 'function.call' },
146
+ // SCREAMING_CASE is a macro or an enumerator; `_t` and CapWords are the
147
+ // two ways a C codebase spells a typedef.
148
+ { match: '\\b[A-Z][A-Z0-9_]+\\b', scope: 'constant' },
149
+ { match: `\\b${ID}_t\\b`, scope: 'type' },
150
+ { match: '\\b[A-Z]\\w*\\b', scope: 'type' },
151
+ // ---- numbers ---------------------------------------------------------
152
+ // Hex first: `0x1p-3` is a float, and the decimal rule would stop at the
153
+ // `0`. Octal needs no rule of its own — `0755` is a number either way.
154
+ {
155
+ match: `\\b0[xX][0-9a-fA-F]+(?:\\.[0-9a-fA-F]*)?(?:[pP][-+]?\\d+)?${SUFFIX}\\b`,
156
+ scope: 'number',
157
+ },
158
+ { match: `\\b0[bB][01]+${SUFFIX}\\b`, scope: 'number' },
159
+ {
160
+ match: `\\b\\d+(?:\\.\\d*)?(?:[eE][-+]?\\d+)?${SUFFIX}\\b`,
161
+ scope: 'number',
162
+ },
163
+ { match: `\\.\\d+(?:[eE][-+]?\\d+)?${SUFFIX}\\b`, scope: 'number' },
164
+ // ---- syntax ----------------------------------------------------------
165
+ {
166
+ match: '->|\\+\\+|--|<<=?|>>=?|&&|\\|\\||\\.{3}|[-+*/%&|^!=<>]=?|[?~]',
167
+ scope: 'operator',
168
+ },
169
+ { match: '[()\\[\\]{}]', scope: 'punctuation.bracket' },
170
+ { match: '[,;:]', scope: 'punctuation.delimiter' },
171
+ ],
172
+ },
173
+ /** Block comments. C does not nest them, so these do not push themselves. */
174
+ 'block-comment': {
175
+ default: 'comment',
176
+ rules: [{ match: '\\*/', pop: true }],
177
+ },
178
+ 'doc-comment': {
179
+ default: 'comment.doc',
180
+ rules: [{ match: '\\*/', pop: true }],
181
+ },
182
+ string: {
183
+ default: 'string',
184
+ rules: [
185
+ { match: ESCAPE, scope: 'string.escape' },
186
+ { match: FORMAT, scope: 'string.special' },
187
+ { match: '"', scope: 'string', pop: true },
188
+ // An unterminated literal ends at the line break instead of swallowing
189
+ // the rest of the file. A continued one does not reach here: the
190
+ // backslash-newline is an escape, and it matches first.
191
+ { match: '$', pop: true },
192
+ ],
193
+ },
194
+ },
195
+ };
196
+ export default c;
@@ -0,0 +1,4 @@
1
+ import type { Grammar } from '../core/types.js';
2
+ declare const clojure: Grammar;
3
+ export default clojure;
4
+ //# sourceMappingURL=clojure.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"clojure.d.ts","sourceRoot":"","sources":["../../src/langs/clojure.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AA0D5C,QAAA,MAAM,OAAO,EAAE,OAmLd,CAAA;AAED,eAAe,OAAO,CAAA"}