|
| 1 | +" Vim syntax file |
| 2 | +" Language: C macro for C preprocessor |
| 3 | +" Maintainer: Wu, Zhenyu <wuzhenyu@ustc.edu> |
| 4 | +" Last Change: 2024 Dec 31 |
| 5 | +" modified from syntax/c.vim |
| 6 | + |
| 7 | +" C compiler has a preprocessor: `cpp -P test.txt` |
| 8 | +" test.txt doesn't need to be a C file |
| 9 | +if exists("b:current_syntax") |
| 10 | + finish |
| 11 | +endif |
| 12 | + |
| 13 | +let s:cpo_save = &cpo |
| 14 | +set cpo&vim |
| 15 | + |
| 16 | +" Accept %: for # (C99) |
| 17 | +syn region cmacroPreCondit start="^\s*\zs\%(%:\|#\)\s*\%(if\|ifdef\|ifndef\|elif\)\>" skip="\\$" end="$" keepend contains=cmacroCppParen,cmacroNumbers |
| 18 | +syn match cmacroPreConditMatch display "^\s*\zs\%(%:\|#\)\s*\%(else\|endif\)\>" |
| 19 | +if !exists("c_no_if0") |
| 20 | + syn cluster cmacroCppOutInGroup contains=cmacroCppInIf,cmacroCppInElse,cmacroCppInElse2,cmacroCppOutIf,cmacroCppOutIf2,cmacroCppOutElse,cmacroCppInSkip,cmacroCppOutSkip |
| 21 | + syn region cmacroCppOutWrapper start="^\s*\zs\%(%:\|#\)\s*if\s\+0\+\s*\%($\|//\|/\*\|&\)" end=".\@=\|$" contains=cmacroCppOutIf,cmacroCppOutElse,@NoSpell fold |
| 22 | + syn region cmacroCppOutIf contained start="0\+" matchgroup=cmacroCppOutWrapper end="^\s*\%(%:\|#\)\s*endif\>" contains=cmacroCppOutIf2,cmacroCppOutElse |
| 23 | + if !exists("c_no_if0_fold") |
| 24 | + syn region cmacroCppOutIf2 contained matchgroup=cmacroCppOutWrapper start="0\+" end="^\s*\%(%:\|#\)\s*\%(else\>\|elif\s\+\%(0\+\s*\%($\|//\|/\*\|&\)\)\@!\|endif\>\)"me=s-1 contains=cmacroCppOutSkip,@Spell fold |
| 25 | + else |
| 26 | + syn region cmacroCppOutIf2 contained matchgroup=cmacroCppOutWrapper start="0\+" end="^\s*\%(%:\|#\)\s*\%(else\>\|elif\s\+\%(0\+\s*\%($\|//\|/\*\|&\)\)\@!\|endif\>\)"me=s-1 contains=cmacroCppOutSkip,@Spell |
| 27 | + endif |
| 28 | + syn region cmacroCppOutElse contained matchgroup=cmacroCppOutWrapper start="^\s*\%(%:\|#\)\s*\%(else\|elif\)" end="^\s*\%(%:\|#\)\s*endif\>"me=s-1 contains=TOP,cmacroPreCondit |
| 29 | + syn region cmacroCppInWrapper start="^\s*\zs\%(%:\|#\)\s*if\s\+0*[1-9]\d*\s*\%($\|//\|/\*\||\)" end=".\@=\|$" contains=cmacroCppInIf,cmacroCppInElse fold |
| 30 | + syn region cmacroCppInIf contained matchgroup=cmacroCppInWrapper start="\d\+" end="^\s*\%(%:\|#\)\s*endif\>" contains=TOP,cmacroPreCondit |
| 31 | + if !exists("c_no_if0_fold") |
| 32 | + syn region cmacroCppInElse contained start="^\s*\%(%:\|#\)\s*\%(else\>\|elif\s\+\%(0*[1-9]\d*\s*\%($\|//\|/\*\||\)\)\@!\)" end=".\@=\|$" containedin=cmacroCppInIf contains=cmacroCppInElse2 fold |
| 33 | + else |
| 34 | + syn region cmacroCppInElse contained start="^\s*\%(%:\|#\)\s*\%(else\>\|elif\s\+\%(0*[1-9]\d*\s*\%($\|//\|/\*\||\)\)\@!\)" end=".\@=\|$" containedin=cmacroCppInIf contains=cmacroCppInElse2 |
| 35 | + endif |
| 36 | + syn region cmacroCppInElse2 contained matchgroup=cmacroCppInWrapper start="^\s*\%(%:\|#\)\s*\%(else\|elif\)\%([^/]\|/[^/*]\)*" end="^\s*\%(%:\|#\)\s*endif\>"me=s-1 contains=cmacroCppOutSkip,@Spell |
| 37 | + syn region cmacroCppOutSkip contained start="^\s*\%(%:\|#\)\s*\%(if\>\|ifdef\>\|ifndef\>\)" skip="\\$" end="^\s*\%(%:\|#\)\s*endif\>" contains=cmacroCppOutSkip |
| 38 | + syn region cmacroCppInSkip contained matchgroup=cmacroCppInWrapper start="^\s*\%(%:\|#\)\s*\%(if\s\+\%(\d\+\s*\%($\|//\|/\*\||\|&\)\)\@!\|ifdef\>\|ifndef\>\)" skip="\\$" end="^\s*\%(%:\|#\)\s*endif\>" containedin=cmacroCppOutElse,cmacroCppInIf,cmacroCppInSkip contains=TOP,cmacroPreProc |
| 39 | +endif |
| 40 | +syn region cmacroIncluded display contained start=+"+ skip=+\\\\\|\\"+ end=+"+ |
| 41 | +syn match cmacroIncluded display contained "<[^>]*>" |
| 42 | +syn match cmacroInclude display "^\s*\zs\%(%:\|#\)\s*include\>\s*["<]" contains=cmacroIncluded |
| 43 | +"syn match cmacroLineSkip "\\$" |
| 44 | +syn cluster cmacroPreProcmacroGroup contains=cmacroPreCondit,cmacroIncluded,cmacroInclude,cmacroDefine,cmacroCppOutWrapper,cmacroCppInWrapper,@cmacroCppOutInGroup,cmacroNumbersCom,@cmacroCommentGroup,cmacroParen,cmacroBracket,cmacroMulti,cmacroBadBlock |
| 45 | +syn region cmacroDefine start="^\s*\zs\%(%:\|#\)\s*\%(define\|undef\)\>" skip="\\$" end="$" keepend contains=ALLBUT,@cmacroPreProcmacroGroup,@Spell |
| 46 | +syn region cmacroPreProc start="^\s*\zs\%(%:\|#\)\s*\%(pragma\>\|line\>\|warning\>\|warn\>\|error\>\)" skip="\\$" end="$" keepend contains=ALLBUT,@cmacroPreProcmacroGroup,@Spell |
| 47 | + |
| 48 | +" be able to fold #pragma regions |
| 49 | +syn region cmacroPragma start="^\s*#pragma\s\+region\>" end="^\s*#pragma\s\+endregion\>" transparent keepend extend fold |
| 50 | + |
| 51 | +syn keyword cmacroTodo contained TODO FIXME XXX NOTE |
| 52 | +syn region cmacroComment start='/\*' end='\*/' contains=cmacroTodo,@Spell |
| 53 | +syn match cmacroCommentError "\*/" |
| 54 | +syn region cmacroComment start='//' end='$' contains=cmacroTodo,@Spell |
| 55 | + |
| 56 | +" Define the default highlighting. |
| 57 | +" Only used when an item doesn't have highlighting yet |
| 58 | +hi def link cmacroInclude Include |
| 59 | +hi def link cmacroPreProc PreProc |
| 60 | +hi def link cmacroDefine Macro |
| 61 | +hi def link cmacroIncluded cmacroString |
| 62 | +hi def link cmacroCppInWrapper cmacroCppOutWrapper |
| 63 | +hi def link cmacroCppOutWrapper cmacroPreCondit |
| 64 | +hi def link cmacroPreConditMatch cmacroPreCondit |
| 65 | +hi def link cmacroPreCondit PreCondit |
| 66 | +hi def link cmacroCppOutSkip cmacroCppOutIf2 |
| 67 | +hi def link cmacroCppInElse2 cmacroCppOutIf2 |
| 68 | +hi def link cmacroCppOutIf2 cmacroCppOut |
| 69 | +hi def link cmacroCppOut Comment |
| 70 | +hi def link cmacroTodo Todo |
| 71 | +hi def link cmacroComment Comment |
| 72 | +hi def link cmacroCommentError Error |
| 73 | + |
| 74 | +let b:current_syntax = "cmacro" |
| 75 | + |
| 76 | +let &cpo = s:cpo_save |
| 77 | +unlet s:cpo_save |
0 commit comments