@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,375 @@
1
+ /**
2
+ * Haskell.
3
+ *
4
+ * Three things decide whether a Haskell highlighter is any good, and all three
5
+ * are lexical rather than syntactic:
6
+ *
7
+ * - **Dashes.** A comment is two or more dashes *not* followed by another symbol
8
+ * character, so `-->` and `--|` are ordinary user-defined operators while
9
+ * `-- |` is a Haddock doc comment. Getting this wrong greys out working code.
10
+ * - **The apostrophe.** `'a'` is a character literal and the identical quote in
11
+ * `xs'` is just a letter. As in OCaml, a literal always closes and a name
12
+ * never does, and both are blocked by a lookbehind from firing mid-name.
13
+ * - **Case.** Every capitalised name is a constructor *or* a type constructor,
14
+ * and nothing in the token itself says which. The grammar answers that with
15
+ * position: a small `signature` state runs from `::` to the end of the line
16
+ * and a `data-body` state runs to the next column-zero declaration, and inside
17
+ * those a capitalised name is a type. Everywhere else it is a constructor.
18
+ *
19
+ * What that deliberately gets wrong:
20
+ *
21
+ * - A signature broken across lines only keeps its colours when each
22
+ * continuation *starts* with `->` or `=>`, which is the dominant style. A
23
+ * trailing-arrow layout drops back to constructor colours on the later lines.
24
+ * The test runs only while the signature is open, so a leading arrow that
25
+ * continues something else — a `case` alternative laid out as `Left e` then
26
+ * `-> Just e` — keeps constructor colours, which is what it should have.
27
+ * - GADT syntax (`data T where C :: …`) has no `=`, so its constructors read as
28
+ * types.
29
+ * - A signature that annotates several names at once (`a, b :: Int`) scopes
30
+ * none of them, because the rule stops at the comma.
31
+ * - Only a column-zero binding is recognised as a definition. Haskell's layout
32
+ * rule makes that reliable at the top level, but the equations inside `where`
33
+ * and `let` are indented and stay unscoped — at that position a name is far
34
+ * more often applied than defined.
35
+ * - Call sites are not scoped at all, because juxtaposition leaves nothing to
36
+ * recognise them by.
37
+ *
38
+ * Ordinary identifiers and type variables deliberately match no rule: they
39
+ * inherit the block's foreground, which keeps both the markup and the visual
40
+ * noise down.
41
+ */
42
+ /**
43
+ * Haskell's symbol alphabet. Operators are user-definable, so there is no fixed
44
+ * list to match — any run of these characters is one operator.
45
+ */
46
+ const SYM = '!#$%&*+./<=>?@\\\\^|~:-';
47
+ /**
48
+ * A variable identifier, with the tail length-capped on purpose.
49
+ *
50
+ * `'` is a legal name character but is not a `\w`, so `\b` sits between every
51
+ * `'` and the letter after it. In `a'a'a'…` — one valid name — that is a word
52
+ * boundary every two characters, and a rule that matches a name and then tests a
53
+ * lookahead would restart at each one and rescan the line. The cap bounds that;
54
+ * names this long do not occur, and one that did would render unscoped.
55
+ */
56
+ const LID = "[a-z_][A-Za-z0-9_']{0,128}";
57
+ /** A constructor, type or module name. Same cap, same reason. */
58
+ const UID = "[A-Z][A-Za-z0-9_']{0,128}";
59
+ /** A module path: `Data`, `Data.Map.Strict`. */
60
+ const MODULE = `${UID}(?:\\.${UID}){0,32}`;
61
+ /**
62
+ * Guards the start of a keyword. `\b` is not enough on its own: `'` is not a
63
+ * word character, so `\bin\b` happily fires inside the name `xs'in`.
64
+ */
65
+ const START = "(?<![\\w'])";
66
+ /** Guards the end of a keyword, for the same reason. */
67
+ const END = "(?![\\w'])";
68
+ /**
69
+ * `\n`, `\\`, `\"`, `\65`, `\x1F`, `\o17`, `\^C`, `\NUL`, `\&`.
70
+ *
71
+ * `[A-Z]{2,3}` stands in for the thirty-odd ASCII control names rather than
72
+ * listing them; it accepts nonsense like `\XYZ` too, which costs nothing since
73
+ * that is not valid Haskell anyway. The final catch-all matters more than the
74
+ * precise forms: it guarantees `\\` and `\"` are consumed as a unit, so a
75
+ * closing quote can never be misread. It excludes newlines so that a backslash
76
+ * left dangling at end of line cannot swallow the line break.
77
+ */
78
+ const ESCAPE = '\\\\(?:\\^[\\x40-\\x5F]|x[0-9a-fA-F]+|o[0-7]+|\\d+|[A-Z]{2,3}|&|[^\\n])';
79
+ const haskell = {
80
+ name: 'haskell',
81
+ aliases: ['hs'],
82
+ states: {
83
+ root: { rules: [{ include: 'expression' }] },
84
+ expression: {
85
+ rules: [
86
+ { include: 'comments' },
87
+ { include: 'literals' },
88
+ // ---- module and import headers -------------------------------------
89
+ // The export list is types and names rather than expressions, so it
90
+ // runs in its own state until the `where` that closes the header.
91
+ {
92
+ match: `${START}(module)([ \\t]+)(${MODULE})`,
93
+ scope: ['keyword.declaration', null, 'namespace'],
94
+ push: 'exports',
95
+ },
96
+ {
97
+ match: `${START}(import)([ \\t]+)(?:(qualified)([ \\t]+))?(${MODULE})`,
98
+ scope: ['keyword.import', null, 'keyword', null, 'namespace'],
99
+ },
100
+ // `as` is a keyword only in an import, and `as` is a common name for a
101
+ // list elsewhere — requiring a module name after it is what keeps
102
+ // `zipWith f as bs` out of this rule.
103
+ {
104
+ match: `${START}(as)([ \\t]+)(${MODULE})`,
105
+ scope: ['keyword', null, 'namespace'],
106
+ },
107
+ // The fallback for the two import modifiers, for a header the rule above
108
+ // could not match whole — a half-typed one, most often.
109
+ { match: `${START}(?:hiding|qualified)${END}`, scope: 'keyword' },
110
+ // ---- declarations ---------------------------------------------------
111
+ {
112
+ match: `${START}(data|newtype)([ \\t]+)(?:(family|instance)([ \\t]+))?(${MODULE})`,
113
+ scope: ['keyword.declaration', null, 'keyword.declaration', null, 'type'],
114
+ push: 'data-body',
115
+ },
116
+ // A type synonym has no constructors, so its whole right-hand side is a
117
+ // type — `signature`, not `data-body`.
118
+ {
119
+ match: `${START}(type)([ \\t]+)(?:(family|instance)([ \\t]+))?(${MODULE})`,
120
+ scope: ['keyword.declaration', null, 'keyword.declaration', null, 'type'],
121
+ push: 'signature',
122
+ },
123
+ // A class or instance head is a type until its `where`, which is one of
124
+ // the two things `signature` pops on.
125
+ {
126
+ match: `${START}(?:class|instance|deriving)${END}`,
127
+ scope: 'keyword.declaration',
128
+ push: 'signature',
129
+ },
130
+ // ---- keywords -------------------------------------------------------
131
+ // Ahead of the definition rules below, so a declaration that starts in
132
+ // column zero is read as a keyword rather than as a binding named
133
+ // `data` or `import`.
134
+ {
135
+ match: `${START}(?:if|then|else|case|of|do)${END}`,
136
+ scope: 'keyword.control',
137
+ },
138
+ {
139
+ match: `${START}(?:let|in|where|module|forall|family|foreign|pattern|default|infix[lr]?|via|stock|anyclass)${END}`,
140
+ scope: 'keyword.declaration',
141
+ },
142
+ { match: `${START}import${END}`, scope: 'keyword.import' },
143
+ // ---- definition sites ------------------------------------------------
144
+ // The name a signature annotates, at any indentation — this is the one
145
+ // definition inside a `where` block the grammar can see.
146
+ { match: `^([ \\t]*)(${LID})(?=[ \\t]*::)`, scope: [null, 'function'] },
147
+ // A binding in column zero, either `name = …` or `name arg… = …`.
148
+ // Haskell draws no line between a value and a function — `total = sum .
149
+ // map price` is both — so both shapes read as a function.
150
+ //
151
+ // The second shape excludes a symbol character after the name, because
152
+ // that is an infix definition (`a --> b = …`) whose left operand is a
153
+ // parameter and not the thing being defined. The cost is that a bang
154
+ // pattern in the first argument (`go !acc xs = …`) hides the name.
155
+ {
156
+ match: `^(${LID})(?=[ \\t]*=(?![${SYM}])|[ \\t]+[^\\s=${SYM}])`,
157
+ scope: 'function',
158
+ },
159
+ // ---- names -----------------------------------------------------------
160
+ { match: `${START}(?:True|False)${END}`, scope: 'boolean' },
161
+ {
162
+ match: `${START}(?:Nothing|Just|Left|Right|LT|EQ|GT|otherwise|undefined|mempty)${END}`,
163
+ scope: 'constant.builtin',
164
+ },
165
+ {
166
+ match: `${START}(?:putStrLn|putStr|print|show|read|error|mapM_?|map|filter|foldr|foldl'?|length|head|tail|reverse|concatMap|concat|zipWith|zip|elem|null|maybe|either|fromMaybe|forM_?|when|unless|return|pure|fmap|traverse|lookup|const|id|not|fst|snd|take|drop|sum|product|maximum|minimum|abs|div|mod|replicate|unwords|words|unlines|lines|flip)${END}`,
167
+ scope: 'function.builtin',
168
+ },
169
+ { include: 'type-names' },
170
+ // Outside a type, a capitalised name is a data constructor — a value —
171
+ // so it reads as a constant rather than as a type.
172
+ { match: `${START}${UID}`, scope: 'constant' },
173
+ // There is deliberately no call rule. Haskell applies functions by
174
+ // juxtaposition, so `f x` is indistinguishable from `a b`, and the one
175
+ // shape a regex could recognise — a name before a `(` — is just as often
176
+ // the head of an equation being defined (`go (x : xs) acc = …`).
177
+ // `::` opens a type, and the guard leaves user operators such as `:::`
178
+ // to the operator rule.
179
+ { match: `::(?![${SYM}])`, scope: 'operator', push: 'signature' },
180
+ { include: 'operators' },
181
+ ],
182
+ },
183
+ // ---- comments ------------------------------------------------------------
184
+ comments: {
185
+ rules: [
186
+ // A pragma is a block comment to the lexer but an instruction to the
187
+ // compiler, so it reads as an annotation. It runs as a state because a
188
+ // lazy `[\s\S]*?#-\}` would rescan to end of input at every `{-#`.
189
+ { match: '\\{-#', scope: 'decorator', push: 'pragma' },
190
+ { match: '\\{-[|^]', scope: 'comment.doc', push: 'block-doc' },
191
+ { match: '\\{-', scope: 'comment', push: 'block-comment' },
192
+ // Haddock. The space is required because `--|` is the operator `--|`,
193
+ // which is exactly why Haddock is spelled `-- |`.
194
+ { match: '--+[ \\t]+[|^][^\\n]*', scope: 'comment.doc' },
195
+ // Two or more dashes open a comment only when what follows is not
196
+ // another symbol character: `-->`, `--|` and `--<>` are operators, and
197
+ // the `+` cannot give back below two, so they fail here and fall
198
+ // through to the operator rule.
199
+ { match: `--+(?![${SYM}])[^\\n]*`, scope: 'comment' },
200
+ ],
201
+ },
202
+ // Block comments nest, so the opener pushes another copy of the state and
203
+ // only the matching `-}` pops. A `"-}"` inside a string inside a comment
204
+ // ends the comment early — GHC lexes strings in comments, we do not.
205
+ 'block-comment': {
206
+ default: 'comment',
207
+ rules: [
208
+ { match: '\\{-', push: 'block-comment' },
209
+ { match: '-\\}', pop: true },
210
+ ],
211
+ },
212
+ 'block-doc': {
213
+ default: 'comment.doc',
214
+ rules: [
215
+ { match: '\\{-', push: 'block-doc' },
216
+ { match: '-\\}', pop: true },
217
+ ],
218
+ },
219
+ pragma: {
220
+ default: 'decorator',
221
+ rules: [{ match: '#-\\}', pop: true }],
222
+ },
223
+ // ---- literals ------------------------------------------------------------
224
+ literals: {
225
+ rules: [
226
+ { match: '"', scope: 'string', push: 'string' },
227
+ // A char literal always closes; the same quote at the end of `xs'` is an
228
+ // ordinary letter. The lookbehind is what stops the quote that ends a
229
+ // name from pairing with the one that opens the next literal.
230
+ { match: `${START}'(?:[^'\\\\\\n]|${ESCAPE})'`, scope: 'string' },
231
+ // Hex covers hex floats (`0x1.8p3`); underscores are NumericUnderscores.
232
+ {
233
+ match: `${START}0[xX][0-9a-fA-F_]+(?:\\.[0-9a-fA-F_]+)?(?:[pP][-+]?\\d+)?`,
234
+ scope: 'number',
235
+ },
236
+ { match: `${START}0[oO][0-7_]+`, scope: 'number' },
237
+ { match: `${START}0[bB][01_]+`, scope: 'number' },
238
+ // A digit is required after the point, so the range `[1..10]` keeps its
239
+ // `..` instead of losing a dot to the float.
240
+ {
241
+ match: `${START}\\d[\\d_]*(?:\\.\\d[\\d_]*)?(?:[eE][-+]?\\d[\\d_]*)?`,
242
+ scope: 'number',
243
+ },
244
+ ],
245
+ },
246
+ string: {
247
+ default: 'string',
248
+ rules: [
249
+ // A string gap — a backslash, whitespace across as many lines as it
250
+ // likes, and a backslash. It has to beat the escape rule, which would
251
+ // otherwise claim the backslash and the space as one escape.
252
+ { match: '\\\\\\s+\\\\', scope: 'string.escape' },
253
+ { match: ESCAPE, scope: 'string.escape' },
254
+ { match: '"', scope: 'string', pop: true },
255
+ // An unterminated string ends at the line break rather than swallowing
256
+ // the rest of the file. A gap is matched above, so a legitimate
257
+ // multi-line string survives this.
258
+ { match: '$', pop: true },
259
+ ],
260
+ },
261
+ // ---- types ---------------------------------------------------------------
262
+ /** Shared by every state that reads a type, and by expressions. */
263
+ 'type-names': {
264
+ rules: [
265
+ // A qualified path: `Data.Map.insert`, `M.Map`. Claiming only the dot
266
+ // leaves the name after it to the rules that follow, so `Map.lookup`
267
+ // still reads as a builtin.
268
+ //
269
+ // `START` is what keeps the path from starting mid-identifier: without
270
+ // it `myMap.lookup` reads as a qualified `Map.lookup`, and the `.` in
271
+ // `showT.getName` becomes punctuation instead of the composition
272
+ // operator. It also carries the cost: unanchored, a run of capitals
273
+ // retries the `{0,32}` path and the `{0,128}` name cap at every column,
274
+ // which measured 29x on a line of `AAAA…` under V8 and 9x under JSC.
275
+ {
276
+ match: `${START}(${MODULE})(\\.)`,
277
+ scope: ['namespace', 'punctuation'],
278
+ },
279
+ {
280
+ match: `${START}(?:Int(?:eger|8|16|32|64)?|Word(?:8|16|32|64)?|Float|Double|Char|String|Bool|Maybe|Either|Ordering|IO|Rational|FilePath|Text|ByteString|Map|Set)${END}`,
281
+ scope: 'type.builtin',
282
+ },
283
+ ],
284
+ },
285
+ 'type-body': {
286
+ rules: [
287
+ { include: 'comments' },
288
+ // Type-level literals — `Nat`s and `Symbol`s under DataKinds.
289
+ { include: 'literals' },
290
+ { match: `${START}forall${END}`, scope: 'keyword.declaration' },
291
+ { include: 'type-names' },
292
+ // Inside a type every capitalised name is a type constructor. A
293
+ // lowercase one is a type variable and stays unscoped, the same as any
294
+ // other ordinary identifier.
295
+ { match: `${START}${UID}`, scope: 'type' },
296
+ { include: 'operators' },
297
+ ],
298
+ },
299
+ /** A module export list: types and names, ending at the header's `where`. */
300
+ exports: {
301
+ rules: [
302
+ {
303
+ match: `${START}where${END}`,
304
+ scope: 'keyword.declaration',
305
+ pop: true,
306
+ },
307
+ { include: 'type-body' },
308
+ ],
309
+ },
310
+ /**
311
+ * A type: everything from `::` to the end of the line, plus the head of a
312
+ * `class` or `instance` up to its `where`. Popping at the line break is what
313
+ * keeps a runaway type from colouring the rest of the file.
314
+ *
315
+ * A multi-line signature stays open instead: the state carries across the
316
+ * break when the next line continues it. Only the state already being open
317
+ * can say that — a leading arrow on its own means nothing, since a `case`
318
+ * alternative laid out as `Left e\n -> Just e` starts with one too and its
319
+ * constructors are values.
320
+ */
321
+ signature: {
322
+ rules: [
323
+ // Consuming the newline keeps the match non-empty. The lookahead is the
324
+ // continuation test: an arrow carries on the type, and a comment line
325
+ // between two arrow lines is passed over rather than ending the type.
326
+ { match: '\\n(?![ \\t]*(?:->|=>|--))', pop: true },
327
+ { include: 'exports' },
328
+ ],
329
+ },
330
+ /**
331
+ * The right-hand side of a `data` or `newtype`, which mixes the constructors
332
+ * being declared with the types they hold.
333
+ */
334
+ 'data-body': {
335
+ rules: [
336
+ // Layout: a line starting in column zero begins a new declaration, so
337
+ // that is where this one ends.
338
+ { match: '\\n(?=[^ \\t\\n])', pop: true },
339
+ { include: 'comments' },
340
+ { include: 'literals' },
341
+ // The name right after `=` or `|` is the constructor being introduced;
342
+ // every other capitalised name in the body is one of its field types.
343
+ {
344
+ match: `([=|])([ \\t]*)(${UID})`,
345
+ scope: ['operator', null, 'constant'],
346
+ },
347
+ // A record field: the lowercase name an inline `::` annotates.
348
+ { match: `${START}(${LID})(?=[ \\t]*::)`, scope: 'property' },
349
+ {
350
+ match: `${START}(?:deriving|where|forall|via|stock|anyclass|instance)${END}`,
351
+ scope: 'keyword.declaration',
352
+ },
353
+ { include: 'type-names' },
354
+ { match: `${START}${UID}`, scope: 'type' },
355
+ { include: 'operators' },
356
+ ],
357
+ },
358
+ // ---- operators and punctuation -------------------------------------------
359
+ operators: {
360
+ rules: [
361
+ // Backticks turn a named function into an infix operator, so that is how
362
+ // the whole `` `div` `` reads. The name cannot contain a backtick, which
363
+ // bounds the scan.
364
+ { match: `\`(?:${MODULE}\\.)?${LID}\``, scope: 'operator' },
365
+ // One rule for every operator: Haskell lets users define their own out
366
+ // of the symbol alphabet, so `>>=`, `<$>`, `-->` and `.&.` are not a
367
+ // fixed list.
368
+ { match: `[${SYM}]+`, scope: 'operator' },
369
+ { match: '[()\\[\\]{}]', scope: 'punctuation.bracket' },
370
+ { match: '[,;]', scope: 'punctuation.delimiter' },
371
+ ],
372
+ },
373
+ },
374
+ };
375
+ export default haskell;
@@ -0,0 +1,14 @@
1
+ import type { Grammar } from '../core/types.js';
2
+ /**
3
+ * HTML, XML and SVG.
4
+ *
5
+ * Attribute names, attribute values and the tag name each get their own scope,
6
+ * and character entities are pulled out of the text so `&amp;` reads as markup
7
+ * rather than as content.
8
+ *
9
+ * `<script>` and `<style>` bodies are left unstyled: highlighting them means
10
+ * running another grammar, which is a follow-up (see README).
11
+ */
12
+ declare const html: Grammar;
13
+ export default html;
14
+ //# sourceMappingURL=html.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"html.d.ts","sourceRoot":"","sources":["../../src/langs/html.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAE5C;;;;;;;;;GASG;AACH,QAAA,MAAM,IAAI,EAAE,OA+DX,CAAA;AAED,eAAe,IAAI,CAAA"}
@@ -0,0 +1,71 @@
1
+ /**
2
+ * HTML, XML and SVG.
3
+ *
4
+ * Attribute names, attribute values and the tag name each get their own scope,
5
+ * and character entities are pulled out of the text so `&amp;` reads as markup
6
+ * rather than as content.
7
+ *
8
+ * `<script>` and `<style>` bodies are left unstyled: highlighting them means
9
+ * running another grammar, which is a follow-up (see README).
10
+ */
11
+ const html = {
12
+ name: 'html',
13
+ aliases: ['xml', 'svg', 'vue'],
14
+ states: {
15
+ root: {
16
+ ignoreCase: true,
17
+ rules: [
18
+ { match: '<!--[\\s\\S]*?-->', scope: 'comment' },
19
+ { match: '<!\\[CDATA\\[[\\s\\S]*?\\]\\]>', scope: 'string' },
20
+ {
21
+ match: '(<!)(DOCTYPE)([^>]*)(>)',
22
+ scope: ['punctuation.bracket', 'keyword', null, 'punctuation.bracket'],
23
+ },
24
+ { match: '<\\?[\\s\\S]*?\\?>', scope: 'comment' },
25
+ {
26
+ match: '(</?)([A-Za-z_][\\w:.-]*)',
27
+ scope: ['punctuation.bracket', 'tag'],
28
+ push: 'tag',
29
+ },
30
+ {
31
+ match: '&(?:#x?[0-9a-fA-F]+|[a-zA-Z][a-zA-Z0-9]*);',
32
+ scope: 'string.escape',
33
+ },
34
+ ],
35
+ },
36
+ tag: {
37
+ ignoreCase: true,
38
+ rules: [
39
+ { match: '/?>', scope: 'punctuation.bracket', pop: true },
40
+ // Framework-flavoured names (`:prop`, `@click`, `v-if`, `{...}`) are
41
+ // common enough in templates to be worth matching as attributes.
42
+ { match: '[@:#v-]?[A-Za-z_][\\w:.-]*', scope: 'tag.attribute' },
43
+ { match: '=', scope: 'operator' },
44
+ { match: '"', scope: 'string', push: 'attr-double' },
45
+ { match: "'", scope: 'string', push: 'attr-single' },
46
+ { match: '[^\\s>]+', scope: 'string' },
47
+ ],
48
+ },
49
+ 'attr-double': {
50
+ default: 'string',
51
+ rules: [
52
+ {
53
+ match: '&(?:#x?[0-9a-fA-F]+|[a-zA-Z][a-zA-Z0-9]*);',
54
+ scope: 'string.escape',
55
+ },
56
+ { match: '"', scope: 'string', pop: true },
57
+ ],
58
+ },
59
+ 'attr-single': {
60
+ default: 'string',
61
+ rules: [
62
+ {
63
+ match: '&(?:#x?[0-9a-fA-F]+|[a-zA-Z][a-zA-Z0-9]*);',
64
+ scope: 'string.escape',
65
+ },
66
+ { match: "'", scope: 'string', pop: true },
67
+ ],
68
+ },
69
+ },
70
+ };
71
+ export default html;
@@ -0,0 +1,4 @@
1
+ import type { Grammar } from '../core/types.js';
2
+ declare const http: Grammar;
3
+ export default http;
4
+ //# sourceMappingURL=http.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/langs/http.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAiC5C,QAAA,MAAM,IAAI,EAAE,OAsKX,CAAA;AAED,eAAe,IAAI,CAAA"}
@@ -0,0 +1,185 @@
1
+ /**
2
+ * HTTP messages, the way API documentation and `.http` request files show them.
3
+ *
4
+ * HTTP has no keywords to speak of — a message is positional — so the grammar
5
+ * is a state machine over the parts of a message rather than a word list:
6
+ *
7
+ * - the request target gets its own state, so a query string comes apart into
8
+ * parameters instead of rendering as one undifferentiated URL
9
+ * - a header name is only matched from the start of a line, which is the only
10
+ * way to tell it from the colons inside `Host: localhost:8080` or a timestamp
11
+ * - the blank line after the headers switches into the body, where JSON — the
12
+ * body that actually shows up in API docs — is highlighted as JSON
13
+ *
14
+ * Comments are not part of HTTP. The `#` and `//` lines and the `###` separator
15
+ * come from the request-file format editors and REST clients use, and they are
16
+ * recognised outside the body only, where a `#` is far more likely to be data.
17
+ *
18
+ * What it does not model: a method has to be uppercase, a body that is not JSON
19
+ * — form-encoded, XML, multipart — renders as plain text, and chunk sizes are
20
+ * not distinguished from the chunks around them.
21
+ */
22
+ /**
23
+ * The methods RFC 9110 defines. Doubles as the anchor for "a new message starts
24
+ * here", which is how a body knows where it ends.
25
+ */
26
+ const METHOD = 'GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS|TRACE|CONNECT';
27
+ /** `HTTP/1.1`, `HTTP/2` — scoped in three pieces so the version reads as a number. */
28
+ const VERSION = '(HTTP)(/)(\\d(?:\\.\\d)?)';
29
+ const http = {
30
+ name: 'http',
31
+ aliases: ['https'],
32
+ states: {
33
+ /** The head of a message: request or status line, then headers. */
34
+ root: {
35
+ rules: [
36
+ { match: '^[ \\t]*(?:#|//)[^\\n]*', scope: 'comment' },
37
+ // Request line. The version at the end of it is left to the rule below,
38
+ // so this only has to get as far as the target.
39
+ {
40
+ match: `^(${METHOD})([ \\t]+)`,
41
+ scope: ['keyword.control', null],
42
+ push: 'target',
43
+ },
44
+ // Status line. The reason phrase has no delimiter in front of it and
45
+ // runs to the end of the line, so it is claimed here rather than left
46
+ // to fall through as unscoped text.
47
+ {
48
+ match: `^${VERSION}([ \\t]+)(\\d{3})([ \\t]*)([^\\r\\n]*)`,
49
+ scope: ['keyword', 'punctuation.delimiter', 'number', null, 'number', null, 'constant'],
50
+ },
51
+ {
52
+ match: VERSION,
53
+ scope: ['keyword', 'punctuation.delimiter', 'number'],
54
+ },
55
+ // A header name, plus the whitespace around its colon so the value
56
+ // state starts on the value itself.
57
+ {
58
+ match: '^([A-Za-z][\\w-]*)([ \\t]*)(:)([ \\t]*)',
59
+ scope: ['property', null, 'punctuation.delimiter', null],
60
+ push: 'header-value',
61
+ },
62
+ // The blank line ends the headers. `set` rather than `push`: the head
63
+ // of this message is over and never resumes.
64
+ //
65
+ // Matched as two line breaks rather than as an empty line, because a
66
+ // real message uses CRLF and JavaScript treats a lone `\r` as a line
67
+ // terminator: `^` matches between the `\r` and the `\n`, so an anchored
68
+ // "empty line" pattern fires inside every CRLF pair.
69
+ { match: '\\r?\\n[ \\t]*\\r?\\n', set: 'body' },
70
+ ],
71
+ },
72
+ /**
73
+ * A request target. `link` covers the path; the query string is taken apart
74
+ * because that is where the interesting part of a documented request is.
75
+ */
76
+ target: {
77
+ default: 'link',
78
+ rules: [
79
+ // Anchored on the `?`/`&` that introduces the name, both so a bare word
80
+ // in a path cannot look like a parameter and so the scan is not retried
81
+ // at every column of the line. The name stops at the next separator as
82
+ // well as at the `=`, so a target that is nothing but `?` costs one
83
+ // character per `?` rather than a rescan of the rest of the line.
84
+ {
85
+ match: '([?&])([^?&=\\s]*)(=)',
86
+ scope: ['punctuation.delimiter', 'variable.parameter', 'operator'],
87
+ },
88
+ { match: '[?&#]', scope: 'punctuation.delimiter' },
89
+ // The target ends at the first space — what follows is the version, and
90
+ // the root state owns that. Zero width, so the space stays unscoped.
91
+ { match: '(?=[ \\t])', pop: true },
92
+ { match: '$', pop: true },
93
+ ],
94
+ },
95
+ /** A header value: everything after the colon, ending with the line. */
96
+ 'header-value': {
97
+ default: 'string',
98
+ rules: [
99
+ { match: '$', pop: true },
100
+ // `charset=utf-8`, `q=0.9`, `session=a3fWa`. The lookbehind keeps the
101
+ // name from being retried at every column of the line, and matches the
102
+ // space the header rule already consumed so the first parameter of a
103
+ // value counts too.
104
+ {
105
+ match: '(?<=[ \\t;,:])([\\w.-]+)(=)',
106
+ scope: ['variable.parameter', 'operator'],
107
+ },
108
+ { match: 'https?://[^\\s;,]+', scope: 'link' },
109
+ // A number has to stand alone: `348`, `:8080`, `q=0.9`. Held off by the
110
+ // characters a token is built from, so the `8` of `utf-8` and the digits
111
+ // buried in a JWT or an ETag stay part of the value.
112
+ { match: '(?<![\\w.-])\\d+(?:\\.\\d+)*(?![\\w-])', scope: 'number' },
113
+ { match: '[;,]', scope: 'punctuation.delimiter' },
114
+ ],
115
+ },
116
+ /**
117
+ * The body. JSON is highlighted as JSON; XML, form-encoded and plain bodies
118
+ * fall through unscoped rather than being guessed at, which is the quiet
119
+ * failure rather than the loud one.
120
+ */
121
+ body: {
122
+ rules: [
123
+ // One document holds several messages, so the body ends where the next
124
+ // one starts: a `###` separator, a status line, or a request line. None
125
+ // of the three can begin a line of JSON — a plain-text body that starts
126
+ // a line with `GET ` is read as a new message, which is the price of
127
+ // supporting multi-message documents at all.
128
+ { match: `^(?=###|HTTP/\\d|(?:${METHOD})[ \\t])`, set: 'root' },
129
+ // A comment introducing the next message also ends the body, but only
130
+ // when the message starts on the very next line. A bare `^#` exit
131
+ // would be wrong far more often than right — `#` is ordinary data in a
132
+ // plain-text or form-encoded body, and misreading one as a separator
133
+ // would hand the rest of the body to `root`.
134
+ //
135
+ // The lookahead spans exactly one line and holds no repetition group.
136
+ // An earlier version matched the whole run — `(?:…\n)+` — which reads
137
+ // better and is O(n²): it scans the run, fails on the tail, backtracks
138
+ // a line at a time, and is retried at every line start. A body of 64 KB
139
+ // of `#` lines (a Dockerfile, a `.env`, a `//`-licensed script) took
140
+ // 10.8 s. The seed table in `test/languages.test.ts` cannot reach this
141
+ // state, so nothing caught it.
142
+ //
143
+ // The cost of the narrower form: in a run of several comment lines only
144
+ // the last is scoped, since only it sits directly above the message.
145
+ {
146
+ // No `[ \t]*` before the message start: `root` anchors its request
147
+ // and status rules at column 0, so accepting an indented one here
148
+ // exits the body to a state that cannot parse what the lookahead
149
+ // matched, leaving the line unscoped and reading the line after it
150
+ // as a header.
151
+ match: `^(?=[ \\t]*(?://|#)[^\\n]*\\n(?:###|HTTP/\\d|(?:${METHOD})[ \\t]))`,
152
+ set: 'root',
153
+ },
154
+ // A string followed by `:` is a key, at any depth.
155
+ {
156
+ match: '("(?:[^"\\\\\\n]|\\\\.)*")([ \\t]*)(:)',
157
+ scope: ['property', null, 'punctuation.delimiter'],
158
+ },
159
+ { match: '"', scope: 'string', push: 'string' },
160
+ { match: '\\b(?:true|false)\\b', scope: 'boolean' },
161
+ { match: '\\bnull\\b', scope: 'constant.builtin' },
162
+ {
163
+ match: '-?\\b\\d+(?:\\.\\d+)?(?:[eE][-+]?\\d+)?\\b',
164
+ scope: 'number',
165
+ },
166
+ { match: '[{}\\[\\]]', scope: 'punctuation.bracket' },
167
+ { match: '[,:]', scope: 'punctuation.delimiter' },
168
+ ],
169
+ },
170
+ string: {
171
+ default: 'string',
172
+ rules: [
173
+ {
174
+ match: '\\\\(?:u[0-9a-fA-F]{4}|["\\\\/bfnrt])',
175
+ scope: 'string.escape',
176
+ },
177
+ { match: '"', scope: 'string', pop: true },
178
+ // A JSON string cannot hold a raw newline, so an unterminated quote in
179
+ // a truncated body stops at the line instead of eating the rest of it.
180
+ { match: '$', pop: true },
181
+ ],
182
+ },
183
+ },
184
+ };
185
+ export default http;
@@ -0,0 +1,4 @@
1
+ import type { Grammar } from '../core/types.js';
2
+ declare const ini: Grammar;
3
+ export default ini;
4
+ //# sourceMappingURL=ini.d.ts.map