-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSwift.sublime-syntax
2169 lines (1980 loc) · 69.5 KB
/
Swift.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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%YAML 1.2
---
#
# This file is initially made based on and tested with Swift 5.6.
#
name: Swift
scope: source.swift
version: 2
first_line_match: |-
(?x:
^[#]!/.*\bswift # .swift with shebang
| ^[/]{2}[ ]swift-interface-format-version # .swiftinterface
)
file_extensions: [swift, swiftinterface]
variables:
# https://docs.swift.org/swift-book/ReferenceManual/zzSummaryOfTheGrammar.html
# [ Whitespace ] #
# whitespace_item: ([\x00\x0B\x0C])
line_break: (?:\x0D\x0A?|\x0A) # Line Feed (x0A), Carriage Return (x0D)
inline_space: (?:[\x09\x20]) # Space (x20), Horizontal Tab (x09)
whitespaces: '\x00\x0B\x0C\x0A\x09\x20'
# [ Identifier ] #
identifier_head: "A-Za-z_\
\\x{00A8}\\x{00AA}\\x{00AD}\\x{00AF}\
\\x{00B2}-\\x{00B5}\\x{00B7}-\\x{00BA}\\x{00BC}-\\x{00BE}\
\\x{00C0}-\\x{00D6}\\x{00D8}-\\x{00F6}\\x{00F8}-\\x{00FF}\
\\x{0100}-\\x{02FF}\\x{0370}-\\x{167F}\\x{1681}-\\x{180D}\
\\x{180F}-\\x{1DBF}\\x{1E00}-\\x{1FFF}\\x{200B}-\\x{200D}\
\\x{202A}-\\x{202E}\\x{203F}-\\x{2040}\\x{2054}\
\\x{2060}-\\x{206F}\\x{2070}-\\x{20CF}\\x{2100}-\\x{218F}\
\\x{2460}-\\x{24FF}\\x{2776}-\\x{2793}\\x{2C00}-\\x{2DFF}\
\\x{2E80}-\\x{2FFF}\\x{3004}-\\x{3007}\\x{3021}-\\x{302F}\
\\x{3031}-\\x{303F}\\x{3040}-\\x{D7FF}\\x{F900}-\\x{FD3D}\
\\x{FD40}-\\x{FDCF}\\x{FDF0}-\\x{FE1F}\\x{FE30}-\\x{FE44}\
\\x{FE47}-\\x{FFFD}\
\\x{10000}-\\x{1FFFD}\\x{20000}-\\x{2FFFD}\\x{30000}-\\x{3FFFD}\
\\x{40000}-\\x{4FFFD}\\x{50000}-\\x{5FFFD}\\x{60000}-\\x{6FFFD}\
\\x{70000}-\\x{7FFFD}\\x{80000}-\\x{8FFFD}\\x{90000}-\\x{9FFFD}\
\\x{A0000}-\\x{AFFFD}\\x{B0000}-\\x{BFFFD}\\x{C0000}-\\x{CFFFD}\
\\x{D0000}-\\x{DFFFD}\\x{E0000}-\\x{EFFFD}"
identifier_character: "0-9\
\\x{0300}-\\x{036F}\\x{1DC0}-\\x{1DFF}\
\\x{20D0}-\\x{20FF}\\x{FE20}-\\x{FE2F}\
{{identifier_head}}"
# [ Operator ] #
operator_head: "\\/=\\-+!*%<>&|^~?\
\\x{00A1}-\\x{00A7}\\x{00A9}\\x{00AB}\\x{00AC}\\x{00AE}\
\\x{00B0}-\\x{00B1}\\x{00B6}\\x{00BB}\\x{00BF}\\x{00D7}\\x{00F7}\
\\x{2016}-\\x{2017}\\x{2020}-\\x{2027}\\x{2030}-\\x{203E}\
\\x{2041}-\\x{2053}\\x{2055}-\\x{205E}\\x{2190}-\\x{23FF}\
\\x{2500}-\\x{2775}\\x{2794}-\\x{2BFF}\\x{2E00}-\\x{2E7F}\
\\x{3001}-\\x{3003}\\x{3008}-\\x{3020}\\x{3030}"
operator_character: "{{operator_head}}\
\\x{0300}-\\x{036F}\\x{1DC0}-\\x{1DFF}\\x{20D0}-\\x{20FF}\
\\x{FE00}-\\x{FE0F}\\x{FE20}-\\x{FE2F}\\x{E0100}-\\x{E01EF}"
# [ Patterns ] #
# Reserved words can be declared as an identifier if surrounded by '`'.
# Moreover at call-site, backticks can be omitted.
# However at file-level, this is not the case even if declared with arguments.
#
# Selected words from:
# swift/test/SourceKit/CodeComplete/complete_override.swift.response
# swift/utils/gyb_syntax_support/Token.py
reserved_word: |-
(?x:
(?=
(?:[^.`]|^)
\b(
actor|associatedtype|async|class|convenience|deinit|distributed|dynamic|
enum|extension|fileprivate|final|func|import|indirect|infix|init|
internal|isolated|lazy|let|mutating|nonisolated|nonmutating|open|operator|
override|postfix|precedencegroup|prefix|private|protocol|public|
required|static|struct|subscript|typealias|unowned|var|weak|
# Manually selected words
guard|if|switch|case
)\b(?!\s*[:(])
)
)
file_scope_word: (?=\b(import|protocol|extension|operator|precedencegroup)\b)
identifier: '[`]?\b[{{identifier_head}}][{{identifier_character}}]*?\b[`]?'
non_reserved_identifier: (?:(?!{{reserved_word}}){{identifier}})
identifier_lookahead: (?=\s+?{{identifier}})
operator: '[{{operator_head}}](?!/)(?:([{{operator_character}}]*)?([.])?)*'
dot_operator: (?![{{operator_character}}])[.][.{{operator_character}}]+
operator_negative_lookahead: (?![.{{operator_character}}])
access_levels: '\b(?:(private)|(fileprivate)|(internal)|(public)|(open))\b'
generic_clause_chars: '{{identifier_character}}{{whitespaces}}<:&\.,\s`>'
generic_clause_chars_call_site: '{{generic_clause_chars}}\?\!\(\)\[\]-'
#
# Aside from contexts in Prototypes, a few contexts have a variant with '-pop' suffix:
# - The ones with the suffix pop themselves (so for pushing).
# - The ones without the suffix don't pop itself (so for including).
# So '-pop' variants should not be included unless it is an intended behavior.
#
contexts:
### [ Main ] ##########################################################################
prototype:
- include: comments
main:
- meta_include_prototype: false
- match: ''
push: [statements, shebang]
shebang:
- meta_include_prototype: false
- match: ^\#!
scope: punctuation.definition.comment.swift
set:
- meta_include_prototype: false
- meta_scope: comment.line.shebang.swift
- match: \bswift\b
scope: constant.language.shebang.swift
- match: \n
pop: 1
- match: ^|(?=\S)
pop: 1
# Scope groupings are the rough categories to make a set of contexts reusable easily.
# Each basically belongs to the deepest scope it can appear.
statements:
- include: statements-scope-file
- include: statements-scope-type-declaration
- include: statements-scope-block
- include: statements-scope-parameter
- include: statements-scope-any
statements-scope-file:
- include: declaration-import
- include: declaration-protocol
- include: declaration-extension
- include: declaration-operator
- include: declaration-precedencegroup
statements-scope-type-declaration:
- include: declaration-init-deinit
- include: declaration-subscript
- include: modifier-class
- include: modifier-access-level
- include: modifier-actor-isolation
- include: modifier-reference-counting
statements-scope-block:
- include: declaration-constant
- include: declaration-variable
- include: declaration-typealias
- include: declaration-func
- include: declaration-enum
- include: declaration-struct
- include: declaration-class
- include: declaration-actor
- include: keyword-declarations
- include: modifier-storage
- include: modifier-mutation
- include: modifier-operator
statements-scope-parameter:
- include: modifier-for-parameter
statements-scope-any:
- include: types
- include: punctuations
- include: literals
# Keywords
- include: keyword-operators
- include: keyword-control-flows
# Modifiers
- include: modifier-types
# Variables
- include: variable-builtins
# Scoped Expressions
- include: parameter-list
- include: inner-code-block
- include: square-brackets
# Generics
- include: generic-parameter-clause-call
# [ TODO: Add dedicated where-clause matches for control-flows ] #
- include: generic-where-clause
# Operator
- include: operator-builtins
- include: operator-sigils
- include: operator-custom
# Compiler
- include: compiler-availability
- include: compiler-directive-clause
- include: compiler-literals
# Attribute
- include: attribute-builtins
- include: attribute-custom
# Support
- include: underscored-keyword
- include: identifier-support
- include: function-call
# Consume all the remaining identifiers
# - match: (?:{{non_reserved_identifier}})(?!\s*\()
# scope: invalid.illegal.swift # uncomment to see what's being consumed
### [ Prototypes ] ####################################################################
# Memo:
# When using ***-pop in Prototypes, every chars in a context has to be consumed until
# the position ***-pop is expected to start matching.
# Otherwise, it will pop halfway unexpectedly.
else-pop:
- match: (?=\S)
pop: 1
# To determine the cause of an unintended else-pop, a match below is quite useful:
# - match: '.'
# scope: invalid.swift
immediately-pop:
- match: ''
pop: 1
# access-level-pop, reserved-word-pop are included inside a scope to
# prevent unintended meta-scope nest and keep matching when closing bracket is missing
# by look-ahead words that cannot appear inside the scope.
# [ TODO ] #
# Those "invalid..." scopes should be commented out or removed in the end.
access-level-pop:
- match: ({{access_levels}})(?!\s*[:(])
scope: invalid.illegal.unexpected-word.swift
pop: 1
reserved-word-pop:
- match: ({{reserved_word}})
scope: invalid.illegal.unexpected-word.swift
pop: 1
reserved-word-pop-2:
- match: ({{reserved_word}})
scope: invalid.illegal.unexpected-word.swift
pop: 2
file-scope-word-pop:
- match: ({{file_scope_word}})
scope: invalid.illegal.unexpected-word.swift
pop: 1
identifier-consumer:
- match: '{{identifier}}'
whitespace-consumer:
- match: '[ ]'
### [ Comments ] ######################################################################
comments:
- include: comment-line-doc
- include: comment-line
- include: comment-block-doc
- include: comment-block
comment-line-doc:
- match: ^\s*(///)
captures:
1: punctuation.definition.comment.begin.swift
push:
- meta_scope: comment.line.documentation.swift
- match: $
pop: 1
- match: (`[^`]+?`)
scope: markup.raw.inline.swift
- match: (\s*-\s+)(.+)(?=\:[ ])
captures:
1: markup.list.unnumbered.bullet.swift punctuation.definition.list_item.swift
2: markup.bold.swift punctuation.definition.annotation.swift
comment-line:
- match: (//)\s*(?:(TODO.*|FIXME.*))$
captures:
0: comment.line.double-slash.swift
1: punctuation.definition.comment.begin.swift
2: meta.toc-list.todo.swift comment.line.swift markup.bold.swift
- match: (//)\s*(?:(MARK:.*)|.*)$
captures:
0: comment.line.double-slash.swift
1: punctuation.definition.comment.begin.swift
2: meta.toc-list.swift comment.line.swift markup.bold.swift
comment-block-doc:
- match: /\*\*
scope: punctuation.definition.comment.begin.swift
push:
- meta_scope: comment.block.documentation.swift
- match: .*\*/
scope: punctuation.definition.comment.end.swift
pop: 1
- match: /\*.*\*/
comment-block:
- match: /\*
scope: punctuation.definition.comment.begin.swift
push:
- meta_scope: comment.block.swift
- match: .*\*/
scope: punctuation.definition.comment.end.swift
pop: 1
- match: /\*.*\*/
### [ Punctuations ] ##################################################################
# ':' is not generically consumed here to distinguish
# type-annotation, conformance, key-value, ternary-conditional and others.
punctuations:
- match: \.{{operator_negative_lookahead}}
scope: punctuation.accessor.dot.swift
- match: ','
scope: punctuation.separator.continuation.swift
- match: ->
scope: punctuation.separator.annotation.return-arrow.swift
- match: ';'
scope: punctuation.terminator.swift
- match: \(\)
scope: variable.other.tuple.swift
### [ Literals ] ######################################################################
literals:
- include: floating-point-literal
- include: integer-literal
- include: extended-string-literal
- include: string-literal
- include: boolean-literal
- include: nil-literal
integer-literal:
# binary-literal
- match: \b(0b)([01][01_]*)\b
captures:
0: meta.number.integer.binary.swift
1: constant.numeric.base.swift
2: constant.numeric.value.swift
# octal-literal
- match: \b(0o)([0-7][0-7_]*)\b
captures:
0: meta.number.integer.octal.swift
1: constant.numeric.base.swift
2: constant.numeric.value.swift
# hexadecimal-literal
- match: \b(0x)([0-9a-fA-F][0-9a-fA-F_]*)\b
captures:
0: meta.number.integer.hexadecimal.swift
1: constant.numeric.base.swift
2: constant.numeric.value.swift
# decimal-literal
- match: \b[0-9][0-9_]*\b
scope: meta.number.integer.decimal.swift constant.numeric.value.swift
floating-point-literal:
# floating-point-literal without decimal-fraction
- match: |-
(?x:\b
([0-9][0-9_]*[eE][-+]?)
(?!\.)
([0-9][0-9_]*)
\b)
captures:
0: meta.number.float.decimal.swift
1: constant.numeric.value.swift
2: constant.numeric.value.swift
# floating-point-literal with decimal-fraction
- match: |-
(?x:\b
([0-9][0-9_]*)
(\.)
([0-9][0-9_]*)
([eE][-+]?[0-9][0-9_]*)?
\b)
captures:
0: meta.number.float.decimal.swift
1: constant.numeric.value.swift
2: punctuation.separator.decimal.swift
3: constant.numeric.value.swift
4: constant.numeric.value.swift
# hexadecimal-literal
- match: |-
(?x:\b
(0x[0-9a-fA-F][0-9a-fA-F_]*)
(\.)?
([0-9a-fA-F][0-9a-fA-F_]*)?
([pP][-+]?[0-9][0-9_]*)
\b)
captures:
0: meta.number.float.hexadecimal.swift
1: constant.numeric.base.swift
2: punctuation.separator.decimal.swift
3: constant.numeric.value.swift
4: constant.numeric.value.swift
string-literal:
# multiline-string-literal
- match: (""")(.*?(?="""|$))
captures:
1: punctuation.definition.string.begin.swift
2: invalid.illegal.content-must-begin-on-a-new-line.swift
push:
- meta_include_prototype: false
- meta_scope: meta.string.swift string.quoted.double.block.swift
- match: ([^{{inline_space}}]*)(""")
captures:
1: invalid.illegal.closing-delimiter-must-begin-on-a-new-line.swift
2: punctuation.definition.string.end.swift
pop: 1
- include: string-literal-content
- match: \\{{inline_space}}*{{line_break}}
scope: punctuation.separator.continuation.line.swift
# [ TODO: escaped newline at the last line is not allowed ] #
# How can we capture \ on the last line, before the closing delimiter?
# Or maybe this is overkill as a syntax's feature.
# \ is invalid if it's not at EOL
- match: (?=[^\s]+?\s*\\(?:\s|\s*[^0\\tnru"'(\s]+?))(?!\s*$)
push:
- include: string-literal-content
- match: \\
scope: invalid.illegal.invalid-escape-sequence.swift
pop: 1
# string-literal
- match: \"
scope: punctuation.definition.string.begin.swift
push:
- meta_include_prototype: false
- meta_scope: meta.string.swift string.quoted.double.swift
- match: \"
scope: punctuation.definition.string.end.swift
pop: 1
- match: '{{line_break}}'
scope: invalid.illegal.unterminated-string-literal.swift
pop: 1
- include: string-literal-content
- match: \\
scope: invalid.illegal.invalid-escape-sequence.swift
string-literal-content:
# escaped-character
- match: \\[0\\tnr"']
scope: constant.character.escape.swift
# unicode-scalar-digits
- match: \\u\{([0-9a-fA-F]{1,8})\}
scope: constant.character.escape.unicode.swift
captures:
1: constant.numeric.value.swift
# interpolated-text-item
- match: \\\(
scope: punctuation.section.interpolation.begin.swift
push:
- clear_scopes: 1
- meta_scope: meta.interpolation.swift
- include: statements-scope-any
- match: \)
scope: punctuation.section.interpolation.end.swift
pop: 1
extended-string-literal:
# multiline extended-string-literal
- match: (\#+)(""")(.*?(?="""|$))
captures:
1: punctuation.definition.annotation.begin.swift
2: punctuation.definition.string.begin.swift
3: invalid.illegal.content-must-begin-on-a-new-line.swift
push:
- meta_include_prototype: false
- meta_scope: meta.string.swift string.quoted.double.block.swift
- match: ([^{{inline_space}}]*)(""")(#+)
captures:
1: invalid.illegal.closing-delimiter-must-begin-on-a-new-line.swift
2: punctuation.definition.string.end.swift
3: punctuation.definition.annotation.end.swift
pop: 1
- include: extended-string-literal-content
- match: '\\{{inline_space}}*{{line_break}}'
scope: punctuation.separator.continuation.line.swift
- match: (?=[^\s]+?\s*\\#\s*[^0\\tnru"'(\s]+?)(?!\s*$)
push:
- match: \\#
scope: invalid.illegal.invalid-escape-sequence.swift
pop: 1
# extended-string-literal
- match: (#+)(")
captures:
1: punctuation.definition.annotation.begin.swift
2: punctuation.definition.string.begin.swift
push:
- meta_scope: meta.string.swift string.quoted.double.swift
- match: (")(#+)
captures:
1: punctuation.definition.string.end.swift
2: punctuation.definition.annotation.end.swift
pop: 1
- match: '{{line_break}}'
scope: invalid.illegal.unterminated-string-literal.swift
pop: 1
- include: extended-string-literal-content
extended-string-literal-content:
# escaped-character
- match: \\#+[0\\tnr"']
scope: constant.character.escape.swift
# unicode-scalar-digits
- match: \\#+u\{([0-9a-fA-F]{1,8})\}
scope: constant.character.escape.unicode.swift
captures:
1: constant.numeric.value.swift
# interpolated-text-item
- match: \\#+\(
scope: punctuation.section.interpolation.begin.swift
push:
- clear_scopes: 1
- meta_scope: meta.interpolation.swift
- include: statements-scope-any
- match: \)
scope: punctuation.section.interpolation.end.swift
pop: 1
boolean-literal:
- match: \b(true|false)\b
scope: constant.language.boolean.swift
nil-literal:
- match: \bnil\b
scope: constant.language.null.swift
### [ Operators ] #####################################################################
operator-builtins:
# swift/stdlib/public/core/Policy.swift
- match: |-
(?x:
(?:
# SIMD
(\.(?:==|!=|<(?!<)=?|>(?!>)=?))
| (\.(?:[&|^]=?))
| (\.!)
# Masked Arithmetic (with Assignment)
| (&[+\-*]=?)
# Arithmetic (with Assignment)
| ([+\-*\/%]=?)
# Pattern Matching
| (~=)
# Logical
| (!(?=[^=)\]\s,])(?!$)|&&|\|\|)
# Bit Shift (with Assignment)
| (&?(?:<<|>>)=?)
# Bitwise (with Assignment)
| ([&|^]=?)
| (~)
# Identity
| (===|!==)
# Comparison
| (==|!=|<(?!<)=?|>(?!>)=?)
# Range Expressions
| (\.\.[.<])
# Nil-Coalescing
| \s(\?\?)
)
{{operator_negative_lookahead}}
)
captures:
1: keyword.operator.simd.comparison.swift
2: keyword.operator.simd.bitwise.swift
3: keyword.operator.simd.logical.swift
4: keyword.operator.arithmetic.overflow.swift
5: keyword.operator.arithmetic.swift
6: keyword.operator.pattern-matching.swift
7: keyword.operator.logical.swift
8: keyword.operator.bitshift.swift
9: keyword.operator.bitwise.swift
10: keyword.operator.bitwise.swift
11: keyword.operator.identity.swift
12: keyword.operator.comparison.swift
13: keyword.operator.range.swift
14: keyword.operator.nil-coalescing.swift
# Unary
- match: ([+\-])(?=\.?{{identifier}})
captures:
1: keyword.operator.arithmetic.swift
# conditional-operator (ternary conditional)
# Note: without a preceded \s, it'll be interpreted as an optional operator.
- match: (\s+|^)(?=\?(?!\?))
push:
- match: \?
scope: keyword.operator.ternary-conditional.swift
# Consume ':' with statements first - then remained : is ternary.
# [ TODO: Not checked comprehensively yet, there might be ':' without any scope. ] #
- include: statements-scope-any
- match: ':'
scope: keyword.operator.ternary-conditional.swift
pop: 1
- include: reserved-word-pop
# Optional
- match: (?:(\?)|(\!))(?=\s*[?!.,:()\[\];\w]|\s|$)
captures:
1: keyword.operator.optional.swift
2: keyword.operator.implicitly-unwrapped-optional.swift
# Assignment
- match: =
scope: keyword.operator.assignment.swift
operator-sigils:
# implicit-parameter-name:
- match: \$([0-9]+)\b
captures:
0: keyword.operator.implicit-parameter-name.swift
1: meta.number.integer.decimal.swift constant.numeric.value.swift
# key-path-expression
- match: (\\)(?=.*?\.)
captures:
1: keyword.operator.key-path.swift
# property-wrapper-projection
- match: (\$)(?={{identifier}})
captures:
1: keyword.operator.property-wrapper-projection.swift
operator-custom:
# operator
- match: '{{operator}}'
captures:
0: variable.function.custom-operator.swift
2: invalid.illegal.dot-not-allowed.swift
# dot-operator
- match: '{{dot_operator}}'
scope: variable.function.custom-dot-operator.swift
### [ Variables ] #####################################################################
variable-builtins:
# self-expression, superclass-expression
- match: \b(self|super)\b
scope: variable.language.swift
# wildcard-expression
- match: (?:^|\s)(\_)(?=[\s,:])
captures:
1: variable.language.wildcard-expression.swift
### [ Types ] #########################################################################
types:
# protocol-composition-type
# - match: |-
# (?x:
# (?=
# # Weak speculation that both protocols are title-cased, so it's
# # not a bit wise expression (variables should begin lower-cased).
# [A-Z][{{identifier_character}}<&>]*
# \s*&\s*
# [A-Z][{{identifier_character}}<&>]*
# )
# )
# push:
# - match: \&
# scope: keyword.operator.protocol-composition-type.swift
# pop: 1
# - include: generic-parameter-clause-declaration
# - include: identifier-support
# metatype-type
- match: (\.)\b(?:(Type)|(Protocol))\b(?!`)
captures:
1: punctuation.accessor.dot.swift
2: storage.type.metatype.type.swift
3: storage.type.metatype.protocol.swift
# any-type
- match: \bAny\b(?!`)
scope: storage.type.any.swift
# self-type
- match: \bSelf\b(?!`)
scope: storage.type.self.swift
### [ Keywords ] ######################################################################
keyword-operators:
# try-operator
- match: \b(try(?![?!]))\b|(try)(?=\?)|\b(try)\b(?=\!)
captures:
1: keyword.control.exception.try.swift
2: keyword.control.exception.try.optional.swift
3: keyword.control.exception.try.forced.swift
# await-operator
- match: (\bawait\b\s+\btry\b[!?]?)|\b(await)\b
captures:
1: invalid.illegal.try-must-precede-await.swift
2: keyword.control.flow.await.swift
# type-casting-operator (is, as, as?, as!)
- match: \b(is)\b|\b(as)\b(?![?!])|(as)(?=\?)|\b(as)\b(?=\!)
captures:
1: keyword.operator.word.type-casting.is.swift
2: keyword.operator.word.type-casting.as.swift
3: keyword.operator.word.type-casting.as.optional.swift
4: keyword.operator.word.type-casting.as.forced.swift
keyword-control-flows:
# loop-statement
- match: (?:{{identifier}}\s*(:)\s*)?\b(for)\b
captures:
1: punctuation.separator.labeled-statement.swift
2: keyword.control.loop.for.swift
push:
- match: \bin\b
scope: keyword.control.loop.in.swift
pop: 1
- include: statements-scope-any
- include: identifier-consumer
- include: whitespace-consumer
- include: else-pop
- match: (?:{{identifier}}\s*(:)\s*)?\b(?:(while)|(repeat))\b
captures:
1: punctuation.separator.labeled-statement.swift
2: keyword.control.loop.while.swift
3: keyword.control.loop.repeat.swift
# branch-statement
- match: (?:{{identifier}}\s*(:)\s*)?\b(if)\b
captures:
1: punctuation.separator.labeled-statement.swift
2: keyword.control.conditional.if.swift
- match: \b(?:(else\s+if)|(else))\b
captures:
1: keyword.control.conditional.elseif.swift
2: keyword.control.conditional.else.swift
# guard-statement
- match: \b(guard)\b
captures:
1: keyword.control.conditional.guard.swift
# switch-statement
- match: |-
(?x:
(?:{{identifier}}\s*(:)\s*)?
\b(switch)\b(?=(\s+[{{identifier_character}}.]+|\s*\())
)
captures:
1: punctuation.separator.labeled-statement.swift
2: keyword.control.conditional.switch.swift
push:
- include: parameter-list
- include: punctuations
- match: \{
scope: punctuation.section.block.begin.swift
set:
- meta_scope: meta.block.swift meta.switch.swift
- match: \}
scope: punctuation.section.block.end.swift
pop: 1
- match: \b(?:(case)|(default))\b
captures:
1: keyword.control.conditional.case.swift
2: keyword.control.conditional.default.swift
push:
- match: '='
scope: keyword.operator.assignment.swift
pop: 1
- match: ':'
scope: punctuation.separator.swift
pop: 1
- match: \b(?:(var)|(let))\b
captures:
1: keyword.declaration.var.swift
2: keyword.declaration.let.swift
- match: (\.)({{identifier}})
captures:
1: punctuation.accessor.dot.swift
2: constant.other.swift
- match: \bwhere\b
scope: keyword.control.conditional.where.swift
- include: parameter-list
- include: punctuations
- include: statements-scope-block
- include: statements-scope-any
- include: statements-scope-block
- include: statements-scope-any
- include: identifier-consumer
- include: else-pop
- match: (?:{{identifier}}\s*(:)\s*)?\b(switch)\b
captures:
1: punctuation.separator.labeled-statement.swift
2: keyword.control.conditional.switch.swift
- match: \bdefault\b
scope: keyword.control.conditional.default.swift
- match: \b(case)\b(?:\s+(\.)({{identifier}})(:))?
captures:
1: keyword.control.conditional.case.swift
2: punctuation.accessor.dot.swift
3: constant.other.swift
4: punctuation.separator.swift
# control-transfer-statement
- match: \b(?:(break)|(continue)|(fallthrough)|(return)|(throw))\b
captures:
1: keyword.control.flow.break.swift
2: keyword.control.flow.continue.swift
3: keyword.control.flow.fallthrough.swift
4: keyword.control.flow.return.swift
5: keyword.control.flow.throw.swift
# defer-statement
- match: \b(defer)\b
captures:
1: keyword.control.flow.defer.swift
# do-statement
- match: (?:{{identifier}}\s*(:)\s*)?\b(do)\b
captures:
1: punctuation.separator.labeled-statement.swift
2: keyword.control.flow.do.swift
- match: \bcatch\b
scope: keyword.control.exception.catch.swift
# Keywords used in declarations
keyword-declarations:
- match: \bthrows\b
scope: keyword.declaration.throws.swift
- match: \brethrows\b
scope: keyword.declaration.rethrows.swift
- match: \basync\b
scope: keyword.declaration.async.swift
# getter-setter
- match: \bget\b
scope: keyword.declaration.get.swift
- match: \bset\b
scope: keyword.declaration.set.swift
# willSet-didSet
- match: \bwillSet\b
scope: keyword.declaration.willSet.swift
- match: \bdidSet\b
scope: keyword.declaration.didSet.swift
# enumeration
- match: \bindirect\b
scope: storage.modifier.indirect.swift
# - match: \bcase\b
# scope: keyword.declaration.case.swift
# protocol
- match: \b(associatedtype)\b(?:\s+({{identifier}})\s*(:))?
captures:
1: keyword.declaration.associatedtype.swift
2: support.other.associatedtype.swift
3: punctuation.separator.annotation.type-annotation.swift
### [ Modifiers ] #####################################################################
modifier-class:
- match: |-
(?x:
\b(?:
(?:(class)(?=\s+(?:var|func)\b))
| (convenience) | (dynamic) | (final)
| (optional) | (override) | (required)
)\b
(?=\s|$)
)
captures:
1: storage.modifier.class.swift
2: storage.modifier.convenience.swift
3: storage.modifier.dynamic.swift
4: storage.modifier.final.swift
5: storage.modifier.optional.swift
6: storage.modifier.override.swift # also for protocol
7: storage.modifier.required.swift
modifier-storage:
- match: (?:\b(lazy)\b|\b(static)\b)(?=\s|$)
captures:
1: storage.modifier.lazy.swift
2: storage.modifier.static.swift
modifier-access-level:
- match: |-
(?x:
\b(?:{{access_levels}})\b
(?:\s*(\()\s*\b(?:(set))\b\s*(\)))?
(?=\s|$)
)
captures:
1: storage.modifier.access-level.private.swift
2: storage.modifier.access-level.fileprivate.swift
3: storage.modifier.access-level.internal.swift
4: storage.modifier.access-level.public.swift
5: storage.modifier.access-level.open.swift
6: punctuation.section.parens.begin.swift
7: variable.parameter.setter-access-level.swift
8: punctuation.section.parens.end.swift
modifier-mutation:
- match: \b(?:(mutating)|(nonmutating))\b(?=\s|$)
captures:
1: storage.modifier.mutation.mutating.swift
2: storage.modifier.mutation.nonmutating.swift
modifier-actor-isolation:
- match: \b(nonisolated)\b(?=\s|$)
captures:
1: storage.modifier.actor-isolation.nonisolated.swift
modifier-reference-counting:
- match: \bunowned\b
scope: storage.modifier.unowned.swift
push:
- match: \)
scope: punctuation.section.parens.end.swift
pop: 1
- match: \(
scope: punctuation.section.parens.begin.swift
- match: (?:un)?safe
scope: constant.language.attribute-option.swift
- include: else-pop
- match: \bweak\b
scope: storage.modifier.weak.swift
modifier-types:
# opaque-type
- match: (?:\s|^)(some)(?=\s)
captures:
1: storage.modifier.opaque.swift
# existential-type
- match: (?:\s|^)(any)(?=\s)
captures:
1: storage.modifier.existential.swift
modifier-operator:
- match: \b(?:(prefix)|(postfix)|(infix))\b
captures:
1: storage.modifier.operator.prefix.swift
2: storage.modifier.operator.postfix.swift
3: storage.modifier.operator.infix.swift
modifier-for-parameter:
# in-out-expression
- match: |-
(?x:
\b(inout)\b
| (&)(?={{identifier}})
)
captures:
1: storage.modifier.inout.swift # Valid only inside parens as an argument type
2: keyword.operator.inout.swift # Valid only inside parens at call-site
# actor-isolation
- match: \b(isolated)\b(?=\s|$)
captures:
1: storage.modifier.actor-isolation.isolated.swift
### [ Scoped Expressions ] ############################################################
parameter-list:
- match: \(
scope: punctuation.section.parens.begin.swift
push:
- meta_scope: meta.function.parameters.swift
- match: \)
scope: punctuation.section.parens.end.swift
pop: 1
- include: parameter-list-body
# - match: \s
# - match: \w
# - match: '[^)]'
# scope: invalid.illegal.missing-closing-paren.swift
# pop: 1
parameter-list-pop:
- include: parameter-list
- include: immediately-pop
# - match: (?=\s)
# pop: 1
# - include: else-pop