" Vim syntax file " Language: Rust " Maintainer: Patrick Walton " Maintainer: Ben Blum " Maintainer: Chris Morgan " Last Change: January 5, 2015 if version < 600 syntax clear elseif exists("b:current_syntax") finish endif " Syntax definitions {{{1 " Basic keywords {{{2 syn keyword rustConditional match if else syn keyword rustOperator as syn match rustAssert "\(); " This is merely a convention; note also the use of [A-Z], restricting it to " latin identifiers rather than the full Unicode uppercase. I have not used " [:upper:] as it depends upon 'noignorecase' "syn match rustCapsIdent display "[A-Z]\w\(\w\)*" syn match rustOperator display "\%(+\|-\|/\|*\|=\|\^\|&\||\|!\|>\|<\|%\)=\?" " This one isn't *quite* right, as we could have binary-& with a reference syn match rustSigil display /&\s\+[&~@*][^)= \t\r\n]/he=e-1,me=e-1 syn match rustSigil display /[&~@*][^)= \t\r\n]/he=e-1,me=e-1 " This isn't actually correct; a closure with no arguments can be `|| { }`. " Last, because the & in && isn't a sigil syn match rustOperator display "&&\|||" syn match rustMacro '\w\(\w\)*!' contains=rustAssert,rustPanic syn match rustMacro '#\w\(\w\)*' contains=rustAssert,rustPanic syn match rustEscapeError display contained /\\./ syn match rustEscape display contained /\\\([nrt0\\'"]\|x\x\{2}\)/ syn match rustEscapeUnicode display contained /\\\(u\x\{4}\|U\x\{8}\)/ syn match rustEscapeUnicode display contained /\\u{\x\{1,6}}/ syn match rustStringContinuation display contained /\\\n\s*/ syn region rustString start=+b"+ skip=+\\\\\|\\"+ end=+"+ contains=rustEscape,rustEscapeError,rustStringContinuation syn region rustString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=rustEscape,rustEscapeUnicode,rustEscapeError,rustStringContinuation,@Spell syn region rustString start='b\?r\z(#*\)"' end='"\z1' contains=@Spell syn region rustAttribute start="#!\?\[" end="\]" contains=rustString,rustDerive syn region rustDerive start="derive(" end=")" contained contains=rustTrait " Number literals syn match rustDecNumber display "\<[0-9][0-9_]*\%([iu]\%(s\|8\|16\|32\|64\)\)\=" syn match rustHexNumber display "\<0x[a-fA-F0-9_]\+\%([iu]\%(s\|8\|16\|32\|64\)\)\=" syn match rustOctNumber display "\<0o[0-7_]\+\%([iu]\%(s\|8\|16\|32\|64\)\)\=" syn match rustBinNumber display "\<0b[01_]\+\%([iu]\%(s\|8\|16\|32\|64\)\)\=" " Special case for numbers of the form "1." which are float literals, unless followed by " an identifier, which makes them integer literals with a method call or field access, " or by another ".", which makes them integer literals followed by the ".." token. " (This must go first so the others take precedence.) syn match rustFloat display "\<[0-9][0-9_]*\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\|\.\)\@!" " To mark a number as a normal float, it must have at least one of the three things integral values don't have: " a decimal point and more numbers; an exponent; and a type suffix. syn match rustFloat display "\<[0-9][0-9_]*\%(\.[0-9][0-9_]*\)\%([eE][+-]\=[0-9_]\+\)\=\(f32\|f64\)\=" syn match rustFloat display "\<[0-9][0-9_]*\%(\.[0-9][0-9_]*\)\=\%([eE][+-]\=[0-9_]\+\)\(f32\|f64\)\=" syn match rustFloat display "\<[0-9][0-9_]*\%(\.[0-9][0-9_]*\)\=\%([eE][+-]\=[0-9_]\+\)\=\(f32\|f64\)" " For the benefit of delimitMate syn region rustLifetimeCandidate display start=/&'\%(\([^'\\]\|\\\(['nrt0\\\"]\|x\x\{2}\|u\x\{4}\|U\x\{8}\)\)'\)\@!/ end=/[[:cntrl:][:space:][:punct:]]\@=\|$/ contains=rustSigil,rustLifetime syn region rustGenericRegion display start=/<\%('\|[^[cntrl:][:space:][:punct:]]\)\@=')\S\@=/ end=/>/ contains=rustGenericLifetimeCandidate syn region rustGenericLifetimeCandidate display start=/\%(<\|,\s*\)\@<='/ end=/[[:cntrl:][:space:][:punct:]]\@=\|$/ contains=rustSigil,rustLifetime "rustLifetime must appear before rustCharacter, or chars will get the lifetime highlighting syn match rustLifetime display "\'\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*" syn match rustCharacterInvalid display contained /b\?'\zs[\n\r\t']\ze'/ " The groups negated here add up to 0-255 but nothing else (they do not seem to go beyond ASCII). syn match rustCharacterInvalidUnicode display contained /b'\zs[^[:cntrl:][:graph:][:alnum:][:space:]]\ze'/ syn match rustCharacter /b'\([^\\]\|\\\(.\|x\x\{2}\)\)'/ contains=rustEscape,rustEscapeError,rustCharacterInvalid,rustCharacterInvalidUnicode syn match rustCharacter /'\([^\\]\|\\\(.\|x\x\{2}\|u\x\{4}\|U\x\{8}\|u{\x\{1,6}}\)\)'/ contains=rustEscape,rustEscapeUnicode,rustEscapeError,rustCharacterInvalid syn region rustCommentLine start="//" end="$" contains=rustTodo,@Spell syn region rustCommentLineDoc start="//\%(//\@!\|!\)" end="$" contains=rustTodo,@Spell syn region rustCommentBlock matchgroup=rustCommentBlock start="/\*\%(!\|\*[*/]\@!\)\@!" end="\*/" contains=rustTodo,rustCommentBlockNest,@Spell syn region rustCommentBlockDoc matchgroup=rustCommentBlockDoc start="/\*\%(!\|\*[*/]\@!\)" end="\*/" contains=rustTodo,rustCommentBlockDocNest,@Spell syn region rustCommentBlockNest matchgroup=rustCommentBlock start="/\*" end="\*/" contains=rustTodo,rustCommentBlockNest,@Spell contained transparent syn region rustCommentBlockDocNest matchgroup=rustCommentBlockDoc start="/\*" end="\*/" contains=rustTodo,rustCommentBlockDocNest,@Spell contained transparent " FIXME: this is a really ugly and not fully correct implementation. Most " importantly, a case like ``/* */*`` should have the final ``*`` not being in " a comment, but in practice at present it leaves comments open two levels " deep. But as long as you stay away from that particular case, I *believe* " the highlighting is correct. Due to the way Vim's syntax engine works " (greedy for start matches, unlike Rust's tokeniser which is searching for " the earliest-starting match, start or end), I believe this cannot be solved. " Oh you who would fix it, don't bother with things like duplicating the Block " rules and putting ``\*\@