@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,337 @@
1
+ /**
2
+ * Mojo.
3
+ *
4
+ * Mojo is Python's syntax with a systems language underneath it, so this
5
+ * grammar is shaped like the Python one and then answers the questions Python
6
+ * never has to ask:
7
+ *
8
+ * - a signature can carry a compile-time parameter list in `[]` *and* a runtime
9
+ * argument list in `()`, so `fn`/`def` push a small signature state that
10
+ * walks both and then reads what follows as a return type
11
+ * - argument conventions (`owned`, `borrowed`, `inout`, `mut`, `read`, `out`,
12
+ * `ref`) are keywords only inside a signature; `read` and `out` are ordinary
13
+ * names everywhere else and are left alone
14
+ * - `struct` and `trait` replace `class`, and the types worth knowing are
15
+ * `Int`, `SIMD` and `DType` rather than `int` and `float`
16
+ * - f-string interpolations are real expressions, with the braces and the
17
+ * `:>10.2f` format specs scoped apart from the literal text
18
+ *
19
+ * Ordinary identifiers deliberately match no rule: they inherit the block's
20
+ * foreground, which keeps both the markup and the visual noise down.
21
+ */
22
+ const ID = '[A-Za-z_\\u0080-\\uFFFF][\\w\\u0080-\\uFFFF]*';
23
+ /** `\n`, `\x41`, `☃`, `\033` — Mojo inherits Python's escape set. */
24
+ const ESCAPE = '\\\\(?:x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}|[0-7]{1,3}|\\r?\\n|[\\\\\'"abfnrtv0])';
25
+ /**
26
+ * Argument conventions. They are only keywords when a name follows, which is
27
+ * what keeps `read` and `out` usable as parameter names in the same list —
28
+ * `f(out=1)` names an argument, `f(out result: Int)` declares a convention.
29
+ */
30
+ const CONVENTION = '\\b(?:owned|borrowed|inout|mut|read|out|ref)\\b(?=[ \\t]+[*A-Za-z_])';
31
+ const mojo = {
32
+ name: 'mojo',
33
+ aliases: ['🔥'],
34
+ states: {
35
+ root: {
36
+ rules: [{ include: 'statement' }, { include: 'expression' }],
37
+ },
38
+ // ---- statement-level constructs ----------------------------------------
39
+ statement: {
40
+ rules: [
41
+ { match: '^#!.*', scope: 'comment' },
42
+ // A triple quote at the start of a line is, in practice, always a
43
+ // docstring: a string used as a value is preceded by `name = `.
44
+ {
45
+ match: '^[ \\t]*"""',
46
+ scope: 'comment.doc',
47
+ push: 'docstring-double',
48
+ },
49
+ {
50
+ match: "^[ \\t]*'''",
51
+ scope: 'comment.doc',
52
+ push: 'docstring-single',
53
+ },
54
+ // Anchored to the line so `@parameter` reads as a decorator while the
55
+ // `@` of a matrix product stays an operator.
56
+ {
57
+ match: `^([ \\t]*)(@${ID}(?:\\.${ID})*)`,
58
+ scope: [null, 'decorator'],
59
+ },
60
+ {
61
+ match: `\\b(fn|def)([ \\t]+)(${ID})`,
62
+ scope: ['keyword.declaration', null, 'function'],
63
+ push: 'signature',
64
+ },
65
+ // A struct's `[]` holds parameters, but its `()` holds the traits it
66
+ // conforms to — `struct Matrix[rows: Int](Copyable)`. Only the bracket
67
+ // list gets the parameter state; the paren list is left to the
68
+ // expression rules, where those names read as types.
69
+ {
70
+ match: `\\b(struct|trait)([ \\t]+)(${ID})([ \\t]*)(\\[)`,
71
+ scope: ['keyword.declaration', null, 'class', null, 'punctuation.bracket'],
72
+ push: 'params',
73
+ },
74
+ {
75
+ match: `\\b(struct|trait)([ \\t]+)(${ID})`,
76
+ scope: ['keyword.declaration', null, 'class'],
77
+ },
78
+ {
79
+ match: `\\b(from|import)([ \\t]+)(${ID}(?:\\.${ID})*)`,
80
+ scope: ['keyword.import', null, 'namespace'],
81
+ },
82
+ { match: '\\b(?:import|from)\\b', scope: 'keyword.import' },
83
+ ],
84
+ },
85
+ /**
86
+ * Between a function's name and the `:` that opens its body.
87
+ *
88
+ * `(` both switches this state for `return-type` and pushes the argument
89
+ * list, so when that list pops the grammar is already past the arguments —
90
+ * that is what keeps the `Int` in `-> List[Int]:` a type instead of another
91
+ * parameter name.
92
+ */
93
+ signature: {
94
+ rules: [
95
+ { match: '\\[', scope: 'punctuation.bracket', push: 'params' },
96
+ {
97
+ match: '\\(',
98
+ scope: 'punctuation.bracket',
99
+ set: 'return-type',
100
+ push: 'params',
101
+ },
102
+ { match: ':', scope: 'punctuation.delimiter', pop: true },
103
+ // A half-typed `fn` must not swallow the rest of the file.
104
+ { match: '$', pop: true },
105
+ ],
106
+ },
107
+ 'return-type': {
108
+ rules: [
109
+ { match: ':', scope: 'punctuation.delimiter', pop: true },
110
+ { match: '$', pop: true },
111
+ { include: 'expression' },
112
+ ],
113
+ },
114
+ // ---- expressions -------------------------------------------------------
115
+ expression: {
116
+ rules: [
117
+ { match: '#[^\\n]*', scope: 'comment' },
118
+ { include: 'strings' },
119
+ { include: 'numbers' },
120
+ { match: '\\.\\.\\.', scope: 'constant.builtin' },
121
+ // Matched at the dot, so `obj.print` never reads as the builtin. A
122
+ // parametric method call — `p.load[4]()` — is read as a member instead,
123
+ // because `.data[0]` is the far more common shape.
124
+ {
125
+ match: `(\\.)([ \\t]*)(${ID})(?=[ \\t]*\\()`,
126
+ scope: ['punctuation', null, 'function.method'],
127
+ },
128
+ {
129
+ match: '(\\.)([ \\t]*)(__\\w+__)',
130
+ scope: ['punctuation', null, 'variable.special'],
131
+ },
132
+ {
133
+ match: `(\\.)([ \\t]*)(${ID})`,
134
+ scope: ['punctuation', null, 'variable.member'],
135
+ },
136
+ { match: '\\bNone\\b', scope: 'constant.builtin' },
137
+ { match: '\\b(?:True|False)\\b', scope: 'boolean' },
138
+ { match: '\\b(?:self|cls)\\b', scope: 'variable.builtin' },
139
+ {
140
+ match: '\\b(?:if|elif|else|for|while|break|continue|return|yield|await|raise|try|except|finally|with|pass|assert|del)\\b',
141
+ scope: 'keyword.control',
142
+ },
143
+ {
144
+ match: '\\b(?:fn|def|struct|trait|alias|var|let|async|global|nonlocal|lambda)\\b',
145
+ scope: 'keyword.declaration',
146
+ },
147
+ { match: '\\b(?:raises|capturing|as)\\b', scope: 'keyword' },
148
+ { match: '\\b(?:and|or|not|in|is)\\b', scope: 'keyword.operator' },
149
+ { match: '\\b__\\w+__\\b', scope: 'variable.special' },
150
+ {
151
+ match: '\\b(?:abs|align_?of|all|any|bin|chr|constrained|debug_assert|enumerate|hex|isinstance|len|max|min|oct|open|ord|pow|print|range|rebind|repr|reversed|round|simd_?width_?of|size_?of|sorted|str|sum|swap|zip)\\b',
152
+ scope: 'function.builtin',
153
+ },
154
+ // `U?Int(?:8|16|32|64)?` collapses the ten sized integers into one
155
+ // branch. Only the primitives are listed: the stdlib traits and the
156
+ // rarer collections are CapWords, so the heuristic below already reads
157
+ // them as types and listing them would buy a shade of colour per name.
158
+ {
159
+ match: '\\b(?:U?Int(?:8|16|32|64)?|Float(?:16|32|64)|SIMD|Scalar|DType|Bool|String(?:Literal|Slice)?|List|Dict|Set|Tuple|Optional|UnsafePointer|Pointer|AnyType|Error|Self)\\b',
160
+ scope: 'type.builtin',
161
+ },
162
+ // SCREAMING_CASE reads as a constant; CapWords as a type. Between them
163
+ // these cover module constants, structs, traits and aliases without
164
+ // shipping a name list.
165
+ { match: '\\b[A-Z][A-Z0-9_]+\\b', scope: 'constant' },
166
+ { match: '\\b[A-Z][A-Za-z0-9_]*\\b', scope: 'type' },
167
+ // Mojo keeps PEP 8, so a keyword argument's `=` has no spaces around it
168
+ // and an assignment's does, which makes them tellable apart.
169
+ { match: `\\b${ID}(?==(?!=))`, scope: 'variable.parameter' },
170
+ { match: `\\b${ID}(?=[ \\t]*\\()`, scope: 'function.call' },
171
+ // `^` is Mojo's transfer sigil as well as xor; both are operators.
172
+ {
173
+ match: '->|:=|\\*\\*=?|//=?|<<=?|>>=?|[-+*/%@&|^~<>!=]=?',
174
+ scope: 'operator',
175
+ },
176
+ { match: '[()\\[\\]{}]', scope: 'punctuation.bracket' },
177
+ { match: '[,;:]', scope: 'punctuation.delimiter' },
178
+ ],
179
+ },
180
+ numbers: {
181
+ rules: [
182
+ { match: '\\b0[xX](?:_?[0-9a-fA-F])+\\b', scope: 'number' },
183
+ { match: '\\b0[oO](?:_?[0-7])+\\b', scope: 'number' },
184
+ { match: '\\b0[bB](?:_?[01])+\\b', scope: 'number' },
185
+ {
186
+ match: '\\b\\d(?:_?\\d)*\\.(?:\\d(?:_?\\d)*)?(?:[eE][-+]?\\d(?:_?\\d)*)?|\\.\\d(?:_?\\d)*(?:[eE][-+]?\\d(?:_?\\d)*)?|\\b\\d(?:_?\\d)*(?:[eE][-+]?\\d(?:_?\\d)*)?\\b',
187
+ scope: 'number',
188
+ },
189
+ ],
190
+ },
191
+ // ---- strings -----------------------------------------------------------
192
+ // Prefixes are scoped separately so `rf"..."` reads as a modifier on the
193
+ // literal rather than as part of it. Every flavour then shares one body:
194
+ // a `{}` is a compile-time interpolation in an f-string and a runtime
195
+ // placeholder in an argument to `String.format`, and neither is literal
196
+ // text, so one set of states covers both instead of two near-copies.
197
+ strings: {
198
+ rules: [
199
+ {
200
+ match: '[rRfF]{0,2}(""")',
201
+ scope: ['string'],
202
+ rest: 'string.special',
203
+ push: 'string-triple-double',
204
+ },
205
+ {
206
+ match: "[rRfF]{0,2}(''')",
207
+ scope: ['string'],
208
+ rest: 'string.special',
209
+ push: 'string-triple-single',
210
+ },
211
+ {
212
+ match: '[rRfF]{0,2}(")',
213
+ scope: ['string'],
214
+ rest: 'string.special',
215
+ push: 'string-double',
216
+ },
217
+ {
218
+ match: "[rRfF]{0,2}(')",
219
+ scope: ['string'],
220
+ rest: 'string.special',
221
+ push: 'string-single',
222
+ },
223
+ ],
224
+ },
225
+ 'string-body': {
226
+ rules: [
227
+ { match: ESCAPE, scope: 'string.escape' },
228
+ // Doubled braces are literal text, not an interpolation.
229
+ { match: '\\{\\{|\\}\\}', scope: 'string.escape' },
230
+ { match: '\\{', scope: 'interpolation', push: 'interpolation' },
231
+ ],
232
+ },
233
+ 'string-double': {
234
+ default: 'string',
235
+ rules: [
236
+ { include: 'string-body' },
237
+ { match: '"', scope: 'string', pop: true },
238
+ // An unterminated single-quote string ends at the line break instead of
239
+ // swallowing the rest of the file.
240
+ { match: '$', pop: true },
241
+ ],
242
+ },
243
+ 'string-single': {
244
+ default: 'string',
245
+ rules: [{ include: 'string-body' }, { match: "'", scope: 'string', pop: true }, { match: '$', pop: true }],
246
+ },
247
+ 'string-triple-double': {
248
+ default: 'string',
249
+ rules: [{ include: 'string-body' }, { match: '"""', scope: 'string', pop: true }],
250
+ },
251
+ 'string-triple-single': {
252
+ default: 'string',
253
+ rules: [{ include: 'string-body' }, { match: "'''", scope: 'string', pop: true }],
254
+ },
255
+ 'docstring-double': {
256
+ default: 'comment.doc',
257
+ rules: [{ match: '"""', pop: true }],
258
+ },
259
+ 'docstring-single': {
260
+ default: 'comment.doc',
261
+ rules: [{ match: "'''", pop: true }],
262
+ },
263
+ interpolation: {
264
+ rules: [
265
+ { match: '\\}', scope: 'interpolation', pop: true },
266
+ // Everything from `:` to the closing brace is a format spec, not more
267
+ // expression. The length cap is what keeps an unclosed `{` cheap: the
268
+ // scan is retried at every `:` on the line, so an uncapped one is
269
+ // quadratic on a run of colons. No real spec — `:>10.2f`, `:%H:%M:%S`
270
+ // — comes close to the bound.
271
+ { match: ':[^{}\\n]{0,120}(?=\\})', scope: 'string.special' },
272
+ { match: '\\{', scope: 'punctuation.bracket', push: 'brace' },
273
+ { match: '\\(', scope: 'punctuation.bracket', push: 'paren' },
274
+ { match: '\\[', scope: 'punctuation.bracket', push: 'bracket' },
275
+ { include: 'expression' },
276
+ ],
277
+ },
278
+ // ---- balanced nesting --------------------------------------------------
279
+ // Used wherever a `,` or `)` means something structural, so a comma inside
280
+ // `SIMD[DType.float32, 4]` does not end a parameter.
281
+ paren: {
282
+ rules: [{ match: '\\)', scope: 'punctuation.bracket', pop: true }, { include: 'nested' }],
283
+ },
284
+ bracket: {
285
+ rules: [{ match: '\\]', scope: 'punctuation.bracket', pop: true }, { include: 'nested' }],
286
+ },
287
+ brace: {
288
+ rules: [{ match: '\\}', scope: 'punctuation.bracket', pop: true }, { include: 'nested' }],
289
+ },
290
+ nested: {
291
+ rules: [
292
+ { match: '\\(', scope: 'punctuation.bracket', push: 'paren' },
293
+ { match: '\\[', scope: 'punctuation.bracket', push: 'bracket' },
294
+ { match: '\\{', scope: 'punctuation.bracket', push: 'brace' },
295
+ { include: 'expression' },
296
+ ],
297
+ },
298
+ // ---- parameters --------------------------------------------------------
299
+ // Shared by the `[]` parameter list and the `()` argument list: both are
300
+ // `name: type = default` separated by commas, and both close on a bracket.
301
+ params: {
302
+ rules: [
303
+ { match: '[)\\]]', scope: 'punctuation.bracket', pop: true },
304
+ { match: CONVENTION, scope: 'keyword' },
305
+ // `self` keeps its identity in a signature.
306
+ { match: '\\b(?:self|cls)\\b', scope: 'variable.builtin' },
307
+ { match: ':', scope: 'punctuation.delimiter', push: 'param-value' },
308
+ { match: '=(?!=)', scope: 'operator', push: 'param-value' },
309
+ // No leading `[ \t]*`: a pattern that can start on the whitespace in
310
+ // front of a name matches further left than the `self` rule above and
311
+ // would win on position, turning the `self` of `inout self` into an
312
+ // ordinary parameter.
313
+ //
314
+ // The anchor sits *inside* the alternation rather than in front of it.
315
+ // Unanchored, the rule restarts an unbounded identifier scan at every
316
+ // column of a long name and goes quadratic. Either the sigil or a "not
317
+ // already inside a name" position has to be there, and the `(?:…)` keeps
318
+ // `\*{1,2}` as group 1 so the scope array still lines up with the
319
+ // captures.
320
+ //
321
+ // The lookbehind spells out the identifier characters rather than using
322
+ // `\b`, because `\b` is ASCII-only while `ID` is not: `fn f(π)` has no
323
+ // word boundary in front of `π`, so a `\b` would stop scoping it.
324
+ {
325
+ match: `(?:(\\*{1,2})|(?<![\\w\\u0080-\\uFFFF]))(${ID})(?=[ \\t]*[,:=)\\]])`,
326
+ scope: ['operator', 'variable.parameter'],
327
+ },
328
+ { include: 'nested' },
329
+ ],
330
+ },
331
+ /** An annotation or a default: expression syntax, but no parameter names. */
332
+ 'param-value': {
333
+ rules: [{ match: '(?=[,)\\]]|=(?!=))', pop: true }, { include: 'nested' }],
334
+ },
335
+ },
336
+ };
337
+ export default mojo;
@@ -0,0 +1,4 @@
1
+ import type { Grammar } from '../core/types.js';
2
+ declare const nginx: Grammar;
3
+ export default nginx;
4
+ //# sourceMappingURL=nginx.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nginx.d.ts","sourceRoot":"","sources":["../../src/langs/nginx.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AA6E5C,QAAA,MAAM,KAAK,EAAE,OA4GZ,CAAA;AAED,eAAe,KAAK,CAAA"}
@@ -0,0 +1,168 @@
1
+ /**
2
+ * nginx configuration files.
3
+ *
4
+ * nginx has several hundred directives and no way to tell them apart by
5
+ * spelling, so this grammar reads the *shape* of a statement instead of
6
+ * shipping a name list:
7
+ *
8
+ * - the first word of a statement is the directive; a word anywhere else is an
9
+ * argument, which is what keeps `proxy_set_header` and `Host` distinct
10
+ * - a directive whose line ends in `{` opens a context and reads as a keyword,
11
+ * so `server {` and the `server 10.0.0.1:80;` inside an `upstream` — the same
12
+ * word, two meanings — do not collide
13
+ * - `~`, `~*` and `rewrite` hand their operand to a state that takes one whole
14
+ * word, so a regex may contain `{`, `}` or a quoted `;` without derailing the
15
+ * block structure
16
+ *
17
+ * Values are separated too: `$variables` expand inside quotes the way nginx
18
+ * expands them, paths and URLs are literals, and `10m`/`30s`/`1d` split into a
19
+ * number and a unit.
20
+ */
21
+ /**
22
+ * A directive name — always the first word of a statement.
23
+ *
24
+ * The trailing `\b` changes nothing about what matches, because `\w*` is greedy
25
+ * and the position after it is a boundary already. It is there to stop the
26
+ * engine backtracking into the name when the `OPENS_BLOCK` lookahead after it
27
+ * fails: without it every shorter prefix of the name re-runs that 300-character
28
+ * scan, which is a second of CPU on a single long word.
29
+ */
30
+ const NAME = '[A-Za-z_]\\w*\\b';
31
+ /**
32
+ * Where a statement may begin: the start of a line, or straight after the `;`
33
+ * or `{` that ended the previous one. Anchoring here is what lets the same word
34
+ * be a directive in one column and a bare argument in another, and it keeps the
35
+ * rule from being retried at every column of a long line.
36
+ */
37
+ const STATEMENT = '(?:^|(?<=[;{]))[ \\t]*';
38
+ /**
39
+ * The rest of the line is `… {`, so this directive opens a context.
40
+ *
41
+ * The scan stops at `;` and the brace has to end the line, which is what keeps
42
+ * a `{` inside a rewrite regex (`^/a{2}$ /b last;`) or inside a quoted value
43
+ * from promoting an ordinary directive. A trailing comment after the brace is
44
+ * allowed because it is common. Each run is capped at 300 characters so a
45
+ * pathological line cannot make this quadratic.
46
+ *
47
+ * Every run stops at `{` rather than running to the cap, and at most three
48
+ * braces may come before the one that opens the block. That bound is what makes
49
+ * the check cheap on brace-dense input: a line of `${a${a` is a statement start
50
+ * every three characters, and letting the scan try every brace in range costs
51
+ * the full window at each one — a hundred times what the line is worth. Three
52
+ * covers `^/v[0-9]{1,2}/x$ {` and anything else written by hand; past that the
53
+ * directive reads as a simple one, the same way the one-liner below does.
54
+ *
55
+ * The heuristic misses the one-liner `location / { return 404; }`, which reads
56
+ * as a simple directive. Only the colour is wrong — the braces still nest.
57
+ */
58
+ const OPENS_BLOCK = '(?=[^;{\\n]{0,300}(?:\\{[^;{\\n]{0,300}){0,3}\\{[ \\t\\r]*(?:#[^\\n]*)?$)';
59
+ /**
60
+ * An unquoted regular expression operand: one whitespace-delimited word.
61
+ *
62
+ * `{` and `}` are ordinary characters here, so `^/v[0-9]{1,2}/x$` stays one
63
+ * token and its braces never reach the block rules. A leading `{` is refused
64
+ * anyway, so a `location ~` with a missing operand cannot swallow the body. A
65
+ * `)` only continues the operand when something non-terminal follows it, which
66
+ * keeps the closing paren of `if ($x ~ ^(a|b)$)` out of the regex; the cost is
67
+ * that a regex genuinely ending in `)` loses its last character, and quoting it
68
+ * makes it exact again.
69
+ */
70
+ const REGEX_WORD = '[^\\s;{](?:[^\\s;)]|\\)(?=[^\\s;]))*';
71
+ const nginx = {
72
+ name: 'nginx',
73
+ aliases: ['nginxconf', 'nginx-conf'],
74
+ states: {
75
+ root: {
76
+ rules: [
77
+ { match: '#[^\\n]*', scope: 'comment' },
78
+ // `rewrite` is the one directive whose first argument is a bare regex
79
+ // with no `~` to announce it, so it hands over explicitly.
80
+ {
81
+ match: `${STATEMENT}(rewrite)\\b`,
82
+ scope: ['property'],
83
+ push: 'regex',
84
+ },
85
+ { match: `${STATEMENT}(${NAME})${OPENS_BLOCK}`, scope: ['keyword'] },
86
+ { match: `${STATEMENT}(${NAME})`, scope: ['property'] },
87
+ // Quotes get a state because nginx allows a value to run across lines
88
+ // and expands `$variables` inside single quotes as well as double.
89
+ { match: '"', scope: 'string', push: 'string-double' },
90
+ { match: "'", scope: 'string', push: 'string-single' },
91
+ { include: 'variables' },
92
+ // `~` and `~*` and their negations always introduce a regex, whether in
93
+ // a `location`, an `if`, a `map` key or a `server_name`.
94
+ { match: '!?~\\*?', scope: 'operator', push: 'regex' },
95
+ { match: '\\^~|!?=', scope: 'operator' },
96
+ // The file tests of `if (-f $request_filename)`. `\B` is what keeps the
97
+ // `-F` of a header name like `X-Forwarded-For` out of this.
98
+ { match: '\\B-!?[fdex]\\b', scope: 'keyword.operator' },
99
+ { match: '\\b(?:on|off)\\b', scope: 'boolean' },
100
+ // Paths and URLs are most of what a config actually says, so they are
101
+ // worth a colour. The scheme is capped rather than open-ended: an
102
+ // unbounded scan for `://` would be retried at every dotted segment of
103
+ // a long hostname, which is quadratic in the length of the line.
104
+ { match: '\\b[a-z][a-z0-9+.-]{0,15}://[^\\s;{}$]*', scope: 'string' },
105
+ // `(?<!\w)` keeps the slash of `application/json` and of `$uri/` out of
106
+ // this: both read as one word rather than as a path.
107
+ { match: '(?<!\\w)/[^\\s;{}$]*', scope: 'string' },
108
+ // `10m`, `30s`, `1d`, and `127.0.0.1` as a single literal. The
109
+ // lookbehind is what stops `TLSv1.2` from ending in a stray number.
110
+ {
111
+ match: '(?<![\\w.])(\\d+(?:\\.\\d+)*)([a-zA-Z]*)',
112
+ scope: ['number', 'unit'],
113
+ },
114
+ { match: '\\{', scope: 'punctuation.bracket', push: 'block' },
115
+ { match: '[()\\[\\]]', scope: 'punctuation.bracket' },
116
+ { match: '[;:,]', scope: 'punctuation.delimiter' },
117
+ ],
118
+ },
119
+ /** A `{ … }` body. Popping on `}` is the whole of how contexts nest. */
120
+ block: {
121
+ rules: [{ match: '\\}', scope: 'punctuation.bracket', pop: true }, { include: 'root' }],
122
+ },
123
+ variables: {
124
+ rules: [
125
+ { match: '\\$\\{[A-Za-z_]\\w*\\}', scope: 'variable' },
126
+ // Covers `$host` and the `$1` capture references a rewrite produces.
127
+ { match: '\\$\\w+', scope: 'variable' },
128
+ ],
129
+ },
130
+ /**
131
+ * The operand of a `~` match or of `rewrite`. Entered right after the
132
+ * operator, so the run of spaces before the operand falls through unscoped,
133
+ * and left again as soon as one operand has been taken.
134
+ */
135
+ regex: {
136
+ rules: [
137
+ // Nothing to take: `location ~ {`, a `#` where the operand should be,
138
+ // or a line truncated mid-edit. Handing the `#` back to `root` is what
139
+ // keeps a trailing comment a comment; a `#` *inside* an operand starts
140
+ // later than the operand does, so leftmost matching still gives the
141
+ // whole word to the regex rule below.
142
+ { match: '(?=[;{#\\n])', pop: true },
143
+ // A quoted operand is the only way to write a regex holding a `;`, and
144
+ // it is still a regex rather than a string.
145
+ { match: '"[^"\\n]{0,512}"', scope: 'regexp', pop: true },
146
+ { match: "'[^'\\n]{0,512}'", scope: 'regexp', pop: true },
147
+ { match: REGEX_WORD, scope: 'regexp', pop: true },
148
+ ],
149
+ },
150
+ 'string-double': {
151
+ default: 'string',
152
+ rules: [
153
+ { match: '\\\\[\\s\\S]', scope: 'string.escape' },
154
+ { include: 'variables' },
155
+ { match: '"', scope: 'string', pop: true },
156
+ ],
157
+ },
158
+ 'string-single': {
159
+ default: 'string',
160
+ rules: [
161
+ { match: '\\\\[\\s\\S]', scope: 'string.escape' },
162
+ { include: 'variables' },
163
+ { match: "'", scope: 'string', pop: true },
164
+ ],
165
+ },
166
+ },
167
+ };
168
+ export default nginx;
@@ -0,0 +1,4 @@
1
+ import type { Grammar } from '../core/types.js';
2
+ declare const objectivec: Grammar;
3
+ export default objectivec;
4
+ //# sourceMappingURL=objectivec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"objectivec.d.ts","sourceRoot":"","sources":["../../src/langs/objectivec.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AA0B5C,QAAA,MAAM,UAAU,EAAE,OA4RjB,CAAA;AAED,eAAe,UAAU,CAAA"}