Mahmoud Abduljawad | 0ce2c59 | 2023-09-10 18:23:04 +0200 | [diff] [blame] | 1 | " Vim syntax file |
| 2 | " Language: Mojo |
| 3 | " Maintainer: Mahmoud Abduljawad <me@mahmoudajawad.com> |
| 4 | " Last Change: 2023 Sep 09 |
| 5 | " Credits: Mahmoud Abduljawad <me@mahmoudajawad.com> |
| 6 | " Neil Schemenauer <nas@python.ca> |
| 7 | " Dmitry Vasiliev |
| 8 | " |
| 9 | " This is based on Vim Python highlighting |
| 10 | " |
| 11 | " - introduced highlighting of doctests |
| 12 | " - updated keywords, built-ins, and exceptions |
| 13 | " - corrected regular expressions for |
| 14 | " |
| 15 | " * functions |
| 16 | " * decorators |
| 17 | " * strings |
| 18 | " * escapes |
| 19 | " * numbers |
| 20 | " * space error |
| 21 | " |
| 22 | " - corrected synchronization |
| 23 | " - more highlighting is ON by default, except |
| 24 | " - space error highlighting is OFF by default |
| 25 | " |
| 26 | " Optional highlighting can be controlled using these variables. |
| 27 | " |
| 28 | " let mojo_no_builtin_highlight = 1 |
| 29 | " let mojo_no_doctest_code_highlight = 1 |
| 30 | " let mojo_no_doctest_highlight = 1 |
| 31 | " let mojo_no_exception_highlight = 1 |
| 32 | " let mojo_no_number_highlight = 1 |
| 33 | " let mojo_space_error_highlight = 1 |
| 34 | " |
| 35 | " All the options above can be switched on together. |
| 36 | " |
| 37 | " let mojo_highlight_all = 1 |
| 38 | " |
| 39 | " The use of Python 2 compatible syntax highlighting can be enforced. |
| 40 | " The straddling code (Python 2 and 3 compatible), up to Python 3.5, |
| 41 | " will be also supported. |
| 42 | " |
| 43 | " let mojo_use_python2_syntax = 1 |
| 44 | " |
| 45 | " This option will exclude all modern Python 3.6 or higher features. |
| 46 | " |
| 47 | |
| 48 | " quit when a syntax file was already loaded. |
| 49 | if exists("b:current_syntax") |
| 50 | finish |
| 51 | endif |
| 52 | |
| 53 | " We need nocompatible mode in order to continue lines with backslashes. |
| 54 | " Original setting will be restored. |
| 55 | let s:cpo_save = &cpo |
| 56 | set cpo&vim |
| 57 | |
| 58 | if exists("mojo_no_doctest_highlight") |
| 59 | let mojo_no_doctest_code_highlight = 1 |
| 60 | endif |
| 61 | |
| 62 | if exists("mojo_highlight_all") |
| 63 | if exists("mojo_no_builtin_highlight") |
| 64 | unlet mojo_no_builtin_highlight |
| 65 | endif |
| 66 | if exists("mojo_no_doctest_code_highlight") |
| 67 | unlet mojo_no_doctest_code_highlight |
| 68 | endif |
| 69 | if exists("mojo_no_doctest_highlight") |
| 70 | unlet mojo_no_doctest_highlight |
| 71 | endif |
| 72 | if exists("mojo_no_exception_highlight") |
| 73 | unlet mojo_no_exception_highlight |
| 74 | endif |
| 75 | if exists("mojo_no_number_highlight") |
| 76 | unlet mojo_no_number_highlight |
| 77 | endif |
| 78 | let mojo_space_error_highlight = 1 |
| 79 | endif |
| 80 | |
| 81 | " These keywords are based on Python syntax highlight, and adds to it struct, |
| 82 | " fn, alias, var, let |
| 83 | " |
| 84 | syn keyword mojoStatement False None True |
| 85 | syn keyword mojoStatement as assert break continue del global |
| 86 | syn keyword mojoStatement lambda nonlocal pass return with yield |
| 87 | syn keyword mojoStatement class def nextgroup=mojoFunction skipwhite |
| 88 | syn keyword mojoStatement struct fn nextgroup=mojoFunction skipwhite |
| 89 | syn keyword mojoStatement alias var let |
| 90 | syn keyword mojoConditional elif else if |
| 91 | syn keyword mojoRepeat for while |
| 92 | syn keyword mojoOperator and in is not or |
| 93 | syn keyword mojoException except finally raise try |
| 94 | syn keyword mojoInclude from import |
| 95 | syn keyword mojoAsync async await |
| 96 | |
| 97 | " Soft keywords |
| 98 | " These keywords do not mean anything unless used in the right context. |
| 99 | " See https://docs.python.org/3/reference/lexical_analysis.html#soft-keywords |
| 100 | " for more on this. |
| 101 | syn match mojoConditional "^\s*\zscase\%(\s\+.*:.*$\)\@=" |
| 102 | syn match mojoConditional "^\s*\zsmatch\%(\s\+.*:\s*\%(#.*\)\=$\)\@=" |
| 103 | |
| 104 | " Decorators |
| 105 | " A dot must be allowed because of @MyClass.myfunc decorators. |
| 106 | syn match mojoDecorator "@" display contained |
| 107 | syn match mojoDecoratorName "@\s*\h\%(\w\|\.\)*" display contains=pythonDecorator |
| 108 | |
| 109 | " Python 3.5 introduced the use of the same symbol for matrix multiplication: |
| 110 | " https://www.python.org/dev/peps/pep-0465/. We now have to exclude the |
| 111 | " symbol from highlighting when used in that context. |
| 112 | " Single line multiplication. |
| 113 | syn match mojoMatrixMultiply |
| 114 | \ "\%(\w\|[])]\)\s*@" |
| 115 | \ contains=ALLBUT,mojoDecoratorName,mojoDecorator,mojoFunction,mojoDoctestValue |
| 116 | \ transparent |
| 117 | " Multiplication continued on the next line after backslash. |
| 118 | syn match mojoMatrixMultiply |
| 119 | \ "[^\\]\\\s*\n\%(\s*\.\.\.\s\)\=\s\+@" |
| 120 | \ contains=ALLBUT,mojoDecoratorName,mojoDecorator,mojoFunction,mojoDoctestValue |
| 121 | \ transparent |
| 122 | " Multiplication in a parenthesized expression over multiple lines with @ at |
| 123 | " the start of each continued line; very similar to decorators and complex. |
| 124 | syn match mojoMatrixMultiply |
| 125 | \ "^\s*\%(\%(>>>\|\.\.\.\)\s\+\)\=\zs\%(\h\|\%(\h\|[[(]\).\{-}\%(\w\|[])]\)\)\s*\n\%(\s*\.\.\.\s\)\=\s\+@\%(.\{-}\n\%(\s*\.\.\.\s\)\=\s\+@\)*" |
| 126 | \ contains=ALLBUT,mojoDecoratorName,mojoDecorator,mojoFunction,mojoDoctestValue |
| 127 | \ transparent |
| 128 | |
| 129 | syn match mojoFunction "\h\w*" display contained |
| 130 | |
| 131 | syn match mojoComment "#.*$" contains=mojoTodo,@Spell |
| 132 | syn keyword mojoTodo FIXME NOTE NOTES TODO XXX contained |
| 133 | |
| 134 | " Triple-quoted strings can contain doctests. |
| 135 | syn region mojoString matchgroup=mojoQuotes |
| 136 | \ start=+[uU]\=\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1" |
| 137 | \ contains=mojoEscape,@Spell |
| 138 | syn region mojoString matchgroup=mojoTripleQuotes |
| 139 | \ start=+[uU]\=\z('''\|"""\)+ end="\z1" keepend |
| 140 | \ contains=mojoEscape,mojoSpaceError,mojoDoctest,@Spell |
| 141 | syn region mojoRawString matchgroup=mojoQuotes |
| 142 | \ start=+[uU]\=[rR]\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1" |
| 143 | \ contains=@Spell |
| 144 | syn region mojoRawString matchgroup=pythonTripleQuotes |
| 145 | \ start=+[uU]\=[rR]\z('''\|"""\)+ end="\z1" keepend |
| 146 | \ contains=pythonSpaceError,mojoDoctest,@Spell |
| 147 | |
| 148 | syn match mojoEscape +\\[abfnrtv'"\\]+ contained |
| 149 | syn match mojoEscape "\\\o\{1,3}" contained |
| 150 | syn match mojoEscape "\\x\x\{2}" contained |
| 151 | syn match mojoEscape "\%(\\u\x\{4}\|\\U\x\{8}\)" contained |
| 152 | " Python allows case-insensitive Unicode IDs: http://www.unicode.org/charts/ |
| 153 | syn match mojoEscape "\\N{\a\+\%(\s\a\+\)*}" contained |
| 154 | syn match mojoEscape "\\$" |
| 155 | |
| 156 | " It is very important to understand all details before changing the |
| 157 | " regular expressions below or their order. |
| 158 | " The word boundaries are *not* the floating-point number boundaries |
| 159 | " because of a possible leading or trailing decimal point. |
| 160 | " The expressions below ensure that all valid number literals are |
| 161 | " highlighted, and invalid number literals are not. For example, |
| 162 | " |
| 163 | " - a decimal point in '4.' at the end of a line is highlighted, |
| 164 | " - a second dot in 1.0.0 is not highlighted, |
| 165 | " - 08 is not highlighted, |
| 166 | " - 08e0 or 08j are highlighted, |
| 167 | " |
| 168 | " and so on, as specified in the 'Python Language Reference'. |
| 169 | " https://docs.python.org/reference/lexical_analysis.html#numeric-literals |
| 170 | if !exists("mojo_no_number_highlight") |
| 171 | " numbers (including complex) |
| 172 | syn match mojoNumber "\<0[oO]\%(_\=\o\)\+\>" |
| 173 | syn match mojoNumber "\<0[xX]\%(_\=\x\)\+\>" |
| 174 | syn match mojoNumber "\<0[bB]\%(_\=[01]\)\+\>" |
| 175 | syn match mojoNumber "\<\%([1-9]\%(_\=\d\)*\|0\+\%(_\=0\)*\)\>" |
| 176 | syn match mojoNumber "\<\d\%(_\=\d\)*[jJ]\>" |
| 177 | syn match mojoNumber "\<\d\%(_\=\d\)*[eE][+-]\=\d\%(_\=\d\)*[jJ]\=\>" |
| 178 | syn match mojoNumber |
| 179 | \ "\<\d\%(_\=\d\)*\.\%([eE][+-]\=\d\%(_\=\d\)*\)\=[jJ]\=\%(\W\|$\)\@=" |
| 180 | syn match mojoNumber |
| 181 | \ "\%(^\|\W\)\zs\%(\d\%(_\=\d\)*\)\=\.\d\%(_\=\d\)*\%([eE][+-]\=\d\%(_\=\d\)*\)\=[jJ]\=\>" |
| 182 | endif |
| 183 | |
| 184 | " The built-ins are added in the same order of appearance in Mojo stdlib docs |
| 185 | " https://docs.modular.com/mojo/lib.html |
| 186 | " |
| 187 | if !exists("mojo_no_builtin_highlight") |
| 188 | " Built-in functions |
| 189 | syn keyword mojoBuiltin slice constrained debug_assert put_new_line print |
| 190 | syn keyword mojoBuiltin print_no_newline len range rebind element_type |
| 191 | syn keyword mojoBuiltin ord chr atol isdigit index address string |
| 192 | " Built-in types |
| 193 | syn keyword mojoType Byte ListLiteral CoroutineContext Coroutine DType |
| 194 | syn keyword mojoType dtype type invalid bool int8 si8 unit8 ui8 int16 |
| 195 | syn keyword mojoType si16 unit16 ui16 int32 si32 uint32 ui32 int64 |
| 196 | syn keyword mojoType si64 uint64 ui64 bfloat16 bf16 float16 f16 float32 |
| 197 | syn keyword mojoType f32 float64 f64 Error FloatLiteral Int Attr SIMD |
| 198 | syn keyword mojoType Int8 UInt8 Int16 UInt16 Int32 UInt32 Int64 UInt64 |
| 199 | syn keyword mojoType Float16 Float32 Float64 element_type _65x13_type |
| 200 | syn keyword mojoType String StringLiteral StringRef Tuple AnyType |
| 201 | syn keyword mojoType NoneType None Lifetime |
| 202 | " avoid highlighting attributes as builtins |
| 203 | syn match mojoAttribute /\.\h\w*/hs=s+1 |
| 204 | \ contains=ALLBUT,mojoBuiltin,mojoFunction,mojoAsync |
| 205 | \ transparent |
| 206 | endif |
| 207 | |
| 208 | " From the 'Python Library Reference' class hierarchy at the bottom. |
| 209 | " http://docs.python.org/library/exceptions.html |
| 210 | if !exists("mojo_no_exception_highlight") |
| 211 | " builtin base exceptions (used mostly as base classes for other exceptions) |
| 212 | syn keyword mojoExceptions BaseException Exception |
| 213 | syn keyword mojoExceptions ArithmeticError BufferError LookupError |
| 214 | " builtin exceptions (actually raised) |
| 215 | syn keyword mojoExceptions AssertionError AttributeError EOFError |
| 216 | syn keyword mojoExceptions FloatingPointError GeneratorExit ImportError |
| 217 | syn keyword mojoExceptions IndentationError IndexError KeyError |
| 218 | syn keyword mojoExceptions KeyboardInterrupt MemoryError |
| 219 | syn keyword mojoExceptions ModuleNotFoundError NameError |
| 220 | syn keyword mojoExceptions NotImplementedError OSError OverflowError |
| 221 | syn keyword mojoExceptions RecursionError ReferenceError RuntimeError |
| 222 | syn keyword mojoExceptions StopAsyncIteration StopIteration SyntaxError |
| 223 | syn keyword mojoExceptions SystemError SystemExit TabError TypeError |
| 224 | syn keyword mojoExceptions UnboundLocalError UnicodeDecodeError |
| 225 | syn keyword mojoExceptions UnicodeEncodeError UnicodeError |
| 226 | syn keyword mojoExceptions UnicodeTranslateError ValueError |
| 227 | syn keyword mojoExceptions ZeroDivisionError |
| 228 | " builtin exception aliases for OSError |
| 229 | syn keyword mojoExceptions EnvironmentError IOError WindowsError |
| 230 | " builtin OS exceptions in Python 3 |
| 231 | syn keyword mojoExceptions BlockingIOError BrokenPipeError |
| 232 | syn keyword mojoExceptions ChildProcessError ConnectionAbortedError |
| 233 | syn keyword mojoExceptions ConnectionError ConnectionRefusedError |
| 234 | syn keyword mojoExceptions ConnectionResetError FileExistsError |
| 235 | syn keyword mojoExceptions FileNotFoundError InterruptedError |
| 236 | syn keyword mojoExceptions IsADirectoryError NotADirectoryError |
| 237 | syn keyword mojoExceptions PermissionError ProcessLookupError TimeoutError |
| 238 | " builtin warnings |
| 239 | syn keyword mojoExceptions BytesWarning DeprecationWarning FutureWarning |
| 240 | syn keyword mojoExceptions ImportWarning PendingDeprecationWarning |
| 241 | syn keyword mojoExceptions ResourceWarning RuntimeWarning |
| 242 | syn keyword mojoExceptions SyntaxWarning UnicodeWarning |
| 243 | syn keyword mojoExceptions UserWarning Warning |
| 244 | endif |
| 245 | |
| 246 | if exists("mojo_space_error_highlight") |
| 247 | " trailing whitespace |
| 248 | syn match mojoSpaceError display excludenl "\s\+$" |
| 249 | " mixed tabs and spaces |
| 250 | syn match mojoSpaceError display " \+\t" |
| 251 | syn match mojoSpaceError display "\t\+ " |
| 252 | endif |
| 253 | |
| 254 | " Do not spell doctests inside strings. |
| 255 | " Notice that the end of a string, either ''', or """, will end the contained |
| 256 | " doctest too. Thus, we do *not* need to have it as an end pattern. |
| 257 | if !exists("mojo_no_doctest_highlight") |
| 258 | if !exists("mojo_no_doctest_code_highlight") |
| 259 | syn region mojoDoctest |
| 260 | \ start="^\s*>>>\s" end="^\s*$" |
| 261 | \ contained contains=ALLBUT,mojoDoctest,mojoFunction,@Spell |
| 262 | syn region mojoDoctestValue |
| 263 | \ start=+^\s*\%(>>>\s\|\.\.\.\s\|"""\|'''\)\@!\S\++ end="$" |
| 264 | \ contained |
| 265 | else |
| 266 | syn region mojoDoctest |
| 267 | \ start="^\s*>>>" end="^\s*$" |
| 268 | \ contained contains=@NoSpell |
| 269 | endif |
| 270 | endif |
| 271 | |
| 272 | " Sync at the beginning of class, function, or method definition. |
| 273 | syn sync match mojoSync grouphere NONE "^\%(def\|class\)\s\+\h\w*\s*[(:]" |
| 274 | |
| 275 | " The default highlight links. Can be overridden later. |
| 276 | hi def link mojoStatement Statement |
| 277 | hi def link mojoConditional Conditional |
| 278 | hi def link mojoRepeat Repeat |
| 279 | hi def link mojoOperator Operator |
| 280 | hi def link mojoException Exception |
| 281 | hi def link mojoInclude Include |
| 282 | hi def link mojoAsync Statement |
| 283 | hi def link mojoDecorator Define |
| 284 | hi def link mojoDecoratorName Function |
| 285 | hi def link mojoFunction Function |
| 286 | hi def link mojoComment Comment |
| 287 | hi def link mojoTodo Todo |
| 288 | hi def link mojoString String |
| 289 | hi def link mojoRawString String |
| 290 | hi def link mojoQuotes String |
| 291 | hi def link mojoTripleQuotes mojoQuotes |
| 292 | hi def link mojoEscape Special |
| 293 | if !exists("mojo_no_number_highlight") |
| 294 | hi def link mojoNumber Number |
| 295 | endif |
| 296 | if !exists("mojo_no_builtin_highlight") |
| 297 | hi def link mojoBuiltin Function |
| 298 | hi def link mojoType Type |
| 299 | endif |
| 300 | if !exists("mojo_no_exception_highlight") |
| 301 | hi def link mojoExceptions Structure |
| 302 | endif |
| 303 | if exists("mojo_space_error_highlight") |
| 304 | hi def link mojoSpaceError Error |
| 305 | endif |
| 306 | if !exists("mojo_no_doctest_highlight") |
| 307 | hi def link mojoDoctest Special |
| 308 | hi def link mojoDoctestValue Define |
| 309 | endif |
| 310 | |
| 311 | let b:current_syntax = "mojo" |
| 312 | |
| 313 | let &cpo = s:cpo_save |
| 314 | unlet s:cpo_save |
| 315 | |
| 316 | " vim:set sw=2 sts=2 ts=8 noet: |