@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 @@
1
+ {"version":3,"file":"ini.d.ts","sourceRoot":"","sources":["../../src/langs/ini.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAsF5C,QAAA,MAAM,GAAG,EAAE,OAkKV,CAAA;AAED,eAAe,GAAG,CAAA"}
@@ -0,0 +1,227 @@
1
+ /**
2
+ * INI, and the TOML superset most files labelled `ini` actually are.
3
+ *
4
+ * Two decisions shape the whole grammar:
5
+ *
6
+ * - A key is only a key where a key can start: at the beginning of a line, or
7
+ * inside an inline table. Everything after the separator is a value, in its
8
+ * own state, which is what stops `search = /usr/bin:/bin` from reading the
9
+ * second half of the path as another key.
10
+ * - A value is only interesting where it is a literal. Bare values are left
11
+ * unscoped instead of guessed at — smaller, and right far more often than a
12
+ * rule that colours every word after an `=`.
13
+ */
14
+ /**
15
+ * A bare value, and the boundary a literal has to end on.
16
+ *
17
+ * A literal only counts when it is the whole token: `2.3` is not a number
18
+ * inside the version `1.2.3`, and `on` is not a boolean inside the path
19
+ * `/opt/on`. `TAIL` rules out the right-hand side. The left-hand side used to be
20
+ * a matching lookbehind, but a rule that *starts* with one costs the merged
21
+ * alternation its first-character scan for the whole state, and JavaScriptCore
22
+ * charges dearly for that: 3x on plain `key = value` lines and 4x on
23
+ * whitespace-heavy values. So `BARE` does the same job by consuming — it sits
24
+ * last in `literals` and swallows the whole token, so the scanner never reaches
25
+ * an interior position where a literal could start.
26
+ *
27
+ * The apostrophe is in the class so the `'` in `don't` is swallowed with the
28
+ * word rather than opening a literal string. A real `'…'` is unaffected — the
29
+ * string rules sit ahead of this one, so a quote only reaches `BARE` when it
30
+ * opens nothing.
31
+ *
32
+ * `=` is deliberately not a token character: `port=8080` with no spaces is
33
+ * ordinary INI and the number still has to be found there.
34
+ */
35
+ const BARE = "[\\w.:+\\-/']+";
36
+ const TAIL = '(?![\\w.:+\\-/])';
37
+ /** TOML's escapes, minus the line continuation that only `"""` strings have. */
38
+ const ESCAPE = '\\\\(?:[btnfr"\\\\]|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8})';
39
+ /**
40
+ * Inside `"""` a trailing backslash joins the line to the next one. It is
41
+ * deliberately absent from `ESCAPE`: a single-quoted `"` string is single-line,
42
+ * so letting it eat a newline there would defeat that state's `$` guard and
43
+ * colour the following line as string content.
44
+ */
45
+ const ML_ESCAPE = '\\\\(?:[btnfr"\\\\]|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}|\\r?\\n)';
46
+ /**
47
+ * RFC 3339 offset date-times, plus the local date and local time TOML also
48
+ * allows. Kept ahead of `NUMBER`, whose decimal branch would otherwise claim
49
+ * the year and leave `-05-27` behind.
50
+ */
51
+ const DATE = '\\d{4}-\\d{2}-\\d{2}(?:[Tt ]\\d{2}:\\d{2}:\\d{2}(?:\\.\\d+)?(?:[Zz]|[-+]\\d{2}:\\d{2})?)?|\\d{2}:\\d{2}:\\d{2}(?:\\.\\d+)?';
52
+ /**
53
+ * Every numeric form, in one alternation rather than one rule each: the merged
54
+ * state regex tries every alternative at every position, and each of these
55
+ * would otherwise re-evaluate the boundary guards below.
56
+ */
57
+ const NUMBER = '0[xX][0-9a-fA-F](?:_?[0-9a-fA-F])*|0[oO][0-7](?:_?[0-7])*|0[bB][01](?:_?[01])*|' +
58
+ '[-+]?(?:\\d(?:_?\\d)*(?:\\.\\d(?:_?\\d)*)?(?:[eE][-+]?\\d(?:_?\\d)*)?|inf|nan)';
59
+ /**
60
+ * A key path followed by its separator, as a lookahead: the key state does the
61
+ * scoping, this only decides whether the line has a key at all.
62
+ *
63
+ * Indentation is part of the run rather than a `[ \t]*` prefix around it, and
64
+ * that is the whole point. When both could match a space there were two
65
+ * readings of every leading blank, and the greedy prefix replayed the run from
66
+ * each of them: a line of 1000 spaces cost 200 ms and each doubling cost 4x.
67
+ * Now no character matches two alternatives, so any prefix parses exactly one
68
+ * way and the run simply stops on the separator, which is in none of them.
69
+ *
70
+ * The cap bounds the single attempt the `^` anchor allows per line; a key
71
+ * longer than it renders unscoped.
72
+ */
73
+ const KEY_AHEAD = '(?=(?:[\\w.\\- \\t]|"[^"\\n]{0,128}"|\'[^\'\\n]{0,128}\'){1,256}[=:])';
74
+ /** An inline table's key: the same three spellings, capped since nothing anchors the end. */
75
+ const IT_KEY = '[\\w.\\-]{1,64}|"[^"\\n]{0,128}"|\'[^\'\\n]{0,128}\'';
76
+ const ini = {
77
+ name: 'ini',
78
+ aliases: ['toml', 'cfg', 'conf', 'properties'],
79
+ states: {
80
+ root: {
81
+ rules: [
82
+ { include: 'comment' },
83
+ // `[[a.b]]` and `[a.b]` differ only in the bracket run, and the header
84
+ // state pops on either, so one rule covers both.
85
+ {
86
+ match: '^[ \\t]*(\\[\\[?)',
87
+ scope: ['punctuation.bracket'],
88
+ push: 'header',
89
+ },
90
+ // Zero-width: the key state re-reads the key path so the dots and
91
+ // quotes inside it can be scoped apart from the segments.
92
+ { match: `^${KEY_AHEAD}`, push: 'key' },
93
+ ],
94
+ },
95
+ comment: {
96
+ rules: [
97
+ // A whole-line comment. `!` only opens one at the start of a line,
98
+ // which is the `.properties` dialect's rule and nobody else's.
99
+ { match: '^[ \\t]*([!#;][^\\n]*)', scope: ['comment'] },
100
+ // A trailing comment, which has to be preceded by whitespace. That is
101
+ // what keeps the `#` in `pass#word` and in a URL fragment out of the
102
+ // comment. It is the TOML reading and it is wrong for the dialects
103
+ // where a `#` after the separator is just another value character.
104
+ { match: '([ \\t])([#;][^\\n]*)', scope: [null, 'comment'] },
105
+ ],
106
+ },
107
+ /** `[table]`, `[a.b]` and `[[array.of.tables]]`, up to the closing bracket run. */
108
+ header: {
109
+ rules: [
110
+ { match: '\\]{1,2}', scope: 'punctuation.bracket', pop: true },
111
+ { match: '"[^"\\n]{0,128}"|\'[^\'\\n]{0,128}\'', scope: 'namespace' },
112
+ { match: '[\\w\\-]+', scope: 'namespace' },
113
+ { match: '\\.', scope: 'punctuation.delimiter' },
114
+ // An unclosed header ends with its line rather than eating the file.
115
+ { match: '$', pop: true },
116
+ ],
117
+ },
118
+ /** The key path only — entered on a lookahead that already saw the separator. */
119
+ key: {
120
+ rules: [
121
+ { match: '[=:]', scope: 'operator', set: 'value' },
122
+ { match: '"[^"\\n]{0,128}"|\'[^\'\\n]{0,128}\'', scope: 'property' },
123
+ { match: '[\\w\\-]+', scope: 'property' },
124
+ { match: '\\.', scope: 'punctuation.delimiter' },
125
+ // Unreachable through the lookahead, but a truncated buffer is cheap to
126
+ // be safe about.
127
+ { match: '$', pop: true },
128
+ ],
129
+ },
130
+ /**
131
+ * Everything after the separator, to the end of the line. A value that
132
+ * opens an array or a multi-line string pushes a state that outlives the
133
+ * line, so the pop below only fires once that state has closed.
134
+ */
135
+ value: {
136
+ rules: [{ include: 'comment' }, { match: '$', pop: true }, { include: 'literals' }],
137
+ },
138
+ literals: {
139
+ rules: [
140
+ { match: '"""', scope: 'string', push: 'ml-basic' },
141
+ { match: "'''", scope: 'string', push: 'ml-literal' },
142
+ { match: '"', scope: 'string', push: 'basic' },
143
+ { match: "'[^'\\n]*'", scope: 'string' },
144
+ // The vocabulary has no date scope, and a timestamp is a numeric
145
+ // literal, so `number` is the closest reading of one.
146
+ { match: `(?:${DATE}|${NUMBER})${TAIL}`, scope: 'number' },
147
+ // `yes`/`no`/`on`/`off` are not TOML, but they are how classic INI
148
+ // spells a boolean. The cost is a value that is genuinely the word
149
+ // "no" reading as one.
150
+ { match: `(?:true|false|yes|no|on|off)${TAIL}`, scope: 'boolean' },
151
+ { match: '\\[', scope: 'punctuation.bracket', push: 'array' },
152
+ // The opening brace is matched together with the first key of the
153
+ // table, which is how that key gets anchored — see `inline-table`.
154
+ {
155
+ match: `(\\{)([ \\t]*)(${IT_KEY})([ \\t]*)([=:])`,
156
+ scope: ['punctuation.bracket', null, 'property', null, 'operator'],
157
+ push: 'inline-table',
158
+ },
159
+ { match: '\\{', scope: 'punctuation.bracket', push: 'inline-table' },
160
+ { match: ',', scope: 'punctuation.delimiter' },
161
+ // Last, and unscoped. Taking a whole bare token in one match is what
162
+ // gives every literal above its left-hand boundary: the scanner is only
163
+ // ever offered the head of a token, never a position inside one. Drop
164
+ // this rule and `1.2.3` starts finding a number in `2.3` again.
165
+ { match: BARE },
166
+ ],
167
+ },
168
+ /** A TOML array, which may span lines and carry comments between elements. */
169
+ array: {
170
+ rules: [
171
+ { match: '\\]', scope: 'punctuation.bracket', pop: true },
172
+ { include: 'comment' },
173
+ { include: 'literals' },
174
+ ],
175
+ },
176
+ /**
177
+ * An inline table, entered on its opening brace. There is no `^` to anchor
178
+ * a key to inside a one-line construct, so the delimiter in front of it is
179
+ * consumed instead: the brace above takes the first key and the comma here
180
+ * takes every later one. Unanchored, the scan was retried at every column
181
+ * of every word and backtracked up to the cap each time.
182
+ */
183
+ 'inline-table': {
184
+ rules: [
185
+ { match: '\\}', scope: 'punctuation.bracket', pop: true },
186
+ // The dotted path stays in one `property` token rather than paying for
187
+ // another state. The gap takes line breaks because a comma is how a
188
+ // multi-line inline table is written, and taking it here is also what
189
+ // carries the table past the `$` below.
190
+ {
191
+ match: `(,)([ \\t\\r\\n]*)(${IT_KEY})([ \\t]*)([=:])`,
192
+ scope: ['punctuation.delimiter', null, 'property', null, 'operator'],
193
+ },
194
+ { include: 'literals' },
195
+ // A half-typed `a = {` is what an editor holds after every keystroke,
196
+ // and without this the rest of the document stayed inside the table and
197
+ // lost every key. So the table ends with its line, exactly as an
198
+ // unclosed header and an unterminated string do — a real one is either
199
+ // closed on that line or carried onto the next by the comma rule above.
200
+ { match: '$', pop: true },
201
+ ],
202
+ },
203
+ basic: {
204
+ default: 'string',
205
+ rules: [
206
+ { match: ESCAPE, scope: 'string.escape' },
207
+ { match: '"', scope: 'string', pop: true },
208
+ // An unterminated string stops at the line break instead of colouring
209
+ // the rest of the file.
210
+ { match: '$', pop: true },
211
+ ],
212
+ },
213
+ 'ml-basic': {
214
+ default: 'string',
215
+ rules: [
216
+ { match: ML_ESCAPE, scope: 'string.escape' },
217
+ { match: '"""', scope: 'string', pop: true },
218
+ ],
219
+ },
220
+ /** `'''…'''` is literal: no escapes, so the only rule is the closing run. */
221
+ 'ml-literal': {
222
+ default: 'string',
223
+ rules: [{ match: "'''", scope: 'string', pop: true }],
224
+ },
225
+ },
226
+ };
227
+ export default ini;
@@ -0,0 +1,4 @@
1
+ import type { Grammar } from '../core/types.js';
2
+ declare const java: Grammar;
3
+ export default java;
4
+ //# sourceMappingURL=java.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"java.d.ts","sourceRoot":"","sources":["../../src/langs/java.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AA0C5C,QAAA,MAAM,IAAI,EAAE,OA6JX,CAAA;AAED,eAAe,IAAI,CAAA"}
@@ -0,0 +1,178 @@
1
+ /**
2
+ * Java.
3
+ *
4
+ * The parts most highlighters flatten into one colour, and that this grammar
5
+ * keeps apart:
6
+ *
7
+ * - a method *declaration* is told from a *call* by what sits in front of it.
8
+ * A declaration always follows a type, so `void run()` and `Order(int id)`
9
+ * are definitions while `run()`, `o.run()` and `return run()` are calls
10
+ * - text blocks (`"""`) are a state, so a `"` or a `//` inside one is text
11
+ * - `%s`-style format specifiers stand out inside a literal, which is as close
12
+ * as Java gets to interpolation
13
+ * - `@interface` declares a type; every other `@Name` is an annotation
14
+ *
15
+ * Ordinary identifiers — locals, fields, parameters — deliberately match no
16
+ * rule and inherit the block's foreground, which keeps the markup small.
17
+ */
18
+ /**
19
+ * A Java identifier, with its tail length-capped.
20
+ *
21
+ * `$` is legal in an identifier but is not a `\w` character, so `\b` sits
22
+ * between every `$` and the letter after it. In `$a$a$a…` — one identifier —
23
+ * that is a word boundary every two characters, and the rules that match a name
24
+ * and then test a lookahead would restart at each one and rescan the line. The
25
+ * cap bounds that scan; names this long do not occur in real code.
26
+ */
27
+ const ID = '[A-Za-z_$\\u0080-\\uFFFF][\\w$\\u0080-\\uFFFF]{0,128}';
28
+ /**
29
+ * `\n`, `\"`, `\\`, `•`, `\uu2022` (legal), `\033`.
30
+ *
31
+ * No line-continuation alternative: Java has none, and accepting one lets a
32
+ * string ending in a backslash swallow the following line of code.
33
+ */
34
+ const ESCAPE = '\\\\(?:u+[0-9a-fA-F]{4}|[0-7]{1,3}|.)';
35
+ /** `java.util.Formatter` specs: `%s`, `%-8s`, `%,.2f`, `%08X`, `%2$tY`, `%n`. */
36
+ const FORMAT = '%(?:\\d{1,3}\\$)?[-#+ 0,(]{0,6}\\d{0,6}(?:\\.\\d{1,6})?[bBhHsScCdoxXeEfgGaAtTn%]';
37
+ const java = {
38
+ name: 'java',
39
+ states: {
40
+ root: {
41
+ rules: [
42
+ // Block comments are states, not a lazy `[\s\S]*?\*/` scan. The lazy
43
+ // form costs O(n²) when the closer is missing — a 128 KB file with an
44
+ // unterminated `/*` took 24 s — and it mis-colours the half-typed
45
+ // `/**` an editor sends on every keystroke, rendering the opener as an
46
+ // operator and the prose as code.
47
+ { match: '/\\*\\*(?![/*])', scope: 'comment.doc', push: 'doc-comment' },
48
+ { match: '/\\*', scope: 'comment', push: 'block-comment' },
49
+ { match: '//[^\\n]*', scope: 'comment' },
50
+ // Text blocks first: `"""` and `"` both start here, and the longer
51
+ // opener has to win.
52
+ { match: '"""', scope: 'string', push: 'text-block' },
53
+ { match: '"', scope: 'string', push: 'string' },
54
+ { match: `'(?:[^'\\\\\\n]|${ESCAPE})'`, scope: 'string' },
55
+ // The whole dotted path in one rule. There is no marker like Rust's
56
+ // `::` that would let a later rule recognise a package name on its own.
57
+ {
58
+ match: `\\b(import|package)([ \\t]+)(?:(static)([ \\t]+))?((?:${ID}\\.)*(?:${ID}|\\*))`,
59
+ scope: ['keyword.import', null, 'keyword.declaration', null, 'namespace'],
60
+ },
61
+ { match: '\\b(?:import|package)\\b', scope: 'keyword.import' },
62
+ // `@interface` declares an annotation type, so it is matched here
63
+ // rather than left to the annotation rule below. The lookbehind is what
64
+ // stops `subclass Foo` from reading as `class Foo`; a leading `\b`
65
+ // cannot do it because `@` is not a word character.
66
+ {
67
+ match: `(?<![\\w$@])(@?(?:class|interface|enum|record)\\b)([ \\t]+)(${ID})`,
68
+ scope: ['keyword.declaration', null, 'class'],
69
+ },
70
+ { match: `(@${ID}(?:\\.${ID})*)`, scope: 'decorator' },
71
+ {
72
+ match: '\\b(?:assert|break|case|catch|continue|default|do|else|finally|for|if|return|switch|throw|try|while|yield)\\b',
73
+ scope: 'keyword.control',
74
+ },
75
+ {
76
+ match: '\\b(?:abstract|class|const|enum|extends|final|implements|interface|native|non-sealed|permits|private|protected|public|sealed|static|strictfp|synchronized|throws|transient|var|volatile)\\b',
77
+ scope: 'keyword.declaration',
78
+ },
79
+ { match: '\\b(?:instanceof|new)\\b', scope: 'keyword.operator' },
80
+ { match: '\\b(?:true|false)\\b', scope: 'boolean' },
81
+ { match: '\\bnull\\b', scope: 'constant.builtin' },
82
+ { match: '\\b(?:this|super)\\b', scope: 'variable.builtin' },
83
+ {
84
+ match: '\\b(?:boolean|byte|char|double|float|int|long|short|void|String|Object|Integer|Long|Double|Float|Short|Byte|Character|Boolean|Number|CharSequence|Class|Math|System|Exception|RuntimeException|Throwable|List|Map|Set|Optional|Stream)\\b',
85
+ scope: 'type.builtin',
86
+ },
87
+ // Matched at the dot, so `logger.print` never reads as a bare name.
88
+ {
89
+ match: `(\\.)([ \\t]*)(${ID})(?=[ \\t]*\\()`,
90
+ scope: ['punctuation', null, 'function.method'],
91
+ },
92
+ {
93
+ match: `(\\.)([ \\t]*)(${ID})`,
94
+ scope: ['punctuation', null, 'variable.member'],
95
+ },
96
+ // A method reference names a method without calling it.
97
+ {
98
+ match: `(::)([ \\t]*)(${ID})`,
99
+ scope: ['operator', null, 'function.method'],
100
+ },
101
+ // A declaration is a name that follows a type, which in Java always
102
+ // ends in an identifier character, a `>` or a `]`: `int run(`,
103
+ // `Money priceOf(`, `List<T> items(`, `String[] main(`. The exclusions
104
+ // are the tokens that end the same way but introduce a *call*: `->` for
105
+ // a lambda body, and the keywords that can sit directly before one.
106
+ //
107
+ // The reading it gets wrong: a `>` comparison against a call looks
108
+ // exactly like a generic return type, so `if (a[i] > f(x))` and
109
+ // `if (list.size() > max(a, b))` scope the call as a definition. A
110
+ // subscript is safe — `if (xs[i]) run();` has a `)` before the space,
111
+ // which the lookbehind class does not accept.
112
+ {
113
+ match: `(?<=[\\w$>\\]])(?<!->)(?<!\\b(?:return|new|throw|else|yield|assert|do))([ \\t]+)(${ID})(?=[ \\t]*\\()`,
114
+ scope: [null, 'function'],
115
+ },
116
+ // SCREAMING_CASE reads as a constant — which also covers enum members —
117
+ // and CapWords as a type. Between them they cover classes, interfaces
118
+ // and the whole exception hierarchy without shipping a name list.
119
+ { match: '\\b[A-Z][A-Z0-9_]+\\b', scope: 'constant' },
120
+ { match: '\\b[A-Z][A-Za-z0-9_$]*\\b', scope: 'type' },
121
+ { match: `\\b${ID}(?=[ \\t]*->)`, scope: 'variable.parameter' },
122
+ {
123
+ match: '\\b[a-z_$][\\w$\\u0080-\\uFFFF]{0,128}(?=[ \\t]*\\()',
124
+ scope: 'function.call',
125
+ },
126
+ { match: '\\b0[xX][0-9a-fA-F][0-9a-fA-F_]*[lL]?\\b', scope: 'number' },
127
+ { match: '\\b0[bB][01][01_]*[lL]?\\b', scope: 'number' },
128
+ // Octal (`0755`) needs no rule of its own: it is decimal-shaped and
129
+ // both readings end up scoped `number` anyway.
130
+ {
131
+ match: '\\b\\d[\\d_]*(?:\\.[\\d_]*)?(?:[eE][-+]?\\d+)?[fFdDlL]?\\b|\\.\\d[\\d_]*(?:[eE][-+]?\\d+)?[fFdDlL]?',
132
+ scope: 'number',
133
+ },
134
+ {
135
+ match: '->|::|\\.{3}|\\+\\+|--|>>>?=?|<<=?|&&|\\|\\||[-+*/%&|^!=<>]=?|[~?]',
136
+ scope: 'operator',
137
+ },
138
+ { match: '[()\\[\\]{}]', scope: 'punctuation.bracket' },
139
+ { match: '[,;:]', scope: 'punctuation.delimiter' },
140
+ ],
141
+ },
142
+ 'string-body': {
143
+ rules: [
144
+ { match: ESCAPE, scope: 'string.escape' },
145
+ { match: FORMAT, scope: 'string.special' },
146
+ ],
147
+ },
148
+ /**
149
+ * Block comments. Java does not nest them — the first closer wins — so
150
+ * these do not push themselves; they exist so an unterminated comment
151
+ * costs one linear scan and still colours as a comment.
152
+ */
153
+ 'block-comment': {
154
+ default: 'comment',
155
+ rules: [{ match: '\\*/', pop: true }],
156
+ },
157
+ 'doc-comment': {
158
+ default: 'comment.doc',
159
+ rules: [{ match: '\\*/', pop: true }],
160
+ },
161
+ string: {
162
+ default: 'string',
163
+ rules: [
164
+ { include: 'string-body' },
165
+ { match: '"', scope: 'string', pop: true },
166
+ // A Java string literal cannot span a line, so an unterminated one ends
167
+ // at the line break instead of swallowing the rest of the file.
168
+ { match: '$', pop: true },
169
+ ],
170
+ },
171
+ /** A text block ends only on `"""`, so quotes and `//` inside it are text. */
172
+ 'text-block': {
173
+ default: 'string',
174
+ rules: [{ include: 'string-body' }, { match: '"""', scope: 'string', pop: true }],
175
+ },
176
+ },
177
+ };
178
+ export default java;
@@ -0,0 +1,4 @@
1
+ import type { Grammar } from '../core/types.js';
2
+ declare const javascript: Grammar;
3
+ export default javascript;
4
+ //# sourceMappingURL=javascript.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"javascript.d.ts","sourceRoot":"","sources":["../../src/langs/javascript.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAsC5C,QAAA,MAAM,UAAU,EAAE,OAmNjB,CAAA;AAED,eAAe,UAAU,CAAA"}
@@ -0,0 +1,226 @@
1
+ /**
2
+ * JavaScript, TypeScript, JSX and TSX.
3
+ *
4
+ * One grammar covers all four: TypeScript-only keywords are inert in a `.js`
5
+ * file, and the JSX rules are gated on a tag-shaped lookahead rather than on
6
+ * the file extension.
7
+ *
8
+ * Two things here are worth knowing about:
9
+ *
10
+ * - A `/` is a regex only when the token before it cannot end an expression.
11
+ * That preceding token is matched and scoped as part of the same rule, which
12
+ * is the cheapest reliable way to tell `a / b` from `/ab+/g`.
13
+ * - Template literals nest properly: `` `${ {a: `x`} }` `` keeps its states
14
+ * straight because interpolations track brace depth.
15
+ */
16
+ /**
17
+ * An identifier, with its tail length-capped.
18
+ *
19
+ * `$` is legal in an identifier but is not a `\w` character, so `\b` sits
20
+ * between every `$` and the letter after it. In `$a$a$a…` — one valid
21
+ * identifier — that is a word boundary every two characters, and the rules that
22
+ * match an identifier and then test a lookahead (`name(`, `name:`, `name =>`)
23
+ * restart at each one, scanning to the end of the line before failing. The cap
24
+ * bounds that scan; identifiers this long do not occur in real code, and one
25
+ * that did would simply render unscoped.
26
+ */
27
+ const ID = '[A-Za-z_$\\u0080-\\uFFFF][\\w$\\u0080-\\uFFFF]{0,128}';
28
+ /** Contexts after which a `/` begins a regex literal rather than division. */
29
+ const BEFORE_REGEX = '(?:[=(,:;\\[!&|?{}+\\-*%^~<>]|=>|\\breturn|\\bcase|\\btypeof|\\bin|\\bof|\\bnew|\\bdo|\\byield|\\bawait|\\belse)';
30
+ /** Body of a regex literal: no unescaped `/`, character classes may hold one. */
31
+ const REGEX = '/(?![/*])(?:[^/\\\\\\n\\[]|\\\\.|\\[(?:[^\\]\\\\\\n]|\\\\.)*\\])+/[dgimsuvy]*';
32
+ const javascript = {
33
+ name: 'javascript',
34
+ aliases: ['js', 'jsx', 'mjs', 'cjs', 'ts', 'tsx', 'typescript'],
35
+ states: {
36
+ root: { rules: [{ include: 'expression' }] },
37
+ expression: {
38
+ rules: [
39
+ { match: '/\\*\\*(?![/*])[\\s\\S]*?\\*/', scope: 'comment.doc' },
40
+ { match: '/\\*[\\s\\S]*?\\*/', scope: 'comment' },
41
+ { match: '//[^\\n]*', scope: 'comment' },
42
+ { match: '^#!.*', scope: 'comment' },
43
+ { include: 'strings' },
44
+ // Scoping the preceding operator here is what makes the lookbehind
45
+ // unnecessary — and lookbehind is still not safe everywhere.
46
+ {
47
+ match: `(${BEFORE_REGEX})(\\s*)(${REGEX})`,
48
+ scope: ['operator', null, 'regexp'],
49
+ },
50
+ { match: `^(\\s*)(${REGEX})`, scope: [null, 'regexp'] },
51
+ { include: 'numbers' },
52
+ { include: 'jsx' },
53
+ { match: `(@)(${ID}(?:\\.${ID})*)`, scope: ['decorator', 'decorator'] },
54
+ {
55
+ match: `(\\.)([ \\t]*)(${ID})(?=[ \\t]*\\()`,
56
+ scope: ['punctuation', null, 'function.method'],
57
+ },
58
+ {
59
+ match: `(\\?\\.|\\.)([ \\t]*)(${ID})`,
60
+ scope: ['punctuation', null, 'variable.member'],
61
+ },
62
+ {
63
+ match: '\\b(?:if|else|for|while|do|switch|case|default|break|continue|return|throw|try|catch|finally|await|yield)\\b',
64
+ scope: 'keyword.control',
65
+ },
66
+ {
67
+ match: '\\b(?:function|class|const|let|var|interface|enum|namespace|module|declare|abstract|implements|extends|constructor|static|readonly|override|public|private|protected|async|get|set|type)\\b',
68
+ scope: 'keyword.declaration',
69
+ },
70
+ {
71
+ match: '\\b(?:import|export|from|require)\\b',
72
+ scope: 'keyword.import',
73
+ },
74
+ {
75
+ match: '\\b(?:typeof|instanceof|in|of|new|delete|void|satisfies|keyof|infer|asserts|as)\\b',
76
+ scope: 'keyword.operator',
77
+ },
78
+ { match: '\\b(?:true|false)\\b', scope: 'boolean' },
79
+ {
80
+ match: '\\b(?:null|undefined|NaN|Infinity)\\b',
81
+ scope: 'constant.builtin',
82
+ },
83
+ {
84
+ match: '\\b(?:this|super|arguments|globalThis)\\b',
85
+ scope: 'variable.builtin',
86
+ },
87
+ {
88
+ match: '\\b(?:console|window|document|process|module|exports)\\b',
89
+ scope: 'variable.builtin',
90
+ },
91
+ {
92
+ match: '\\b(?:string|number|boolean|symbol|bigint|object|unknown|never|any|void|readonly)\\b(?=[^\\w$]|$)',
93
+ scope: 'type.builtin',
94
+ },
95
+ { match: '\\b[A-Z][A-Z0-9_]+\\b(?![\\w$])', scope: 'constant' },
96
+ { match: '\\b[A-Z][A-Za-z0-9_$]*\\b', scope: 'type' },
97
+ // `key:` in an object literal, and the name half of a TS annotation.
98
+ // Requiring no space before the colon keeps `cond ? a : b` out.
99
+ { match: `\\b${ID}(?=\\??:)`, scope: 'property' },
100
+ { match: `\\b${ID}(?=\\s*=>)`, scope: 'variable.parameter' },
101
+ { match: `\\b${ID}(?=[ \\t]*[(<])`, scope: 'function.call' },
102
+ {
103
+ // Bare `=` goes last so `=>`, `==`, `===` and the compound forms all
104
+ // get first refusal. Without it plain assignment matched nothing.
105
+ match: '=>|\\?\\?=?|\\?\\.|\\.{3}|&&=?|\\|\\|=?|\\*\\*=?|[-+*/%&|^]=?|[!=]==?|[<>]=?|<<=?|>>>?=?|[?~!]|=',
106
+ scope: 'operator',
107
+ },
108
+ { match: '[()\\[\\]{}]', scope: 'punctuation.bracket' },
109
+ { match: '[,;:]', scope: 'punctuation.delimiter' },
110
+ ],
111
+ },
112
+ numbers: {
113
+ rules: [
114
+ { match: '\\b0[xX](?:_?[0-9a-fA-F])+n?\\b', scope: 'number' },
115
+ { match: '\\b0[oO](?:_?[0-7])+n?\\b', scope: 'number' },
116
+ { match: '\\b0[bB](?:_?[01])+n?\\b', scope: 'number' },
117
+ {
118
+ match: '\\b\\d(?:_?\\d)*\\.(?:\\d(?:_?\\d)*)?(?:[eE][-+]?\\d+)?|\\.\\d(?:_?\\d)*(?:[eE][-+]?\\d+)?|\\b\\d(?:_?\\d)*(?:[eE][-+]?\\d+)?n?\\b',
119
+ scope: 'number',
120
+ },
121
+ ],
122
+ },
123
+ strings: {
124
+ rules: [
125
+ { match: '"', scope: 'string', push: 'string-double' },
126
+ { match: "'", scope: 'string', push: 'string-single' },
127
+ { match: '`', scope: 'string', push: 'template' },
128
+ ],
129
+ },
130
+ 'string-double': {
131
+ default: 'string',
132
+ rules: [
133
+ {
134
+ match: '\\\\(?:u\\{[0-9a-fA-F]+\\}|u[0-9a-fA-F]{4}|x[0-9a-fA-F]{2}|\\r?\\n|.)',
135
+ scope: 'string.escape',
136
+ },
137
+ { match: '"', scope: 'string', pop: true },
138
+ { match: '$', pop: true },
139
+ ],
140
+ },
141
+ 'string-single': {
142
+ default: 'string',
143
+ rules: [
144
+ {
145
+ match: '\\\\(?:u\\{[0-9a-fA-F]+\\}|u[0-9a-fA-F]{4}|x[0-9a-fA-F]{2}|\\r?\\n|.)',
146
+ scope: 'string.escape',
147
+ },
148
+ { match: "'", scope: 'string', pop: true },
149
+ { match: '$', pop: true },
150
+ ],
151
+ },
152
+ template: {
153
+ default: 'string',
154
+ rules: [
155
+ {
156
+ match: '\\\\(?:u\\{[0-9a-fA-F]+\\}|u[0-9a-fA-F]{4}|x[0-9a-fA-F]{2}|.)',
157
+ scope: 'string.escape',
158
+ },
159
+ {
160
+ match: '\\$\\{',
161
+ scope: 'interpolation',
162
+ push: 'template-expression',
163
+ },
164
+ { match: '`', scope: 'string', pop: true },
165
+ ],
166
+ },
167
+ 'template-expression': {
168
+ rules: [
169
+ { match: '\\}', scope: 'interpolation', pop: true },
170
+ { match: '\\{', scope: 'punctuation.bracket', push: 'brace' },
171
+ { include: 'expression' },
172
+ ],
173
+ },
174
+ /** Tracks brace depth so an object literal cannot end an interpolation. */
175
+ brace: {
176
+ rules: [
177
+ { match: '\\}', scope: 'punctuation.bracket', pop: true },
178
+ { match: '\\{', scope: 'punctuation.bracket', push: 'brace' },
179
+ { include: 'expression' },
180
+ ],
181
+ },
182
+ // ---- JSX ---------------------------------------------------------------
183
+ jsx: {
184
+ rules: [
185
+ // Two guards, because `<` is the most overloaded character in the
186
+ // language. The trailing lookahead rules out `a < b` — a comparison
187
+ // has a space or a non-tag character where a tag has `>`, `/` or an
188
+ // attribute. The lookbehind rules out type arguments: `Handler<E
189
+ // extends Event>` and `Array<string>` both satisfy the lookahead, and
190
+ // what separates them from JSX is that a tag never follows an
191
+ // identifier, `)` or `]` directly.
192
+ {
193
+ match: '(?<![\\w$)\\]])(<)(/?)([A-Z][\\w.]*|[a-z][\\w-]*(?:\\.[\\w-]+)*)(?=[\\s/>])',
194
+ scope: ['punctuation.bracket', 'punctuation', 'tag'],
195
+ push: 'jsx-tag',
196
+ },
197
+ {
198
+ match: '(<)(>)',
199
+ scope: ['punctuation.bracket', 'punctuation.bracket'],
200
+ },
201
+ {
202
+ match: '(</)(>)',
203
+ scope: ['punctuation.bracket', 'punctuation.bracket'],
204
+ },
205
+ ],
206
+ },
207
+ 'jsx-tag': {
208
+ rules: [
209
+ { match: '/?>', scope: 'punctuation.bracket', pop: true },
210
+ { match: '\\{', scope: 'interpolation', push: 'jsx-expression' },
211
+ { match: `(${ID}(?:-${ID})*)(?=\\s*=)`, scope: 'tag.attribute' },
212
+ { match: `\\b${ID}(?:-${ID})*`, scope: 'tag.attribute' },
213
+ { match: '=', scope: 'operator' },
214
+ { include: 'strings' },
215
+ ],
216
+ },
217
+ 'jsx-expression': {
218
+ rules: [
219
+ { match: '\\}', scope: 'interpolation', pop: true },
220
+ { match: '\\{', scope: 'punctuation.bracket', push: 'brace' },
221
+ { include: 'expression' },
222
+ ],
223
+ },
224
+ },
225
+ };
226
+ export default javascript;
@@ -0,0 +1,10 @@
1
+ import type { Grammar } from '../core/types.js';
2
+ /**
3
+ * JSON, with JSONC comments and trailing-comma tolerance.
4
+ *
5
+ * Keys and string values get different scopes — the single change that does
6
+ * the most for a config file's readability.
7
+ */
8
+ declare const json: Grammar;
9
+ export default json;
10
+ //# sourceMappingURL=json.d.ts.map