rhysd | bc8f79d | 2023-11-14 16:46:07 +0100 | [diff] [blame] | 1 | " Vim syntax file |
| 2 | " Language: WebAssembly |
| 3 | " Maintainer: rhysd <lin90162@yahoo.co.jp> |
| 4 | " Last Change: Nov 14, 2023 |
| 5 | " For bugs, patches and license go to https://github.com/rhysd/vim-wasm |
| 6 | |
| 7 | if exists("b:current_syntax") |
| 8 | finish |
| 9 | endif |
| 10 | |
| 11 | let s:cpo_save = &cpo |
| 12 | set cpo&vim |
| 13 | |
| 14 | syn cluster watNotTop contains=watModule,watInstWithType,watInstGetSet,watInstGeneral,watParamInst,watControlInst,watSimdInst,watString,watNamedVar,watUnnamedVar,watFloat,watNumber,watComment,watList,watType |
| 15 | |
| 16 | " Instructions |
| 17 | " https://webassembly.github.io/spec/core/text/instructions.html |
| 18 | " Note: memarg (align=,offset=) can be added to memory instructions |
| 19 | syn match watInstWithType "\%((\s*\)\@<=\<\%(i32\|i64\|f32\|f64\|memory\)\.[[:alnum:]_]\+\%(/\%(i32\|i64\|f32\|f64\)\)\=\>\%(\s\+\%(align\|offset\)=\)\=" contained display |
| 20 | syn match watInstGeneral "\%((\s*\)\@<=\<[[:alnum:]_]\+\>" contained display |
| 21 | syn match watInstGetSet "\%((\s*\)\@<=\<\%(local\|global\)\.\%(get\|set\)\>" contained display |
| 22 | " https://webassembly.github.io/spec/core/text/instructions.html#control-instructions |
| 23 | syn match watControlInst "\%((\s*\)\@<=\<\%(block\|end\|loop\|if\|then\|else\|unreachable\|nop\|br\|br_if\|br_table\|return\|call\|call_indirect\)\>" contained display |
| 24 | " https://webassembly.github.io/spec/core/text/instructions.html#parametric-instructions |
| 25 | syn match watParamInst "\%((\s*\)\@<=\<\%(drop\|select\)\>" contained display |
| 26 | " SIMD instructions |
| 27 | " https://webassembly.github.io/simd/core/text/instructions.html#simd-instructions |
| 28 | syn match watSimdInst "\<\%(v128\|i8x16\|i16x8\|i32x4\|i64x2\|f32x4\|f64x2)\)\.[[:alnum:]_]\+\%(\s\+\%(i8x16\|i16x8\|i32x4\|i64x2\|f32x4\|f64x2\)\)\=\>" contained display |
| 29 | |
| 30 | " Identifiers |
| 31 | " https://webassembly.github.io/spec/core/text/values.html#text-id |
| 32 | syn match watNamedVar "$\+[[:alnum:]!#$%&'∗./:=><?@\\^_`~+-]*" contained contains=watEscapeUtf8 |
| 33 | syn match watUnnamedVar "$\+\d\+[[:alnum:]!#$%&'∗./:=><?@\\^_`~+-]\@!" contained display |
| 34 | " Presuming the source text is itself encoded correctly, strings that do not |
| 35 | " contain any uses of hexadecimal byte escapes are always valid names. |
| 36 | " https://webassembly.github.io/spec/core/text/values.html#names |
| 37 | syn match watEscapedUtf8 "\\\x\{1,6}" contained containedin=watNamedVar display |
| 38 | |
| 39 | " String literals |
| 40 | " https://webassembly.github.io/spec/core/text/values.html#strings |
| 41 | syn region watString start=+"+ skip=+\\\\\|\\"+ end=+"+ contained contains=watStringSpecial |
| 42 | syn match watStringSpecial "\\\x\x\|\\[tnr'\\\"]\|\\u\x\+" contained containedin=watString display |
| 43 | |
| 44 | " Float literals |
| 45 | " https://webassembly.github.io/spec/core/text/values.html#floating-point |
| 46 | syn match watFloat "\<-\=\d\%(_\=\d\)*\%(\.\d\%(_\=\d\)*\)\=\%([eE][-+]\=\d\%(_\=\d\)*\)\=" display contained |
| 47 | syn match watFloat "\<-\=0x\x\%(_\=\x\)*\%(\.\x\%(_\=\x\)*\)\=\%([pP][-+]\=\d\%(_\=\d\)*\)\=" display contained |
| 48 | syn keyword watFloat inf nan contained |
| 49 | syn match watFloat "nan:0x\x\%(_\=\x\)*" display contained |
| 50 | |
| 51 | " Integer literals |
| 52 | " https://webassembly.github.io/spec/core/text/values.html#integers |
| 53 | syn match watNumber "\<-\=\d\%(_\=\d\)*\>" display contained |
| 54 | syn match watNumber "\<-\=0x\x\%(_\=\x\)*\>" display contained |
| 55 | |
| 56 | " Comments |
| 57 | " https://webassembly.github.io/spec/core/text/lexical.html#comments |
| 58 | syn region watComment start=";;" end="$" |
| 59 | syn region watComment start="(;;\@!" end=";)" |
| 60 | |
| 61 | syn region watList matchgroup=watListDelimiter start="(;\@!" matchgroup=watListDelimiter end=";\@<!)" contains=@watNotTop |
| 62 | |
| 63 | " Types |
| 64 | " https://webassembly.github.io/spec/core/text/types.html |
| 65 | " Note: `mut` was changed to `const`/`var` at Wasm 2.0 |
| 66 | syn keyword watType i64 i32 f64 f32 param result funcref func externref extern mut v128 const var contained |
| 67 | syn match watType "\%((\_s*\)\@<=func\%(\_s*[()]\)\@=" display contained |
| 68 | |
| 69 | " Modules |
| 70 | " https://webassembly.github.io/spec/core/text/modules.html |
| 71 | syn keyword watModule module type export import table memory global data elem contained |
| 72 | syn match watModule "\%((\_s*\)\@<=func\%(\_s\+\$\)\@=" display contained |
| 73 | |
| 74 | syn sync maxlines=100 |
| 75 | |
| 76 | hi def link watModule PreProc |
| 77 | hi def link watListDelimiter Delimiter |
| 78 | hi def link watInstWithType Operator |
| 79 | hi def link watInstGetSet Operator |
| 80 | hi def link watInstGeneral Operator |
| 81 | hi def link watControlInst Statement |
| 82 | hi def link watSimdInst Operator |
| 83 | hi def link watParamInst Conditional |
| 84 | hi def link watString String |
| 85 | hi def link watStringSpecial Special |
| 86 | hi def link watNamedVar Identifier |
| 87 | hi def link watUnnamedVar PreProc |
| 88 | hi def link watFloat Float |
| 89 | hi def link watNumber Number |
| 90 | hi def link watComment Comment |
| 91 | hi def link watType Type |
| 92 | hi def link watEscapedUtf8 Special |
| 93 | |
| 94 | let b:current_syntax = "wat" |
| 95 | |
| 96 | let &cpo = s:cpo_save |
| 97 | unlet s:cpo_save |