-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoonBit.sublime-syntax
472 lines (432 loc) · 14.6 KB
/
MoonBit.sublime-syntax
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
%YAML 1.2
---
# - https://www.sublimetext.com/docs/syntax.html
# - https://docs.moonbitlang.com/en/latest/language/introduction.html
# - https://github.com/moonbitlang/moonbit-docs/blob/main/next/_ext/lexer.py
file_extensions:
- mbt
first_line_match: |-
(?xi:
^ \s* // .*? -\*- .*? \bmoonbit\b .*? -\*- # editorconfig
)
name: MoonBit
scope: source.moonbit
version: 2
variables:
# https://docs.moonbitlang.com/en/latest/language/introduction.html#naming-conventions
ident_lower: '[a-z][\w_]*'
ident_upper: '[A-Z][\w_]*'
ident_path: '[a-z][\w_/]*'
builtin_types: '(?:Eq|Compare|Hash|Show|Logger|Default|ToJson|FromJson|None|Some|ArrayView|Array|FixedArray|Int16|Int64|Int|UInt16|UInt64|UInt|BigInt|Option|Result|BytesView|Bytes?|Bool|Unit|Char|String|Float|Double|Error|Tuple|Hasher|Iter2?|IterResult|Json|Map|Set|StringBuilder|UninitializedArray)'
op_functions: '(?:not|land|lnot|lor|lxor|op_add|op_sub|op_div|op_mul|op_mod|op_equal|op_shl|op_shr)'
escaped_char: '\\(?:[nrtb\"''\\]|x\h{2}|o[0-3][0-7]{2}|u\{(?:\h_*){1,6}\})'
int_suffixes: '[uUlL]+'
dec_literal: '\d[\d_]*'
hex_literal: '\h[\h_]*'
float_exponent: '[eE][+-]?{{dec_literal}}'
float_power: '[pP][+-]?{{dec_literal}}'
contexts:
prototype:
- include: comments
main:
- include: statements
comments:
- include: line-comments
line-comments:
- match: ///\s*(@[_\.\w]+)?
scope: punctuation.definition.comment.moonbit
captures:
1: entity.other.attribute-name.documentation.moonbit
push: doc-comment-body
- match: //
scope: punctuation.definition.comment.moonbit
push: line-comment-body
# https://docs.moonbitlang.com/en/latest/language/docs.html
doc-comment-body:
- meta_include_prototype: false
- meta_scope: comment.block.documentation.moonbit
- match: $\n?
pop: true
line-comment-body:
- meta_include_prototype: false
- meta_scope: comment.line.double-slash.moonbit
- match: $\n?
pop: true
statements:
- include: type-declarations
- include: function-definitions
- include: expressions
type-declarations:
- match: \b(type!?)\s+({{ident_upper}})
captures:
1: keyword.declaration.type.moonbit
2: entity.name.type.moonbit
- match: \b(impl)\s*(\[)
captures:
1: keyword.declaration.trait.moonbit
2: punctuation.definition.generic.begin.moonbit
push: type-parameters
- match: \b(impl)\b
scope: keyword.declaration.trait.moonbit
- match: \b(trait)\s+({{ident_upper}})
captures:
1: keyword.declaration.trait.moonbit
2: entity.name.trait.moonbit
- match: \b(enum)\s+({{ident_upper}})\s*(\[)
captures:
1: keyword.declaration.enum.moonbit
2: entity.name.enum.moonbit
3: punctuation.definition.generic.begin.moonbit
push: type-parameters
- match: \b(enum)\s+({{ident_upper}})
captures:
1: keyword.declaration.enum.moonbit
2: entity.name.enum.moonbit
- match: \b(struct)\s+({{ident_upper}})\s*(\[)
captures:
1: keyword.declaration.struct.moonbit
2: entity.name.struct.moonbit
3: punctuation.definition.generic.begin.moonbit
push: type-parameters
- match: \b(struct)\s+({{ident_upper}})
captures:
1: keyword.declaration.struct.moonbit
2: entity.name.struct.moonbit
function-definitions:
- match: \b(fn)\s+(?:({{ident_upper}})(\:\:))*({{ident_lower}})(!?)\s*(\[)
captures:
1: keyword.declaration.function.moonbit
2: support.type.moonbit
3: punctuation.accessor.double-colon.moonbit
4: entity.name.function.moonbit
5: keyword.declaration.error.moonbit
6: punctuation.definition.generic.begin.moonbit
push: type-parameters
- match: \b(fn)\s+(?:({{ident_upper}})(\:\:))*({{ident_lower}})(!?)\s*(\()
captures:
1: keyword.declaration.function.moonbit
2: support.type.moonbit
3: punctuation.accessor.double-colon.moonbit
4: entity.name.function.moonbit
5: keyword.declaration.error.moonbit
6: punctuation.section.group.begin.moonbit
push: parameters
- match: \b(fn)\s+(main)\s*(\{)
captures:
1: keyword.declaration.function.moonbit
2: keyword.other.moonbit
3: punctuation.section.group.begin.moonbit
- match: \b(with)\s+({{ident_lower}})\s*(\()
captures:
1: keyword.declaration.impl.moonbit
2: entity.name.function.moonbit
3: punctuation.section.group.begin.moonbit
push: parameters
type-parameters:
- match: '(\])\s*(\()'
captures:
1: punctuation.definition.generic.end.moonbit
2: punctuation.section.group.begin.moonbit
set: parameters
- match: \]
scope: punctuation.definition.generic.end.moonbit
pop: true
- match: '{{ident_upper}}'
scope: variable.parameter.generic.moonbit
- include: operators
parameters:
- match: (\))\s*(->)
captures:
1: punctuation.section.group.end.moonbit
2: storage.type.function.arrow.moonbit
set: return-type
- match: \)
scope: punctuation.section.group.end.moonbit
pop: true
- include: expressions
- match: '\b{{ident_lower}}\b'
scope: variable.parameter.moonbit
- match: '\b{{ident_upper}}\b(!?)'
scope: support.type.moonbit
captures:
1: keyword.declaration.error.moonbit
- match: ':'
scope: punctuation.separator.type.moonbit
- match: \(
scope: punctuation.section.group.begin.moonbit
push: brace-body
- match: \[
scope: punctuation.definition.generic.begin.moonbit
push: bracket-body
return-type:
- match: $\n?
pop: true
- match: \{
scope: punctuation.section.group.begin.moonbit
pop: true
- match: '='
scope: keyword.operator.assignment.moonbit
pop: true
- match: \!\!
scope: keyword.declaration.async.moonbit
- match: \!
scope: keyword.declaration.error.moonbit
- match: '({{ident_upper}})(\[)'
captures:
1: support.type.moonbit
2: punctuation.definition.generic.begin.moonbit
push: bracket-body
- include: types
brace-body:
- match: \)
scope: punctuation.section.group.end.moonbit
pop: true
- include: expressions
bracket-body:
- match: \]
scope: punctuation.definition.generic.end.moonbit
pop: true
- include: type-parameters
expressions:
- include: variables
- include: strings
- include: keywords
- include: function-calls
- include: numbers
- include: operators
function-calls:
- match: '\b(not)(\()'
captures:
1: keyword.operator.logical.moonbit
2: punctuation.section.group.begin.moonbit
push: function-arguments
- match: '\b({{ident_lower}}!{,2}\??)\s*(\()'
captures:
1: variable.function.moonbit
2: punctuation.section.group.begin.moonbit
push: function-arguments
function-arguments:
- match: '\)'
scope: punctuation.section.group.end.moonbit
pop: true
- include: expressions
variables:
- match: \b({{ident_upper}})(\:\:)
captures:
1: support.type.moonbit
2: punctuation.accessor.double-colon.moonbit
- match: '(@)({{ident_path}})'
captures:
1: punctuation.definition.variable.moonbit
2: variable.other.constant.moonbit
- match: '({{ident_lower}})(~)'
captures:
1: entity.name.label.moonbit variable.parameter.moonbit
2: punctuation.definition.label.moonbit
- match: '({{ident_lower}})(=)'
captures:
1: variable.parameter.moonbit
2: keyword.operator.assignment.moonbit
types:
- match: \b({{builtin_types}})(!?)\s*(\[)
captures:
1: support.type.builtin.moonbit
2: keyword.declaration.error.moonbit
3: punctuation.definition.generic.begin.moonbit
push: type-parameters
- match: \b({{builtin_types}})\b(!?)(\??)
scope: support.type.builtin.moonbit
captures:
2: keyword.declaration.error.moonbit
3: keyword.declaration.optional.moonbit
- match: \b({{ident_upper}})(!?)\s*(\[)
captures:
1: storage.type.moonbit
2: keyword.declaration.error.moonbit
3: punctuation.definition.generic.begin.moonbit
push: type-parameters
- match: \b({{ident_upper}})\b(!?)(\??)
scope: storage.type.moonbit
captures:
2: keyword.declaration.error.moonbit
3: keyword.declaration.optional.moonbit
keywords:
- include: bool
- match: \b(pub)\((all|open|readonly)\)
captures:
1: storage.modifier.moonbit
2: keyword.other.moonbit
- match: \b(pub|priv)\b
scope: storage.modifier.moonbit
- match: \b(extern|test|init|async|as)\b
scope: keyword.other.moonbit
- include: types
- match: \b(self)\b
scope: keyword.other.moonbit
- match: \b(Self)\b
scope: storage.type.moonbit
- match: \b(let)\b
scope: keyword.declaration.variable.moonbit
- match: \b(const|mut)\b
scope: storage.modifier.moonbit
- match: \b(fn)\b(!?)
scope: keyword.declaration.function.moonbit
captures:
2: keyword.declaration.error.moonbit
- match: \b(impl|with|derive)\b
scope: keyword.declaration.impl.moonbit
- match: \b(type!?|enum|struct|trait|typealias)\b
scope: keyword.declaration.type.moonbit
- match: \b(guard|else|for|if|loop|match|while|in)\b
scope: keyword.control.moonbit
- match: \b(catch!)
scope: keyword.control.moonbit
- match: \b(try|catch|raise)\b
scope: keyword.control.moonbit
- match: \b(break|continue)\b
scope: keyword.control.moonbit
- match: \b(return)\b
scope: keyword.control.moonbit
# https://docs.moonbitlang.com/en/latest/language/fundamentals.html#boolean
bool:
- match: \b(false)\b
scope: constant.language.boolean.false.moonbit
- match: \b(true)\b
scope: constant.language.boolean.true.moonbit
# https://docs.moonbitlang.com/en/latest/language/fundamentals.html#number
numbers:
- include: floats
- include: integers
- include: bigints
floats:
- match: '\b0x{{hex_literal}}\.(?:{{hex_literal}})?(?:{{float_exponent}})?(?:{{float_power}})?'
scope: constant.numeric.float.moonbit
- match: '\b{{dec_literal}}\.(?:{{dec_literal}})?(?:{{float_exponent}})?(?:{{float_power}})?'
scope: constant.numeric.float.moonbit
integers:
- match: '\b({{dec_literal}})({{int_suffixes}})?\b'
captures:
1: constant.numeric.integer.decimal.moonbit
2: constant.numeric.suffix.moonbit
- match: '\b(0x{{hex_literal}})({{int_suffixes}})?\b'
captures:
1: constant.numeric.integer.hexadecimal.moonbit
2: constant.numeric.suffix.moonbit
- match: '\b(0o[0-7][0-7_]*)({{int_suffixes}})?\b'
captures:
1: constant.numeric.integer.octal.moonbit
2: constant.numeric.suffix.moonbit
- match: '\b(0b[0-1][0-1_]*)({{int_suffixes}})?\b'
captures:
1: constant.numeric.integer.binary.moonbit
2: constant.numeric.suffix.moonbit
bigints:
- match: '\b({{dec_literal}})([nN])\b'
captures:
1: constant.numeric.integer.decimal.moonbit
2: constant.numeric.suffix.moonbit
- match: '\b(0x{{hex_literal}})([nN])\b'
captures:
1: constant.numeric.integer.decimal.moonbit
2: constant.numeric.suffix.moonbit
# https://docs.moonbitlang.com/en/latest/language/fundamentals.html#string
strings:
- match: '(#\|)?b"'
scope: punctuation.definition.string.begin.moonbit
push: binary-string-content
- match: '"'
scope: punctuation.definition.string.begin.moonbit
push: string-content
- match: '#\|'
scope: punctuation.definition.string.begin.moonbit
push: raw-string-content
- match: '\$\|'
scope: punctuation.definition.string.begin.moonbit
push: escape-string-content
- match: "'"
scope: punctuation.definition.string.begin.moonbit
push: char-content
raw-string-content:
- meta_include_prototype: false
- meta_scope: string.unquoted.moonbit
- match: $\n?
pop: true
escape-string-content:
- meta_include_prototype: false
- meta_scope: string.unquoted.moonbit
- match: $\n?
pop: true
- include: string-interpolations
- include: escaped-chars
string-content:
- meta_include_prototype: false
- meta_scope: string.quoted.double.moonbit
- match: '"'
scope: punctuation.definition.string.end.moonbit
pop: true
- include: string-interpolations
- include: escaped-chars
- include: internal-functions
internal-functions:
- match: '%[_\.\w]+'
scope: keyword.other.moonbit
binary-string-content:
- meta_include_prototype: false
- meta_scope: string.unquoted.moonbit
- match: '"'
scope: punctuation.definition.string.end.moonbit
pop: true
- include: escaped-chars
char-content:
- meta_include_prototype: false
- meta_scope: string.quoted.single.moonbit
- match: "'"
scope: punctuation.definition.string.end.moonbit
pop: true
- include: escaped-chars
string-interpolations:
- match: '\\{'
scope: punctuation.section.interpolation.begin.moonbit
push: string-interpolation-content
string-interpolation-content:
- clear_scopes: true
- meta_scope: meta.interpolation.moonbit
- meta_content_scope: source.moonbit.embedded
- match: '}'
scope: punctuation.section.interpolation.end.moonbit
pop: true
- include: expressions
escaped-chars:
- match: '{{escaped_char}}'
scope: constant.character.escape.moonbit
operators:
- match: \.\.\.
scope: keyword.operator.word.moonbit
- match: '=>'
scope: keyword.operator.arrow.moonbit
- match: '->'
scope: storage.type.function.arrow.moonbit
- match: ':(?!:)'
scope: punctuation.separator.moonbit
- match: '=(?!=)'
scope: keyword.operator.assignment.moonbit
- match: '\.\.<|\.\.=|\.\.(?!\.)'
scope: keyword.operator.range.moonbit
- match: '~'
scope: punctuation.definition.label.moonbit
- match: ','
scope: punctuation.separator.moonbit
- match: ';'
scope: punctuation.terminator.moonbit
- match: '\.'
scope: punctuation.accessor.dot.moonbit
- match: '{'
scope: punctuation.section.block.begin.moonbit
- match: '}'
scope: punctuation.section.block.end.moonbit
- match: '\?'
scope: keyword.operator.none.moonbit
- match: '\+|-|\*|\/|%|\|>|>>|<<|&&|\|\||\&|\||<=?|>=?|==|!='
scope: keyword.operator.moonbit
- match: \b{{op_functions}}\b
scope: keyword.operator.word.moonbit