@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,365 @@
1
+ /**
2
+ * Ruby.
3
+ *
4
+ * Ruby overloads more punctuation than most languages, so most of this grammar
5
+ * is about telling the readings apart:
6
+ *
7
+ * - `/` opens a regex only after a token that cannot end an expression — the
8
+ * same guard `javascript.ts` uses — so `total / count` stays division.
9
+ * - `?x` is a character literal, but the `?` of `empty?` is not: predicate and
10
+ * bang methods are matched from their first letter, so they claim their own
11
+ * `?` before the character-literal rule is ever tried at it.
12
+ * - `<<` opens a heredoc only when what follows looks like a marker, and the
13
+ * body starts on the *next* line, so `sql = <<~SQL.strip` keeps the rest of
14
+ * the opening line highlighted as code.
15
+ * - `%w[]`, `%i()` and `%q{}` get one state per delimiter. Matching the closer
16
+ * in a single pattern would need a backreference, which the compiler rejects.
17
+ * - `{` always pushes a state, so a hash or a block inside `#{…}` cannot close
18
+ * the interpolation early.
19
+ *
20
+ * Ordinary locals deliberately match no rule and inherit the block foreground,
21
+ * which is what keeps both the markup and the visual noise down.
22
+ */
23
+ /** Local, method and parameter names. Non-ASCII identifiers render unscoped. */
24
+ const ID = '[A-Za-z_]\\w*';
25
+ /** The operators a method can be named after, longest form first. */
26
+ const OPS = '\\[\\]=?|<=>|===?|=~|<<|>>|\\*\\*|[<>]=?|[-+*/%&|^~]';
27
+ /** Everything `def` can name: `save!`, `[]=`, `<=>`, `!`. */
28
+ const METHOD = `(?:${ID}[?!=]?|${OPS}|![=~]?)`;
29
+ /**
30
+ * A `%`-literal introducer. The type letter is required: bare `%(…)` is legal
31
+ * Ruby but far rarer than `x %(y)` is a typo for modulo, so `%` alone stays an
32
+ * operator.
33
+ */
34
+ const PCT = '%[qQwWiIrsx]';
35
+ /**
36
+ * Operators after which a `/` opens a regex literal instead of dividing.
37
+ * Openers (`(`, `[`, `{`) and keywords have rules of their own so they keep
38
+ * their own scope.
39
+ */
40
+ const BEFORE_REGEX = '(?:=~|!~|=>|&&|\\|\\||==|!=|[=,;|&!?:])';
41
+ /**
42
+ * A regex body: no unescaped `/`, though a character class may hold one. The
43
+ * leading guard keeps the `/=` operator and an empty `//` out.
44
+ *
45
+ * Both repetitions are capped. An uncapped `\[…*\]` scans to end of line
46
+ * looking for a `]`, and because the rule is retried at every `,`/`(`/`[`
47
+ * that precedes a `/`, a line of `,/[` costs O(n²) — 1.2 s for one 4 KB line,
48
+ * which is a denial of service rather than a wrong colour. The caps bound that
49
+ * scan; a regex literal or character class
50
+ * longer than the cap simply is not recognised, which is the cheap failure.
51
+ */
52
+ const REGEX = '/(?![/=])(?:[^/\\\\\\n\\[]|\\\\.|\\[(?:[^\\]\\\\\\n]|\\\\.){0,120}\\]){1,400}/[imxounse]*';
53
+ const ruby = {
54
+ name: 'ruby',
55
+ aliases: ['rb', 'gemspec', 'rake'],
56
+ states: {
57
+ root: {
58
+ rules: [
59
+ // `=begin` opens a block comment only at column 0, which is also the
60
+ // only place Ruby accepts it.
61
+ { match: '^=begin', scope: 'comment', push: 'block-comment' },
62
+ { match: '#[^\\n]*', scope: 'comment' },
63
+ // A character literal is a `?` glued to one character. Predicate names
64
+ // (`empty?`) start earlier in the line and are matched first, so what
65
+ // is left here really is a literal — except for a ternary written with
66
+ // no space after the `?`, which Ruby itself reads this way too.
67
+ { match: '\\?(?:\\\\\\w|[^\\s\\\\])(?![\\w?!])', scope: 'string' },
68
+ { match: '"', scope: 'string', push: 'string-double' },
69
+ { match: "'", scope: 'string', push: 'string-single' },
70
+ { match: '`', scope: 'string', push: 'command' },
71
+ // One state per closing delimiter, each tracking its own nesting so
72
+ // `%r{\\d{2}}` and `%w[a [b]]` close where they should. A `%r` body is
73
+ // scoped as a string: `regexp` is reserved for `/…/`, which is what
74
+ // reads as a pattern at a glance.
75
+ {
76
+ match: `(${PCT})(\\[)`,
77
+ scope: ['string.special', 'string'],
78
+ push: 'pct-bracket',
79
+ },
80
+ {
81
+ match: `(${PCT})(\\{)`,
82
+ scope: ['string.special', 'string'],
83
+ push: 'pct-brace',
84
+ },
85
+ {
86
+ match: `(${PCT})(\\()`,
87
+ scope: ['string.special', 'string'],
88
+ push: 'pct-paren',
89
+ },
90
+ // Heredocs. The marker is not remembered — that needs a backreference —
91
+ // so the body ends at the first line holding only a bare word.
92
+ {
93
+ match: '(<<[-~]?)([\'"])(\\w+)([\'"])',
94
+ scope: ['operator', 'string', 'string.special', 'string'],
95
+ push: 'heredoc-line',
96
+ },
97
+ {
98
+ match: '(<<[-~])(\\w+)',
99
+ scope: ['operator', 'string.special'],
100
+ push: 'heredoc-line',
101
+ },
102
+ // Undecorated `<<` has to look like a marker, or `items << other` would
103
+ // swallow the rest of the file.
104
+ {
105
+ match: '(<<)([A-Z_]\\w*)',
106
+ scope: ['operator', 'string.special'],
107
+ push: 'heredoc-line',
108
+ },
109
+ // The token before the `/` is matched and scoped here, which is cheaper
110
+ // and more portable than a lookbehind. Three rules, because that token
111
+ // needs its own scope: `split(/,/)` opens with a bracket, `when /x/`
112
+ // with a keyword, `line =~ /x/` with an operator.
113
+ // `{` is split out so it can still push `brace`. Consuming it here
114
+ // without pushing breaks the invariant the `brace` state exists for —
115
+ // `"#{xs.select { /a/ }.join}"` would let the *block's* `}` close the
116
+ // interpolation, and every boundary after it shifts.
117
+ {
118
+ match: `([(\\[])([ \\t]*)(${REGEX})`,
119
+ scope: ['punctuation.bracket', null, 'regexp'],
120
+ },
121
+ {
122
+ match: `(\\{)([ \\t]*)(${REGEX})`,
123
+ scope: ['punctuation.bracket', null, 'regexp'],
124
+ push: 'brace',
125
+ },
126
+ {
127
+ match: `(\\b(?:when|if|elsif|unless|while|until|case|return|then)\\b)([ \\t]*)(${REGEX})`,
128
+ scope: ['keyword.control', null, 'regexp'],
129
+ },
130
+ {
131
+ match: `(${BEFORE_REGEX})([ \\t]*)(${REGEX})`,
132
+ scope: ['operator', null, 'regexp'],
133
+ },
134
+ // A symbol needs a name or an operator right after the colon, which
135
+ // keeps both `::` and the `: b` of a ternary out. The vocabulary has no
136
+ // symbol scope, so it borrows `constant` — the literal colour, next to
137
+ // numbers, which is where most Ruby themes put symbols anyway.
138
+ { match: `:(?:${ID}[?!=]?|${OPS})`, scope: 'constant' },
139
+ { match: `@@?${ID}`, scope: 'variable' },
140
+ { match: `\\$(?:${ID}|[0-9!@&*$?:~])`, scope: 'variable' },
141
+ // Block parameters: `each do |item|` and `map { |k, v| }`.
142
+ {
143
+ match: '(\\bdo)([ \\t]*)(\\|)',
144
+ scope: ['keyword.control', null, 'punctuation.delimiter'],
145
+ push: 'do-params',
146
+ },
147
+ {
148
+ match: '(\\{)([ \\t]*)(\\|)',
149
+ scope: ['punctuation.bracket', null, 'punctuation.delimiter'],
150
+ push: 'brace-params',
151
+ },
152
+ // The name is scoped in a state of its own so `def <=>` and `def []=`
153
+ // work without listing every operator twice.
154
+ { match: '\\bdef\\b', scope: 'keyword.declaration', push: 'def' },
155
+ {
156
+ match: '(\\bclass)([ \\t]+)([A-Z]\\w*(?:::[A-Z]\\w*)*)',
157
+ scope: ['keyword.declaration', null, 'class'],
158
+ },
159
+ {
160
+ match: '(\\bmodule)([ \\t]+)([A-Z]\\w*(?:::[A-Z]\\w*)*)',
161
+ scope: ['keyword.declaration', null, 'namespace'],
162
+ },
163
+ // A label: `key: value`, `def f(name:)`. Before the keyword rules so
164
+ // `{ if: true }` reads as a hash key, and requiring no space before the
165
+ // colon so `cond ? a : b` is left alone.
166
+ { match: `\\b${ID}(?=:(?![:=]))`, scope: 'property' },
167
+ {
168
+ match: '\\b(?:if|elsif|else|unless|case|when|while|until|for|break|next|redo|retry|return|yield|begin|rescue|ensure|end|do|then)\\b',
169
+ scope: 'keyword.control',
170
+ },
171
+ {
172
+ match: '\\b(?:def|class|module|alias|undef|private|public|protected|module_function)\\b',
173
+ scope: 'keyword.declaration',
174
+ },
175
+ // `include` and `extend` pull another module's methods in, which reads
176
+ // the same way an import does.
177
+ {
178
+ match: '\\b(?:require|require_relative|load|autoload|include|extend|prepend|using)\\b',
179
+ scope: 'keyword.import',
180
+ },
181
+ // `\b` cannot terminate `defined?`: `?` is not a word character, so the
182
+ // boundary would fall in the wrong place. Same for the builtins below.
183
+ {
184
+ match: '\\b(?:and|or|not|in|defined\\?)(?![\\w?!])',
185
+ scope: 'keyword.operator',
186
+ },
187
+ { match: '\\b(?:true|false)\\b', scope: 'boolean' },
188
+ { match: '\\bnil\\b', scope: 'constant.builtin' },
189
+ { match: '\\b(?:self|super)\\b', scope: 'variable.builtin' },
190
+ {
191
+ match: '\\b__(?:FILE|LINE|ENCODING|dir|method)__\\b',
192
+ scope: 'variable.special',
193
+ },
194
+ {
195
+ match: '\\b(?:puts|pp|print|raise|fail|abort|loop|lambda|proc|format|sprintf|gets|exit|freeze|attr_accessor|attr_reader|attr_writer|define_method|block_given\\?)(?![\\w?!])',
196
+ scope: 'function.builtin',
197
+ },
198
+ // A method with a block is still a call, so `.each do |x|` and
199
+ // `.map { }` count alongside `.push(x)`.
200
+ {
201
+ match: `(&?\\.)([ \\t]*)(${ID}[?!]?)(?=[ \\t]*(?:\\(|\\{|do\\b))`,
202
+ scope: ['punctuation', null, 'function.method'],
203
+ },
204
+ // A `?` or `!` suffix only ever ends a method name.
205
+ {
206
+ match: `(&?\\.)([ \\t]*)(${ID}[?!])`,
207
+ scope: ['punctuation', null, 'function.method'],
208
+ },
209
+ {
210
+ match: `(&?\\.)([ \\t]*)(${ID})`,
211
+ scope: ['punctuation', null, 'variable.member'],
212
+ },
213
+ { match: '\\b[A-Z]\\w*(?=::)', scope: 'namespace' },
214
+ // SCREAMING_CASE reads as a constant, CapWords as a type — between them
215
+ // that covers module constants, classes and the exception hierarchy
216
+ // without shipping a name list.
217
+ { match: '\\b[A-Z][A-Z0-9_]+\\b', scope: 'constant' },
218
+ { match: '\\b[A-Z]\\w*', scope: 'type' },
219
+ { match: '\\b[a-z_]\\w*[?!]?(?=\\()', scope: 'function.call' },
220
+ // A parenthesis-free predicate or bang call: `valid?`, `save!`. The
221
+ // lookahead is what keeps the `a!` of `a!=b` out of it.
222
+ { match: '\\b[a-z_]\\w*[?!](?![=~\\w?!])', scope: 'function.call' },
223
+ { match: '\\b0[xX][0-9a-fA-F_]+\\b', scope: 'number' },
224
+ { match: '\\b0[bB][01_]+\\b', scope: 'number' },
225
+ { match: '\\b0[oO]?[0-7_]+\\b', scope: 'number' },
226
+ // `r` and `i` are the rational and imaginary suffixes.
227
+ {
228
+ match: '\\b\\d[\\d_]*(?:\\.\\d[\\d_]*)?(?:[eE][-+]?\\d+)?[ri]?\\b',
229
+ scope: 'number',
230
+ },
231
+ // Every `{` pushes, so `}` always closes the thing that opened it.
232
+ { match: '\\{', scope: 'punctuation.bracket', push: 'brace' },
233
+ {
234
+ match: '<=>|=~|!~|\\.\\.\\.?|::|=>|\\*\\*=?|\\|\\|=?|&&=?|<<=?|>>=?|[-+*/%^&|]=?|[<>!=]=?=?|[?~]',
235
+ scope: 'operator',
236
+ },
237
+ { match: '[()\\[\\]}]', scope: 'punctuation.bracket' },
238
+ { match: '[,;:]', scope: 'punctuation.delimiter' },
239
+ ],
240
+ },
241
+ 'block-comment': {
242
+ default: 'comment',
243
+ rules: [{ match: '^=end[^\\n]*', pop: true }],
244
+ },
245
+ // ---- strings -----------------------------------------------------------
246
+ /** Shared by every interpolating literal: strings, commands, heredocs, `%` literals. */
247
+ 'interp-body': {
248
+ rules: [
249
+ {
250
+ match: '\\\\(?:u\\{[0-9a-fA-F ]+\\}|u[0-9a-fA-F]{4}|x[0-9a-fA-F]{1,2}|[0-7]{1,3}|.)',
251
+ scope: 'string.escape',
252
+ },
253
+ { match: '#\\{', scope: 'interpolation', push: 'interpolation' },
254
+ ],
255
+ },
256
+ 'string-double': {
257
+ default: 'string',
258
+ // No rule pops at the line break: a Ruby string really does span lines.
259
+ rules: [{ include: 'interp-body' }, { match: '"', pop: true }],
260
+ },
261
+ 'string-single': {
262
+ default: 'string',
263
+ rules: [
264
+ { match: "\\\\['\\\\]", scope: 'string.escape' },
265
+ { match: "'", pop: true },
266
+ ],
267
+ },
268
+ command: {
269
+ default: 'string',
270
+ rules: [{ include: 'interp-body' }, { match: '`', pop: true }],
271
+ },
272
+ // `%q{…}` and `%w[…]` do not interpolate, but they share these states with
273
+ // `%Q{…}` and `%W[…]`, which do. A literal `#{` inside a non-interpolating
274
+ // `%` literal is the price of three states instead of six. The trailing
275
+ // letters on the closer are `%r`'s flags; nothing else can follow one.
276
+ 'pct-bracket': {
277
+ default: 'string',
278
+ rules: [
279
+ { match: '\\[', push: 'pct-bracket' },
280
+ { match: '\\][imxounse]*', pop: true },
281
+ { include: 'interp-body' },
282
+ ],
283
+ },
284
+ 'pct-brace': {
285
+ default: 'string',
286
+ rules: [{ match: '\\{', push: 'pct-brace' }, { match: '\\}[imxounse]*', pop: true }, { include: 'interp-body' }],
287
+ },
288
+ 'pct-paren': {
289
+ default: 'string',
290
+ rules: [{ match: '\\(', push: 'pct-paren' }, { match: '\\)[imxounse]*', pop: true }, { include: 'interp-body' }],
291
+ },
292
+ /** The rest of the opening line is code; the heredoc body starts after it. */
293
+ 'heredoc-line': {
294
+ rules: [{ match: '\\n', set: 'heredoc' }, { include: 'root' }],
295
+ },
296
+ heredoc: {
297
+ default: 'string',
298
+ rules: [{ match: `^[ \\t]*${ID}[ \\t]*$`, scope: 'string.special', pop: true }, { include: 'interp-body' }],
299
+ },
300
+ // ---- interpolation and braces ------------------------------------------
301
+ interpolation: {
302
+ rules: [{ match: '\\}', scope: 'interpolation', pop: true }, { include: 'root' }],
303
+ },
304
+ /** Tracks brace depth so a hash or a block cannot close an interpolation. */
305
+ brace: {
306
+ rules: [{ match: '\\}', scope: 'punctuation.bracket', pop: true }, { include: 'root' }],
307
+ },
308
+ // ---- parameters --------------------------------------------------------
309
+ /** Entered on `def`, left as soon as the method name has been scoped. */
310
+ def: {
311
+ rules: [
312
+ { match: '(self)(\\.)', scope: ['variable.builtin', 'punctuation'] },
313
+ // A parenthesis-free signature: `def write path, mode`. The first name
314
+ // has no delimiter in front of it for `params` to hang off, so it is
315
+ // taken here, with the rest of the list.
316
+ {
317
+ match: `(${METHOD})([ \\t]+)(${ID})`,
318
+ scope: ['function', null, 'variable.parameter'],
319
+ set: 'params',
320
+ },
321
+ { match: METHOD, scope: 'function', set: 'params' },
322
+ { match: '$', pop: true },
323
+ ],
324
+ },
325
+ /**
326
+ * A signature. Only a name introduced by `(` or `,` becomes a parameter, so
327
+ * a default value is not mistaken for one. A call inside a default value
328
+ * (`def f(a = g(1))`) ends the signature at the inner `)`, which costs the
329
+ * parameters after it their scope.
330
+ */
331
+ params: {
332
+ rules: [
333
+ {
334
+ match: `(\\()(\\s*)([&*]{0,2})(${ID})`,
335
+ scope: ['punctuation.bracket', null, 'operator', 'variable.parameter'],
336
+ },
337
+ {
338
+ match: `(,)(\\s*)([&*]{0,2})(${ID})`,
339
+ scope: ['punctuation.delimiter', null, 'operator', 'variable.parameter'],
340
+ },
341
+ { match: '\\)', scope: 'punctuation.bracket', pop: true },
342
+ // A signature ends at the line break unless the line is continued.
343
+ { match: '(?<![,(\\\\])$', pop: true },
344
+ { include: 'root' },
345
+ ],
346
+ },
347
+ 'do-params': {
348
+ rules: [{ match: '\\|', scope: 'punctuation.delimiter', pop: true }, { include: 'block-params' }],
349
+ },
350
+ /** The closing `|` hands over to `brace`, so the block's `}` still pops. */
351
+ 'brace-params': {
352
+ rules: [{ match: '\\|', scope: 'punctuation.delimiter', set: 'brace' }, { include: 'block-params' }],
353
+ },
354
+ 'block-params': {
355
+ rules: [
356
+ { match: ID, scope: 'variable.parameter' },
357
+ { match: '[(),]', scope: 'punctuation.delimiter' },
358
+ { match: '[&*]', scope: 'operator' },
359
+ // An unclosed `|` list should not eat the file.
360
+ { match: '$', pop: true },
361
+ ],
362
+ },
363
+ },
364
+ };
365
+ export default ruby;
@@ -0,0 +1,10 @@
1
+ import type { Grammar } from '../core/types.js';
2
+ /**
3
+ * Rust.
4
+ *
5
+ * Lifetimes are distinguished from char literals, macros keep their `!`, and
6
+ * attributes read as annotations rather than as comments.
7
+ */
8
+ declare const rust: Grammar;
9
+ export default rust;
10
+ //# sourceMappingURL=rust.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rust.d.ts","sourceRoot":"","sources":["../../src/langs/rust.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAE5C;;;;;GAKG;AACH,QAAA,MAAM,IAAI,EAAE,OAsHX,CAAA;AAED,eAAe,IAAI,CAAA"}
@@ -0,0 +1,116 @@
1
+ /**
2
+ * Rust.
3
+ *
4
+ * Lifetimes are distinguished from char literals, macros keep their `!`, and
5
+ * attributes read as annotations rather than as comments.
6
+ */
7
+ const rust = {
8
+ name: 'rust',
9
+ aliases: ['rs'],
10
+ states: {
11
+ root: {
12
+ rules: [
13
+ { match: '//[/!][^\\n]*', scope: 'comment.doc' },
14
+ { match: '//[^\\n]*', scope: 'comment' },
15
+ { match: '/\\*[\\s\\S]*?\\*/', scope: 'comment' },
16
+ // Raw strings, longest hash run first. Beyond two hashes is vanishingly
17
+ // rare and would need a backreference to match exactly.
18
+ {
19
+ match: '(b?r##)("[\\s\\S]*?"##)',
20
+ scope: ['string.special', 'string'],
21
+ },
22
+ { match: '(b?r#)("[\\s\\S]*?"#)', scope: ['string.special', 'string'] },
23
+ {
24
+ match: '(b?r)("(?:[^"\\\\]|\\\\.)*")',
25
+ scope: ['string.special', 'string'],
26
+ },
27
+ {
28
+ match: '(b?)(")',
29
+ scope: ['string.special', 'string'],
30
+ push: 'string',
31
+ },
32
+ // A char literal always closes; a lifetime never does.
33
+ {
34
+ match: "'(?:[^'\\\\]|\\\\(?:u\\{[0-9a-fA-F]{1,6}\\}|x[0-9a-fA-F]{2}|.))'",
35
+ scope: 'string',
36
+ },
37
+ { match: "'(?:static|_|[a-z][\\w]*)\\b", scope: 'variable.special' },
38
+ // `[` excluded as well as `]`, so a run of `#[` cannot cost O(n) per
39
+ // attribute. An attribute holding a nested `[` renders unscoped.
40
+ { match: '#!?\\[[^[\\]]*\\]', scope: 'decorator' },
41
+ {
42
+ match: '\\b(?:if|else|match|loop|while|for|break|continue|return|await|yield)\\b',
43
+ scope: 'keyword.control',
44
+ },
45
+ // Ahead of the bare keyword list below, which also matches `fn`. Rule
46
+ // order is priority order, so listing this second would mean `fn`
47
+ // always won as a plain keyword and the name never got scoped.
48
+ {
49
+ match: '(fn)(\\s+)([A-Za-z_]\\w*)',
50
+ scope: ['keyword.declaration', null, 'function'],
51
+ },
52
+ {
53
+ match: '\\b(?:fn|let|const|static|struct|enum|trait|impl|type|mod|union|async|move|dyn|pub|crate|unsafe|extern|ref|mut|where)\\b',
54
+ scope: 'keyword.declaration',
55
+ },
56
+ { match: '\\b(?:use|as|super|self|Self)\\b', scope: 'keyword.import' },
57
+ { match: '\\b(?:in|box)\\b', scope: 'keyword.operator' },
58
+ { match: '\\b(?:true|false)\\b', scope: 'boolean' },
59
+ { match: '\\b(?:None|Some|Ok|Err)\\b', scope: 'constant.builtin' },
60
+ {
61
+ match: '\\b(?:i8|i16|i32|i64|i128|isize|u8|u16|u32|u64|u128|usize|f32|f64|bool|char|str|String|Vec|Option|Result|Box|Rc|Arc)\\b',
62
+ scope: 'type.builtin',
63
+ },
64
+ // Anchored to a word boundary. Unanchored, the name is retried at every
65
+ // column of every word and each attempt scans to the end of the line
66
+ // before failing for want of the `!`, which is quadratic in line length.
67
+ {
68
+ match: '\\b([A-Za-z_]\\w*)(!)',
69
+ scope: ['function.builtin', 'function.builtin'],
70
+ },
71
+ {
72
+ match: '(\\.)([A-Za-z_]\\w*)(?=\\s*\\()',
73
+ scope: ['punctuation', 'function.method'],
74
+ },
75
+ {
76
+ match: '(\\.)([A-Za-z_]\\w*)',
77
+ scope: ['punctuation', 'variable.member'],
78
+ },
79
+ { match: '\\b[A-Z][A-Z0-9_]+\\b', scope: 'constant' },
80
+ { match: '\\b[A-Z][A-Za-z0-9_]*\\b', scope: 'type' },
81
+ { match: '\\b[a-z_]\\w*(?=\\s*\\()', scope: 'function.call' },
82
+ { match: '\\b[a-z_]\\w*(?=::)', scope: 'namespace' },
83
+ {
84
+ match: '\\b0[xX][0-9a-fA-F_]+(?:[iu](?:8|16|32|64|128|size))?\\b',
85
+ scope: 'number',
86
+ },
87
+ {
88
+ match: '\\b0[bo][0-9_]+(?:[iu](?:8|16|32|64|128|size))?\\b',
89
+ scope: 'number',
90
+ },
91
+ {
92
+ match: '\\b\\d[\\d_]*(?:\\.\\d[\\d_]*)?(?:[eE][-+]?\\d+)?(?:[iuf](?:8|16|32|64|128|size))?\\b',
93
+ scope: 'number',
94
+ },
95
+ {
96
+ match: '->|=>|::|\\.\\.=?|&&|\\|\\||[-+*/%!&|^<>=]=?|[?@]',
97
+ scope: 'operator',
98
+ },
99
+ { match: '[()\\[\\]{}]', scope: 'punctuation.bracket' },
100
+ { match: '[,;:]', scope: 'punctuation.delimiter' },
101
+ ],
102
+ },
103
+ string: {
104
+ default: 'string',
105
+ rules: [
106
+ {
107
+ match: '\\\\(?:u\\{[0-9a-fA-F]{1,6}\\}|x[0-9a-fA-F]{2}|.)',
108
+ scope: 'string.escape',
109
+ },
110
+ { match: '\\{[^{}\\n]*\\}', scope: 'string.special' },
111
+ { match: '"', scope: 'string', pop: true },
112
+ ],
113
+ },
114
+ },
115
+ };
116
+ export default rust;
@@ -0,0 +1,4 @@
1
+ import type { Grammar } from '../core/types.js';
2
+ declare const scala: Grammar;
3
+ export default scala;
4
+ //# sourceMappingURL=scala.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scala.d.ts","sourceRoot":"","sources":["../../src/langs/scala.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AA0E5C,QAAA,MAAM,KAAK,EAAE,OAqQZ,CAAA;AAED,eAAe,KAAK,CAAA"}