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 14
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 continue do
22 syn keyword rustKeyword extern nextgroup=rustExternCrate skipwhite
23 syn keyword rustKeyword for in if impl let
24 syn keyword rustKeyword loop once priv pub
25 syn keyword rustKeyword return
26 syn keyword rustKeyword unsafe while
27 syn keyword rustKeyword use nextgroup=rustModPath skipwhite
28 " FIXME: Scoped impl's name is also fallen in this category
29 syn keyword rustKeyword mod trait struct enum type nextgroup=rustIdentifier skipwhite
30 syn keyword rustKeyword fn nextgroup=rustFuncName skipwhite
31 syn keyword rustKeyword proc
32 syn keyword rustStorage mut ref static
33 syn keyword rustObsoleteStorage const
35 syn keyword rustInvalidBareKeyword crate
37 syn keyword rustExternCrate crate contained nextgroup=rustIdentifier skipwhite
39 syn match rustIdentifier contains=rustIdentifierPrime "\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*" display contained
40 syn match rustFuncName "\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*" display contained
42 " Reserved (but not yet used) keywords {{{2
43 syn keyword rustReservedKeyword alignof be offsetof pure sizeof typeof yield
46 syn keyword rustType int uint float char bool u8 u16 u32 u64 f32
47 syn keyword rustType f64 i8 i16 i32 i64 str Self
49 " Things from the prelude (src/libstd/prelude.rs) {{{2
50 " This section is just straight transformation of the contents of the prelude,
51 " to make it easy to update.
54 syn keyword rustTrait Sized
55 syn keyword rustTrait Freeze Send
56 syn keyword rustTrait Add Sub Mul Div Rem Neg Not
57 syn keyword rustTrait BitAnd BitOr BitXor
58 syn keyword rustTrait Drop
59 syn keyword rustTrait Shl Shr Index
60 syn keyword rustEnum Option
61 syn keyword rustEnumVariant Some None
62 syn keyword rustEnum Result
63 syn keyword rustEnumVariant Ok Err
66 "syn keyword rustFunction print println
67 "syn keyword rustFunction range
68 "syn keyword rustFunction from_str
70 " Types and traits {{{3
71 syn keyword rustTrait Any AnyOwnExt AnyRefExt AnyMutRefExt
72 syn keyword rustTrait Ascii AsciiCast OwnedAsciiCast AsciiStr IntoBytes
73 syn keyword rustTrait Bool
74 syn keyword rustTrait ToCStr
75 syn keyword rustTrait Char
76 syn keyword rustTrait Clone DeepClone
77 syn keyword rustTrait Eq Ord TotalEq TotalOrd Ordering Equiv
78 syn keyword rustEnumVariant Less Equal Greater
79 syn keyword rustTrait Container Mutable Map MutableMap Set MutableSet
80 syn keyword rustTrait Default
81 syn keyword rustTrait Hash
82 syn keyword rustTrait FromStr
83 syn keyword rustTrait FromIterator Extendable
84 syn keyword rustTrait Iterator DoubleEndedIterator RandomAccessIterator CloneableIterator
85 syn keyword rustTrait OrdIterator MutableDoubleEndedIterator ExactSize
87 syn keyword rustTrait Algebraic Trigonometric Exponential Hyperbolic
88 syn keyword rustTrait Bitwise Bounded Integer
89 syn keyword rustTrait Num NumCast CheckedAdd CheckedSub CheckedMul CheckedDiv
90 syn keyword rustTrait Orderable Signed Unsigned Round
91 syn keyword rustTrait Primitive Int Float ToStrRadix ToPrimitive FromPrimitive
92 syn keyword rustTrait GenericPath Path PosixPath WindowsPath
93 syn keyword rustTrait RawPtr
94 syn keyword rustTrait Buffer Writer Reader Seek
95 syn keyword rustTrait Str StrVector StrSlice OwnedStr IntoMaybeOwned
96 syn keyword rustTrait IterBytes
97 syn keyword rustTrait ToStr IntoStr
98 syn keyword rustTrait Tuple1 Tuple2 Tuple3 Tuple4
99 syn keyword rustTrait Tuple5 Tuple6 Tuple7 Tuple8
100 syn keyword rustTrait Tuple9 Tuple10 Tuple11 Tuple12
101 syn keyword rustTrait ImmutableEqVector ImmutableTotalOrdVector ImmutableCloneableVector
102 syn keyword rustTrait OwnedVector OwnedCloneableVector OwnedEqVector MutableVector
103 syn keyword rustTrait Vector VectorVector CloneableVector ImmutableVector
105 "syn keyword rustFunction stream
106 syn keyword rustTrait Port Chan GenericChan GenericSmartChan GenericPort Peekable
107 "syn keyword rustFunction spawn
109 syn keyword rustSelf self
110 syn keyword rustBoolean true false
112 syn keyword rustConstant Some None " option
113 syn keyword rustConstant Ok Err " result
114 syn keyword rustConstant Less Equal Greater " Ordering
118 " If foo::bar changes to foo.bar, change this ("::" to "\.").
119 " If foo::bar changes to Foo::bar, change this (first "\w" to "\u").
120 syn match rustModPath "\w\(\w\)*::[^<]"he=e-3,me=e-3
121 syn match rustModPath "\w\(\w\)*" contained " only for 'use path;'
122 syn match rustModPathSep "::"
124 syn match rustFuncCall "\w\(\w\)*("he=e-1,me=e-1
125 syn match rustFuncCall "\w\(\w\)*::<"he=e-3,me=e-3 " foo::<T>();
127 " This is merely a convention; note also the use of [A-Z], restricting it to
128 " latin identifiers rather than the full Unicode uppercase. I have not used
129 " [:upper:] as it depends upon 'noignorecase'
130 "syn match rustCapsIdent display "[A-Z]\w\(\w\)*"
132 syn match rustOperator display "\%(+\|-\|/\|*\|=\|\^\|&\||\|!\|>\|<\|%\)=\?"
133 " This one isn't *quite* right, as we could have binary-& with a reference
134 syn match rustSigil display /&\s\+[&~@*][^)= \t\r\n]/he=e-1,me=e-1
135 syn match rustSigil display /[&~@*][^)= \t\r\n]/he=e-1,me=e-1
136 " This isn't actually correct; a closure with no arguments can be `|| { }`.
137 " Last, because the & in && isn't a sigil
138 syn match rustOperator display "&&\|||"
140 syn match rustMacro '\w\(\w\)*!' contains=rustAssert,rustFail
141 syn match rustMacro '#\w\(\w\)*' contains=rustAssert,rustFail
143 syn match rustSpecialError display contained /\\./
144 syn match rustSpecial display contained /\\\([nrt0\\'"]\|x\x\{2}\|u\x\{4}\|U\x\{8}\)/
145 syn match rustStringContinuation display contained /\\\n\s*/
146 syn region rustString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=rustSpecial,rustSpecialError,rustStringContinuation,@Spell
147 syn region rustString start='r\z(#*\)"' end='"\z1' contains=@Spell
149 syn region rustAttribute start="#\[" end="\]" contains=rustString,rustDeriving
150 syn region rustDeriving start="deriving(" end=")" contained contains=rustTrait
153 syn match rustDecNumber display "\<[0-9][0-9_]*\%([iu]\%(8\|16\|32\|64\)\=\)\="
154 syn match rustHexNumber display "\<0x[a-fA-F0-9_]\+\%([iu]\%(8\|16\|32\|64\)\=\)\="
155 syn match rustOctNumber display "\<0o[0-7_]\+\%([iu]\%(8\|16\|32\|64\)\=\)\="
156 syn match rustBinNumber display "\<0b[01_]\+\%([iu]\%(8\|16\|32\|64\)\=\)\="
158 " Special case for numbers of the form "1." which are float literals, unless followed by
159 " an identifier, which makes them integer literals with a method call or field access.
160 " (This must go first so the others take precedence.)
161 syn match rustFloat display "\<[0-9][0-9_]*\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\@!"
162 " To mark a number as a normal float, it must have at least one of the three things integral values don't have:
163 " a decimal point and more numbers; an exponent; and a type suffix.
164 syn match rustFloat display "\<[0-9][0-9_]*\%(\.[0-9][0-9_]*\)\%([eE][+-]\=[0-9_]\+\)\=\(f32\|f64\)\="
165 syn match rustFloat display "\<[0-9][0-9_]*\%(\.[0-9][0-9_]*\)\=\%([eE][+-]\=[0-9_]\+\)\(f32\|f64\)\="
166 syn match rustFloat display "\<[0-9][0-9_]*\%(\.[0-9][0-9_]*\)\=\%([eE][+-]\=[0-9_]\+\)\=\(f32\|f64\)"
168 " For the benefit of delimitMate
169 syn region rustLifetimeCandidate display start=/&'\%(\([^'\\]\|\\\(['nrt0\\\"]\|x\x\{2}\|u\x\{4}\|U\x\{8}\)\)'\)\@!/ end=/[[:cntrl:][:space:][:punct:]]\@=\|$/ contains=rustSigil,rustLifetime
170 syn region rustGenericRegion display start=/<\%('\|[^[cntrl:][:space:][:punct:]]\)\@=')\S\@=/ end=/>/ contains=rustGenericLifetimeCandidate
171 syn region rustGenericLifetimeCandidate display start=/\%(<\|,\s*\)\@<='/ end=/[[:cntrl:][:space:][:punct:]]\@=\|$/ contains=rustSigil,rustLifetime
173 "rustLifetime must appear before rustCharacter, or chars will get the lifetime highlighting
174 syn match rustLifetime display "\'\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*"
175 syn match rustCharacter /'\([^'\\]\|\\\(.\|x\x\{2}\|u\x\{4}\|U\x\{8}\)\)'/ contains=rustSpecial,rustSpecialError
177 syn cluster rustComment contains=rustCommentLine,rustCommentLineDoc,rustCommentBlock,rustCommentBlockDoc
178 syn region rustCommentLine start="//" end="$" contains=rustTodo,@Spell
179 syn region rustCommentLineDoc start="//\%(//\@!\|!\)" end="$" contains=rustTodo,@Spell
180 syn region rustCommentBlock matchgroup=rustCommentBlock start="/\*\%(!\|\*[*/]\@!\)\@!" end="\*/" contains=rustTodo,@rustComment,@Spell keepend extend
181 syn region rustCommentBlockDoc matchgroup=rustCommentBlockDoc start="/\*\%(!\|\*[*/]\@!\)" end="\*/" contains=rustTodo,@rustComment,@Spell keepend extend
182 " FIXME: this is a really ugly and not fully correct implementation. Most
183 " importantly, a case like ``/* */*`` should have the final ``*`` not being in
184 " a comment, but in practice at present it leaves comments open two levels
185 " deep. But as long as you stay away from that particular case, I *believe*
186 " the highlighting is correct. Due to the way Vim's syntax engine works
187 " (greedy for start matches, unlike Rust's tokeniser which is searching for
188 " the earliest-starting match, start or end), I believe this cannot be solved.
189 " Oh you who would fix it, don't bother with things like duplicating the Block
190 " rules and putting ``\*\@<!`` at the start of them; it makes it worse, as
191 " then you must deal with cases like ``/*/**/*/``. And don't try making it
192 " worse with ``\%(/\@<!\*\)\@<!``, either...
194 syn keyword rustTodo contained TODO FIXME XXX NB NOTE
197 " Trivial folding rules to begin with.
198 " TODO: use the AST to make really good folding
199 syn region rustFoldBraces start="{" end="}" transparent fold
200 " If you wish to enable this, setlocal foldmethod=syntax
201 " It's not enabled by default as it would drive some people mad.
203 " Default highlighting {{{1
204 hi def link rustDecNumber rustNumber
205 hi def link rustHexNumber rustNumber
206 hi def link rustOctNumber rustNumber
207 hi def link rustBinNumber rustNumber
208 hi def link rustIdentifierPrime rustIdentifier
209 hi def link rustTrait rustType
211 hi def link rustSigil StorageClass
212 hi def link rustSpecial Special
213 hi def link rustSpecialError Error
214 hi def link rustStringContinuation Special
215 hi def link rustString String
216 hi def link rustCharacter Character
217 hi def link rustNumber Number
218 hi def link rustBoolean Boolean
219 hi def link rustEnum rustType
220 hi def link rustEnumVariant rustConstant
221 hi def link rustConstant Constant
222 hi def link rustSelf Constant
223 hi def link rustFloat Float
224 hi def link rustOperator Operator
225 hi def link rustKeyword Keyword
226 hi def link rustReservedKeyword Error
227 hi def link rustConditional Conditional
228 hi def link rustIdentifier Identifier
229 hi def link rustCapsIdent rustIdentifier
230 hi def link rustModPath Include
231 hi def link rustModPathSep Delimiter
232 hi def link rustFunction Function
233 hi def link rustFuncName Function
234 hi def link rustFuncCall Function
235 hi def link rustCommentLine Comment
236 hi def link rustCommentLineDoc SpecialComment
237 hi def link rustCommentBlock rustCommentLine
238 hi def link rustCommentBlockDoc rustCommentLineDoc
239 hi def link rustAssert PreCondit
240 hi def link rustFail PreCondit
241 hi def link rustMacro Macro
242 hi def link rustType Type
243 hi def link rustTodo Todo
244 hi def link rustAttribute PreProc
245 hi def link rustDeriving PreProc
246 hi def link rustStorage StorageClass
247 hi def link rustObsoleteStorage Error
248 hi def link rustLifetime Special
249 hi def link rustInvalidBareKeyword Error
250 hi def link rustExternCrate rustKeyword
253 " hi rustAttribute ctermfg=cyan
254 " hi rustDeriving ctermfg=cyan
255 " hi rustAssert ctermfg=yellow
256 " hi rustFail ctermfg=red
257 " hi rustMacro ctermfg=magenta
259 syn sync minlines=200
260 syn sync maxlines=500
262 let b:current_syntax = "rust"