@scalar/highlight 0.3.1 → 0.4.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 (61) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/README.md +52 -13
  3. package/dist/all.d.ts.map +1 -1
  4. package/dist/all.js +2 -0
  5. package/dist/compat/languages.d.ts.map +1 -1
  6. package/dist/compat/languages.js +2 -4
  7. package/dist/compat/syntax-highlight.d.ts.map +1 -1
  8. package/dist/compat/syntax-highlight.js +101 -27
  9. package/dist/core/compile.d.ts +9 -1
  10. package/dist/core/compile.d.ts.map +1 -1
  11. package/dist/core/compile.js +120 -6
  12. package/dist/core/index.d.ts +1 -0
  13. package/dist/core/index.d.ts.map +1 -1
  14. package/dist/core/index.js +1 -0
  15. package/dist/core/tokenize.d.ts.map +1 -1
  16. package/dist/core/tokenize.js +143 -7
  17. package/dist/core/types.d.ts +74 -0
  18. package/dist/core/types.d.ts.map +1 -1
  19. package/dist/core/types.js +5 -1
  20. package/dist/index.d.ts +1 -0
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +1 -0
  23. package/dist/langs/bash.d.ts.map +1 -1
  24. package/dist/langs/bash.js +60 -13
  25. package/dist/langs/csharp.d.ts.map +1 -1
  26. package/dist/langs/csharp.js +11 -5
  27. package/dist/langs/css.d.ts.map +1 -1
  28. package/dist/langs/css.js +84 -7
  29. package/dist/langs/go.d.ts.map +1 -1
  30. package/dist/langs/go.js +7 -1
  31. package/dist/langs/html.d.ts +0 -10
  32. package/dist/langs/html.d.ts.map +1 -1
  33. package/dist/langs/html.js +63 -24
  34. package/dist/langs/http.d.ts.map +1 -1
  35. package/dist/langs/http.js +25 -9
  36. package/dist/langs/javascript.d.ts.map +1 -1
  37. package/dist/langs/javascript.js +146 -30
  38. package/dist/langs/json.d.ts.map +1 -1
  39. package/dist/langs/json.js +7 -1
  40. package/dist/langs/julia.d.ts +4 -0
  41. package/dist/langs/julia.d.ts.map +1 -0
  42. package/dist/langs/julia.js +181 -0
  43. package/dist/langs/markdown.d.ts +9 -3
  44. package/dist/langs/markdown.d.ts.map +1 -1
  45. package/dist/langs/markdown.js +127 -6
  46. package/dist/langs/ocaml.d.ts.map +1 -1
  47. package/dist/langs/ocaml.js +16 -7
  48. package/dist/langs/php.d.ts.map +1 -1
  49. package/dist/langs/php.js +69 -19
  50. package/dist/langs/r.js +9 -9
  51. package/dist/langs/rust.d.ts.map +1 -1
  52. package/dist/langs/rust.js +19 -10
  53. package/dist/langs/sql.d.ts.map +1 -1
  54. package/dist/langs/sql.js +9 -3
  55. package/dist/langs/yaml.d.ts +0 -7
  56. package/dist/langs/yaml.d.ts.map +1 -1
  57. package/dist/langs/yaml.js +157 -28
  58. package/dist/lazy.d.ts +1 -0
  59. package/dist/lazy.d.ts.map +1 -1
  60. package/dist/lazy.js +2 -0
  61. package/package.json +2 -2
@@ -12,6 +12,9 @@
12
12
  * is the cheapest reliable way to tell `a / b` from `/ab+/g`.
13
13
  * - Template literals nest properly: `` `${ {a: `x`} }` `` keeps its states
14
14
  * straight because interpolations track brace depth.
15
+ * - JSX children are text, not code. Once an opening tag closes, everything up
16
+ * to the matching closing tag is read as text with `{…}` expressions, so the
17
+ * apostrophe in `<p>Don't</p>` cannot open a string.
15
18
  */
16
19
  /**
17
20
  * An identifier, with its tail length-capped.
@@ -27,6 +30,41 @@
27
30
  const ID = '[A-Za-z_$\\u0080-\\uFFFF][\\w$\\u0080-\\uFFFF]{0,128}';
28
31
  /** Contexts after which a `/` begins a regex literal rather than division. */
29
32
  const BEFORE_REGEX = '(?:[=(,:;\\[!&|?{}+\\-*%^~<>]|=>|\\breturn|\\bcase|\\btypeof|\\bin|\\bof|\\bnew|\\bdo|\\byield|\\bawait|\\belse)';
33
+ /**
34
+ * Contexts after which a `<` can open a JSX element: wherever an expression
35
+ * can start. Everything else — an identifier, `)` or `]` — makes `<` a
36
+ * comparison or a type argument list.
37
+ */
38
+ const BEFORE_JSX = '(?:^|[=(,?:{\\[;!&|]|=>|\\breturn|\\byield|\\bdefault|\\bawait)';
39
+ /**
40
+ * TypeScript angle brackets that look like an element with no attributes: a
41
+ * generic arrow function `<T>(x: T) => x`, a constrained one `<T extends X>`,
42
+ * a defaulted one `<T = unknown>`, and the old-style assertion `<HTMLElement>el`. Reading any of them as an
43
+ * opening tag would turn the rest of the file into JSX text.
44
+ *
45
+ * An assertion is followed by an expression on the same line, and so is an
46
+ * element's text, so the line decides: an element without attributes opens
47
+ * only when its line ends after the `>` or holds another tag. A closing tag
48
+ * counts, and so does a `<` that does not follow a name, which keeps the type
49
+ * arguments in `get<Store>()` out. An element whose text runs on to a later
50
+ * closing tag reads as code instead, which costs that one element and never
51
+ * the rest of the file.
52
+ */
53
+ const NOT_TS_AFTER = '(?!>[ \\t]*\\()(?!\\s+(?:extends\\b|=))(?!>(?![ \\t]*$)(?![^\\n]{0,512}(?:<\\/|(?<![\\w$)\\]])<[A-Za-z>])))';
54
+ /**
55
+ * Ends a keyword rule: the word is not followed by the `:` of an object key or
56
+ * a type member. Checked only after a keyword has matched, so it costs nothing
57
+ * on ordinary identifiers.
58
+ */
59
+ const NOT_KEY = '(?!\\??:)';
60
+ /** A JSX tag name: a component, a member expression, or a lowercase element. */
61
+ const TAG = '[A-Z][\\w.]*|[a-z][\\w-]*(?:[.:][\\w-]+)*';
62
+ /**
63
+ * What follows a name that is assigned a function: `= () =>`, `= async x =>`,
64
+ * `= function`. The parameter list cannot nest parentheses, which covers real
65
+ * code and keeps the scan bounded.
66
+ */
67
+ const ASSIGNS_FUNCTION = `[ \\t]*=[ \\t]*(?:async[ \\t]*)?(?:function\\b|\\([^()]{0,512}\\)[ \\t]*(?::[^=;\\n]{0,256})?=>|${ID}[ \\t]*=>)`;
30
68
  /** Body of a regex literal: no unescaped `/`, character classes may hold one. */
31
69
  const REGEX = '/(?![/*])(?:[^/\\\\\\n\\[]|\\\\.|\\[(?:[^\\]\\\\\\n]|\\\\.)*\\])+/[dgimsuvy]*';
32
70
  const javascript = {
@@ -36,8 +74,10 @@ const javascript = {
36
74
  root: { rules: [{ include: 'expression' }] },
37
75
  expression: {
38
76
  rules: [
39
- { match: '/\\*\\*(?![/*])[\\s\\S]*?\\*/', scope: 'comment.doc' },
40
- { match: '/\\*[\\s\\S]*?\\*/', scope: 'comment' },
77
+ // A state, not a lazy `[\\s\\S]*?` scan: that form runs to EOF and fails
78
+ // once per opener, which is O(n²) on a run of unterminated `/*`.
79
+ { match: '/\\*\\*(?![/*])', scope: 'comment.doc', push: 'doc-comment' },
80
+ { match: '/\\*', scope: 'comment', push: 'block-comment' },
41
81
  { match: '//[^\\n]*', scope: 'comment' },
42
82
  { match: '^#!.*', scope: 'comment' },
43
83
  { include: 'strings' },
@@ -47,32 +87,68 @@ const javascript = {
47
87
  match: `(${BEFORE_REGEX})(\\s*)(${REGEX})`,
48
88
  scope: ['operator', null, 'regexp'],
49
89
  },
50
- { match: `^(\\s*)(${REGEX})`, scope: [null, 'regexp'] },
90
+ // `if (ok) /re/.test(s)`: a `)` that closes a control condition starts
91
+ // an expression. The lookbehind sits after the `/` so the rule still
92
+ // starts with a literal character.
93
+ {
94
+ match: `/(?<=(?<![\\w$.])(?:if|while|for)[ \\t]*\\([^()\\n]{0,256}\\)[ \\t]*/)${REGEX.slice(1)}`,
95
+ scope: 'regexp',
96
+ },
97
+ // Blanks only: `\s*` would run across every blank line below, and
98
+ // retrying that at each line start is O(n²) on a run of blank lines.
99
+ { match: `^([ \\t]*)(${REGEX})`, scope: [null, 'regexp'] },
51
100
  { include: 'numbers' },
52
101
  { include: 'jsx' },
53
102
  { match: `(@)(${ID}(?:\\.${ID})*)`, scope: ['decorator', 'decorator'] },
54
103
  {
55
- match: `(\\.)([ \\t]*)(${ID})(?=[ \\t]*\\()`,
104
+ match: `(\\.)([ \\t]*)(#?${ID})(?=[ \\t]*\\()`,
56
105
  scope: ['punctuation', null, 'function.method'],
57
106
  },
58
107
  {
59
- match: `(\\?\\.|\\.)([ \\t]*)(${ID})`,
108
+ match: `(\\?\\.|\\.)([ \\t]*)(#?${ID})`,
60
109
  scope: ['punctuation', null, 'variable.member'],
61
110
  },
111
+ // A private field or method where it is declared, or `#x in obj`.
112
+ { match: `#${ID}`, scope: 'variable.member' },
113
+ // `handler = function …` and `this.run = () => …` name a function as
114
+ // surely as a declaration does.
115
+ { match: `\\b${ID}(?=${ASSIGNS_FUNCTION})`, scope: 'function' },
116
+ // A keyword followed by `:` is an object key or a property in a type —
117
+ // `{ type: 'object' }`, `{ from: start, to: end }` — so each keyword
118
+ // rule ends with `NOT_KEY` and the word falls through to the property
119
+ // rule. `default:` needs telling apart: in a `switch` it is followed by
120
+ // a statement or nothing, as an object key by a value. A braced block
121
+ // after it reads as a key, since `responses: { default: { … } }` is the
122
+ // commoner of the two in API code.
123
+ //
124
+ // The contextual keywords that are also common method names (`get`,
125
+ // `set`, `of`, `delete`, `from`, `type`) stay keywords only when no `(`
126
+ // follows directly, so `static of(x)` and `cache.get(key)` read as calls.
127
+ {
128
+ match: '\\bdefault\\b(?=[ \\t]*:[ \\t]*(?:$|\\/[/*]|\\b(?:return|break|continue|throw|case)\\b))|\\bdefault\\b(?!\\??:)',
129
+ scope: 'keyword.control',
130
+ },
62
131
  {
63
- match: '\\b(?:if|else|for|while|do|switch|case|default|break|continue|return|throw|try|catch|finally|await|yield)\\b',
132
+ match: `\\b(?:if|else|for|while|do|switch|case|break|continue|return|throw|try|catch|finally|await|yield)\\b${NOT_KEY}`,
64
133
  scope: 'keyword.control',
65
134
  },
135
+ // A TypeScript assertion ends an expression; it does not declare the
136
+ // next name, even when that name begins a new line without a semicolon.
137
+ { match: '\\bconst\\b(?<=\\bas\\s+const)', scope: 'keyword.declaration' },
138
+ // The declared name is scoped by a state of its own rather than by a
139
+ // rule here: a rule that has to look back at `const` runs at every
140
+ // identifier in the file, while the state only runs after the keyword.
141
+ { match: '\\b(?:const|let|var)\\b', scope: 'keyword.declaration', push: 'declared-name' },
66
142
  {
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',
143
+ match: `\\b(?:function|class|interface|enum|namespace|module|declare|abstract|implements|extends|constructor|static|readonly|override|public|private|protected|async|get(?!\\()|set(?!\\()|type(?!\\())\\b${NOT_KEY}`,
68
144
  scope: 'keyword.declaration',
69
145
  },
70
146
  {
71
- match: '\\b(?:import|export|from|require)\\b',
147
+ match: `\\b(?:import|export|from(?!\\()|require)\\b${NOT_KEY}`,
72
148
  scope: 'keyword.import',
73
149
  },
74
150
  {
75
- match: '\\b(?:typeof|instanceof|in|of|new|delete|void|satisfies|keyof|infer|asserts|as)\\b',
151
+ match: `\\b(?:typeof|instanceof|in|of(?!\\()|new|delete(?!\\()|void|satisfies|keyof|infer|asserts|as)\\b${NOT_KEY}`,
76
152
  scope: 'keyword.operator',
77
153
  },
78
154
  { match: '\\b(?:true|false)\\b', scope: 'boolean' },
@@ -85,20 +161,23 @@ const javascript = {
85
161
  scope: 'variable.builtin',
86
162
  },
87
163
  {
88
- match: '\\b(?:console|window|document|process|module|exports)\\b',
164
+ match: `\\b(?:console|window|document|process|module|exports)\\b${NOT_KEY}`,
89
165
  scope: 'variable.builtin',
90
166
  },
91
167
  {
92
- match: '\\b(?:string|number|boolean|symbol|bigint|object|unknown|never|any|void|readonly)\\b(?=[^\\w$]|$)',
168
+ match: `\\b(?:string|number|boolean|symbol|bigint|object|unknown|never|any|void|readonly)\\b(?=[^\\w$]|$)${NOT_KEY}`,
93
169
  scope: 'type.builtin',
94
170
  },
95
171
  { match: '\\b[A-Z][A-Z0-9_]+\\b(?![\\w$])', scope: 'constant' },
172
+ // A capitalised name followed by a call is still a call — `Error('x')`,
173
+ // `new Date()`, a React component invoked directly — not a type.
174
+ { match: '\\b[A-Z][\\w$]{0,128}(?=[ \\t]*\\()', scope: 'function.call' },
96
175
  { match: '\\b[A-Z][A-Za-z0-9_$]*\\b', scope: 'type' },
97
176
  // `key:` in an object literal, and the name half of a TS annotation.
98
177
  // Requiring no space before the colon keeps `cond ? a : b` out.
99
178
  { match: `\\b${ID}(?=\\??:)`, scope: 'property' },
100
179
  { match: `\\b${ID}(?=\\s*=>)`, scope: 'variable.parameter' },
101
- { match: `\\b${ID}(?=[ \\t]*[(<])`, scope: 'function.call' },
180
+ { match: `\\b${ID}(?=[ \\t]*[(<]|\`)`, scope: 'function.call' },
102
181
  {
103
182
  // Bare `=` goes last so `=>`, `==`, `===` and the compound forms all
104
183
  // get first refusal. Without it plain assignment matched nothing.
@@ -109,6 +188,18 @@ const javascript = {
109
188
  { match: '[,;:]', scope: 'punctuation.delimiter' },
110
189
  ],
111
190
  },
191
+ /**
192
+ * Right after `const`, `let` or `var`: the one name it declares. A
193
+ * destructuring pattern or anything else ends the state without a scope.
194
+ */
195
+ 'declared-name': {
196
+ rules: [
197
+ { match: '\\benum\\b', scope: 'keyword.declaration', pop: true },
198
+ { match: `${ID}(?=${ASSIGNS_FUNCTION})`, scope: 'function', pop: true },
199
+ { match: ID, scope: 'variable', pop: true },
200
+ { match: '(?=\\S)', pop: true },
201
+ ],
202
+ },
112
203
  numbers: {
113
204
  rules: [
114
205
  { match: '\\b0[xX](?:_?[0-9a-fA-F])+n?\\b', scope: 'number' },
@@ -182,38 +273,55 @@ const javascript = {
182
273
  // ---- JSX ---------------------------------------------------------------
183
274
  jsx: {
184
275
  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.
276
+ // `<` is the most overloaded character in the language, so an element
277
+ // opens only where an expression can start. The lookbehind sits after
278
+ // the `<` so the rule still starts with a literal character, which
279
+ // keeps the engine's first-character scan.
192
280
  {
193
- match: '(?<![\\w$)\\]])(<)(/?)([A-Z][\\w.]*|[a-z][\\w-]*(?:\\.[\\w-]+)*)(?=[\\s/>])',
194
- scope: ['punctuation.bracket', 'punctuation', 'tag'],
195
- push: 'jsx-tag',
281
+ match: `(<)(?<=${BEFORE_JSX}\\s*<)(${TAG})(?=[\\s/>])${NOT_TS_AFTER}`,
282
+ scope: ['punctuation.bracket', 'tag'],
283
+ push: 'jsx-open-tag',
196
284
  },
197
285
  {
198
- match: '(<)(>)',
199
- scope: ['punctuation.bracket', 'punctuation.bracket'],
286
+ match: `<(?<=${BEFORE_JSX}\\s*<)>`,
287
+ scope: 'punctuation.bracket',
288
+ push: 'jsx-children',
200
289
  },
290
+ // A closing tag with no opener, as in a snippet cut out of a larger file.
201
291
  {
202
- match: '(</)(>)',
203
- scope: ['punctuation.bracket', 'punctuation.bracket'],
292
+ match: `(</)(${TAG})?([ \\t]*)(>)`,
293
+ scope: ['punctuation.bracket', 'tag', null, 'punctuation.bracket'],
204
294
  },
205
295
  ],
206
296
  },
207
- 'jsx-tag': {
297
+ 'jsx-open-tag': {
208
298
  rules: [
209
- { match: '/?>', scope: 'punctuation.bracket', pop: true },
299
+ { match: '/>', scope: 'punctuation.bracket', pop: true },
300
+ { match: '>', scope: 'punctuation.bracket', set: 'jsx-children' },
210
301
  { match: '\\{', scope: 'interpolation', push: 'jsx-expression' },
211
- { match: `(${ID}(?:-${ID})*)(?=\\s*=)`, scope: 'tag.attribute' },
212
- { match: `\\b${ID}(?:-${ID})*`, scope: 'tag.attribute' },
302
+ { match: `\\b${ID}(?:[-:]${ID})*`, scope: 'tag.attribute' },
213
303
  { match: '=', scope: 'operator' },
214
304
  { include: 'strings' },
215
305
  ],
216
306
  },
307
+ /** Between an opening tag and its closing tag: text, elements and `{…}`. */
308
+ 'jsx-children': {
309
+ rules: [
310
+ {
311
+ match: `(</)(${TAG})?([ \\t]*)(>)`,
312
+ scope: ['punctuation.bracket', 'tag', null, 'punctuation.bracket'],
313
+ pop: true,
314
+ },
315
+ {
316
+ match: `(<)(${TAG})(?=[\\s/>])`,
317
+ scope: ['punctuation.bracket', 'tag'],
318
+ push: 'jsx-open-tag',
319
+ },
320
+ { match: '<>', scope: 'punctuation.bracket', push: 'jsx-children' },
321
+ { match: '\\{', scope: 'interpolation', push: 'jsx-expression' },
322
+ { match: '&(?:#\\d{1,8}|#x[0-9a-fA-F]{1,8}|[A-Za-z][A-Za-z0-9]{1,32});', scope: 'string.escape' },
323
+ ],
324
+ },
217
325
  'jsx-expression': {
218
326
  rules: [
219
327
  { match: '\\}', scope: 'interpolation', pop: true },
@@ -221,6 +329,14 @@ const javascript = {
221
329
  { include: 'expression' },
222
330
  ],
223
331
  },
332
+ 'block-comment': {
333
+ default: 'comment',
334
+ rules: [{ match: '\\*/', scope: 'comment', pop: true }],
335
+ },
336
+ 'doc-comment': {
337
+ default: 'comment.doc',
338
+ rules: [{ match: '\\*/', scope: 'comment.doc', pop: true }],
339
+ },
224
340
  },
225
341
  };
226
342
  export default javascript;
@@ -1 +1 @@
1
- {"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../src/langs/json.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAE5C;;;;;GAKG;AACH,QAAA,MAAM,IAAI,EAAE,OAwCX,CAAA;AAED,eAAe,IAAI,CAAA"}
1
+ {"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../src/langs/json.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAE5C;;;;;GAKG;AACH,QAAA,MAAM,IAAI,EAAE,OA8CX,CAAA;AAED,eAAe,IAAI,CAAA"}
@@ -11,7 +11,9 @@ const json = {
11
11
  root: {
12
12
  rules: [
13
13
  { match: '//[^\\n]*', scope: 'comment' },
14
- { match: '/\\*[\\s\\S]*?\\*/', scope: 'comment' },
14
+ // A state, not a lazy `[\\s\\S]*?` scan: that form runs to EOF and fails
15
+ // once per opener, which is O(n²) on a run of unterminated `/*`.
16
+ { match: '/\\*', scope: 'comment', push: 'block-comment' },
15
17
  // A string followed by `:` is a key, whatever nesting it sits in.
16
18
  {
17
19
  match: '("(?:[^"\\\\\\n]|\\\\.)*")(\\s*)(:)',
@@ -41,6 +43,10 @@ const json = {
41
43
  { match: '"', scope: 'string', pop: true },
42
44
  ],
43
45
  },
46
+ 'block-comment': {
47
+ default: 'comment',
48
+ rules: [{ match: '\\*/', scope: 'comment', pop: true }],
49
+ },
44
50
  },
45
51
  };
46
52
  export default json;
@@ -0,0 +1,4 @@
1
+ import type { Grammar } from '../core/types.js';
2
+ declare const julia: Grammar;
3
+ export default julia;
4
+ //# sourceMappingURL=julia.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"julia.d.ts","sourceRoot":"","sources":["../../src/langs/julia.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAyB5C,QAAA,MAAM,KAAK,EAAE,OA6KZ,CAAA;AAED,eAAe,KAAK,CAAA"}
@@ -0,0 +1,181 @@
1
+ /**
2
+ * Julia.
3
+ *
4
+ * Things worth knowing about:
5
+ *
6
+ * - `'` is both a character literal and the transpose operator. It opens a
7
+ * literal only where a value cannot end — not after a name, a closing
8
+ * bracket, a number or another `'` — so `A'` and `x'y` stay operators.
9
+ * - `:` is a range, a ternary branch and the start of a symbol. A symbol needs
10
+ * a name straight after the colon and nothing value-like straight before it,
11
+ * which keeps `1:n` and `a ? b : c` out.
12
+ * - Interpolation is real: `"$(user.name) has $n items"` tokenizes the
13
+ * expressions. Prefixed string macros (`r"…"`, `raw"…"`, `b"…"`) do not
14
+ * interpolate, so they get a state without it.
15
+ * - Block comments `#= … =#` nest.
16
+ * - Names may end in `!` (`push!`) and carry Unicode (`α`, `∇f`).
17
+ */
18
+ const ID = '[A-Za-z_\\u00A1-\\uFFFF][\\w!\\u00A1-\\uFFFF]{0,128}';
19
+ /** Something after which `'` is a transpose rather than a character literal. */
20
+ const VALUE_END = "[\\w)\\]}'.\\u00A1-\\uFFFF]";
21
+ const julia = {
22
+ name: 'julia',
23
+ aliases: ['jl'],
24
+ states: {
25
+ root: { rules: [{ include: 'expression' }] },
26
+ expression: {
27
+ rules: [
28
+ { match: '#=', scope: 'comment', push: 'block-comment' },
29
+ { match: '#[^\\n]*', scope: 'comment' },
30
+ // ---- strings -------------------------------------------------------
31
+ { match: '"""', scope: 'string', push: 'string-triple' },
32
+ {
33
+ match: `(${ID})(""")`,
34
+ scope: ['string.special', 'string'],
35
+ push: 'string-raw-triple',
36
+ },
37
+ { match: `(${ID})(")`, scope: ['string.special', 'string'], push: 'string-raw' },
38
+ { match: '"', scope: 'string', push: 'string' },
39
+ { match: '`', scope: 'string', push: 'command' },
40
+ {
41
+ // The lookbehind sits after the quote so the rule still starts with a
42
+ // literal character, which keeps the engine's first-character scan.
43
+ match: `'(?<!${VALUE_END}')(?:[^'\\\\\\n]|\\\\(?:x[0-9a-fA-F]{1,2}|u[0-9a-fA-F]{1,4}|U[0-9a-fA-F]{1,8}|[0-7]{1,3}|[^\\n]))'`,
44
+ scope: 'string',
45
+ },
46
+ // ---- numbers -------------------------------------------------------
47
+ { match: '\\b0x[0-9a-fA-F_]+(?:p[-+]?\\d+)?\\b', scope: 'number' },
48
+ { match: '\\b0b[01_]+\\b', scope: 'number' },
49
+ { match: '\\b0o[0-7_]+\\b', scope: 'number' },
50
+ {
51
+ match: '(?:\\b\\d[\\d_]*(?:\\.(?!\\.)[\\d_]*)?|\\.\\d[\\d_]*)(?:[eEf][-+]?\\d+)?(?:im\\b)?',
52
+ scope: 'number',
53
+ },
54
+ // ---- macros, symbols and types -------------------------------------
55
+ { match: `@${ID}(?:\\.${ID})*`, scope: 'decorator' },
56
+ {
57
+ match: `(:(?<![\\w)\\]}:'".\\u00A1-\\uFFFF]:))(${ID})`,
58
+ scope: ['constant', 'constant'],
59
+ },
60
+ {
61
+ match: `(::|<:|>:)([ \\t]*)(${ID}(?:\\.${ID})*)`,
62
+ scope: ['operator', null, 'type'],
63
+ },
64
+ // ---- definitions ---------------------------------------------------
65
+ {
66
+ match: `\\b(function|macro)([ \\t]+)(${ID}(?:\\.${ID})*)`,
67
+ scope: ['keyword.declaration', null, 'function'],
68
+ },
69
+ {
70
+ match: `\\b((?:mutable[ \\t]+)?struct|abstract[ \\t]+type|primitive[ \\t]+type)([ \\t]+)(${ID})`,
71
+ scope: ['keyword.declaration', null, 'class'],
72
+ },
73
+ {
74
+ match: `\\b((?:bare)?module)([ \\t]+)(${ID})`,
75
+ scope: ['keyword.declaration', null, 'namespace'],
76
+ },
77
+ // The short form `area(r) = π * r^2` defines a function. The argument
78
+ // list cannot hold parentheses here, which covers the one-liners this
79
+ // form is used for and keeps the scan bounded.
80
+ {
81
+ match: `^([ \\t]*)(${ID})(?=\\([^()\\n]{0,256}\\)[ \\t]*(?:where[^=\\n]{0,256})?=(?!=))`,
82
+ scope: [null, 'function'],
83
+ },
84
+ // ---- keywords ------------------------------------------------------
85
+ {
86
+ match: '\\b(?:if|elseif|else|end|for|while|break|continue|return|try|catch|finally|do|begin|let|quote)\\b',
87
+ scope: 'keyword.control',
88
+ },
89
+ {
90
+ match: '\\b(?:function|macro|struct|mutable|abstract|primitive|type|module|baremodule|const|global|local|where|outer)\\b',
91
+ scope: 'keyword.declaration',
92
+ },
93
+ { match: '\\b(?:using|import|export|public|as)\\b', scope: 'keyword.import' },
94
+ { match: '\\b(?:in|isa)\\b', scope: 'keyword.operator' },
95
+ { match: '\\b(?:true|false)\\b', scope: 'boolean' },
96
+ { match: '\\b(?:nothing|missing|NaN|Inf|NaN32|Inf32|undef|stdout|stderr|stdin)\\b', scope: 'constant.builtin' },
97
+ // ---- calls ---------------------------------------------------------
98
+ {
99
+ match: '\\b(?:println|print|show|display|string|length|size|push!|pop!|append!|map|filter|reduce|sum|prod|maximum|minimum|collect|typeof|isa|convert|parse|zeros|ones|rand|round|floor|ceil|abs|sqrt|exp|log|error|throw|isempty|haskey|get|keys|values|pairs|enumerate|zip|sort|sort!|occursin|replace|split|join|startswith|endswith|open|read|write|close|eachline|readline|readlines|sprint|repr|Dict|Vector|Array|Tuple)(?=\\.?\\()',
100
+ scope: 'function.builtin',
101
+ },
102
+ { match: `(\\.)(${ID})(?=\\.?\\()`, scope: ['punctuation', 'function.method'] },
103
+ { match: `(\\.)(${ID})`, scope: ['punctuation', 'variable.member'] },
104
+ // `f(x)` and the broadcast `f.(x)`.
105
+ { match: `${ID}(?=\\.?\\()`, scope: 'function.call' },
106
+ // CapWords reads as a type, which is how Julia names them.
107
+ { match: '\\b[A-Z][A-Za-z0-9_]*\\b', scope: 'type' },
108
+ // ---- syntax --------------------------------------------------------
109
+ {
110
+ match: "\\.?(?:===|!==|==|!=|<=|>=|=>|->|\\|\\||&&|\\|>|<\\||\\.\\.\\.|[-+*/\\\\^%÷<>=!&|~⊻∈∉≤≥≠]=?)|'",
111
+ scope: 'operator',
112
+ },
113
+ { match: '[()\\[\\]{}]', scope: 'punctuation.bracket' },
114
+ { match: '[,;]', scope: 'punctuation.delimiter' },
115
+ { match: '[.:?]', scope: 'punctuation' },
116
+ ],
117
+ },
118
+ 'block-comment': {
119
+ default: 'comment',
120
+ rules: [
121
+ { match: '#=', scope: 'comment', push: 'block-comment' },
122
+ { match: '=#', scope: 'comment', pop: true },
123
+ ],
124
+ },
125
+ // ---- strings -------------------------------------------------------------
126
+ string: {
127
+ default: 'string',
128
+ rules: [{ include: 'escapes' }, { include: 'interpolation' }, { match: '"', scope: 'string', pop: true }],
129
+ },
130
+ 'string-triple': {
131
+ default: 'string',
132
+ rules: [{ include: 'escapes' }, { include: 'interpolation' }, { match: '"""', scope: 'string', pop: true }],
133
+ },
134
+ /** Prefixed string macros: no interpolation, and `\"` is the only escape. */
135
+ 'string-raw': {
136
+ default: 'string',
137
+ rules: [
138
+ { match: '\\\\"', scope: 'string.escape' },
139
+ { match: '"', scope: 'string', pop: true },
140
+ ],
141
+ },
142
+ 'string-raw-triple': {
143
+ default: 'string',
144
+ rules: [{ match: '"""', scope: 'string', pop: true }],
145
+ },
146
+ command: {
147
+ default: 'string',
148
+ rules: [{ include: 'escapes' }, { include: 'interpolation' }, { match: '`', scope: 'string', pop: true }],
149
+ },
150
+ escapes: {
151
+ rules: [
152
+ {
153
+ match: '\\\\(?:x[0-9a-fA-F]{1,2}|u[0-9a-fA-F]{1,4}|U[0-9a-fA-F]{1,8}|[0-7]{1,3}|[\\s\\S])',
154
+ scope: 'string.escape',
155
+ },
156
+ ],
157
+ },
158
+ interpolation: {
159
+ rules: [
160
+ { match: '\\$\\(', scope: 'interpolation', push: 'interpolation-body' },
161
+ { match: `(\\$)(${ID})`, scope: ['interpolation', 'variable'] },
162
+ ],
163
+ },
164
+ /** The expression inside `$( … )`. Nested parentheses are tracked. */
165
+ 'interpolation-body': {
166
+ rules: [
167
+ { match: '\\)', scope: 'interpolation', pop: true },
168
+ { match: '\\(', scope: 'punctuation.bracket', push: 'parens' },
169
+ { include: 'expression' },
170
+ ],
171
+ },
172
+ parens: {
173
+ rules: [
174
+ { match: '\\)', scope: 'punctuation.bracket', pop: true },
175
+ { match: '\\(', scope: 'punctuation.bracket', push: 'parens' },
176
+ { include: 'expression' },
177
+ ],
178
+ },
179
+ },
180
+ };
181
+ export default julia;
@@ -2,9 +2,15 @@ import type { Grammar } from '../core/types.js';
2
2
  /**
3
3
  * Markdown.
4
4
  *
5
- * Prose is left alone; only the marks are styled. Fenced code blocks become a
6
- * literal state so their contents are never mistaken for Markdown syntax —
7
- * highlighting the fence body with its own language is a follow-up.
5
+ * Prose is left alone; only the marks are styled. Three kinds of block belong
6
+ * to other languages and are handed to them when they are registered:
7
+ *
8
+ * - a fenced code block that names its language
9
+ * - YAML front matter
10
+ * - HTML that starts a line, and `<script>`, `<pre>` and `<style>` blocks
11
+ *
12
+ * Any other fence becomes a literal state, so its contents are never mistaken
13
+ * for Markdown syntax.
8
14
  */
9
15
  declare const markdown: Grammar;
10
16
  export default markdown;
@@ -1 +1 @@
1
- {"version":3,"file":"markdown.d.ts","sourceRoot":"","sources":["../../src/langs/markdown.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAE5C;;;;;;GAMG;AACH,QAAA,MAAM,QAAQ,EAAE,OAqEf,CAAA;AAED,eAAe,QAAQ,CAAA"}
1
+ {"version":3,"file":"markdown.d.ts","sourceRoot":"","sources":["../../src/langs/markdown.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAS,MAAM,eAAe,CAAA;AA2CnD;;;;;;;;;;;;GAYG;AACH,QAAA,MAAM,QAAQ,EAAE,OAqJf,CAAA;AAED,eAAe,QAAQ,CAAA"}