robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
ab20681c52
master
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. 49if exists ( "b:current_syntax" ) 50finish 51endif 52 53" We need nocompatible mode in order to continue lines with backslashes. 54" Original setting will be restored. 55let s: cpo_save = & cpo 56set cpo & vim 57 58if exists ( "mojo_no_doctest_highlight" ) 59let mojo_no_doctest_code_highlight = 1 60endif 61 62if exists ( "mojo_highlight_all" ) 63if exists ( "mojo_no_builtin_highlight" ) 64unlet mojo_no_builtin_highlight 65endif 66if exists ( "mojo_no_doctest_code_highlight" ) 67unlet mojo_no_doctest_code_highlight 68endif 69if exists ( "mojo_no_doctest_highlight" ) 70unlet mojo_no_doctest_highlight 71endif 72if exists ( "mojo_no_exception_highlight" ) 73unlet mojo_no_exception_highlight 74endif 75if exists ( "mojo_no_number_highlight" ) 76unlet mojo_no_number_highlight 77endif 78let mojo_space_error_highlight = 1 79endif 80 81" These keywords are based on Python syntax highlight, and adds to it struct, 82" fn, alias, var, let 83" 84syn keyword mojoStatement False None True 85syn keyword mojoStatement as assert break continue del global 86syn keyword mojoStatement lambda nonlocal pass return with yield 87syn keyword mojoStatement class def nextgroup = mojoFunction skipwhite 88syn keyword mojoStatement struct fn nextgroup = mojoFunction skipwhite 89syn keyword mojoStatement alias var let 90syn keyword mojoConditional elif else if 91syn keyword mojoRepeat for while 92syn keyword mojoOperator and in is not or 93syn keyword mojoException except finally raise try 94syn keyword mojoInclude from import 95syn 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. 101syn match mojoConditional "^\s * \zscase\%(\s\+. * :. * $\)\@= " 102syn match mojoConditional "^\s * \zsmatch\%(\s\+. * :\s * \%(#. * \)\=$\)\@= " 103 104" Decorators 105" A dot must be allowed because of @MyClass.myfunc decorators. 106syn match mojoDecorator "@ "display contained 107syn 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. 113syn match mojoMatrixMultiply 114 \ "\%(\w\| [] )]\)\s * @ " 115 \contains = ALLBUT , mojoDecoratorName , mojoDecorator , mojoFunction , mojoDoctestValue 116 \transparent 117" Multiplication continued on the next line after backslash. 118syn 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. 124syn 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 129syn match mojoFunction "\h\w * "display contained 130 131syn match mojoComment "#. * $ "contains = mojoTodo , @Spell 132syn keyword mojoTodo FIXME NOTE NOTES TODO XXX contained 133 134" Triple-quoted strings can contain doctests. 135syn region mojoString matchgroup = mojoQuotes 136 \start = +[ uU ] \=\z( [ '" ] \) +end = "\z1 " skip="\\\\\|\\\z1" 137\ contains =mojoEscape,@Spell 138syn region mojoString matchgroup = mojoTripleQuotes 139 \start = +[ uU ] \=\z('''\|"""\) +end = "\z1 "keepend 140 \contains = mojoEscape , mojoSpaceError , mojoDoctest , @Spell 141syn region mojoRawString matchgroup = mojoQuotes 142 \start = +[ uU ] \= [ rR ] \z( [ '" ] \) +end = "\z1 " skip="\\\\\|\\\z1" 143\ contains =@Spell 144syn region mojoRawString matchgroup = pythonTripleQuotes 145 \start = +[ uU ] \= [ rR ] \z('''\|"""\) +end = "\z1 "keepend 146 \contains = pythonSpaceError , mojoDoctest , @Spell 147 148syn match mojoEscape +\\ [ abfnrtv'"\\ ] +contained 149syn match mojoEscape "\\\o\{1,3} "contained 150syn match mojoEscape "\\x\x\{2} "contained 151syn match mojoEscape "\%(\\u\x\{4}\|\\U\x\{8}\) "contained 152" Python allows case-insensitive Unicode IDs: http://www.unicode.org/charts/ 153syn match mojoEscape "\\N{\a\+\%(\s\a\+\) * } "contained 154syn 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 170if ! exists ( "mojo_no_number_highlight" ) 171" numbers (including complex) 172syn match mojoNumber "\<0 [ oO ] \%(_\=\o\)\+\> " 173syn match mojoNumber "\<0 [ xX ] \%(_\=\x\)\+\> " 174syn match mojoNumber "\<0 [ bB ] \%(_\= [ 01 ] \)\+\> " 175syn match mojoNumber "\<\%( [ 1-9 ] \%(_\=\d\) * \|0\+\%(_\=0\) * \)\> " 176syn match mojoNumber "\<\d\%(_\=\d\) * [ jJ ] \> " 177syn match mojoNumber "\<\d\%(_\=\d\) * [ eE ][ +- ] \=\d\%(_\=\d\) * [ jJ ] \=\> " 178syn match mojoNumber 179 \ "\<\d\%(_\=\d\) * \.\%( [ eE ][ +- ] \=\d\%(_\=\d\) * \)\= [ jJ ] \=\%(\W\|$\)\@= " 180syn match mojoNumber 181 \ "\%(^\|\W\)\zs\%(\d\%(_\=\d\) * \)\=\.\d\%(_\=\d\) * \%( [ eE ][ +- ] \=\d\%(_\=\d\) * \)\= [ jJ ] \=\> " 182endif 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" 187if ! exists ( "mojo_no_builtin_highlight" ) 188" Built-in functions 189syn keyword mojoBuiltin slice constrained debug_assert put_new_line syn keyword mojoBuiltin print_no_newline len range rebind element_type 191syn keyword mojoBuiltin ord chr atol isdigit index address string 192" Built-in types 193syn keyword mojoType Byte ListLiteral CoroutineContext Coroutine DType 194syn keyword mojoType dtype type invalid bool int8 si8 unit8 ui8 int16 195syn keyword mojoType si16 unit16 ui16 int32 si32 uint32 ui32 int64 196syn keyword mojoType si64 uint64 ui64 bfloat16 bf16 float16 f16 float32 197syn keyword mojoType f32 float64 f64 Error FloatLiteral Int Attr SIMD 198syn keyword mojoType Int8 UInt8 Int16 UInt16 Int32 UInt32 Int64 UInt64 199syn keyword mojoType Float16 Float32 Float64 element_type _65x13_type 200syn keyword mojoType String StringLiteral StringRef Tuple AnyType 201syn keyword mojoType NoneType None Lifetime 202" avoid highlighting attributes as builtins 203syn match mojoAttribute /\.\h\w * /hs= s+1 204 \contains = ALLBUT , mojoBuiltin , mojoFunction , mojoAsync 205 \transparent 206endif 207 208" From the 'Python Library Reference' class hierarchy at the bottom. 209" http://docs.python.org/library/exceptions.html 210if ! exists ( "mojo_no_exception_highlight" ) 211" builtin base exceptions (used mostly as base classes for other exceptions) 212syn keyword mojoExceptions BaseException Exception 213syn keyword mojoExceptions ArithmeticError BufferError LookupError 214" builtin exceptions (actually raised) 215syn keyword mojoExceptions AssertionError AttributeError EOFError 216syn keyword mojoExceptions FloatingPointError GeneratorExit ImportError 217syn keyword mojoExceptions IndentationError IndexError KeyError 218syn keyword mojoExceptions KeyboardInterrupt MemoryError 219syn keyword mojoExceptions ModuleNotFoundError NameError 220syn keyword mojoExceptions NotImplementedError OSError OverflowError 221syn keyword mojoExceptions RecursionError ReferenceError RuntimeError 222syn keyword mojoExceptions StopAsyncIteration StopIteration SyntaxError 223syn keyword mojoExceptions SystemError SystemExit TabError TypeError 224syn keyword mojoExceptions UnboundLocalError UnicodeDecodeError 225syn keyword mojoExceptions UnicodeEncodeError UnicodeError 226syn keyword mojoExceptions UnicodeTranslateError ValueError 227syn keyword mojoExceptions ZeroDivisionError 228" builtin exception aliases for OSError 229syn keyword mojoExceptions EnvironmentError IOError WindowsError 230" builtin OS exceptions in Python 3 231syn keyword mojoExceptions BlockingIOError BrokenPipeError 232syn keyword mojoExceptions ChildProcessError ConnectionAbortedError 233syn keyword mojoExceptions ConnectionError ConnectionRefusedError 234syn keyword mojoExceptions ConnectionResetError FileExistsError 235syn keyword mojoExceptions FileNotFoundError InterruptedError 236syn keyword mojoExceptions IsADirectoryError NotADirectoryError 237syn keyword mojoExceptions PermissionError ProcessLookupError TimeoutError 238" builtin warnings 239syn keyword mojoExceptions BytesWarning DeprecationWarning FutureWarning 240syn keyword mojoExceptions ImportWarning PendingDeprecationWarning 241syn keyword mojoExceptions ResourceWarning RuntimeWarning 242syn keyword mojoExceptions SyntaxWarning UnicodeWarning 243syn keyword mojoExceptions UserWarning Warning 244endif 245 246if exists ( "mojo_space_error_highlight" ) 247" trailing whitespace 248syn match mojoSpaceError display excludenl "\s\+$ " 249" mixed tabs and spaces 250syn match mojoSpaceError display "\+\t " 251syn match mojoSpaceError display "\t\+ " 252endif 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. 257if ! exists ( "mojo_no_doctest_highlight" ) 258if ! exists ( "mojo_no_doctest_code_highlight" ) 259syn region mojoDoctest 260 \start = "^\s * >>>\s "end = "^\s * $ " 261 \contained contains = ALLBUT , mojoDoctest , mojoFunction , @Spell 262syn region mojoDoctestValue 263 \start = +^\s * \%(>>>\s\|\.\.\.\s\|"""\|'''\)\@!\S\+ +end = "$ " 264 \contained 265else 266syn region mojoDoctest 267 \start = "^\s * >>> "end = "^\s * $ " 268 \contained contains = @NoSpell 269endif 270endif 271 272" Sync at the beginning of class, function, or method definition. 273syn syncmatch mojoSync grouphereNONE "^\%(def\|class\)\s\+\h\w * \s * [ (: ] " 274 275" The default highlight links. Can be overridden later. 276hi def link mojoStatement Statement 277hi def link mojoConditional Conditional 278hi def link mojoRepeat Repeat 279hi def link mojoOperator Operator 280hi def link mojoException Exception 281hi def link mojoInclude Include 282hi def link mojoAsync Statement 283hi def link mojoDecorator Define 284hi def link mojoDecoratorName Function 285hi def link mojoFunction Function 286hi def link mojoComment Comment 287hi def link mojoTodo Todo 288hi def link mojoString String 289hi def link mojoRawString String 290hi def link mojoQuotes String 291hi def link mojoTripleQuotes mojoQuotes 292hi def link mojoEscape Special 293if ! exists ( "mojo_no_number_highlight" ) 294hi def link mojoNumber Number 295endif 296if ! exists ( "mojo_no_builtin_highlight" ) 297hi def link mojoBuiltin Function 298hi def link mojoType Type 299endif 300if ! exists ( "mojo_no_exception_highlight" ) 301hi def link mojoExceptions Structure 302endif 303if exists ( "mojo_space_error_highlight" ) 304hi def link mojoSpaceError Error 305endif 306if ! exists ( "mojo_no_doctest_highlight" ) 307hi def link mojoDoctest Special 308hi def link mojoDoctestValue Define 309endif 310 311let b: current_syntax = "mojo" 312 313let & cpo = s: cpo_save 314unlet s: cpo_save 315 316" vim:set sw=2 sts=2 ts=8 noet: