vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

hare.vim (11055B)


      1 vim9script
      2 
      3 # Vim syntax file.
      4 # Language:    Hare
      5 # Maintainer:  Amelia Clarke <selene@perilune.dev>
      6 # Last Change: 2026 Feb 15
      7 # Upstream:    https://git.sr.ht/~sircmpwn/hare.vim
      8 
      9 if exists('b:current_syntax')
     10   finish
     11 endif
     12 
     13 # Syntax {{{1
     14 syn case match
     15 syn iskeyword @,48-57,@-@,_
     16 
     17 # Identifiers {{{2
     18 syn cluster hareIdentifier contains=hareName,hareScopeDelimiter,@hareReserved
     19 syn match hareIdentifier '\v<\h\w*%(::\h\w*)*>' contains=@hareIdentifier nextgroup=hareScopeDelimiter,@harePostfix skipempty skipwhite
     20 syn match hareName '\<\h\w*\>' contained transparent
     21 
     22 # Reserved keywords.
     23 syn cluster hareReserved contains=hareBoolean,hareBuiltin,hareConditional,hareConstant,hareDefine,hareInclude,hareKeyword,hareLabel,hareOperator,hareRepeat,hareStatement,hareStorageClass,hareStructure,hareType,hareTypedef
     24 
     25 # Punctuators {{{2
     26 
     27 # Balanced tokens.
     28 syn region hareBraces matchgroup=hareBrace start='{' end='}' contains=TOP fold transparent
     29 syn region hareBrackets matchgroup=hareBracket start='\[' end=']' contains=TOP transparent
     30 syn region hareParens matchgroup=hareParen start='(' end=')' contains=TOP nextgroup=@harePostfix skipempty skipwhite transparent
     31 
     32 # Symbolic operators.
     33 syn match hareSymbolOperator '\.\{2,3}'
     34 syn match hareSymbolOperator '[!<=>]=\?'
     35 syn match hareSymbolOperator '=>'
     36 
     37 # Additive and multiplicative arithmetic.
     38 syn match hareSymbolOperator '[-+*/%]=\?'
     39 
     40 # Bitwise arithmetic.
     41 syn match hareSymbolOperator '\%(<<\|>>\)=\?'
     42 syn match hareSymbolOperator '[&^|]=\?'
     43 syn match hareSymbolOperator '\~'
     44 
     45 # Logical arithmetic.
     46 syn match hareSymbolOperator '\%(&&\|^^\|||\)=\?'
     47 
     48 # Types {{{2
     49 syn cluster hareType contains=hareArray,hareError,harePointer,hareStorageClass,hareStructure,hareTaggedUnion,hareType
     50 syn keyword hareType bool
     51 syn keyword hareType done
     52 syn keyword hareType f32 f64
     53 syn keyword hareType i8 i16 i32 i64 int
     54 syn keyword hareType never
     55 syn keyword hareType nomem
     56 syn keyword hareType opaque
     57 syn keyword hareType rune str
     58 syn keyword hareType u8 u16 u32 u64 uint uintptr
     59 syn keyword hareType void
     60 
     61 # C ABI.
     62 syn keyword hareType valist
     63 
     64 # Pointer types.
     65 syn match harePointer '*' contained containedin=hareTaggedUnion,hareTypeParens nextgroup=@hareType skipempty skipwhite
     66 syn keyword hareStorageClass nullable nextgroup=harePointer skipempty skipwhite
     67 
     68 # Slice and array types.
     69 syn region hareArray matchgroup=hareBracket start='\[' end=']' contained containedin=hareTaggedUnion,hareTypeParens contains=TOP nextgroup=@hareType skipempty skipwhite transparent
     70 syn match hareArray '\[[*_]]' contains=hareArrayBounds nextgroup=@hareType skipempty skipwhite transparent
     71 syn match hareArrayBounds '*' contained display
     72 
     73 # Tagged union and tuple types.
     74 syn region hareTaggedUnion matchgroup=hareParen start='(' end=')' contained containedin=hareTaggedUnion,hareTypeParens contains=TOP transparent
     75 syn match hareTaggedUnionBar '|' contained containedin=hareTaggedUnion
     76 
     77 # Other types.
     78 syn match hareError '!' contained containedin=hareTaggedUnion,hareTypeParens nextgroup=@hareType skipempty skipwhite
     79 syn keyword hareStructure enum struct union
     80 
     81 # Declarations {{{2
     82 syn keyword hareDefine def
     83 syn keyword hareInclude use
     84 syn keyword hareKeyword export static
     85 syn keyword hareKeyword fn nextgroup=@hareFunction,@hareReserved skipempty skipwhite
     86 syn keyword hareStatement const let
     87 syn keyword hareTypedef type nextgroup=hareTypedefBinding,@hareReserved skipempty skipwhite
     88 
     89 # Highlight `const` as a storage-class in places types are expected.
     90 syn keyword hareStorageClass const contained containedin=hareTaggedUnion,hareTypeParens nextgroup=@hareType skipempty skipwhite
     91 
     92 # Function declarations.
     93 syn cluster hareFunction contains=hareFunction,hareFunctionParams
     94 syn match hareFunction '\v<\h\w*%(::\h\w*)*>' contained contains=@hareIdentifier nextgroup=hareFunctionParams skipempty skipwhite
     95 syn region hareFunctionParams matchgroup=hareParen start='(' end=')' contained contains=TOP nextgroup=@hareType skipempty skipwhite transparent
     96 
     97 # Type declarations.
     98 # XXX: Does not yet account for type declarations with multiple bindings.
     99 syn match hareTypedefBinding '\v<\h\w*%(::\h\w*)*>' contained nextgroup=hareTypedefEquals skipempty skipwhite transparent
    100 syn match hareTypedefEquals '=' contained nextgroup=@hareType skipempty skipwhite transparent
    101 
    102 # Attributes {{{3
    103 syn keyword hareAttribute @init @fini @test
    104 syn keyword hareAttribute @packed
    105 syn keyword hareAttribute @symbol nextgroup=hareAttributeParens skipempty skipwhite
    106 syn keyword hareAttribute @threadlocal
    107 syn keyword hareAttribute @undefined
    108 
    109 # Match the parens following attributes.
    110 syn region hareAttributeParens matchgroup=hareParen start='(' end=')' contained contains=TOP transparent
    111 
    112 # Expressions {{{2
    113 syn keyword hareConditional else
    114 syn keyword hareConditional if nextgroup=hareConditionParens skipempty skipwhite
    115 syn keyword hareConditional match switch nextgroup=@hareCondition skipempty skipwhite
    116 syn keyword hareLabel case nextgroup=@hareType skipempty skipwhite
    117 syn keyword hareOperator as is nextgroup=@hareType skipempty skipwhite
    118 syn keyword hareRepeat for nextgroup=@hareCondition skipempty skipwhite
    119 syn keyword hareStatement break continue return yield
    120 syn keyword hareStatement defer
    121 
    122 # Match the parens in conditionals and loops.
    123 syn cluster hareCondition contains=hareConditionLabel,hareConditionParens
    124 syn match hareConditionLabel ':\h\w*\>' contained nextgroup=hareConditionParens skipempty skipwhite transparent
    125 syn region hareConditionParens matchgroup=hareParen start='(' end=')' contained contains=TOP transparent
    126 
    127 # Builtins {{{3
    128 syn keyword hareBuiltin abort assert
    129 syn keyword hareBuiltin align nextgroup=hareTypeParens skipempty skipwhite
    130 syn keyword hareBuiltin alloc free
    131 syn keyword hareBuiltin append insert delete
    132 syn keyword hareBuiltin len offset
    133 
    134 # C ABI.
    135 syn keyword hareBuiltin vastart vaarg vaend
    136 
    137 # Highlight `size` as a type unless it is followed by an open paren.
    138 syn match hareType '\<size\>'
    139 syn match hareBuiltin '\<size\ze(' nextgroup=hareTypeParens
    140 
    141 # Match the parens in builtin expressions expecting a type.
    142 syn region hareTypeParens matchgroup=hareParen start='(' end=')' contained contains=TOP nextgroup=@harePostfix skipempty skipwhite transparent
    143 
    144 # Postfix expressions {{{3
    145 # TODO: Match postfix expressions after literals.
    146 syn cluster harePostfix contains=hareCast,hareField,hareSlice,hareSpecial
    147 
    148 # Casts and type hints.
    149 syn match hareCast ':' nextgroup=@hareType skipempty skipwhite
    150 
    151 # Error checking.
    152 syn match hareSpecial '!=\@!' contained nextgroup=@harePostfix skipempty skipwhite
    153 syn match hareSpecial '?' nextgroup=@harePostfix skipempty skipwhite
    154 
    155 # Field access.
    156 syn match hareField '\.\w\+\>' contained contains=hareName,hareNumber,@hareReserved nextgroup=@harePostfix skipempty skipwhite transparent
    157 
    158 # Indexing and slicing.
    159 syn region hareSlice matchgroup=hareBracket start='\[' end=']' contained contains=TOP nextgroup=@harePostfix skipempty skipwhite transparent
    160 
    161 # Literals {{{2
    162 syn keyword hareBoolean true false
    163 syn keyword hareConstant null
    164 
    165 # Integers {{{3
    166 syn match hareNumber '\v<%(0|[1-9]%(_?\d)*)%([Ee]\+?\d+)?%([iu]%(8|16|32|64)?|z)?>'
    167 syn match hareNumber '\v<0b[01]%(_?[01])*%([iu]%(8|16|32|64)?|z)?>'
    168 syn match hareNumber '\v<0o\o%(_?\o)*%([iu]%(8|16|32|64)?|z)?>'
    169 syn match hareNumber '\v<0x\x%(_?\x)*%([iu]%(8|16|32|64)?|z)?>'
    170 
    171 # Floats {{{3
    172 # XXX: Technically, the third form is not a valid floating literal according to
    173 #      the specification, but is currently accepted by the Hare compiler and
    174 #      used occasionally within the standard library.
    175 syn match hareFloat '\v<%(0|[1-9]%(_?\d)*)\.\d%(_?\d)*%([Ee][+-]?\d+)?%(f32|f64)?>'
    176 syn match hareFloat '\v<%(0|[1-9]%(_?\d)*)%([Ee][+-]?\d+)?%(f32|f64)>'
    177 syn match hareFloat '\v<%(0|[1-9]%(_?\d)*)[Ee]-\d+>'
    178 syn match hareFloat '\v<0x\x%(_?\x)*%(\.\x%(_?\x)*)?[Pp][+-]?\d+%(f32|f64)?>'
    179 
    180 # Rune and string literals {{{3
    181 syn region hareRune matchgroup=hareRuneDelimiter start="'" skip="\\'" end="'" contains=hareEscape
    182 syn region hareString matchgroup=hareStringDelimiter start='"' skip='\\"' end='"' contains=hareEscape,hareFormat
    183 syn region hareString matchgroup=hareStringDelimiter start='`' end='`' contains=hareFormat
    184 
    185 # Escape sequences.
    186 syn match hareEscape '\\[0abfnrtv\\'"]' contained
    187 syn match hareEscape '\v\\%(x\x{2}|u\x{4}|U\x{8})' contained display
    188 
    189 # Format sequences.
    190 syn match hareFormat '\v\{\d*%(:%(\.?\d+|[- +=befgoxX]|F[.2EsSU]|_%(\_[^\\]|\\%([0abfnrtv\\'"]|x\x{2}|u\x{4}|U\x{8})))*)?}' contained contains=hareEscape
    191 syn match hareFormat '{\d*%\d*}' contained display
    192 syn match hareFormat '{{\|}}' contained
    193 
    194 # Miscellaneous {{{2
    195 
    196 # Annotations.
    197 syn region hareAnnotation start='#\[' end=']' contains=hareAnnotationIdentifier,hareComment,hareRune,hareString
    198 syn match hareAnnotationIdentifier '\v#\[\s*\zs\h\w*%(::\h\w*)*>' contained contains=hareName,@hareReserved nextgroup=hareAnnotationParens skipempty skipwhite
    199 syn region hareAnnotationParens matchgroup=hareAnnotationParen start='(' end=')' contained contains=TOP
    200 
    201 # Comments.
    202 syn region hareComment excludenl start='//' end='$' contains=hareSpecialComment,hareTodo,@Spell
    203 syn match hareSpecialComment '\v\[\[\h\w*%(::\h\w*)*%(::)?]]' contained contains=@NoSpell display
    204 syn keyword hareTodo FIXME TODO XXX contained
    205 
    206 # Scope delimiters.
    207 syn match hareScopeDelimiter '::'
    208 
    209 # User labels.
    210 syn match hareUserLabel ':\h\w*\>' contains=hareName,@hareReserved
    211 
    212 # Default highlighting {{{1
    213 hi def link hareAnnotation Special
    214 hi def link hareAnnotationIdentifier hareAnnotation
    215 hi def link hareAnnotationParen hareAnnotation
    216 hi def link hareArrayBounds harePointer
    217 hi def link hareAttribute PreProc
    218 hi def link hareBoolean Boolean
    219 hi def link hareBuiltin hareOperator
    220 hi def link hareComment Comment
    221 hi def link hareConditional Conditional
    222 hi def link hareConstant Constant
    223 hi def link hareDefine Define
    224 hi def link hareError hareSpecial
    225 hi def link hareEscape SpecialChar
    226 hi def link hareFloat Float
    227 hi def link hareFormat SpecialChar
    228 hi def link hareFunction Function
    229 hi def link hareInclude Include
    230 hi def link hareKeyword Keyword
    231 hi def link hareLabel Label
    232 hi def link hareNumber Number
    233 hi def link hareOperator Operator
    234 hi def link harePointer hareStorageClass
    235 hi def link hareRepeat Repeat
    236 hi def link hareRune Character
    237 hi def link hareRuneDelimiter hareRune
    238 hi def link hareScopeDelimiter Delimiter
    239 hi def link hareSpecial Special
    240 hi def link hareSpecialComment SpecialComment
    241 hi def link hareStatement Statement
    242 hi def link hareStorageClass StorageClass
    243 hi def link hareString String
    244 hi def link hareStringDelimiter hareString
    245 hi def link hareStructure Structure
    246 hi def link hareTodo Todo
    247 hi def link hareType Type
    248 hi def link hareTypedef Typedef
    249 hi def link hareUserLabel Identifier
    250 
    251 # Highlight incorrect whitespace by default.
    252 syn match hareSpaceError excludenl '\s\+$' containedin=ALL display
    253 syn match hareSpaceError ' \+\ze\t' display
    254 if get(g:, 'hare_space_error', 1)
    255   hi! def link hareSpaceError Error
    256 else
    257   hi! def link hareSpaceError NONE
    258 endif
    259 
    260 b:current_syntax = 'hare'
    261 
    262 # vim: fdm=marker et sts=2 sw=2 ts=8 tw=80