<?xml version="1.0" encoding="UTF-8"?>
<!--

(C) 2025 Bikkel

-->

<language id="v" name="V" version="2.0" _section="Source">

  <metadata>
    <property name="mimetypes">text/x-v</property>
    <property name="globs">*.v</property>
    <property name="line-comment-start">//</property>
    <property name="block-comment-start">/*</property>
    <property name="block-comment-end">*/</property>
  </metadata>

  <styles>
    <style id="builtin"           name="Builtin Value"           map-to="def:builtin"/>
    <style id="comment"           name="Comment"                 map-to="def:comment"/>
    <style id="declaration"       name="Declaration"             map-to="def:type"/>
    <style id="default"           name="Default"/>
    <style id="error"             name="Error"                   map-to="def:error"/>
    <style id="escaped-character" name="Escaped Character"       map-to="def:special-char"/>
    <style id="external"          name="External"                map-to="def:preprocessor"/>
    <style id="identifier"        name="Identifier"              map-to="def:identifier"/>
    <style id="interpolated"      name="Interpolated Expression" map-to="def:identifier"/>
    <style id="keyword"           name="Keyword"                 map-to="def:keyword"/>
    <style id="modifier"          name="Modifier"                map-to="def:type"/>
    <style id="number"            name="Number"                  map-to="def:decimal"/>
    <style id="operator"          name="Operator"                map-to="def:operator"/>
    <style id="special"           name="Special Constant"        map-to="def:special-constant"/>
    <style id="string"            name="String"                  map-to="def:string"/>
    <style id="type"              name="Data Type"               map-to="def:identifier"/>
  </styles>

  <definitions>

    <define-regex id="simple-name">[a-zA-Z_$][a-zA-Z_$0-9]*</define-regex>

    <define-regex id="compound-name">\%{simple-name}(\.\%{simple-name})*</define-regex>

    <define-regex id="annotation-target" extended="true">
      field|file|property|get|set|receiver|param|setparam|delegate
    </define-regex>

    <context id="annotation" style-ref="special">
      <match extended="true">
          @(\%{annotation-target}:)?
          ( \%{compound-name} | \[ (\%{compound-name} \s*)+ \] )
      </match>
    </context>

    <context id="block-comment" style-ref="comment">
      <start>/\*</start>
      <end>\*/</end>
      <include>
        <context ref="def:in-comment"/>
        <context ref="block-comment"/>
      </include>
    </context>

    <context id="builtin" style-ref="builtin">
      <keyword>it</keyword>
      <keyword>super</keyword>
      <keyword>this@?</keyword>
    </context>

    <define-regex id="escaped-character" extended="true">
      \\(               # leading backslash
      [\\\"\'nrbt$] |   # escaped character
      u[0-9a-fA-F]{4}   # unicode char
      )
    </define-regex>

    <context id="character" style-ref="special">
      <match>'(\%{escaped-character}|.)'</match>
    </context>

    <context id="character-error" style-ref="error">
      <match>'(\%{escaped-character}|.)[^\s]+'</match>
    </context>

    <context id="declaration" style-ref="declaration">
      <keyword>const</keyword>
      <keyword>mut</keyword>            
    </context>

    <context id="variance-annotation">
      <match>(&lt;|,) *(in|out)</match>
      <include>
        <context sub-pattern="1" style-ref="operator" />
        <context sub-pattern="2" style-ref="modifier" />
      </include>
    </context>

    <context id="expression" style-ref="keyword">      
      <keyword>fn</keyword>            
      <keyword>module</keyword>
      <keyword>as</keyword>
      <keyword>asm</keyword>
      <keyword>assert</keyword>
      <keyword>atomic</keyword>
      <keyword>break</keyword>
      <keyword>continue</keyword>
      <keyword>defer</keyword>
      <keyword>else</keyword>
      <keyword>enum</keyword>
      <keyword>false</keyword>
      <keyword>for</keyword>
      <keyword>go</keyword>
      <keyword>goto</keyword>
      <keyword>if</keyword>
      <keyword>implements</keyword>
      <keyword>import</keyword>
      <keyword>in</keyword>
      <keyword>interface</keyword>
      <keyword>is</keyword>
      <keyword>isreftype</keyword>
      <keyword>lock</keyword>
      <keyword>match</keyword>
      <keyword>module</keyword>
      <keyword>mut</keyword>
      <keyword>none</keyword>
      <keyword>or</keyword>
      <keyword>pub</keyword>
      <keyword>return</keyword>
      <keyword>rlock</keyword>
      <keyword>select</keyword>
      <keyword>shared</keyword>
      <keyword>sizeof</keyword>
      <keyword>spawn</keyword>
      <keyword>static</keyword>
      <keyword>struct</keyword>
      <keyword>true</keyword>
      <keyword>type</keyword>
      <keyword>typeof</keyword>
      <keyword>union</keyword>
      <keyword>unsafe</keyword>
      <keyword>volatile</keyword>
      <keyword>__global</keyword>
      <keyword>__offsetof</keyword>      
    </context>

    <context id="external" style-ref="external">
      <keyword>import</keyword>
      <keyword>package</keyword>
    </context>

    <context id="literal-identifier">
      <start>`</start>
      <end>`</end>
    </context>

    <context id="modifier" style-ref="modifier">
      <keyword>pub</keyword>
      <keyword>const</keyword>      
    </context>

    <context id="numeric" style-ref="number">
      <match extended="true">
        \b ( 0x [0-9A-Fa-f][0-9A-Fa-f_]*   # hex literal
           | 0b [01][01_]*                 # binary literal
           | ([0-9]+[Ee][-]?[0-9]+|
              ([0-9]*\.[0-9]+|[0-9]+\.)([Ee][-]?[0-9]+)?)[fFdD]?|
              [0-9]+[FfDd]                 # floating-point literal
           | [0-9][0-9_]*L?                # integer literal
           )
      </match>
    </context>

    <context id="operator" style-ref="operator">
      <match>[-+*/%=.!|?@:;,_&amp;&lt;&gt;()\[\]]</match>
    </context>

    <context id="special" style-ref="special">
      <keyword>false</keyword>
      <keyword>true</keyword>
      <keyword>null</keyword>
    </context>

    <context id="interpolated-identifier" style-ref="interpolated">
      <match>\$[a-zA-Z]+</match>
    </context>

    <context id="interpolated-expression" style-ref="interpolated">
      <start>\${</start>
      <end>}</end>
      <include>
        <context ref="v"/>
      </include>
    </context>

    <context id="string" style-ref="string" end-at-line-end="true"
             class="string" class-disabled="no-spell-check">
      <start>"</start>
      <end>"</end>
      <include>
        <context id="escaped-character" style-ref="escaped-character">
          <match>\%{escaped-character}</match>
        </context>
        <context ref="interpolated-identifier"/>
        <context ref="interpolated-expression"/>
      </include>
    </context>

    <context id="multiline-string" style-ref="string" end-at-line-end="false" class="string" class-disabled="no-spell-check">
      <start>"""</start>
      <end>"""</end>
      <include>
        <context ref="interpolated-identifier"/>
        <context ref="interpolated-expression"/>
      </include>
    </context>

    <context id="type" style-ref="type">
      <keyword>string</keyword>      
      <keyword>bool</keyword>
      <keyword>string</keyword>
      <keyword>i8</keyword>
      <keyword>i16</keyword>
      <keyword>int</keyword>
      <keyword>i64</keyword>
      <keyword>i128</keyword>
      <keyword>u8</keyword>
      <keyword>u16</keyword>
      <keyword>u32</keyword>
      <keyword>u64</keyword>
      <keyword>u128</keyword>
      <keyword>rune</keyword>
      <keyword>f32</keyword>
      <keyword>f64</keyword>
      <keyword>isize</keyword>
      <keyword>usize</keyword>
      <keyword>voidptr</keyword>
      <keyword>any</keyword>      
    </context>

    <context id="v" class="no-spell-check">
      <include>
        <context ref="def:c-like-comment"/>
        <context ref="def:c-like-close-comment-outside-comment"/>
        <context ref="annotation"/>
        <context ref="block-comment"/>
        <context ref="builtin"/>
        <context ref="character"/>
        <context ref="character-error"/>
        <context ref="declaration"/>
        <context ref="variance-annotation"/>
        <context ref="expression"/>
        <context ref="external"/>
        <context ref="literal-identifier"/>
        <context ref="modifier"/>
        <context ref="multiline-string"/>
        <context ref="numeric"/>
        <context ref="operator"/>
        <context ref="special"/>
        <context ref="string"/>
        <context ref="type"/>
      </include>
    </context>
  </definitions>

</language>

