blob: 533d3fb7aca8853580b975ce87772a59fec7957d [file] [log] [blame]
Bram Moolenaar1d9215b2020-01-25 13:27:42 +01001" This source file is part of the Swift.org open source project
2"
3" Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
4" Licensed under Apache License v2.0 with Runtime Library Exception
5"
6" See https://swift.org/LICENSE.txt for license information
7" See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8"
9" Vim syntax file
10" Language: swift
11" Maintainer: Joe Groff <jgroff@apple.com>
12" Last Change: 2018 Jan 21
13"
14" Vim maintainer: Emir SARI <bitigchi@me.com>
15
16if exists("b:current_syntax")
17 finish
18endif
19
20let s:keepcpo = &cpo
21set cpo&vim
22
23syn keyword swiftKeyword
24 \ associatedtype
25 \ break
26 \ catch
27 \ continue
28 \ defer
29 \ do
30 \ else
31 \ fallthrough
32 \ for
33 \ guard
34 \ if
35 \ in
36 \ repeat
37 \ return
38 \ switch
39 \ throw
40 \ try
41 \ where
42 \ while
43syn match swiftMultiwordKeyword
44 \ "indirect case"
45
46syn keyword swiftImport skipwhite skipempty nextgroup=swiftImportModule
47 \ import
48
49syn keyword swiftDefinitionModifier
50 \ convenience
51 \ dynamic
52 \ fileprivate
53 \ final
54 \ internal
55 \ lazy
56 \ nonmutating
57 \ open
58 \ override
59 \ prefix
60 \ private
61 \ public
62 \ required
63 \ rethrows
64 \ static
65 \ throws
66 \ weak
67
68syn keyword swiftInOutKeyword skipwhite skipempty nextgroup=swiftTypeName
69 \ inout
70
71syn keyword swiftIdentifierKeyword
72 \ Self
73 \ metatype
74 \ self
75 \ super
76
77syn keyword swiftFuncKeywordGeneral skipwhite skipempty nextgroup=swiftTypeParameters
78 \ init
79
80syn keyword swiftFuncKeyword
81 \ deinit
82 \ subscript
83
84syn keyword swiftScope
85 \ autoreleasepool
86
87syn keyword swiftMutating skipwhite skipempty nextgroup=swiftFuncDefinition
88 \ mutating
89syn keyword swiftFuncDefinition skipwhite skipempty nextgroup=swiftTypeName,swiftOperator
90 \ func
91
92syn keyword swiftTypeDefinition skipwhite skipempty nextgroup=swiftTypeName
93 \ class
94 \ enum
95 \ extension
96 \ protocol
97 \ struct
98
99syn keyword swiftTypeAliasDefinition skipwhite skipempty nextgroup=swiftTypeAliasName
100 \ typealias
101
102syn match swiftMultiwordTypeDefinition skipwhite skipempty nextgroup=swiftTypeName
103 \ "indirect enum"
104
105syn keyword swiftVarDefinition skipwhite skipempty nextgroup=swiftVarName
106 \ let
107 \ var
108
109syn keyword swiftLabel
110 \ get
111 \ set
112 \ didSet
113 \ willSet
114
115syn keyword swiftBoolean
116 \ false
117 \ true
118
119syn keyword swiftNil
120 \ nil
121
122syn match swiftImportModule contained nextgroup=swiftImportComponent
123 \ /\<[A-Za-z_][A-Za-z_0-9]*\>/
124syn match swiftImportComponent contained nextgroup=swiftImportComponent
125 \ /\.\<[A-Za-z_][A-Za-z_0-9]*\>/
126
127syn match swiftTypeAliasName contained skipwhite skipempty nextgroup=swiftTypeAliasValue
128 \ /\<[A-Za-z_][A-Za-z_0-9]*\>/
129syn match swiftTypeName contained skipwhite skipempty nextgroup=swiftTypeParameters
130 \ /\<[A-Za-z_][A-Za-z_0-9\.]*\>/
131syn match swiftVarName contained skipwhite skipempty nextgroup=swiftTypeDeclaration
132 \ /\<[A-Za-z_][A-Za-z_0-9]*\>/
133syn match swiftImplicitVarName
134 \ /\$\<[A-Za-z_0-9]\+\>/
135
136" TypeName[Optionality]?
137syn match swiftType contained skipwhite skipempty nextgroup=swiftTypeParameters
138 \ /\<[A-Za-z_][A-Za-z_0-9\.]*\>[!?]\?/
139" [Type:Type] (dictionary) or [Type] (array)
140syn region swiftType contained contains=swiftTypePair,swiftType
141 \ matchgroup=Delimiter start=/\[/ end=/\]/
142syn match swiftTypePair contained skipwhite skipempty nextgroup=swiftTypeParameters,swiftTypeDeclaration
143 \ /\<[A-Za-z_][A-Za-z_0-9\.]*\>[!?]\?/
144" (Type[, Type]) (tuple)
145" FIXME: we should be able to use skip="," and drop swiftParamDelim
146syn region swiftType contained contains=swiftType,swiftParamDelim
147 \ matchgroup=Delimiter start="[^@]\?(" end=")" matchgroup=NONE skip=","
148syn match swiftParamDelim contained
149 \ /,/
150" <Generic Clause> (generics)
151syn region swiftTypeParameters contained contains=swiftVarName,swiftConstraint
152 \ matchgroup=Delimiter start="<" end=">" matchgroup=NONE skip=","
153syn keyword swiftConstraint contained
154 \ where
155
156syn match swiftTypeAliasValue skipwhite skipempty nextgroup=swiftType
157 \ /=/
158syn match swiftTypeDeclaration skipwhite skipempty nextgroup=swiftType,swiftInOutKeyword
159 \ /:/
160syn match swiftTypeDeclaration skipwhite skipempty nextgroup=swiftType
161 \ /->/
162
163syn match swiftKeyword
164 \ /\<case\>/
165syn region swiftCaseLabelRegion
166 \ matchgroup=swiftKeyword start=/\<case\>/ matchgroup=Delimiter end=/:/ oneline contains=TOP
167syn region swiftDefaultLabelRegion
168 \ matchgroup=swiftKeyword start=/\<default\>/ matchgroup=Delimiter end=/:/ oneline
169
170syn region swiftParenthesisRegion contains=TOP
171 \ matchgroup=NONE start=/(/ end=/)/
172
173syn region swiftString contains=swiftInterpolationRegion
174 \ start=/"/ skip=/\\\\\|\\"/ end=/"/
175syn region swiftInterpolationRegion contained contains=TOP
176 \ matchgroup=swiftInterpolation start=/\\(/ end=/)/
177syn region swiftComment contains=swiftComment,swiftLineComment,swiftTodo
178 \ start="/\*" end="\*/"
179syn region swiftLineComment contains=swiftComment,swiftTodo
180 \ start="//" end="$"
181
182syn match swiftDecimal
183 \ /[+\-]\?\<\([0-9][0-9_]*\)\([.][0-9_]*\)\?\([eE][+\-]\?[0-9][0-9_]*\)\?\>/
184syn match swiftHex
185 \ /[+\-]\?\<0x[0-9A-Fa-f][0-9A-Fa-f_]*\(\([.][0-9A-Fa-f_]*\)\?[pP][+\-]\?[0-9][0-9_]*\)\?\>/
186syn match swiftOct
187 \ /[+\-]\?\<0o[0-7][0-7_]*\>/
188syn match swiftBin
189 \ /[+\-]\?\<0b[01][01_]*\>/
190
191syn match swiftOperator skipwhite skipempty nextgroup=swiftTypeParameters
192 \ "\.\@<!\.\.\.\@!\|[/=\-+*%<>!&|^~]\@<!\(/[/*]\@![/=\-+*%<>!&|^~]*\|*/\@![/=\-+*%<>!&|^~]*\|->\@![/=\-+*%<>!&|^~]*\|[=+%<>!&|^~][/=\-+*%<>!&|^~]*\)"
193syn match swiftOperator skipwhite skipempty nextgroup=swiftTypeParameters
194 \ "\.\.[<.]"
195
196syn match swiftChar
197 \ /'\([^'\\]\|\\\(["'tnr0\\]\|x[0-9a-fA-F]\{2}\|u[0-9a-fA-F]\{4}\|U[0-9a-fA-F]\{8}\)\)'/
198
199syn match swiftTupleIndexNumber contains=swiftDecimal
200 \ /\.[0-9]\+/
201syn match swiftDecimal contained
202 \ /[0-9]\+/
203
204syn match swiftPreproc
205 \ /#\(\<file\>\|\<line\>\|\<function\>\)/
206syn match swiftPreproc
207 \ /^\s*#\(\<if\>\|\<else\>\|\<elseif\>\|\<endif\>\|\<error\>\|\<warning\>\)/
208syn region swiftPreprocFalse
209 \ start="^\s*#\<if\>\s\+\<false\>" end="^\s*#\(\<else\>\|\<elseif\>\|\<endif\>\)"
210
211syn match swiftAttribute
212 \ /@\<\w\+\>/ skipwhite skipempty nextgroup=swiftType,swiftTypeDefinition
213
214syn keyword swiftTodo MARK TODO FIXME contained
215
216syn match swiftCastOp skipwhite skipempty nextgroup=swiftType
217 \ "\<is\>"
218syn match swiftCastOp skipwhite skipempty nextgroup=swiftType
219 \ "\<as\>[!?]\?"
220
221syn match swiftNilOps
222 \ "??"
223
224syn region swiftReservedIdentifier oneline
225 \ start=/`/ end=/`/
226
227hi def link swiftImport Include
228hi def link swiftImportModule Title
229hi def link swiftImportComponent Identifier
230hi def link swiftKeyword Statement
231hi def link swiftMultiwordKeyword Statement
232hi def link swiftTypeDefinition Define
233hi def link swiftMultiwordTypeDefinition Define
234hi def link swiftType Type
235hi def link swiftTypePair Type
236hi def link swiftTypeAliasName Identifier
237hi def link swiftTypeName Function
238hi def link swiftConstraint Special
239hi def link swiftFuncDefinition Define
240hi def link swiftDefinitionModifier Operator
241hi def link swiftInOutKeyword Define
242hi def link swiftFuncKeyword Function
243hi def link swiftFuncKeywordGeneral Function
244hi def link swiftTypeAliasDefinition Define
245hi def link swiftVarDefinition Define
246hi def link swiftVarName Identifier
247hi def link swiftImplicitVarName Identifier
248hi def link swiftIdentifierKeyword Identifier
249hi def link swiftTypeAliasValue Delimiter
250hi def link swiftTypeDeclaration Delimiter
251hi def link swiftTypeParameters Delimiter
252hi def link swiftBoolean Boolean
253hi def link swiftString String
254hi def link swiftInterpolation Special
255hi def link swiftComment Comment
256hi def link swiftLineComment Comment
257hi def link swiftDecimal Number
258hi def link swiftHex Number
259hi def link swiftOct Number
260hi def link swiftBin Number
261hi def link swiftOperator Function
262hi def link swiftChar Character
263hi def link swiftLabel Operator
264hi def link swiftMutating Statement
265hi def link swiftPreproc PreCondit
266hi def link swiftPreprocFalse Comment
267hi def link swiftAttribute Type
268hi def link swiftTodo Todo
269hi def link swiftNil Constant
270hi def link swiftCastOp Operator
271hi def link swiftNilOps Operator
272hi def link swiftScope PreProc
273
274let b:current_syntax = "swift"
275
276let &cpo = s:keepcpo
277unlet s:keepcpo