@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,350 @@
1
+ /**
2
+ * Python.
3
+ *
4
+ * The parts that most highlighters flatten into one color, and that this
5
+ * grammar keeps apart:
6
+ *
7
+ * - f-string interpolations are real expressions, with the braces, `!r`
8
+ * conversions and `:>10.2f` format specs each scoped separately
9
+ * - parameters are tracked through a state machine, so annotations and default
10
+ * values are not mistaken for parameter names (and vice versa)
11
+ * - `self`/`cls`, dunder names, builtins and builtin types are distinct from
12
+ * ordinary identifiers
13
+ * - docstrings read as documentation, not as string literals
14
+ *
15
+ * Ordinary identifiers deliberately match no rule: they inherit the block's
16
+ * foreground, which keeps both the markup and the visual noise down.
17
+ */
18
+ const ID = '[A-Za-z_\\u0080-\\uFFFF][\\w\\u0080-\\uFFFF]*';
19
+ /** `\n`, `\x41`, `☃`, `\N{BULLET}`, `\033`. */
20
+ const ESCAPE = '\\\\(?:x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}|N\\{[^}\\n]*\\}|[0-7]{1,3}|\\r?\\n|[\\\\\'"abfnrtv0])';
21
+ /** printf-style placeholders — still everywhere in real Python. */
22
+ const PERCENT_FORMAT = '%[-+ #0]*(?:\\d+|\\*)?(?:\\.(?:\\d+|\\*))?[hlL]?[diouxXeEfFgGcrsa%]';
23
+ const python = {
24
+ name: 'python',
25
+ aliases: ['py', 'python3'],
26
+ states: {
27
+ root: {
28
+ rules: [{ include: 'statement' }, { include: 'expression' }],
29
+ },
30
+ // ---- statement-level constructs ----------------------------------------
31
+ statement: {
32
+ rules: [
33
+ { match: '^#!.*', scope: 'comment' },
34
+ // A triple quote at the start of a line is, in practice, always a
35
+ // docstring: a string used as a value is preceded by `name = `.
36
+ {
37
+ match: '^[ \\t]*"""',
38
+ scope: 'comment.doc',
39
+ push: 'docstring-double',
40
+ },
41
+ {
42
+ match: "^[ \\t]*'''",
43
+ scope: 'comment.doc',
44
+ push: 'docstring-single',
45
+ },
46
+ {
47
+ match: `^([ \\t]*)(@${ID}(?:\\.${ID})*)`,
48
+ scope: [null, 'decorator'],
49
+ },
50
+ // Consume the opening paren so parameters get their own state.
51
+ {
52
+ match: `\\b(def)([ \\t]+)(${ID})([ \\t]*)(\\()`,
53
+ scope: ['keyword.declaration', null, 'function', null, 'punctuation.bracket'],
54
+ push: 'params',
55
+ },
56
+ {
57
+ match: `\\b(class)([ \\t]+)(${ID})`,
58
+ scope: ['keyword.declaration', null, 'class'],
59
+ },
60
+ {
61
+ match: `\\b(from|import)([ \\t]+)(${ID}(?:\\.${ID})*)`,
62
+ scope: ['keyword.import', null, 'namespace'],
63
+ },
64
+ { match: '\\b(?:import|from)\\b', scope: 'keyword.import' },
65
+ // `match`/`case` are soft keywords: only treat them as control flow in
66
+ // the statement shape that introduces a block.
67
+ {
68
+ match: '^([ \\t]*)(match|case)\\b(?=[^\\n]*:[ \\t]*(?:#[^\\n]*)?$)',
69
+ scope: [null, 'keyword.control'],
70
+ },
71
+ ],
72
+ },
73
+ // ---- expressions -------------------------------------------------------
74
+ expression: {
75
+ rules: [
76
+ { match: '#[^\\n]*', scope: 'comment' },
77
+ { include: 'strings' },
78
+ { include: 'numbers' },
79
+ { match: '\\.\\.\\.', scope: 'constant.builtin' },
80
+ // Matched at the dot, so `obj.print` never reads as the builtin.
81
+ {
82
+ match: `(\\.)([ \\t]*)(${ID})(?=[ \\t]*\\()`,
83
+ scope: ['punctuation', null, 'function.method'],
84
+ },
85
+ {
86
+ match: '(\\.)([ \\t]*)(__\\w+__)',
87
+ scope: ['punctuation', null, 'variable.special'],
88
+ },
89
+ {
90
+ match: `(\\.)([ \\t]*)(${ID})`,
91
+ scope: ['punctuation', null, 'variable.member'],
92
+ },
93
+ {
94
+ match: '\\b(?:None|True|False|NotImplemented|Ellipsis)\\b',
95
+ scope: 'constant.builtin',
96
+ },
97
+ { match: '\\b(?:self|cls|super)\\b', scope: 'variable.builtin' },
98
+ {
99
+ match: '\\b(?:if|elif|else|for|while|break|continue|return|yield|await|raise|try|except|finally|with|pass|assert|del)\\b',
100
+ scope: 'keyword.control',
101
+ },
102
+ {
103
+ match: '\\blambda\\b',
104
+ scope: 'keyword.declaration',
105
+ push: 'lambda-params',
106
+ },
107
+ {
108
+ match: '\\b(?:def|class|async|global|nonlocal)\\b',
109
+ scope: 'keyword.declaration',
110
+ },
111
+ { match: '\\b(?:and|or|not|in|is)\\b', scope: 'keyword.operator' },
112
+ { match: '\\bas\\b', scope: 'keyword' },
113
+ { match: '\\b__\\w+__\\b', scope: 'variable.special' },
114
+ {
115
+ match: '\\b(?:abs|aiter|anext|all|any|ascii|bin|breakpoint|callable|chr|classmethod|compile|delattr|dir|divmod|enumerate|eval|exec|filter|format|getattr|globals|hasattr|hash|help|hex|id|input|isinstance|issubclass|iter|len|locals|map|max|min|next|oct|open|ord|pow|print|property|range|repr|reversed|round|setattr|slice|sorted|staticmethod|sum|vars|zip)\\b',
116
+ scope: 'function.builtin',
117
+ },
118
+ {
119
+ match: '\\b(?:bool|bytearray|bytes|complex|dict|float|frozenset|int|list|memoryview|object|set|str|tuple|type)\\b',
120
+ scope: 'type.builtin',
121
+ },
122
+ // SCREAMING_CASE reads as a constant; CapWords as a type. Between them
123
+ // these cover module constants, classes and the whole exception
124
+ // hierarchy without shipping a name list.
125
+ { match: '\\b[A-Z][A-Z0-9_]+\\b', scope: 'constant' },
126
+ { match: '\\b[A-Z][A-Za-z0-9_]*\\b', scope: 'type' },
127
+ // PEP 8 puts no spaces around a keyword argument's `=` and spaces
128
+ // around an assignment's, which makes them tellable apart.
129
+ { match: `\\b${ID}(?==(?!=))`, scope: 'variable.parameter' },
130
+ { match: `\\b${ID}(?=[ \\t]*\\()`, scope: 'function.call' },
131
+ {
132
+ match: '->|:=|\\*\\*=?|//=?|<<=?|>>=?|[-+*/%@&|^~<>!=]=?',
133
+ scope: 'operator',
134
+ },
135
+ { match: '[()\\[\\]{}]', scope: 'punctuation.bracket' },
136
+ { match: '[,;:]', scope: 'punctuation.delimiter' },
137
+ ],
138
+ },
139
+ numbers: {
140
+ rules: [
141
+ { match: '\\b0[xX](?:_?[0-9a-fA-F])+\\b', scope: 'number' },
142
+ { match: '\\b0[oO](?:_?[0-7])+\\b', scope: 'number' },
143
+ { match: '\\b0[bB](?:_?[01])+\\b', scope: 'number' },
144
+ {
145
+ match: '\\b\\d(?:_?\\d)*\\.(?:\\d(?:_?\\d)*)?(?:[eE][-+]?\\d(?:_?\\d)*)?[jJ]?|\\.\\d(?:_?\\d)*(?:[eE][-+]?\\d(?:_?\\d)*)?[jJ]?|\\b\\d(?:_?\\d)*(?:[eE][-+]?\\d(?:_?\\d)*)?[jJ]?\\b',
146
+ scope: 'number',
147
+ },
148
+ ],
149
+ },
150
+ // ---- strings -----------------------------------------------------------
151
+ // Prefixes are scoped separately so `rb"..."` reads as a modifier on the
152
+ // literal rather than as part of it.
153
+ strings: {
154
+ rules: [
155
+ {
156
+ match: '(?:[rR][fF]|[fF][rR]?)(""")',
157
+ scope: ['string'],
158
+ rest: 'string.special',
159
+ push: 'fstring-triple-double',
160
+ },
161
+ {
162
+ match: "(?:[rR][fF]|[fF][rR]?)(''')",
163
+ scope: ['string'],
164
+ rest: 'string.special',
165
+ push: 'fstring-triple-single',
166
+ },
167
+ {
168
+ match: '(?:[rR][fF]|[fF][rR]?)(")',
169
+ scope: ['string'],
170
+ rest: 'string.special',
171
+ push: 'fstring-double',
172
+ },
173
+ {
174
+ match: "(?:[rR][fF]|[fF][rR]?)(')",
175
+ scope: ['string'],
176
+ rest: 'string.special',
177
+ push: 'fstring-single',
178
+ },
179
+ {
180
+ match: '(?:[rRbBuU]|[rR][bB]|[bB][rR])?(""")',
181
+ scope: ['string'],
182
+ rest: 'string.special',
183
+ push: 'string-triple-double',
184
+ },
185
+ {
186
+ match: "(?:[rRbBuU]|[rR][bB]|[bB][rR])?(''')",
187
+ scope: ['string'],
188
+ rest: 'string.special',
189
+ push: 'string-triple-single',
190
+ },
191
+ {
192
+ match: '(?:[rRbBuU]|[rR][bB]|[bB][rR])?(")',
193
+ scope: ['string'],
194
+ rest: 'string.special',
195
+ push: 'string-double',
196
+ },
197
+ {
198
+ match: "(?:[rRbBuU]|[rR][bB]|[bB][rR])?(')",
199
+ scope: ['string'],
200
+ rest: 'string.special',
201
+ push: 'string-single',
202
+ },
203
+ ],
204
+ },
205
+ 'string-body': {
206
+ rules: [
207
+ { match: ESCAPE, scope: 'string.escape' },
208
+ { match: PERCENT_FORMAT, scope: 'string.special' },
209
+ ],
210
+ },
211
+ 'string-double': {
212
+ default: 'string',
213
+ rules: [
214
+ { include: 'string-body' },
215
+ { match: '"', scope: 'string', pop: true },
216
+ // An unterminated single-quote string ends at the line break instead of
217
+ // swallowing the rest of the file.
218
+ { match: '$', pop: true },
219
+ ],
220
+ },
221
+ 'string-single': {
222
+ default: 'string',
223
+ rules: [{ include: 'string-body' }, { match: "'", scope: 'string', pop: true }, { match: '$', pop: true }],
224
+ },
225
+ 'string-triple-double': {
226
+ default: 'string',
227
+ rules: [{ include: 'string-body' }, { match: '"""', scope: 'string', pop: true }],
228
+ },
229
+ 'string-triple-single': {
230
+ default: 'string',
231
+ rules: [{ include: 'string-body' }, { match: "'''", scope: 'string', pop: true }],
232
+ },
233
+ 'docstring-double': {
234
+ default: 'comment.doc',
235
+ rules: [{ match: '"""', pop: true }],
236
+ },
237
+ 'docstring-single': {
238
+ default: 'comment.doc',
239
+ rules: [{ match: "'''", pop: true }],
240
+ },
241
+ // ---- f-strings ---------------------------------------------------------
242
+ 'fstring-body': {
243
+ rules: [
244
+ { match: ESCAPE, scope: 'string.escape' },
245
+ // Doubled braces are literal text, not an interpolation.
246
+ { match: '\\{\\{|\\}\\}', scope: 'string.escape' },
247
+ { match: '\\{', scope: 'interpolation', push: 'interpolation' },
248
+ ],
249
+ },
250
+ 'fstring-double': {
251
+ default: 'string',
252
+ rules: [{ include: 'fstring-body' }, { match: '"', scope: 'string', pop: true }, { match: '$', pop: true }],
253
+ },
254
+ 'fstring-single': {
255
+ default: 'string',
256
+ rules: [{ include: 'fstring-body' }, { match: "'", scope: 'string', pop: true }, { match: '$', pop: true }],
257
+ },
258
+ 'fstring-triple-double': {
259
+ default: 'string',
260
+ rules: [{ include: 'fstring-body' }, { match: '"""', scope: 'string', pop: true }],
261
+ },
262
+ 'fstring-triple-single': {
263
+ default: 'string',
264
+ rules: [{ include: 'fstring-body' }, { match: "'''", scope: 'string', pop: true }],
265
+ },
266
+ interpolation: {
267
+ rules: [
268
+ { match: '\\}', scope: 'interpolation', pop: true },
269
+ // `f"{value=}"` — self-documenting expressions.
270
+ { match: '=(?=[!:}])', scope: 'string.special' },
271
+ // `!r` conversion and everything from `:` to the closing brace is a
272
+ // format spec, not more expression. The length cap is what keeps an
273
+ // unclosed `{` cheap: the scan runs to the end of the line and then
274
+ // fails the lookahead, once per `:`, so an uncapped one is quadratic on
275
+ // a run of colons. No real spec — `:>8.2f`, `:%H:%M:%S` — comes close
276
+ // to the bound.
277
+ { match: '![rsa](?=[:}])', scope: 'string.special' },
278
+ { match: ':[^{}\\n]{0,120}(?=\\})', scope: 'string.special' },
279
+ { match: '\\{', scope: 'punctuation.bracket', push: 'brace' },
280
+ { match: '\\(', scope: 'punctuation.bracket', push: 'paren' },
281
+ { match: '\\[', scope: 'punctuation.bracket', push: 'bracket' },
282
+ { include: 'expression' },
283
+ ],
284
+ },
285
+ // ---- balanced nesting --------------------------------------------------
286
+ // Used wherever a `,` or `)` means something structural, so a comma inside
287
+ // `Dict[str, int]` does not end a parameter.
288
+ paren: {
289
+ rules: [{ match: '\\)', scope: 'punctuation.bracket', pop: true }, { include: 'nested' }],
290
+ },
291
+ bracket: {
292
+ rules: [{ match: '\\]', scope: 'punctuation.bracket', pop: true }, { include: 'nested' }],
293
+ },
294
+ brace: {
295
+ rules: [{ match: '\\}', scope: 'punctuation.bracket', pop: true }, { include: 'nested' }],
296
+ },
297
+ nested: {
298
+ rules: [
299
+ { match: '\\(', scope: 'punctuation.bracket', push: 'paren' },
300
+ { match: '\\[', scope: 'punctuation.bracket', push: 'bracket' },
301
+ { match: '\\{', scope: 'punctuation.bracket', push: 'brace' },
302
+ { include: 'expression' },
303
+ ],
304
+ },
305
+ // ---- parameters --------------------------------------------------------
306
+ params: {
307
+ rules: [
308
+ { match: '\\)', scope: 'punctuation.bracket', pop: true },
309
+ // `self`/`cls` keep their identity in a signature.
310
+ { match: '\\b(?:self|cls)\\b', scope: 'variable.builtin' },
311
+ { match: ':', scope: 'punctuation.delimiter', push: 'param-value' },
312
+ { match: '=(?!=)', scope: 'operator', push: 'param-value' },
313
+ // The anchor sits *inside* the alternation rather than in front of it.
314
+ // Unanchored, the rule restarts an unbounded identifier scan at every
315
+ // column of a long name and goes quadratic whenever the lookahead fails
316
+ // — an unclosed `(`, which is what an editor sees mid-keystroke. Either
317
+ // the sigil or a "not already inside a name" position has to be there,
318
+ // and the `(?:…)` keeps `\*{1,2}` as group 1 so the scope array still
319
+ // lines up with the captures.
320
+ //
321
+ // The lookbehind spells out the identifier characters rather than using
322
+ // `\b`, because `\b` is ASCII-only while `ID` is not: `def f(π)` has no
323
+ // word boundary in front of `π`, so a `\b` would stop scoping it.
324
+ {
325
+ match: `(?:(\\*{1,2})[ \\t]*|(?<![\\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
+ 'lambda-params': {
336
+ rules: [
337
+ { match: ':', scope: 'punctuation.delimiter', pop: true },
338
+ { match: '=(?!=)', scope: 'operator', push: 'param-value' },
339
+ // Anchored for the same reason as the `params` rule above.
340
+ {
341
+ match: `(?:(\\*{1,2})[ \\t]*|(?<![\\w\\u0080-\\uFFFF]))(${ID})(?=[ \\t]*[,:=])`,
342
+ scope: ['operator', 'variable.parameter'],
343
+ },
344
+ { match: '$', pop: true },
345
+ { include: 'nested' },
346
+ ],
347
+ },
348
+ },
349
+ };
350
+ export default python;
@@ -0,0 +1,4 @@
1
+ import type { Grammar } from '../core/types.js';
2
+ declare const r: Grammar;
3
+ export default r;
4
+ //# sourceMappingURL=r.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"r.d.ts","sourceRoot":"","sources":["../../src/langs/r.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAyE5C,QAAA,MAAM,CAAC,EAAE,OAgOR,CAAA;AAED,eAAe,CAAC,CAAA"}
@@ -0,0 +1,265 @@
1
+ /**
2
+ * R.
3
+ *
4
+ * Three things make R awkward for a regex tokenizer, and each is handled
5
+ * explicitly here:
6
+ *
7
+ * - `.` is an ordinary name character, so `\b` cannot mark the edge of a name.
8
+ * In `model.1` there is a word boundary in front of the `1`, which would read
9
+ * as a number, and in `f.if` there is one in front of `if`. Every name-shaped
10
+ * rule is guarded with `NAME_START` / `NAME_END` instead.
11
+ * - `%…%` is user-definable infix syntax, so the operator set is open-ended.
12
+ * One rule covers `%%`, `%/%`, `%in%`, `%>%` and whatever a package invents.
13
+ * - A `(` is where R decides what a name means, so every `(` pushes a state:
14
+ * `f(n = 5)` names an argument where the same `n = 5` at top level assigns.
15
+ * Definitions push a richer state, because there a bare name is a parameter.
16
+ */
17
+ /**
18
+ * A name may start with `.` (`.Machine`) and may contain `.` and `_`, so the
19
+ * leading class allows a dot but not a digit.
20
+ *
21
+ * The tail is length-capped: `NAME_START` already stops a rule being retried in
22
+ * the middle of a name, and the cap bounds the one scan that does happen so a
23
+ * pathological name cannot make a lookahead rule walk the whole line. A name
24
+ * longer than the cap simply renders unscoped.
25
+ */
26
+ const ID = '[A-Za-z.][\\w.]{0,64}';
27
+ /** Backticks quote a non-syntactic name: `` `p value` ``, `` `%+%` ``. */
28
+ const BACKTICK = '`[^`\\n]*`';
29
+ /** Stands in for `\b` at the start of a name, which `.` would otherwise break. */
30
+ const NAME_START = '(?<![\\w.])';
31
+ /** Stands in for `\b` at the end of a name. */
32
+ const NAME_END = '(?![\\w.])';
33
+ /**
34
+ * `1`, `1.`, `.5`, `1e-3`, `0xFF`, plus R's `L` integer and `i` complex
35
+ * suffixes. Guarded rather than `\b`-anchored so the `1` in `model.1` is left
36
+ * alone.
37
+ */
38
+ const NUMBER = `${NAME_START}(?:0[xX][0-9a-fA-F]+|(?:\\d+\\.?\\d*|\\.\\d+)(?:[eE][-+]?\\d+)?)[Li]?${NAME_END}`;
39
+ /**
40
+ * The atomic modes plus the two classes that behave like them — what `class()`
41
+ * answers and what the `as.*` family converts to. `list`, `vector` and `matrix`
42
+ * read as constructors far more often than as type names, so they sit with the
43
+ * builtin functions below instead.
44
+ */
45
+ const TYPES = `${NAME_START}(?:numeric|character|logical|integer|double|complex|factor|data\\.frame)${NAME_END}`;
46
+ /**
47
+ * Base functions worth their bytes.
48
+ *
49
+ * The list is deliberately short. A name left out of it still reads as a call
50
+ * wherever a `(` follows, so the only names that earn a place are the ones a
51
+ * reader expects to recognise even when passed by name — `sapply(x, mean)`.
52
+ * The `is.*` / `as.*` / `Sys.*` families are matched by shape instead: the list
53
+ * would be enormous, packages add to it, and a user-defined S3 method like
54
+ * `is.station` belongs in the same colour anyway.
55
+ */
56
+ const BUILTINS = `${NAME_START}(?:(?:is|as)\\.[A-Za-z][\\w.]{0,32}|Sys\\.[a-z]\\w{0,32}|` +
57
+ 'c|length|n(?:row|col|ames|char)|seq(?:_len|_along)?|rep|paste0?|sprintf|print|cat|message|warning|' +
58
+ 'stop(?:ifnot)?|sum|mean|min|max|round|sqrt|log|[lsvm]apply|apply|do\\.call|head|tail|g?sub|grepl?|strsplit|' +
59
+ 'to(?:upper|lower)|unlist|which|sort|unique|[rc]bind|ifelse|invisible|list|vector|matrix|tryCatch|Map|Reduce|' +
60
+ `Filter|file\\.(?:exists|path))${NAME_END}`;
61
+ /** printf-style placeholders, which reach R through `sprintf` and `format`. */
62
+ const FORMAT = '%[-+ #0]*(?:\\d+|\\*)?(?:\\.(?:\\d+|\\*))?[diouxXeEfgGsaA%]';
63
+ const r = {
64
+ name: 'r',
65
+ states: {
66
+ root: { rules: [{ include: 'expression' }] },
67
+ expression: {
68
+ rules: [
69
+ // Roxygen is a comment to R and documentation to everyone else. It gets
70
+ // a state so its `@param` tags can be scoped inside the block.
71
+ { match: "#'", scope: 'comment.doc', push: 'roxygen' },
72
+ { match: '#[^\\n]*', scope: 'comment' },
73
+ { include: 'strings' },
74
+ { match: NUMBER, scope: 'number' },
75
+ // `name <- function(…)` and `name <- \(…)`. The keyword is left to the
76
+ // rules below — they are what pushes `params` — so this only claims the
77
+ // name, which is the half R has no keyword for.
78
+ {
79
+ match: `${NAME_START}(${ID}|${BACKTICK})([ \\t]*)(<<-|<-|=)([ \\t]*)(?=function${NAME_END}|\\\\[ \\t]*\\()`,
80
+ scope: ['function', null, 'operator'],
81
+ },
82
+ // The `(` is consumed here so parameters get their own state.
83
+ {
84
+ match: `${NAME_START}(function)([ \\t]*)(\\()`,
85
+ scope: ['keyword.declaration', null, 'punctuation.bracket'],
86
+ push: 'params',
87
+ },
88
+ // `\(x) x + 1`, the shorthand added in R 4.1.
89
+ {
90
+ match: '(\\\\)([ \\t]*)(\\()',
91
+ scope: ['keyword.declaration', null, 'punctuation.bracket'],
92
+ push: 'params',
93
+ },
94
+ {
95
+ match: `${NAME_START}function${NAME_END}`,
96
+ scope: 'keyword.declaration',
97
+ },
98
+ // `library(dplyr)` takes a bare package name, so the argument is a
99
+ // namespace rather than an ordinary variable. Consuming the `(` means
100
+ // this rule has to push `args` itself to keep the bracket balanced.
101
+ {
102
+ match: `${NAME_START}(library|require)([ \\t]*)(\\()([ \\t]*)(${ID})`,
103
+ scope: ['keyword.import', null, 'punctuation.bracket', null, 'namespace'],
104
+ push: 'args',
105
+ },
106
+ {
107
+ match: `${NAME_START}(?:library|require|requireNamespace)${NAME_END}`,
108
+ scope: 'keyword.import',
109
+ },
110
+ // `$` is R's member access and `@` reaches an S4 slot; matching at the
111
+ // operator keeps `df$mean` from reading as the builtin.
112
+ {
113
+ match: `([$@])([ \\t]*)(${ID})([ \\t]*)(\\()`,
114
+ scope: ['punctuation', null, 'function.method', null, 'punctuation.bracket'],
115
+ push: 'args',
116
+ },
117
+ {
118
+ match: `([$@])([ \\t]*)(${ID}|${BACKTICK})`,
119
+ scope: ['punctuation', null, 'variable.member'],
120
+ },
121
+ {
122
+ match: `${NAME_START}(${ID})(:::?)`,
123
+ scope: ['namespace', 'operator'],
124
+ },
125
+ {
126
+ match: `${NAME_START}(?:if|else|for|while|repeat|break|next|return)${NAME_END}`,
127
+ scope: 'keyword.control',
128
+ },
129
+ // `%in%` is spelled like an operator but reads like `in` does elsewhere.
130
+ { match: '%in%', scope: 'keyword.operator' },
131
+ { match: `${NAME_START}in${NAME_END}`, scope: 'keyword.operator' },
132
+ { match: `${NAME_START}(?:TRUE|FALSE)${NAME_END}`, scope: 'boolean' },
133
+ {
134
+ match: `${NAME_START}(?:NULL|NA(?:_(?:integer|real|complex|character)_)?|Inf|NaN)${NAME_END}`,
135
+ scope: 'constant.builtin',
136
+ },
137
+ { match: '\\.\\.\\.', scope: 'variable.parameter' },
138
+ { match: TYPES, scope: 'type.builtin' },
139
+ { match: BUILTINS, scope: 'function.builtin' },
140
+ { match: BACKTICK, scope: 'variable' },
141
+ // Before the name-shape rules below, unlike in most grammars here:
142
+ // R capitalises plenty of ordinary functions (`Map`, `Reduce`,
143
+ // `Sys.time`), so a following `(` outranks the capital.
144
+ { match: `${NAME_START}${ID}(?=[ \\t]*\\()`, scope: 'function.call' },
145
+ // SCREAMING_CASE reads as a constant, CapWords as a class — the R6 and
146
+ // S4 generators (`Person$new()`) are the names this is aimed at. Both
147
+ // need a second character, or the one-letter names R code leans on
148
+ // (`T`, `F`, `N`, `X`) would all come out as classes.
149
+ { match: `${NAME_START}[A-Z][A-Z0-9_]+${NAME_END}`, scope: 'constant' },
150
+ { match: `${NAME_START}[A-Z][\\w.]{1,64}${NAME_END}`, scope: 'type' },
151
+ // `:` is a sequence operator in R rather than a delimiter, so it is
152
+ // scoped with the operators and only `,` and `;` separate.
153
+ {
154
+ // `[&|]` goes last so `&&`, `||` and `|>` all get first refusal.
155
+ // Without it R's elementwise and/or — the common form in a filter —
156
+ // matched nothing.
157
+ match: '<<-|<-|->>|->|\\|>|:::?|%[^%\\n]*%|&&|\\|\\||[-+*/^!<>=]=?|[~?:@$]|[&|]',
158
+ scope: 'operator',
159
+ },
160
+ { match: '\\(', scope: 'punctuation.bracket', push: 'args' },
161
+ { match: '[)\\[\\]{}]', scope: 'punctuation.bracket' },
162
+ { match: '[,;]', scope: 'punctuation.delimiter' },
163
+ ],
164
+ },
165
+ /**
166
+ * Inside `(…)`. Every `(` pushes this, which is the only thing that tells
167
+ * the named argument in `f(n = 5)` from the assignment in `n = 5`.
168
+ */
169
+ args: {
170
+ rules: [
171
+ { match: '\\)', scope: 'punctuation.bracket', pop: true },
172
+ {
173
+ match: `${NAME_START}(${ID}|${BACKTICK})([ \\t]*)(=)(?!=)`,
174
+ scope: ['variable.parameter', null, 'operator'],
175
+ },
176
+ { include: 'expression' },
177
+ ],
178
+ },
179
+ /** A parameter list, where a bare name is a parameter rather than a value. */
180
+ params: {
181
+ rules: [
182
+ // Repeats the named-argument rule from `args` rather than inheriting it,
183
+ // because here the right-hand side is a default that has to stop the
184
+ // bare-name rule below from claiming the value in `f(x = y, z)`.
185
+ {
186
+ match: `${NAME_START}(${ID}|${BACKTICK})([ \\t]*)(=)(?!=)`,
187
+ scope: ['variable.parameter', null, 'operator'],
188
+ push: 'default-value',
189
+ },
190
+ {
191
+ match: `${NAME_START}(?:${ID}|${BACKTICK})(?=[ \\t]*[,)])`,
192
+ scope: 'variable.parameter',
193
+ },
194
+ { include: 'args' },
195
+ ],
196
+ },
197
+ /** A default value: expression syntax, but no more parameter names. */
198
+ 'default-value': {
199
+ rules: [{ match: '(?=[,)])', pop: true }, { include: 'expression' }],
200
+ },
201
+ /** One `#'` line. The zero-width `$` ends it without eating the newline. */
202
+ roxygen: {
203
+ default: 'comment.doc',
204
+ rules: [
205
+ { match: '@\\w+', scope: 'decorator' },
206
+ { match: '$', pop: true },
207
+ ],
208
+ },
209
+ strings: {
210
+ rules: [
211
+ // A raw string is `r"(…)"`, with any of three bracket pairs and any
212
+ // number of dashes for padding. Matching the dashes exactly would need
213
+ // a backreference, so the closer accepts any run of them.
214
+ {
215
+ match: '([rR])(["\'][-]*[(\\[{])',
216
+ scope: ['string.special', 'string'],
217
+ push: 'raw-string',
218
+ },
219
+ { match: '"', scope: 'string', push: 'string-double' },
220
+ { match: "'", scope: 'string', push: 'string-single' },
221
+ ],
222
+ },
223
+ 'string-body': {
224
+ rules: [
225
+ // `\u{…}` and `\U{…}` share one alternative because the brace already
226
+ // says where the escape ends; only the braceless forms need their own
227
+ // digit counts.
228
+ {
229
+ match: '\\\\(?:x[0-9a-fA-F]{1,2}|[uU]\\{[0-9a-fA-F]{1,8}\\}|u[0-9a-fA-F]{1,4}|U[0-9a-fA-F]{1,8}|[0-7]{1,3}|.)',
230
+ scope: 'string.escape',
231
+ },
232
+ { match: FORMAT, scope: 'string.special' },
233
+ ],
234
+ },
235
+ 'string-double': {
236
+ default: 'string',
237
+ rules: [
238
+ { include: 'string-body' },
239
+ { match: '"', scope: 'string', pop: true },
240
+ // R strings may span lines, but an unterminated one should not swallow
241
+ // the file. Stopping at a blank line is the compromise: it is never
242
+ // inside a real literal, and it bounds the damage to one paragraph.
243
+ { match: '\\n[ \\t]*\\n', pop: true },
244
+ ],
245
+ },
246
+ 'string-single': {
247
+ default: 'string',
248
+ rules: [
249
+ { include: 'string-body' },
250
+ { match: "'", scope: 'string', pop: true },
251
+ { match: '\\n[ \\t]*\\n', pop: true },
252
+ ],
253
+ },
254
+ /**
255
+ * Raw string contents: no escapes, and the closer is a bracket followed by
256
+ * the opening quote character. A body holding `)"` — legal, if perverse —
257
+ * ends the literal early.
258
+ */
259
+ 'raw-string': {
260
+ default: 'string',
261
+ rules: [{ match: '[)\\]}][-]*["\']', pop: true }],
262
+ },
263
+ },
264
+ };
265
+ export default r;
@@ -0,0 +1,4 @@
1
+ import type { Grammar } from '../core/types.js';
2
+ declare const ruby: Grammar;
3
+ export default ruby;
4
+ //# sourceMappingURL=ruby.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ruby.d.ts","sourceRoot":"","sources":["../../src/langs/ruby.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AA6D5C,QAAA,MAAM,IAAI,EAAE,OAkVX,CAAA;AAED,eAAe,IAAI,CAAA"}