@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.
- package/CHANGELOG.md +13 -0
- package/README.md +324 -0
- package/dist/all.d.ts +28 -0
- package/dist/all.d.ts.map +1 -0
- package/dist/all.js +105 -0
- package/dist/compat/hljs.d.ts +24 -0
- package/dist/compat/hljs.d.ts.map +1 -0
- package/dist/compat/hljs.js +95 -0
- package/dist/compat/index.d.ts +24 -0
- package/dist/compat/index.d.ts.map +1 -0
- package/dist/compat/index.js +21 -0
- package/dist/compat/languages.d.ts +46 -0
- package/dist/compat/languages.d.ts.map +1 -0
- package/dist/compat/languages.js +236 -0
- package/dist/compat/syntax-highlight.d.ts +37 -0
- package/dist/compat/syntax-highlight.d.ts.map +1 -0
- package/dist/compat/syntax-highlight.js +133 -0
- package/dist/core/compile.d.ts +3 -0
- package/dist/core/compile.d.ts.map +1 -0
- package/dist/core/compile.js +142 -0
- package/dist/core/index.d.ts +9 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +5 -0
- package/dist/core/registry.d.ts +14 -0
- package/dist/core/registry.d.ts.map +1 -0
- package/dist/core/registry.js +77 -0
- package/dist/core/render.d.ts +67 -0
- package/dist/core/render.d.ts.map +1 -0
- package/dist/core/render.js +216 -0
- package/dist/core/scopes.d.ts +94 -0
- package/dist/core/scopes.d.ts.map +1 -0
- package/dist/core/scopes.js +113 -0
- package/dist/core/tokenize.d.ts +18 -0
- package/dist/core/tokenize.d.ts.map +1 -0
- package/dist/core/tokenize.js +131 -0
- package/dist/core/types.d.ts +99 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +10 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/langs/bash.d.ts +4 -0
- package/dist/langs/bash.d.ts.map +1 -0
- package/dist/langs/bash.js +123 -0
- package/dist/langs/c.d.ts +4 -0
- package/dist/langs/c.d.ts.map +1 -0
- package/dist/langs/c.js +196 -0
- package/dist/langs/clojure.d.ts +4 -0
- package/dist/langs/clojure.d.ts.map +1 -0
- package/dist/langs/clojure.js +213 -0
- package/dist/langs/cpp.d.ts +4 -0
- package/dist/langs/cpp.d.ts.map +1 -0
- package/dist/langs/cpp.js +391 -0
- package/dist/langs/csharp.d.ts +4 -0
- package/dist/langs/csharp.d.ts.map +1 -0
- package/dist/langs/csharp.js +307 -0
- package/dist/langs/css.d.ts +4 -0
- package/dist/langs/css.d.ts.map +1 -0
- package/dist/langs/css.js +94 -0
- package/dist/langs/dart.d.ts +4 -0
- package/dist/langs/dart.d.ts.map +1 -0
- package/dist/langs/dart.js +262 -0
- package/dist/langs/diff.d.ts +10 -0
- package/dist/langs/diff.d.ts.map +1 -0
- package/dist/langs/diff.js +26 -0
- package/dist/langs/dockerfile.d.ts +4 -0
- package/dist/langs/dockerfile.d.ts.map +1 -0
- package/dist/langs/dockerfile.js +308 -0
- package/dist/langs/elixir.d.ts +4 -0
- package/dist/langs/elixir.d.ts.map +1 -0
- package/dist/langs/elixir.js +343 -0
- package/dist/langs/fsharp.d.ts +4 -0
- package/dist/langs/fsharp.d.ts.map +1 -0
- package/dist/langs/fsharp.js +330 -0
- package/dist/langs/go.d.ts +5 -0
- package/dist/langs/go.d.ts.map +1 -0
- package/dist/langs/go.js +90 -0
- package/dist/langs/graphql.d.ts +4 -0
- package/dist/langs/graphql.d.ts.map +1 -0
- package/dist/langs/graphql.js +185 -0
- package/dist/langs/haskell.d.ts +4 -0
- package/dist/langs/haskell.d.ts.map +1 -0
- package/dist/langs/haskell.js +375 -0
- package/dist/langs/html.d.ts +14 -0
- package/dist/langs/html.d.ts.map +1 -0
- package/dist/langs/html.js +71 -0
- package/dist/langs/http.d.ts +4 -0
- package/dist/langs/http.d.ts.map +1 -0
- package/dist/langs/http.js +185 -0
- package/dist/langs/ini.d.ts +4 -0
- package/dist/langs/ini.d.ts.map +1 -0
- package/dist/langs/ini.js +227 -0
- package/dist/langs/java.d.ts +4 -0
- package/dist/langs/java.d.ts.map +1 -0
- package/dist/langs/java.js +178 -0
- package/dist/langs/javascript.d.ts +4 -0
- package/dist/langs/javascript.d.ts.map +1 -0
- package/dist/langs/javascript.js +226 -0
- package/dist/langs/json.d.ts +10 -0
- package/dist/langs/json.d.ts.map +1 -0
- package/dist/langs/json.js +46 -0
- package/dist/langs/kotlin.d.ts +4 -0
- package/dist/langs/kotlin.d.ts.map +1 -0
- package/dist/langs/kotlin.js +220 -0
- package/dist/langs/lua.d.ts +4 -0
- package/dist/langs/lua.d.ts.map +1 -0
- package/dist/langs/lua.js +330 -0
- package/dist/langs/makefile.d.ts +4 -0
- package/dist/langs/makefile.d.ts.map +1 -0
- package/dist/langs/makefile.js +266 -0
- package/dist/langs/markdown.d.ts +11 -0
- package/dist/langs/markdown.d.ts.map +1 -0
- package/dist/langs/markdown.js +71 -0
- package/dist/langs/matlab.d.ts +4 -0
- package/dist/langs/matlab.d.ts.map +1 -0
- package/dist/langs/matlab.js +352 -0
- package/dist/langs/mojo.d.ts +4 -0
- package/dist/langs/mojo.d.ts.map +1 -0
- package/dist/langs/mojo.js +337 -0
- package/dist/langs/nginx.d.ts +4 -0
- package/dist/langs/nginx.d.ts.map +1 -0
- package/dist/langs/nginx.js +168 -0
- package/dist/langs/objectivec.d.ts +4 -0
- package/dist/langs/objectivec.d.ts.map +1 -0
- package/dist/langs/objectivec.js +287 -0
- package/dist/langs/ocaml.d.ts +4 -0
- package/dist/langs/ocaml.d.ts.map +1 -0
- package/dist/langs/ocaml.js +309 -0
- package/dist/langs/perl.d.ts +4 -0
- package/dist/langs/perl.d.ts.map +1 -0
- package/dist/langs/perl.js +435 -0
- package/dist/langs/php.d.ts +4 -0
- package/dist/langs/php.d.ts.map +1 -0
- package/dist/langs/php.js +325 -0
- package/dist/langs/powershell.d.ts +4 -0
- package/dist/langs/powershell.d.ts.map +1 -0
- package/dist/langs/powershell.js +308 -0
- package/dist/langs/python.d.ts +4 -0
- package/dist/langs/python.d.ts.map +1 -0
- package/dist/langs/python.js +350 -0
- package/dist/langs/r.d.ts +4 -0
- package/dist/langs/r.d.ts.map +1 -0
- package/dist/langs/r.js +265 -0
- package/dist/langs/ruby.d.ts +4 -0
- package/dist/langs/ruby.d.ts.map +1 -0
- package/dist/langs/ruby.js +365 -0
- package/dist/langs/rust.d.ts +10 -0
- package/dist/langs/rust.d.ts.map +1 -0
- package/dist/langs/rust.js +116 -0
- package/dist/langs/scala.d.ts +4 -0
- package/dist/langs/scala.d.ts.map +1 -0
- package/dist/langs/scala.js +308 -0
- package/dist/langs/sql.d.ts +5 -0
- package/dist/langs/sql.d.ts.map +1 -0
- package/dist/langs/sql.js +57 -0
- package/dist/langs/swift.d.ts +33 -0
- package/dist/langs/swift.d.ts.map +1 -0
- package/dist/langs/swift.js +256 -0
- package/dist/langs/yaml.d.ts +11 -0
- package/dist/langs/yaml.d.ts.map +1 -0
- package/dist/langs/yaml.js +123 -0
- package/dist/lazy.d.ts +69 -0
- package/dist/lazy.d.ts.map +1 -0
- package/dist/lazy.js +200 -0
- package/dist/style.css +308 -0
- package/package.json +86 -0
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Swift.
|
|
3
|
+
*
|
|
4
|
+
* The four things that make Swift awkward for a regex tokenizer, and what this
|
|
5
|
+
* grammar does about each:
|
|
6
|
+
*
|
|
7
|
+
* - Block comments nest. No single regex can count, so an opener pushes the
|
|
8
|
+
* comment state onto itself and a closer pops one level.
|
|
9
|
+
* - `\(…)` interpolations are real expressions and they nest, so the string
|
|
10
|
+
* states hand off to a paren-counting state rather than matching to the first
|
|
11
|
+
* `)`.
|
|
12
|
+
* - Raw strings change what an escape looks like — `\n` is two literal
|
|
13
|
+
* characters and `\#n` is the newline — which is a different state, not a
|
|
14
|
+
* flag on the ordinary one.
|
|
15
|
+
* - Argument labels are ordinary words in call position (`f(for: x)`,
|
|
16
|
+
* `f(in: y)`), so they are matched before the keyword lists and anchored to
|
|
17
|
+
* the `(` or `,` that introduces them.
|
|
18
|
+
*
|
|
19
|
+
* Identifiers are treated as ASCII. Swift allows most of Unicode in a name, but
|
|
20
|
+
* a non-ASCII name here simply renders unscoped rather than wrong, and the
|
|
21
|
+
* casing rules below — which is where the type/constant/call distinctions come
|
|
22
|
+
* from — only ever apply to the ASCII spelling anyway.
|
|
23
|
+
*
|
|
24
|
+
* Two readings are knowingly wrong, both where the shape is genuinely
|
|
25
|
+
* ambiguous: a trailing-closure call (`items.map { … }`) reads as a property
|
|
26
|
+
* access, because `if user.isActive { … }` is the same shape and more common;
|
|
27
|
+
* and an enum case with associated values (`case notFound(sku: String)`) reads
|
|
28
|
+
* as a call, which is what it looks like everywhere else it appears.
|
|
29
|
+
*/
|
|
30
|
+
const swift = {
|
|
31
|
+
name: 'swift',
|
|
32
|
+
states: {
|
|
33
|
+
root: { rules: [{ include: 'expression' }] },
|
|
34
|
+
expression: {
|
|
35
|
+
rules: [
|
|
36
|
+
{ include: 'comments' },
|
|
37
|
+
{ include: 'strings' },
|
|
38
|
+
// `@escaping`, `@MainActor`, `@available(iOS 15, *)`. The argument list
|
|
39
|
+
// is deliberately left as ordinary code so `iOS 15` still reads as one.
|
|
40
|
+
{ match: '@[A-Za-z_]\\w*', scope: 'decorator' },
|
|
41
|
+
// The `#if` / `#available` / `#selector` family. The raw-string rules
|
|
42
|
+
// run first, so a `#` that opens a string never reaches this.
|
|
43
|
+
{ match: '#[A-Za-z_]\\w*', scope: 'keyword' },
|
|
44
|
+
{ include: 'labels' },
|
|
45
|
+
// Matched at the dot — including the `?.` of an optional chain — so a
|
|
46
|
+
// property named `print` never reads as the global function.
|
|
47
|
+
{
|
|
48
|
+
match: '(\\??\\.)([A-Za-z_]\\w*)(?=\\s*\\()',
|
|
49
|
+
scope: ['punctuation', 'function.method'],
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
match: '(\\??\\.)([A-Za-z_]\\w*)',
|
|
53
|
+
scope: ['punctuation', 'variable.member'],
|
|
54
|
+
},
|
|
55
|
+
// Definition sites first: they claim the name before the keyword lists
|
|
56
|
+
// and the casing heuristics get to it.
|
|
57
|
+
{
|
|
58
|
+
match: '(func)(\\s+)([A-Za-z_]\\w*)',
|
|
59
|
+
scope: ['keyword.declaration', null, 'function'],
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
match: '(class|struct|enum|protocol|extension|actor)(\\s+)([A-Za-z_]\\w*)',
|
|
63
|
+
scope: ['keyword.declaration', null, 'class'],
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
match: '(import)(\\s+)([A-Za-z_][\\w.]*)',
|
|
67
|
+
scope: ['keyword.import', null, 'namespace'],
|
|
68
|
+
},
|
|
69
|
+
{ match: '\\bimport\\b', scope: 'keyword.import' },
|
|
70
|
+
{
|
|
71
|
+
match: '\\b(?:if|else|guard|switch|case|default|for|while|repeat|break|continue|return|fallthrough|throw|try|catch|defer|do|where|await)\\b',
|
|
72
|
+
scope: 'keyword.control',
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
match: '\\b(?:func|var|let|class|struct|enum|protocol|extension|actor|init|deinit|subscript|typealias|associatedtype|operator|precedencegroup|static|final|lazy|weak|unowned|mutating|nonmutating|override|open|public|internal|fileprivate|private|package|required|convenience|indirect|inout|dynamic|async|throws|rethrows|some|any|get|set|willSet|didSet)\\b',
|
|
76
|
+
scope: 'keyword.declaration',
|
|
77
|
+
},
|
|
78
|
+
{ match: '\\b(?:is|as|in)\\b', scope: 'keyword.operator' },
|
|
79
|
+
{ match: '\\b(?:true|false)\\b', scope: 'boolean' },
|
|
80
|
+
{ match: '\\bnil\\b', scope: 'constant.builtin' },
|
|
81
|
+
{ match: '\\b(?:self|Self|super)\\b', scope: 'variable.builtin' },
|
|
82
|
+
// Longest alternative first: `Any` would otherwise take the front of
|
|
83
|
+
// `AnyObject` and leave the rest to the casing rules.
|
|
84
|
+
{
|
|
85
|
+
match: '\\b(?:U?Int(?:8|16|32|64)?|Float|Double|Bool|String|Character|Array|Dictionary|Set|Optional|Result|AnyObject|AnyHashable|Any|Void|Never|Error)\\b',
|
|
86
|
+
scope: 'type.builtin',
|
|
87
|
+
},
|
|
88
|
+
// Guarded by the call lookahead, so `min` as a property name stays a
|
|
89
|
+
// property.
|
|
90
|
+
{
|
|
91
|
+
match: '\\b(?:print|debugPrint|dump|assert|assertionFailure|precondition|preconditionFailure|fatalError|abs|min|max|zip|stride|swap|type)\\b(?=\\s*\\()',
|
|
92
|
+
scope: 'function.builtin',
|
|
93
|
+
},
|
|
94
|
+
// SCREAMING_CASE reads as a constant and UpperCamelCase as a type,
|
|
95
|
+
// which covers the standard library without shipping a name list. An
|
|
96
|
+
// all-caps type such as `URL` reads as a constant, which is the price.
|
|
97
|
+
{ match: '\\b[A-Z][A-Z0-9_]+\\b', scope: 'constant' },
|
|
98
|
+
{ match: '\\b[A-Z]\\w*\\b', scope: 'type' },
|
|
99
|
+
{ match: '\\b[a-z_]\\w*(?=\\s*\\()', scope: 'function.call' },
|
|
100
|
+
// `$0` in a closure and `$binding` for a property wrapper's projected
|
|
101
|
+
// value: compiler-generated names rather than declared ones.
|
|
102
|
+
{ match: '\\$\\w+', scope: 'variable.special' },
|
|
103
|
+
{ include: 'numbers' },
|
|
104
|
+
{
|
|
105
|
+
match: '->|\\.\\.[.<]|\\?\\?|&&|\\|\\||[!=]==?|<<=?|>>=?|[-+*/%&|^]=?|[<>]=?|[?!~=]',
|
|
106
|
+
scope: 'operator',
|
|
107
|
+
},
|
|
108
|
+
{ match: '[()\\[\\]{}]', scope: 'punctuation.bracket' },
|
|
109
|
+
{ match: '[,;:]', scope: 'punctuation.delimiter' },
|
|
110
|
+
],
|
|
111
|
+
},
|
|
112
|
+
// ---- argument labels -----------------------------------------------------
|
|
113
|
+
// Anchored on the `(` or `,` in front of the label with a lookbehind rather
|
|
114
|
+
// than by consuming it: a `(` and a `,` want different punctuation scopes,
|
|
115
|
+
// and one captured group can only carry one of them.
|
|
116
|
+
labels: {
|
|
117
|
+
rules: [
|
|
118
|
+
// `func move(from source: Point)` — an external label and the internal
|
|
119
|
+
// name it binds to. Two words before the colon only happen in a
|
|
120
|
+
// declaration, so this never fires on a call.
|
|
121
|
+
{
|
|
122
|
+
match: '(?<=[(,])\\s*([a-z_]\\w*)(\\s+)([a-z_]\\w*)(?=\\s*:)',
|
|
123
|
+
scope: ['variable.parameter', null, 'variable.parameter'],
|
|
124
|
+
},
|
|
125
|
+
// `f(count: 1)` and `(x: Int)`. A ternary is spelled `a ? b : c` with
|
|
126
|
+
// spaces around the colon, so requiring the label to sit directly after
|
|
127
|
+
// the bracket keeps `(flag ? a : b)` out.
|
|
128
|
+
{
|
|
129
|
+
match: '(?<=[(,])\\s*([a-z_]\\w*)(?=\\s*:)',
|
|
130
|
+
scope: ['variable.parameter'],
|
|
131
|
+
},
|
|
132
|
+
],
|
|
133
|
+
},
|
|
134
|
+
// ---- comments ------------------------------------------------------------
|
|
135
|
+
comments: {
|
|
136
|
+
rules: [
|
|
137
|
+
{ match: '///[^\\n]*', scope: 'comment.doc' },
|
|
138
|
+
{ match: '//[^\\n]*', scope: 'comment' },
|
|
139
|
+
// `(?![/*])` so the empty comment `/**/` opens a plain block instead of
|
|
140
|
+
// a doc block that the same `/` has to close.
|
|
141
|
+
{ match: '/\\*\\*(?![/*])', scope: 'comment.doc', push: 'comment-doc' },
|
|
142
|
+
{ match: '/\\*', scope: 'comment', push: 'comment-block' },
|
|
143
|
+
],
|
|
144
|
+
},
|
|
145
|
+
'comment-block': {
|
|
146
|
+
default: 'comment',
|
|
147
|
+
rules: [
|
|
148
|
+
{ match: '/\\*', push: 'comment-block' },
|
|
149
|
+
{ match: '\\*/', pop: true },
|
|
150
|
+
],
|
|
151
|
+
},
|
|
152
|
+
/** A doc block stays documentation all the way down, nesting included. */
|
|
153
|
+
'comment-doc': {
|
|
154
|
+
default: 'comment.doc',
|
|
155
|
+
rules: [
|
|
156
|
+
{ match: '/\\*', push: 'comment-doc' },
|
|
157
|
+
{ match: '\\*/', pop: true },
|
|
158
|
+
],
|
|
159
|
+
},
|
|
160
|
+
// ---- strings -------------------------------------------------------------
|
|
161
|
+
// The `#` of a raw string is scoped separately, the way a prefix reads as a
|
|
162
|
+
// modifier on the literal rather than as part of it.
|
|
163
|
+
strings: {
|
|
164
|
+
rules: [
|
|
165
|
+
{
|
|
166
|
+
match: '(#)(""")',
|
|
167
|
+
scope: ['string.special', 'string'],
|
|
168
|
+
push: 'raw-multiline',
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
match: '(#)(")',
|
|
172
|
+
scope: ['string.special', 'string'],
|
|
173
|
+
push: 'raw-string',
|
|
174
|
+
},
|
|
175
|
+
{ match: '"""', scope: 'string', push: 'multiline' },
|
|
176
|
+
{ match: '"', scope: 'string', push: 'string' },
|
|
177
|
+
],
|
|
178
|
+
},
|
|
179
|
+
/** Escape and interpolation rules shared by both ordinary string states. */
|
|
180
|
+
'string-body': {
|
|
181
|
+
rules: [
|
|
182
|
+
// `\(` opens an interpolation and every other backslash pair is inert,
|
|
183
|
+
// so the interpolation has to be tried first.
|
|
184
|
+
{ match: '\\\\\\(', scope: 'interpolation', push: 'interpolation' },
|
|
185
|
+
{ match: '\\\\(?:u\\{[0-9a-fA-F]{1,8}\\}|.)', scope: 'string.escape' },
|
|
186
|
+
],
|
|
187
|
+
},
|
|
188
|
+
string: {
|
|
189
|
+
default: 'string',
|
|
190
|
+
rules: [
|
|
191
|
+
{ include: 'string-body' },
|
|
192
|
+
{ match: '"', pop: true },
|
|
193
|
+
// An unterminated single-line string ends at the line break instead of
|
|
194
|
+
// swallowing the rest of the file.
|
|
195
|
+
{ match: '$', pop: true },
|
|
196
|
+
],
|
|
197
|
+
},
|
|
198
|
+
multiline: {
|
|
199
|
+
default: 'string',
|
|
200
|
+
rules: [{ include: 'string-body' }, { match: '"""', pop: true }],
|
|
201
|
+
},
|
|
202
|
+
/** In a raw string the escape marker is `\#`; a lone `\` is literal text. */
|
|
203
|
+
'raw-body': {
|
|
204
|
+
rules: [
|
|
205
|
+
{ match: '\\\\#\\(', scope: 'interpolation', push: 'interpolation' },
|
|
206
|
+
{ match: '\\\\#(?:u\\{[0-9a-fA-F]{1,8}\\}|.)', scope: 'string.escape' },
|
|
207
|
+
],
|
|
208
|
+
},
|
|
209
|
+
'raw-string': {
|
|
210
|
+
default: 'string',
|
|
211
|
+
rules: [
|
|
212
|
+
{ include: 'raw-body' },
|
|
213
|
+
// A bare `"` is ordinary text in a raw string; only `"#` closes it.
|
|
214
|
+
{ match: '(")(#)', scope: ['string', 'string.special'], pop: true },
|
|
215
|
+
{ match: '$', pop: true },
|
|
216
|
+
],
|
|
217
|
+
},
|
|
218
|
+
'raw-multiline': {
|
|
219
|
+
default: 'string',
|
|
220
|
+
rules: [{ include: 'raw-body' }, { match: '(""")(#)', scope: ['string', 'string.special'], pop: true }],
|
|
221
|
+
},
|
|
222
|
+
/** `\(…)`, closed by the `)` that balances the one the escape opened. */
|
|
223
|
+
interpolation: {
|
|
224
|
+
rules: [
|
|
225
|
+
{ match: '\\)', scope: 'interpolation', pop: true },
|
|
226
|
+
{ match: '\\(', scope: 'punctuation.bracket', push: 'paren' },
|
|
227
|
+
{ include: 'expression' },
|
|
228
|
+
],
|
|
229
|
+
},
|
|
230
|
+
/** Depth tracking, so `"\(f(1))"` does not end at the inner `)`. */
|
|
231
|
+
paren: {
|
|
232
|
+
rules: [
|
|
233
|
+
{ match: '\\)', scope: 'punctuation.bracket', pop: true },
|
|
234
|
+
{ match: '\\(', scope: 'punctuation.bracket', push: 'paren' },
|
|
235
|
+
{ include: 'expression' },
|
|
236
|
+
],
|
|
237
|
+
},
|
|
238
|
+
// ---- numbers -------------------------------------------------------------
|
|
239
|
+
numbers: {
|
|
240
|
+
rules: [
|
|
241
|
+
// Hex, optionally a hex fraction and a binary exponent: `0xFF`, `0x1p-8`.
|
|
242
|
+
{
|
|
243
|
+
match: '\\b0[xX][0-9a-fA-F_]+(?:\\.[0-9a-fA-F_]+)?(?:[pP][-+]?\\d[\\d_]*)?\\b',
|
|
244
|
+
scope: 'number',
|
|
245
|
+
},
|
|
246
|
+
{ match: '\\b0[bB][01_]+\\b', scope: 'number' },
|
|
247
|
+
{ match: '\\b0[oO][0-7_]+\\b', scope: 'number' },
|
|
248
|
+
{
|
|
249
|
+
match: '\\b\\d[\\d_]*(?:\\.\\d[\\d_]*)?(?:[eE][-+]?\\d[\\d_]*)?\\b',
|
|
250
|
+
scope: 'number',
|
|
251
|
+
},
|
|
252
|
+
],
|
|
253
|
+
},
|
|
254
|
+
},
|
|
255
|
+
};
|
|
256
|
+
export default swift;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Grammar } from '../core/types.js';
|
|
2
|
+
/**
|
|
3
|
+
* YAML.
|
|
4
|
+
*
|
|
5
|
+
* Keys are the load-bearing thing in a YAML file, so they are matched from the
|
|
6
|
+
* line start and scoped separately from their values. Block scalars (`|`, `>`)
|
|
7
|
+
* switch into a literal state so their contents are not re-parsed as YAML.
|
|
8
|
+
*/
|
|
9
|
+
declare const yaml: Grammar;
|
|
10
|
+
export default yaml;
|
|
11
|
+
//# sourceMappingURL=yaml.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"yaml.d.ts","sourceRoot":"","sources":["../../src/langs/yaml.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAE5C;;;;;;GAMG;AACH,QAAA,MAAM,IAAI,EAAE,OAmIX,CAAA;AAED,eAAe,IAAI,CAAA"}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* YAML.
|
|
3
|
+
*
|
|
4
|
+
* Keys are the load-bearing thing in a YAML file, so they are matched from the
|
|
5
|
+
* line start and scoped separately from their values. Block scalars (`|`, `>`)
|
|
6
|
+
* switch into a literal state so their contents are not re-parsed as YAML.
|
|
7
|
+
*/
|
|
8
|
+
const yaml = {
|
|
9
|
+
name: 'yaml',
|
|
10
|
+
aliases: ['yml'],
|
|
11
|
+
states: {
|
|
12
|
+
root: {
|
|
13
|
+
rules: [
|
|
14
|
+
{ match: '#[^\\n]*', scope: 'comment' },
|
|
15
|
+
{ match: '^(?:---|\\.\\.\\.)$', scope: 'punctuation.delimiter' },
|
|
16
|
+
{
|
|
17
|
+
match: '^([ \\t]*)(-)(?=[ \\t]|$)',
|
|
18
|
+
scope: [null, 'punctuation.delimiter'],
|
|
19
|
+
push: 'value',
|
|
20
|
+
},
|
|
21
|
+
// `key:` — quoted or bare, optionally after a list dash.
|
|
22
|
+
//
|
|
23
|
+
// The bare form is length-capped rather than open-ended. It is tried at
|
|
24
|
+
// every column, and an uncapped lazy scan runs to the end of the line
|
|
25
|
+
// before failing on a line with no `:`, which is quadratic in line
|
|
26
|
+
// length — a 20 KB single-line value is seconds of blocking. No real
|
|
27
|
+
// key approaches the cap; past it the text simply renders unscoped.
|
|
28
|
+
{
|
|
29
|
+
match: '((?:"[^"\\n]*"|\'[^\'\\n]*\'|[^\\s#:][^:\\n]{0,256}?))([ \\t]*)(:)(?=[ \\t]|$)',
|
|
30
|
+
scope: ['property', null, 'punctuation.delimiter'],
|
|
31
|
+
push: 'value',
|
|
32
|
+
},
|
|
33
|
+
{ match: '[&*][\\w-]+', scope: 'variable.special' },
|
|
34
|
+
{ match: '!!?[\\w/-]+', scope: 'type' },
|
|
35
|
+
{
|
|
36
|
+
match: '([|>][-+]?)([ \\t]*)$',
|
|
37
|
+
scope: ['operator', null],
|
|
38
|
+
push: 'block-scalar',
|
|
39
|
+
},
|
|
40
|
+
{ match: '"(?:[^"\\\\\\n]|\\\\.)*"', scope: 'string' },
|
|
41
|
+
{ match: "'(?:[^'\\n]|'')*'", scope: 'string' },
|
|
42
|
+
{
|
|
43
|
+
match: '\\b(?:true|false|yes|no|on|off|True|False|Yes|No|On|Off)\\b',
|
|
44
|
+
scope: 'boolean',
|
|
45
|
+
},
|
|
46
|
+
{ match: '\\b(?:null|Null|NULL)\\b|~', scope: 'constant.builtin' },
|
|
47
|
+
{
|
|
48
|
+
match: '(?:^|[\\s,\\[{])(-?\\d+(?:\\.\\d+)?(?:[eE][-+]?\\d+)?)\\b',
|
|
49
|
+
scope: ['number'],
|
|
50
|
+
},
|
|
51
|
+
{ match: '[\\[\\]{}]', scope: 'punctuation.bracket' },
|
|
52
|
+
{ match: ',', scope: 'punctuation.delimiter' },
|
|
53
|
+
],
|
|
54
|
+
},
|
|
55
|
+
/**
|
|
56
|
+
* Everything to the right of a `key:` or a list `-`.
|
|
57
|
+
*
|
|
58
|
+
* A plain (unquoted) scalar is still a string, and in a real document —
|
|
59
|
+
* an OpenAPI description, a CI workflow — it is most of the file. Scoping
|
|
60
|
+
* it needs to know it sits in value position, which is what this state is
|
|
61
|
+
* for: `on:` is a key, `on` after a colon is a string.
|
|
62
|
+
*
|
|
63
|
+
* The state lasts one line. `$` pops it, so a nested block on the next
|
|
64
|
+
* line is read as keys again.
|
|
65
|
+
*/
|
|
66
|
+
value: {
|
|
67
|
+
rules: [
|
|
68
|
+
{ match: '#[^\\n]*', scope: 'comment' },
|
|
69
|
+
{
|
|
70
|
+
match: '([|>][-+]?)([ \\t]*)$',
|
|
71
|
+
scope: ['operator', null],
|
|
72
|
+
set: 'block-scalar',
|
|
73
|
+
},
|
|
74
|
+
// `- uses: actions/checkout@v4` reaches here through the dash, so the
|
|
75
|
+
// key form has to be recognised in value position too.
|
|
76
|
+
{
|
|
77
|
+
match: '((?:"[^"\\n]*"|\'[^\'\\n]*\'|[^\\s#:][^:\\n]{0,256}?))([ \\t]*)(:)(?=[ \\t]|$)',
|
|
78
|
+
scope: ['property', null, 'punctuation.delimiter'],
|
|
79
|
+
},
|
|
80
|
+
{ match: '"(?:[^"\\\\\\n]|\\\\.)*"', scope: 'string' },
|
|
81
|
+
{ match: "'(?:[^'\\n]|'')*'", scope: 'string' },
|
|
82
|
+
{ match: '[&*][\\w-]+', scope: 'variable.special' },
|
|
83
|
+
{ match: '!!?[\\w/-]+', scope: 'type' },
|
|
84
|
+
// Anchored to a value boundary so `no-cache` stays one plain scalar
|
|
85
|
+
// rather than the boolean `no` followed by `-cache`.
|
|
86
|
+
{
|
|
87
|
+
match: '(?:true|false|yes|no|on|off|True|False|Yes|No|On|Off)(?=[ \\t]*(?:[,\\]}]|#|$))',
|
|
88
|
+
scope: 'boolean',
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
match: '(?:null|Null|NULL|~)(?=[ \\t]*(?:[,\\]}]|#|$))',
|
|
92
|
+
scope: 'constant.builtin',
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
match: '-?\\d+(?:\\.\\d+)?(?:[eE][-+]?\\d+)?(?=[ \\t]*(?:[,\\]}]|#|$))',
|
|
96
|
+
scope: 'number',
|
|
97
|
+
},
|
|
98
|
+
{ match: '[\\[\\]{}]', scope: 'punctuation.bracket' },
|
|
99
|
+
{ match: ',', scope: 'punctuation.delimiter' },
|
|
100
|
+
// The plain scalar itself. Greedy and unanchored on the right rather
|
|
101
|
+
// than a capped lazy scan: a lazy `{0,n}?` here is retried at every
|
|
102
|
+
// column and is what makes a long single-line value quadratic.
|
|
103
|
+
{ match: '[^\\s#,\\[\\]{}][^,\\[\\]{}\\n#]*', scope: 'string' },
|
|
104
|
+
{ match: '$', pop: true },
|
|
105
|
+
],
|
|
106
|
+
},
|
|
107
|
+
/**
|
|
108
|
+
* A block scalar runs until a line indented no further than its
|
|
109
|
+
* introducer. Approximated as "until a line that starts a new key", which
|
|
110
|
+
* is what the content of a real document looks like.
|
|
111
|
+
*/
|
|
112
|
+
'block-scalar': {
|
|
113
|
+
default: 'string',
|
|
114
|
+
rules: [
|
|
115
|
+
{
|
|
116
|
+
match: '^(?=[ \\t]*(?:[^\\s#-][^:\\n]*:(?:[ \\t]|$)|-{3}|\\.{3}))',
|
|
117
|
+
pop: true,
|
|
118
|
+
},
|
|
119
|
+
],
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
export default yaml;
|
package/dist/lazy.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { CompiledGrammar } from './core/types.js';
|
|
2
|
+
/**
|
|
3
|
+
* On-demand language loading.
|
|
4
|
+
*
|
|
5
|
+
* Every entry is a bare `import()` of a static path, which is the form
|
|
6
|
+
* bundlers recognize: each language becomes its own chunk, and a page that
|
|
7
|
+
* only ever highlights Python downloads only Python.
|
|
8
|
+
*
|
|
9
|
+
* Import this module only if you want that. `import python from
|
|
10
|
+
* "@scalar/highlight/langs/python"` is the static equivalent and pulls in nothing
|
|
11
|
+
* else.
|
|
12
|
+
*/
|
|
13
|
+
declare const loaders: {
|
|
14
|
+
bash: () => Promise<typeof import("./langs/bash.js")>;
|
|
15
|
+
c: () => Promise<typeof import("./langs/c.js")>;
|
|
16
|
+
clojure: () => Promise<typeof import("./langs/clojure.js")>;
|
|
17
|
+
cpp: () => Promise<typeof import("./langs/cpp.js")>;
|
|
18
|
+
csharp: () => Promise<typeof import("./langs/csharp.js")>;
|
|
19
|
+
css: () => Promise<typeof import("./langs/css.js")>;
|
|
20
|
+
dart: () => Promise<typeof import("./langs/dart.js")>;
|
|
21
|
+
diff: () => Promise<typeof import("./langs/diff.js")>;
|
|
22
|
+
dockerfile: () => Promise<typeof import("./langs/dockerfile.js")>;
|
|
23
|
+
elixir: () => Promise<typeof import("./langs/elixir.js")>;
|
|
24
|
+
fsharp: () => Promise<typeof import("./langs/fsharp.js")>;
|
|
25
|
+
go: () => Promise<typeof import("./langs/go.js")>;
|
|
26
|
+
graphql: () => Promise<typeof import("./langs/graphql.js")>;
|
|
27
|
+
haskell: () => Promise<typeof import("./langs/haskell.js")>;
|
|
28
|
+
html: () => Promise<typeof import("./langs/html.js")>;
|
|
29
|
+
http: () => Promise<typeof import("./langs/http.js")>;
|
|
30
|
+
ini: () => Promise<typeof import("./langs/ini.js")>;
|
|
31
|
+
java: () => Promise<typeof import("./langs/java.js")>;
|
|
32
|
+
javascript: () => Promise<typeof import("./langs/javascript.js")>;
|
|
33
|
+
json: () => Promise<typeof import("./langs/json.js")>;
|
|
34
|
+
kotlin: () => Promise<typeof import("./langs/kotlin.js")>;
|
|
35
|
+
lua: () => Promise<typeof import("./langs/lua.js")>;
|
|
36
|
+
makefile: () => Promise<typeof import("./langs/makefile.js")>;
|
|
37
|
+
markdown: () => Promise<typeof import("./langs/markdown.js")>;
|
|
38
|
+
matlab: () => Promise<typeof import("./langs/matlab.js")>;
|
|
39
|
+
mojo: () => Promise<typeof import("./langs/mojo.js")>;
|
|
40
|
+
nginx: () => Promise<typeof import("./langs/nginx.js")>;
|
|
41
|
+
objectivec: () => Promise<typeof import("./langs/objectivec.js")>;
|
|
42
|
+
ocaml: () => Promise<typeof import("./langs/ocaml.js")>;
|
|
43
|
+
perl: () => Promise<typeof import("./langs/perl.js")>;
|
|
44
|
+
php: () => Promise<typeof import("./langs/php.js")>;
|
|
45
|
+
powershell: () => Promise<typeof import("./langs/powershell.js")>;
|
|
46
|
+
python: () => Promise<typeof import("./langs/python.js")>;
|
|
47
|
+
r: () => Promise<typeof import("./langs/r.js")>;
|
|
48
|
+
ruby: () => Promise<typeof import("./langs/ruby.js")>;
|
|
49
|
+
rust: () => Promise<typeof import("./langs/rust.js")>;
|
|
50
|
+
scala: () => Promise<typeof import("./langs/scala.js")>;
|
|
51
|
+
sql: () => Promise<typeof import("./langs/sql.js")>;
|
|
52
|
+
swift: () => Promise<typeof import("./langs/swift.js")>;
|
|
53
|
+
yaml: () => Promise<typeof import("./langs/yaml.js")>;
|
|
54
|
+
};
|
|
55
|
+
export type BundledLanguage = keyof typeof loaders;
|
|
56
|
+
export declare const bundledLanguages: BundledLanguage[];
|
|
57
|
+
/** Resolves a name or alias to a bundled language, or `undefined`. */
|
|
58
|
+
export declare const resolveLanguageName: (name: string) => BundledLanguage | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* Loads and registers a language, returning its compiled grammar.
|
|
61
|
+
*
|
|
62
|
+
* Concurrent calls for the same language share one import, and an already
|
|
63
|
+
* registered language resolves without touching the network.
|
|
64
|
+
*/
|
|
65
|
+
export declare const loadLanguage: (name: string) => Promise<CompiledGrammar>;
|
|
66
|
+
/** Loads several languages at once. */
|
|
67
|
+
export declare const loadLanguages: (...names: string[]) => Promise<CompiledGrammar[]>;
|
|
68
|
+
export {};
|
|
69
|
+
//# sourceMappingURL=lazy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lazy.d.ts","sourceRoot":"","sources":["../src/lazy.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAEnD;;;;;;;;;;GAUG;AACH,QAAA,MAAM,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAyCkD,CAAA;AAE/D,MAAM,MAAM,eAAe,GAAG,MAAM,OAAO,OAAO,CAAA;AAqGlD,eAAO,MAAM,gBAAgB,EAA2B,eAAe,EAAE,CAAA;AAEzE,sEAAsE;AACtE,eAAO,MAAM,mBAAmB,GAAI,MAAM,MAAM,KAAG,eAAe,GAAG,SAOpE,CAAA;AAID;;;;;GAKG;AACH,eAAO,MAAM,YAAY,GAAI,MAAM,MAAM,KAAG,OAAO,CAAC,eAAe,CA0BlE,CAAA;AAED,uCAAuC;AACvC,eAAO,MAAM,aAAa,GAAI,GAAG,OAAO,MAAM,EAAE,KAAG,OAAO,CAAC,eAAe,EAAE,CAE3E,CAAA"}
|
package/dist/lazy.js
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { getLanguage, registerLanguage } from './core/registry.js';
|
|
2
|
+
/**
|
|
3
|
+
* On-demand language loading.
|
|
4
|
+
*
|
|
5
|
+
* Every entry is a bare `import()` of a static path, which is the form
|
|
6
|
+
* bundlers recognize: each language becomes its own chunk, and a page that
|
|
7
|
+
* only ever highlights Python downloads only Python.
|
|
8
|
+
*
|
|
9
|
+
* Import this module only if you want that. `import python from
|
|
10
|
+
* "@scalar/highlight/langs/python"` is the static equivalent and pulls in nothing
|
|
11
|
+
* else.
|
|
12
|
+
*/
|
|
13
|
+
const loaders = {
|
|
14
|
+
bash: () => import('./langs/bash.js'),
|
|
15
|
+
c: () => import('./langs/c.js'),
|
|
16
|
+
clojure: () => import('./langs/clojure.js'),
|
|
17
|
+
cpp: () => import('./langs/cpp.js'),
|
|
18
|
+
csharp: () => import('./langs/csharp.js'),
|
|
19
|
+
css: () => import('./langs/css.js'),
|
|
20
|
+
dart: () => import('./langs/dart.js'),
|
|
21
|
+
diff: () => import('./langs/diff.js'),
|
|
22
|
+
dockerfile: () => import('./langs/dockerfile.js'),
|
|
23
|
+
elixir: () => import('./langs/elixir.js'),
|
|
24
|
+
fsharp: () => import('./langs/fsharp.js'),
|
|
25
|
+
go: () => import('./langs/go.js'),
|
|
26
|
+
graphql: () => import('./langs/graphql.js'),
|
|
27
|
+
haskell: () => import('./langs/haskell.js'),
|
|
28
|
+
html: () => import('./langs/html.js'),
|
|
29
|
+
http: () => import('./langs/http.js'),
|
|
30
|
+
ini: () => import('./langs/ini.js'),
|
|
31
|
+
java: () => import('./langs/java.js'),
|
|
32
|
+
javascript: () => import('./langs/javascript.js'),
|
|
33
|
+
json: () => import('./langs/json.js'),
|
|
34
|
+
kotlin: () => import('./langs/kotlin.js'),
|
|
35
|
+
lua: () => import('./langs/lua.js'),
|
|
36
|
+
makefile: () => import('./langs/makefile.js'),
|
|
37
|
+
markdown: () => import('./langs/markdown.js'),
|
|
38
|
+
matlab: () => import('./langs/matlab.js'),
|
|
39
|
+
mojo: () => import('./langs/mojo.js'),
|
|
40
|
+
nginx: () => import('./langs/nginx.js'),
|
|
41
|
+
objectivec: () => import('./langs/objectivec.js'),
|
|
42
|
+
ocaml: () => import('./langs/ocaml.js'),
|
|
43
|
+
perl: () => import('./langs/perl.js'),
|
|
44
|
+
php: () => import('./langs/php.js'),
|
|
45
|
+
powershell: () => import('./langs/powershell.js'),
|
|
46
|
+
python: () => import('./langs/python.js'),
|
|
47
|
+
r: () => import('./langs/r.js'),
|
|
48
|
+
ruby: () => import('./langs/ruby.js'),
|
|
49
|
+
rust: () => import('./langs/rust.js'),
|
|
50
|
+
scala: () => import('./langs/scala.js'),
|
|
51
|
+
sql: () => import('./langs/sql.js'),
|
|
52
|
+
swift: () => import('./langs/swift.js'),
|
|
53
|
+
yaml: () => import('./langs/yaml.js'),
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Aliases, so `loadLanguage('tsx')` finds the JavaScript grammar without
|
|
57
|
+
* loading every module to ask each one what it answers to.
|
|
58
|
+
*/
|
|
59
|
+
const aliases = {
|
|
60
|
+
bsdmake: 'makefile',
|
|
61
|
+
'c#': 'csharp',
|
|
62
|
+
'c++': 'cpp',
|
|
63
|
+
cc: 'cpp',
|
|
64
|
+
cfg: 'ini',
|
|
65
|
+
cjs: 'javascript',
|
|
66
|
+
clj: 'clojure',
|
|
67
|
+
cljc: 'clojure',
|
|
68
|
+
cljs: 'clojure',
|
|
69
|
+
conf: 'ini',
|
|
70
|
+
console: 'bash',
|
|
71
|
+
containerfile: 'dockerfile',
|
|
72
|
+
cppm: 'cpp',
|
|
73
|
+
cs: 'csharp',
|
|
74
|
+
curl: 'bash',
|
|
75
|
+
cxx: 'cpp',
|
|
76
|
+
docker: 'dockerfile',
|
|
77
|
+
edn: 'clojure',
|
|
78
|
+
ex: 'elixir',
|
|
79
|
+
exs: 'elixir',
|
|
80
|
+
'f#': 'fsharp',
|
|
81
|
+
fs: 'fsharp',
|
|
82
|
+
fsi: 'fsharp',
|
|
83
|
+
fsx: 'fsharp',
|
|
84
|
+
gemspec: 'ruby',
|
|
85
|
+
golang: 'go',
|
|
86
|
+
gql: 'graphql',
|
|
87
|
+
h: 'c',
|
|
88
|
+
'h++': 'cpp',
|
|
89
|
+
hpp: 'cpp',
|
|
90
|
+
hs: 'haskell',
|
|
91
|
+
https: 'http',
|
|
92
|
+
hxx: 'cpp',
|
|
93
|
+
js: 'javascript',
|
|
94
|
+
json5: 'json',
|
|
95
|
+
jsonc: 'json',
|
|
96
|
+
jsx: 'javascript',
|
|
97
|
+
kt: 'kotlin',
|
|
98
|
+
kts: 'kotlin',
|
|
99
|
+
less: 'css',
|
|
100
|
+
m: 'matlab',
|
|
101
|
+
make: 'makefile',
|
|
102
|
+
md: 'markdown',
|
|
103
|
+
mdx: 'markdown',
|
|
104
|
+
mjs: 'javascript',
|
|
105
|
+
mk: 'makefile',
|
|
106
|
+
ml: 'ocaml',
|
|
107
|
+
mli: 'ocaml',
|
|
108
|
+
mysql: 'sql',
|
|
109
|
+
'nginx-conf': 'nginx',
|
|
110
|
+
nginxconf: 'nginx',
|
|
111
|
+
node: 'javascript',
|
|
112
|
+
'obj-c': 'objectivec',
|
|
113
|
+
objc: 'objectivec',
|
|
114
|
+
'objective-c': 'objectivec',
|
|
115
|
+
octave: 'matlab',
|
|
116
|
+
patch: 'diff',
|
|
117
|
+
php3: 'php',
|
|
118
|
+
php4: 'php',
|
|
119
|
+
php5: 'php',
|
|
120
|
+
php8: 'php',
|
|
121
|
+
pl: 'perl',
|
|
122
|
+
pm: 'perl',
|
|
123
|
+
posh: 'powershell',
|
|
124
|
+
postgres: 'sql',
|
|
125
|
+
postgresql: 'sql',
|
|
126
|
+
properties: 'ini',
|
|
127
|
+
ps: 'powershell',
|
|
128
|
+
ps1: 'powershell',
|
|
129
|
+
pwsh: 'powershell',
|
|
130
|
+
py: 'python',
|
|
131
|
+
py3: 'python',
|
|
132
|
+
python3: 'python',
|
|
133
|
+
rake: 'ruby',
|
|
134
|
+
rb: 'ruby',
|
|
135
|
+
rs: 'rust',
|
|
136
|
+
sbt: 'scala',
|
|
137
|
+
sc: 'scala',
|
|
138
|
+
scss: 'css',
|
|
139
|
+
sh: 'bash',
|
|
140
|
+
shell: 'bash',
|
|
141
|
+
sqlite: 'sql',
|
|
142
|
+
svg: 'html',
|
|
143
|
+
toml: 'ini',
|
|
144
|
+
ts: 'javascript',
|
|
145
|
+
tsx: 'javascript',
|
|
146
|
+
typescript: 'javascript',
|
|
147
|
+
vue: 'html',
|
|
148
|
+
xml: 'html',
|
|
149
|
+
yml: 'yaml',
|
|
150
|
+
zsh: 'bash',
|
|
151
|
+
'🔥': 'mojo',
|
|
152
|
+
};
|
|
153
|
+
export const bundledLanguages = Object.keys(loaders);
|
|
154
|
+
/** Resolves a name or alias to a bundled language, or `undefined`. */
|
|
155
|
+
export const resolveLanguageName = (name) => {
|
|
156
|
+
const key = name.toLowerCase();
|
|
157
|
+
// `hasOwn` rather than `in`: `in` walks the prototype chain, so a caller
|
|
158
|
+
// asking for `constructor` would get past this check and then try to call
|
|
159
|
+
// `Object.prototype.constructor` as a loader.
|
|
160
|
+
if (Object.hasOwn(loaders, key))
|
|
161
|
+
return key;
|
|
162
|
+
return Object.hasOwn(aliases, key) ? aliases[key] : undefined;
|
|
163
|
+
};
|
|
164
|
+
const pending = new Map();
|
|
165
|
+
/**
|
|
166
|
+
* Loads and registers a language, returning its compiled grammar.
|
|
167
|
+
*
|
|
168
|
+
* Concurrent calls for the same language share one import, and an already
|
|
169
|
+
* registered language resolves without touching the network.
|
|
170
|
+
*/
|
|
171
|
+
export const loadLanguage = (name) => {
|
|
172
|
+
const resolved = resolveLanguageName(name);
|
|
173
|
+
if (!resolved) {
|
|
174
|
+
return Promise.reject(new Error(`Unknown language "${name}". Available: ${bundledLanguages.join(', ')}.`));
|
|
175
|
+
}
|
|
176
|
+
const registered = getLanguage(resolved);
|
|
177
|
+
if (registered)
|
|
178
|
+
return Promise.resolve(registered);
|
|
179
|
+
let inFlight = pending.get(resolved);
|
|
180
|
+
if (!inFlight) {
|
|
181
|
+
inFlight = loaders[resolved]()
|
|
182
|
+
.then((module) => {
|
|
183
|
+
registerLanguage(module.default);
|
|
184
|
+
return getLanguage(resolved);
|
|
185
|
+
})
|
|
186
|
+
.catch((error) => {
|
|
187
|
+
// Forget a failed import. A chunk request that lost the network would
|
|
188
|
+
// otherwise leave the rejected promise cached, and every later call for
|
|
189
|
+
// that language would replay the failure instead of retrying.
|
|
190
|
+
pending.delete(resolved);
|
|
191
|
+
throw error;
|
|
192
|
+
});
|
|
193
|
+
pending.set(resolved, inFlight);
|
|
194
|
+
}
|
|
195
|
+
return inFlight;
|
|
196
|
+
};
|
|
197
|
+
/** Loads several languages at once. */
|
|
198
|
+
export const loadLanguages = (...names) => {
|
|
199
|
+
return Promise.all(names.map(loadLanguage));
|
|
200
|
+
};
|