|
| 1 | +" Vim syntax file |
| 2 | +" Language: PKL |
| 3 | +" Maintainer: Jan Claußen <jan DOT claussen10 AT web DOT de> |
| 4 | +" Last Change: 2025 Sep 24 |
| 5 | + |
| 6 | +if exists("b:current_syntax") |
| 7 | + finish |
| 8 | +endif |
| 9 | + |
| 10 | +" We use line-continuation here |
| 11 | +let s:cpo_save = &cpo |
| 12 | +set cpo&vim |
| 13 | + |
| 14 | +" Needed to properly highlight multiline strings |
| 15 | +syn sync fromstart |
| 16 | + |
| 17 | +" PKL supports non-Unicode identifiers. So we modify the keyword character |
| 18 | +" class to include them |
| 19 | +syn iskeyword @,48-57,192-255,$,_ |
| 20 | + |
| 21 | +" Declare a variable for identifiers |
| 22 | +let s:id = '\%(\K\+\d*[_$]*\K*\d*[_$]*\)' |
| 23 | + |
| 24 | +" --- Decorator --- |
| 25 | +exe $'syn match pklDecorator "@{s:id}\{{1,}}"' |
| 26 | + |
| 27 | +" --- Comments --- |
| 28 | +syn match pklComment "\/\{2}.*" |
| 29 | +syn match pklDocComment "\/\{3}.*" |
| 30 | +syn region pklMultiComment start="\/\*" end="\*\/" keepend fold |
| 31 | + |
| 32 | +" --- Strings --- |
| 33 | +syn region pklString start=+"+ end=+"+ contains=pklEscape,pklUnicodeEscape,pklStringInterpolation oneline |
| 34 | +syn region pklMultiString start=+"""+ skip=+\\."+ end=+"""+ contains=pklEscape,pklUnicodeEscape keepend fold |
| 35 | +syn match pklEscape "\\[\\nt0rbaeuf"']" contained containedin=pklString,pklMultiString |
| 36 | +syn match pklUnicode "[0-9A-Fa-f]\+" contained |
| 37 | + |
| 38 | +" --- String interpolation --- |
| 39 | +" Standard interpolation |
| 40 | +syn region pklStringInterpolation matchgroup=pklDelimiter |
| 41 | + \ start=+\\(+ end=+)+ contains=pklNumbers,pklOperator,pklIdentifier,pklFunction,pklParen,pklString |
| 42 | + \ contained containedin=pklString,pklMultiString oneline |
| 43 | +" Unicode escape sequences |
| 44 | +syn region pklUnicodeEscape matchgroup=pklDelimiter |
| 45 | + \ start=+\\u{+ end=+}+ contains=pklUnicode |
| 46 | + \ contained containedin=pklString,pklMultiString |
| 47 | + |
| 48 | +" --- Basic data types --- |
| 49 | +syn keyword pklType |
| 50 | + \ UInt UInt8 UInt16 UInt32 UInt64 UInt128 |
| 51 | + \ Int Int8 Int16 Int32 Int64 Int128 |
| 52 | + \ Float |
| 53 | + \ Number |
| 54 | + \ String |
| 55 | + \ Boolean |
| 56 | + \ Null |
| 57 | + \ Any |
| 58 | + |
| 59 | +syn keyword pklCollections |
| 60 | + \ Map Mapping |
| 61 | + \ List Listing |
| 62 | + \ Set |
| 63 | + |
| 64 | +" --- Custom string delimiters --- |
| 65 | +function! s:DefineCustomStringDelimiters(n) |
| 66 | + for x in range(1, a:n) |
| 67 | + exe $'syn region pklString{x}Pound start=+{repeat("#", x)}"+ end=+"{repeat("#", x)}+ contains=pklStringInterpolation{x}Pound,pklEscape{x}Pound oneline' |
| 68 | + exe $'hi def link pklString{x}Pound String' |
| 69 | + |
| 70 | + exe $'syn region pklMultiString{x}Pound start=+{repeat("#", x)}"""+ end=+"""{repeat("#", x)}+ contains=pklStringInterpolation{x}Pound,pklEscape{x}Pound keepend fold' |
| 71 | + exe $'hi def link pklMultiString{x}Pound String' |
| 72 | + |
| 73 | + exe $'syn match pklEscape{x}Pound "\\{repeat("#", x) }[\\nt0rbaeuf"'']" contained containedin=pklString{x}Pound,pklMultiString{x}Pound' |
| 74 | + exe $'hi def link pklEscape{x}Pound SpecialChar' |
| 75 | + |
| 76 | + exe $'syn region pklStringInterpolation{x}Pound matchgroup=pklDelimiter start=+\\{repeat("#", x)}(+ end=+)+ contains=pklNumbers,pklOperator,pklIdentifier,pklFunction,pklParen,pklString contained containedin=pklString{x}Pound,pklMultiString{x}Pound oneline' |
| 77 | + |
| 78 | + exe $'syn region pklUnicodeEscape{x}Pound matchgroup=pklDelimiter start=+\\{repeat("#", x)}u{{+ end=+}}+ contains=pklUnicode contained containedin=pklString{x}Pound,pklMultiString{x}Pound' |
| 79 | + exe $'hi def link pklUnicodeEscape{x}Pound SpecialChar' |
| 80 | + endfor |
| 81 | +endfunction |
| 82 | + |
| 83 | +call s:DefineCustomStringDelimiters(5) |
| 84 | + |
| 85 | +" --- Keywords --- |
| 86 | +syn keyword pklBoolean false true |
| 87 | +syn keyword pklClass outer super this module new |
| 88 | +syn keyword pklConditional if else when |
| 89 | +syn keyword pklConstant null NaN Infinity |
| 90 | +syn keyword pklException throw |
| 91 | +syn keyword pklInclude amends import extends as |
| 92 | +syn keyword pklKeyword function let out is |
| 93 | +syn keyword pklModifier abstract const external fixed hidden local open |
| 94 | +syn keyword pklReserved case delete override protected record switch vararg |
| 95 | +syn keyword pklRepeat for in |
| 96 | +syn keyword pklSpecial nothing unknown |
| 97 | +syn keyword pklStatement trace read |
| 98 | +syn keyword pklStruct typealias class |
| 99 | + |
| 100 | +" Include all unicode letters |
| 101 | +exe $'syn match pklIdentifier "{s:id}"' |
| 102 | + |
| 103 | +" Explicitely make keywords identifiers with backticks |
| 104 | +syn region pklIdentifierExplicit start=+`+ end=+`+ |
| 105 | + |
| 106 | +syn match pklOperator ",\||\|+\|*\|->\|?\|-\|==\|=\|!=\|!" contained containedin=pklType |
| 107 | + |
| 108 | +" --- Numbers --- |
| 109 | +" decimal numbers |
| 110 | +syn match pklNumbers display transparent "\<\d\|\.\d" contains=pklNumber,pklFloat,pklOctal |
| 111 | +syn match pklNumber display contained "\d\%(\d\+\)*\>" |
| 112 | +" hex numbers |
| 113 | +syn match pklNumber display contained "0x\x\%('\=\x\+\)\>" |
| 114 | +" binary numbers |
| 115 | +syn match pklNumber display contained "0b[01]\%('\=[01]\+\)\>" |
| 116 | +" octal numbers |
| 117 | +syn match pklOctal display contained "0o\o\+\>" |
| 118 | + |
| 119 | +"floating point number, with dot, optional exponent |
| 120 | +syn match pklFloat display contained "\d\+\.\d\+\%(e[-+]\=\d\+\)\=" |
| 121 | +"floating point number, starting with a dot, optional exponent |
| 122 | +syn match pklFloat display contained "\.\d\+\%(e[-+]\=\d\+\)\=\>" |
| 123 | +"floating point number, without dot, with exponent |
| 124 | +syn match pklFloat display contained "\d\+e[-+]\=\d\+\>" |
| 125 | + |
| 126 | +" --- Brackets, operators, functions --- |
| 127 | +syn region pklParen matchgroup=pklBrackets start='(' end=')' contains=ALLBUT,pklUnicode transparent |
| 128 | +syn region pklBracket matchgroup=pklBrackets start='\[\|<::\@!' end=']\|:>' contains=ALLBUT,pklUnicode transparent |
| 129 | +syn region pklBlock matchgroup=pklBrackets start="{" end="}" contains=ALLBUT,pklUnicode fold transparent |
| 130 | + |
| 131 | +exe $'syn match pklFunction "\<\h{s:id}*\>\ze\_s*[?|\*]\?(" contains=pklType' |
| 132 | + |
| 133 | +" --- Highlight links --- |
| 134 | +hi def link pklBoolean Boolean |
| 135 | +hi def link pklBrackets Delimiter |
| 136 | +hi def link pklClass Statement |
| 137 | +hi def link pklCollections Type |
| 138 | +hi def link pklComment Comment |
| 139 | +hi def link pklConditional Conditional |
| 140 | +hi def link pklConstant Constant |
| 141 | +hi def link pklDecorator Special |
| 142 | +hi def link pklDelimiter Delimiter |
| 143 | +hi def link pklDocComment Comment |
| 144 | +hi def link pklEscape SpecialChar |
| 145 | +hi def link pklException Exception |
| 146 | +hi def link pklFloat Number |
| 147 | +hi def link pklFunction Function |
| 148 | +hi def link pklInclude Include |
| 149 | +hi def link pklKeyword Keyword |
| 150 | +hi def link pklModifier StorageClass |
| 151 | +hi def link pklMultiComment Comment |
| 152 | +hi def link pklMultiString String |
| 153 | +hi def link pklNumber Number |
| 154 | +hi def link pklNumbers Number |
| 155 | +hi def link pklOctal Number |
| 156 | +hi def link pklRepeat Repeat |
| 157 | +hi def link pklReserved Error |
| 158 | +hi def link pklShebang Comment |
| 159 | +hi def link pklSpecial Special |
| 160 | +hi def link pklStatement Statement |
| 161 | +hi def link pklString String |
| 162 | +hi def link pklStruct Structure |
| 163 | +hi def link pklType Type |
| 164 | +hi def link pklUnicodeEscape SpecialChar |
| 165 | + |
| 166 | +let b:current_syntax = "pkl" |
| 167 | + |
| 168 | +let &cpo = s:cpo_save |
| 169 | +unlet s:cpo_save |
0 commit comments