3 " Maintainer: Patrick Walton <pcwalton@mozilla.com>
4 " Maintainer: Ben Blum <bblum@cs.cmu.edu>
5 " Maintainer: Chris Morgan <me@chrismorgan.info>
6 " Last Change: 2014 Feb 27
10 elseif exists("b:current_syntax")
14 " Syntax definitions {{{1
16 syn keyword rustConditional match if else
17 syn keyword rustOperator as
19 syn match rustAssert "\<assert\(\w\)*!" contained
20 syn match rustFail "\<fail\(\w\)*!" contained
21 syn keyword rustKeyword break
22 syn keyword rustKeyword box nextgroup=rustBoxPlacement skipwhite skipempty
23 syn keyword rustKeyword continue
24 syn keyword rustKeyword extern nextgroup=rustExternCrate,rustObsoleteExternMod skipwhite skipempty
25 syn keyword rustKeyword fn nextgroup=rustFuncName skipwhite skipempty
26 syn keyword rustKeyword for in if impl let
27 syn keyword rustKeyword loop once proc pub
28 syn keyword rustKeyword return super
29 syn keyword rustKeyword unsafe virtual while
30 syn keyword rustKeyword use nextgroup=rustModPath skipwhite skipempty
31 " FIXME: Scoped impl's name is also fallen in this category
32 syn keyword rustKeyword mod trait struct enum type nextgroup=rustIdentifier skipwhite skipempty
33 syn keyword rustStorage mut ref static
34 syn keyword rustObsoleteStorage const
36 syn keyword rustInvalidBareKeyword crate
38 syn keyword rustExternCrate crate contained nextgroup=rustIdentifier skipwhite skipempty
39 syn keyword rustObsoleteExternMod mod contained nextgroup=rustIdentifier skipwhite skipempty
41 syn match rustIdentifier contains=rustIdentifierPrime "\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*" display contained
42 syn match rustFuncName "\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*" display contained
44 syn region rustBoxPlacement matchgroup=rustBoxPlacementParens start="(" end=")" contains=TOP contained
45 syn keyword rustBoxPlacementExpr GC containedin=rustBoxPlacement
46 " Ideally we'd have syntax rules set up to match arbitrary expressions. Since
47 " we don't, we'll just define temporary contained rules to handle balancing
49 syn region rustBoxPlacementBalance start="(" end=")" containedin=rustBoxPlacement transparent
50 syn region rustBoxPlacementBalance start="\[" end="\]" containedin=rustBoxPlacement transparent
51 " {} are handled by rustFoldBraces
53 " Reserved (but not yet used) keywords {{{2
54 syn keyword rustReservedKeyword alignof be do offsetof priv pure sizeof typeof unsized yield
57 syn keyword rustType int uint float char bool u8 u16 u32 u64 f32
58 syn keyword rustType f64 i8 i16 i32 i64 str Self
60 " Things from the prelude (src/libstd/prelude.rs) {{{2
61 " This section is just straight transformation of the contents of the prelude,
62 " to make it easy to update.
65 syn keyword rustTrait Copy Send Sized Share
66 syn keyword rustTrait Add Sub Mul Div Rem Neg Not
67 syn keyword rustTrait BitAnd BitOr BitXor
68 syn keyword rustTrait Drop Deref DerefMut
69 syn keyword rustTrait Shl Shr Index
70 syn keyword rustEnum Option
71 syn keyword rustEnumVariant Some None
72 syn keyword rustEnum Result
73 syn keyword rustEnumVariant Ok Err
76 "syn keyword rustFunction from_str
77 "syn keyword rustFunction range
78 "syn keyword rustFunction drop
80 " Types and traits {{{3
81 syn keyword rustTrait Ascii AsciiCast OwnedAsciiCast AsciiStr IntoBytes
82 syn keyword rustTrait ToCStr
83 syn keyword rustTrait Char
84 syn keyword rustTrait Clone
85 syn keyword rustTrait Eq Ord TotalEq TotalOrd Ordering Equiv
86 syn keyword rustEnumVariant Less Equal Greater
87 syn keyword rustTrait Container Mutable Map MutableMap Set MutableSet
88 syn keyword rustTrait FromIterator Extendable
89 syn keyword rustTrait Iterator DoubleEndedIterator RandomAccessIterator CloneableIterator
90 syn keyword rustTrait OrdIterator MutableDoubleEndedIterator ExactSize
91 syn keyword rustTrait Num NumCast CheckedAdd CheckedSub CheckedMul
92 syn keyword rustTrait Signed Unsigned
93 syn keyword rustTrait Primitive Int Float FloatMath ToPrimitive FromPrimitive
94 "syn keyword rustTrait Expect
95 syn keyword rustTrait Box
96 syn keyword rustTrait GenericPath Path PosixPath WindowsPath
97 syn keyword rustTrait RawPtr
98 syn keyword rustTrait Buffer Writer Reader Seek
99 syn keyword rustTrait Str StrVector StrSlice OwnedStr IntoMaybeOwned
100 syn keyword rustTrait StrAllocating
101 syn keyword rustTrait ToStr IntoStr
102 syn keyword rustTrait Tuple1 Tuple2 Tuple3 Tuple4
103 syn keyword rustTrait Tuple5 Tuple6 Tuple7 Tuple8
104 syn keyword rustTrait Tuple9 Tuple10 Tuple11 Tuple12
105 syn keyword rustTrait CloneableVector ImmutableCloneableVector MutableCloneableVector
106 syn keyword rustTrait ImmutableVector MutableVector
107 syn keyword rustTrait ImmutableEqVector ImmutableTotalOrdVector MutableTotalOrdVector
108 syn keyword rustTrait Vector VectorVector OwnedVector MutableVectorAllocating
109 syn keyword rustTrait String
110 syn keyword rustTrait Vec
112 "syn keyword rustFunction sync_channel channel
113 syn keyword rustTrait SyncSender Sender Receiver
114 "syn keyword rustFunction spawn
116 "syn keyword rustConstant GC
118 syn keyword rustSelf self
119 syn keyword rustBoolean true false
123 " If foo::bar changes to foo.bar, change this ("::" to "\.").
124 " If foo::bar changes to Foo::bar, change this (first "\w" to "\u").
125 syn match rustModPath "\w\(\w\)*::[^<]"he=e-3,me=e-3
126 syn match rustModPath "\w\(\w\)*" contained " only for 'use path;'
127 syn match rustModPathSep "::"
129 syn match rustFuncCall "\w\(\w\)*("he=e-1,me=e-1
130 syn match rustFuncCall "\w\(\w\)*::<"he=e-3,me=e-3 " foo::<T>();
132 " This is merely a convention; note also the use of [A-Z], restricting it to
133 " latin identifiers rather than the full Unicode uppercase. I have not used
134 " [:upper:] as it depends upon 'noignorecase'
135 "syn match rustCapsIdent display "[A-Z]\w\(\w\)*"
137 syn match rustOperator display "\%(+\|-\|/\|*\|=\|\^\|&\||\|!\|>\|<\|%\)=\?"
138 " This one isn't *quite* right, as we could have binary-& with a reference
139 syn match rustSigil display /&\s\+[&~@*][^)= \t\r\n]/he=e-1,me=e-1
140 syn match rustSigil display /[&~@*][^)= \t\r\n]/he=e-1,me=e-1
141 " This isn't actually correct; a closure with no arguments can be `|| { }`.
142 " Last, because the & in && isn't a sigil
143 syn match rustOperator display "&&\|||"
145 syn match rustMacro '\w\(\w\)*!' contains=rustAssert,rustFail
146 syn match rustMacro '#\w\(\w\)*' contains=rustAssert,rustFail
148 syn match rustEscapeError display contained /\\./
149 syn match rustEscape display contained /\\\([nrt0\\'"]\|x\x\{2}\)/
150 syn match rustEscapeUnicode display contained /\\\(u\x\{4}\|U\x\{8}\)/
151 syn match rustStringContinuation display contained /\\\n\s*/
152 syn region rustString start=+b"+ skip=+\\\\\|\\"+ end=+"+ contains=rustEscape,rustEscapeError,rustStringContinuation
153 syn region rustString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=rustEscape,rustEscapeUnicode,rustEscapeError,rustStringContinuation,@Spell
154 syn region rustString start='b\?r\z(#*\)"' end='"\z1' contains=@Spell
156 syn region rustAttribute start="#!\?\[" end="\]" contains=rustString,rustDeriving
157 syn region rustDeriving start="deriving(" end=")" contained contains=rustTrait
160 syn match rustDecNumber display "\<[0-9][0-9_]*\%([iu]\%(8\|16\|32\|64\)\=\)\="
161 syn match rustHexNumber display "\<0x[a-fA-F0-9_]\+\%([iu]\%(8\|16\|32\|64\)\=\)\="
162 syn match rustOctNumber display "\<0o[0-7_]\+\%([iu]\%(8\|16\|32\|64\)\=\)\="
163 syn match rustBinNumber display "\<0b[01_]\+\%([iu]\%(8\|16\|32\|64\)\=\)\="
165 " Special case for numbers of the form "1." which are float literals, unless followed by
166 " an identifier, which makes them integer literals with a method call or field access.
167 " (This must go first so the others take precedence.)
168 syn match rustFloat display "\<[0-9][0-9_]*\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\@!"
169 " To mark a number as a normal float, it must have at least one of the three things integral values don't have:
170 " a decimal point and more numbers; an exponent; and a type suffix.
171 syn match rustFloat display "\<[0-9][0-9_]*\%(\.[0-9][0-9_]*\)\%([eE][+-]\=[0-9_]\+\)\=\(f32\|f64\)\="
172 syn match rustFloat display "\<[0-9][0-9_]*\%(\.[0-9][0-9_]*\)\=\%([eE][+-]\=[0-9_]\+\)\(f32\|f64\)\="
173 syn match rustFloat display "\<[0-9][0-9_]*\%(\.[0-9][0-9_]*\)\=\%([eE][+-]\=[0-9_]\+\)\=\(f32\|f64\)"
175 " For the benefit of delimitMate
176 syn region rustLifetimeCandidate display start=/&'\%(\([^'\\]\|\\\(['nrt0\\\"]\|x\x\{2}\|u\x\{4}\|U\x\{8}\)\)'\)\@!/ end=/[[:cntrl:][:space:][:punct:]]\@=\|$/ contains=rustSigil,rustLifetime
177 syn region rustGenericRegion display start=/<\%('\|[^[cntrl:][:space:][:punct:]]\)\@=')\S\@=/ end=/>/ contains=rustGenericLifetimeCandidate
178 syn region rustGenericLifetimeCandidate display start=/\%(<\|,\s*\)\@<='/ end=/[[:cntrl:][:space:][:punct:]]\@=\|$/ contains=rustSigil,rustLifetime
180 "rustLifetime must appear before rustCharacter, or chars will get the lifetime highlighting
181 syn match rustLifetime display "\'\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*"
182 syn match rustCharacter /b'\([^'\\]\|\\\(.\|x\x\{2}\)\)'/ contains=rustEscape,rustEscapeError
183 syn match rustCharacter /'\([^'\\]\|\\\(.\|x\x\{2}\|u\x\{4}\|U\x\{8}\)\)'/ contains=rustEscape,rustEscapeUnicode,rustEscapeError
185 syn region rustCommentLine start="//" end="$" contains=rustTodo,@Spell
186 syn region rustCommentLineDoc start="//\%(//\@!\|!\)" end="$" contains=rustTodo,@Spell
187 syn region rustCommentBlock matchgroup=rustCommentBlock start="/\*\%(!\|\*[*/]\@!\)\@!" end="\*/" contains=rustTodo,rustCommentBlockNest,@Spell
188 syn region rustCommentBlockDoc matchgroup=rustCommentBlockDoc start="/\*\%(!\|\*[*/]\@!\)" end="\*/" contains=rustTodo,rustCommentBlockDocNest,@Spell
189 syn region rustCommentBlockNest matchgroup=rustCommentBlock start="/\*" end="\*/" contains=rustTodo,rustCommentBlockNest,@Spell contained transparent
190 syn region rustCommentBlockDocNest matchgroup=rustCommentBlockDoc start="/\*" end="\*/" contains=rustTodo,rustCommentBlockDocNest,@Spell contained transparent
191 " FIXME: this is a really ugly and not fully correct implementation. Most
192 " importantly, a case like ``/* */*`` should have the final ``*`` not being in
193 " a comment, but in practice at present it leaves comments open two levels
194 " deep. But as long as you stay away from that particular case, I *believe*
195 " the highlighting is correct. Due to the way Vim's syntax engine works
196 " (greedy for start matches, unlike Rust's tokeniser which is searching for
197 " the earliest-starting match, start or end), I believe this cannot be solved.
198 " Oh you who would fix it, don't bother with things like duplicating the Block
199 " rules and putting ``\*\@<!`` at the start of them; it makes it worse, as
200 " then you must deal with cases like ``/*/**/*/``. And don't try making it
201 " worse with ``\%(/\@<!\*\)\@<!``, either...
203 syn keyword rustTodo contained TODO FIXME XXX NB NOTE
206 " Trivial folding rules to begin with.
207 " TODO: use the AST to make really good folding
208 syn region rustFoldBraces start="{" end="}" transparent fold
209 " If you wish to enable this, setlocal foldmethod=syntax
210 " It's not enabled by default as it would drive some people mad.
212 " Default highlighting {{{1
213 hi def link rustDecNumber rustNumber
214 hi def link rustHexNumber rustNumber
215 hi def link rustOctNumber rustNumber
216 hi def link rustBinNumber rustNumber
217 hi def link rustIdentifierPrime rustIdentifier
218 hi def link rustTrait rustType
220 hi def link rustSigil StorageClass
221 hi def link rustEscape Special
222 hi def link rustEscapeUnicode rustEscape
223 hi def link rustEscapeError Error
224 hi def link rustStringContinuation Special
225 hi def link rustString String
226 hi def link rustCharacter Character
227 hi def link rustNumber Number
228 hi def link rustBoolean Boolean
229 hi def link rustEnum rustType
230 hi def link rustEnumVariant rustConstant
231 hi def link rustConstant Constant
232 hi def link rustSelf Constant
233 hi def link rustFloat Float
234 hi def link rustOperator Operator
235 hi def link rustKeyword Keyword
236 hi def link rustReservedKeyword Error
237 hi def link rustConditional Conditional
238 hi def link rustIdentifier Identifier
239 hi def link rustCapsIdent rustIdentifier
240 hi def link rustModPath Include
241 hi def link rustModPathSep Delimiter
242 hi def link rustFunction Function
243 hi def link rustFuncName Function
244 hi def link rustFuncCall Function
245 hi def link rustCommentLine Comment
246 hi def link rustCommentLineDoc SpecialComment
247 hi def link rustCommentBlock rustCommentLine
248 hi def link rustCommentBlockDoc rustCommentLineDoc
249 hi def link rustAssert PreCondit
250 hi def link rustFail PreCondit
251 hi def link rustMacro Macro
252 hi def link rustType Type
253 hi def link rustTodo Todo
254 hi def link rustAttribute PreProc
255 hi def link rustDeriving PreProc
256 hi def link rustStorage StorageClass
257 hi def link rustObsoleteStorage Error
258 hi def link rustLifetime Special
259 hi def link rustInvalidBareKeyword Error
260 hi def link rustExternCrate rustKeyword
261 hi def link rustObsoleteExternMod Error
262 hi def link rustBoxPlacementParens Delimiter
263 hi def link rustBoxPlacementExpr rustKeyword
266 " hi rustAttribute ctermfg=cyan
267 " hi rustDeriving ctermfg=cyan
268 " hi rustAssert ctermfg=yellow
269 " hi rustFail ctermfg=red
270 " hi rustMacro ctermfg=magenta
272 syn sync minlines=200
273 syn sync maxlines=500
275 let b:current_syntax = "rust"