1" Vim syntax file 2" Language: awk, nawk, gawk, mawk 3" Maintainer: Antonio Colombo <[email protected]> 4" Last Change: 2014 Oct 21 5 6" AWK ref. is: Alfred V. Aho, Brian W. Kernighan, Peter J. Weinberger 7" The AWK Programming Language, Addison-Wesley, 1988 8 9" GAWK ref. is: Arnold D. Robbins 10" Effective AWK Programming, Third Edition, O'Reilly, 2001 11" Effective AWK Programming, Fourth Edition, O'Reilly, 2015 12" (also available with the gawk source distribution) 13 14" MAWK is a "new awk" meaning it implements AWK ref. 15" mawk conforms to the Posix 1003.2 (draft 11.3) 16" definition of the AWK language which contains a few features 17" not described in the AWK book, and mawk provides a small number of extensions. 18 19" TODO: 20" Dig into the commented out syntax expressions below. 21 22" For version 5.x: Clear all syntax items 23" For version 6.x: Quit when a syntax file was already loaded 24if version < 600 25 syn clear 26elseif exists("b:current_syntax") 27 finish 28endif 29 30let s:cpo_save = &cpo 31set cpo&vim 32 33" A bunch of useful Awk keywords 34" AWK ref. p. 188 35syn keyword awkStatement break continue delete exit 36syn keyword awkStatement function getline next 37syn keyword awkStatement print printf return 38" GAWK ref. Chapter 7 39syn keyword awkStatement nextfile 40" 41" GAWK ref. Chapter 9, Functions 42" 43" Numeric Functions 44syn keyword awkFunction atan2 cos div exp int log rand sin sqrt srand 45" String Manipulation Functions 46syn keyword awkFunction asort asort1 gensub gsub index length match 47syn keyword awkFunction patsplit split sprintf strtonum sub substr 48syn keyword awkFunction tolower toupper 49" Input Output Functions 50syn keyword awkFunction close fflush system 51" Time Functions 52syn keyword awkFunction mktime strftime systime 53" Bit Manipulation Functions 54syn keyword awkFunction and compl lshift or rshift xor 55" Getting Type Function 56syn keyword awkFunction isarray 57" String-Translation Functions 58syn keyword awkFunction bindtextdomain dcgettext dcngetext 59 60syn keyword awkConditional if else 61syn keyword awkRepeat while for 62 63syn keyword awkTodo contained TODO 64 65syn keyword awkPatterns BEGIN END 66 67" GAWK ref. Chapter 7 68" Built-in Variables That Control awk 69syn keyword awkVariables BINMODE CONVFMT FIELDWIDTHS FPAT FS 70syn keyword awkVariables IGNORECASE LINT OFMT OFS ORS PREC 71syn keyword awkVariables ROUNDMODE RS SUBSEP TEXTDOMAIN 72" Built-in Variables That Convey Information 73syn keyword awkVariables ARGC ARGV ARGIND ENVIRON ERRNO FILENAME 74syn keyword awkVariables FNR NF FUNCTAB NR PROCINFO RLENGTH RSTART 75syn keyword awkVariables RT SYMTAB 76 77syn keyword awkRepeat do 78 79" Octal format character. 80syn match awkSpecialCharacter display contained "\\[0-7]\{1,3\}" 81syn keyword awkStatement func nextfile 82" Hex format character. 83syn match awkSpecialCharacter display contained "\\x[0-9A-Fa-f]\+" 84 85syn match awkFieldVars "\$\d\+" 86 87"catch errors caused by wrong parenthesis 88syn region awkParen transparent start="(" end=")" contains=ALLBUT,awkParenError,awkSpecialCharacter,awkArrayElement,awkArrayArray,awkTodo,awkRegExp,awkBrktRegExp,awkBrackets,awkCharClass 89syn match awkParenError display ")" 90syn match awkInParen display contained "[{}]" 91 92" 64 lines for complex &&'s, and ||'s in a big "if" 93syn sync ccomment awkParen maxlines=64 94 95" Search strings & Regular Expressions therein. 96syn region awkSearch oneline start="^[ \t]*/"ms=e start="\(,\|!\=\~\)[ \t]*/"ms=e skip="\\\\\|\\/" end="/" contains=awkBrackets,awkRegExp,awkSpecialCharacter 97syn region awkBrackets contained start="\[\^\]\="ms=s+2 start="\[[^\^]"ms=s+1 end="\]"me=e-1 contains=awkBrktRegExp,awkCharClass 98syn region awkSearch oneline start="[ \t]*/"hs=e skip="\\\\\|\\/" end="/" contains=awkBrackets,awkRegExp,awkSpecialCharacter 99 100syn match awkCharClass contained "\[:[^:\]]*:\]" 101syn match awkBrktRegExp contained "\\.\|.\-[^]]" 102syn match awkRegExp contained "/\^"ms=s+1 103syn match awkRegExp contained "\$/"me=e-1 104syn match awkRegExp contained "[?.*{}|+]" 105 106" String and Character constants 107" Highlight special characters (those which have a backslash) differently 108syn region awkString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=@Spell,awkSpecialCharacter,awkSpecialPrintf 109syn match awkSpecialCharacter contained "\\." 110 111" Some of these combinations may seem weird, but they work. 112syn match awkSpecialPrintf contained "%[-+ #]*\d*\.\=\d*[cdefgiosuxEGX%]" 113 114" Numbers, allowing signs (both -, and +) 115" Integer number. 116syn match awkNumber display "[+-]\=\<\d\+\>" 117" Floating point number. 118syn match awkFloat display "[+-]\=\<\d\+\.\d+\>" 119" Floating point number, starting with a dot. 120syn match awkFloat display "[+-]\=\<.\d+\>" 121syn case ignore 122"floating point number, with dot, optional exponent 123syn match awkFloat display "\<\d\+\.\d*\(e[-+]\=\d\+\)\=\>" 124"floating point number, starting with a dot, optional exponent 125syn match awkFloat display "\.\d\+\(e[-+]\=\d\+\)\=\>" 126"floating point number, without dot, with exponent 127syn match awkFloat display "\<\d\+e[-+]\=\d\+\>" 128syn case match 129 130"syn match awkIdentifier "\<[a-zA-Z_][a-zA-Z0-9_]*\>" 131 132" Arithmetic operators: +, and - take care of ++, and -- 133syn match awkOperator "+\|-\|\*\|/\|%\|=" 134syn match awkOperator "+=\|-=\|\*=\|/=\|%=" 135syn match awkOperator "^\|^=" 136 137" Comparison expressions. 138syn match awkExpression "==\|>=\|=>\|<=\|=<\|\!=" 139syn match awkExpression "\~\|\!\~" 140syn match awkExpression "?\|:" 141syn keyword awkExpression in 142 143" Boolean Logic (OR, AND, NOT) 144"syn match awkBoolLogic "||\|&&\|\!" 145 146" This is overridden by less-than & greater-than. 147" Put this above those to override them. 148" Put this in a 'match "\<printf\=\>.*;\="' to make it not override 149" less/greater than (most of the time), but it won't work yet because 150" keywords always have precedence over match & region. 151" File I/O: (print foo, bar > "filename") & for nawk (getline < "filename") 152"syn match awkFileIO contained ">" 153"syn match awkFileIO contained "<" 154 155" Expression separators: ';' and ',' 156syn match awkSemicolon ";" 157syn match awkComma "," 158 159syn match awkComment "#.*" contains=@Spell,awkTodo 160 161syn match awkLineSkip "\\$" 162 163" Highlight array element's (recursive arrays allowed). 164" Keeps nested array names' separate from normal array elements. 165" Keeps numbers separate from normal array elements (variables). 166syn match awkArrayArray contained "[^][, \t]\+\["me=e-1 167syn match awkArrayElement contained "[^][, \t]\+" 168syn region awkArray transparent start="\[" end="\]" contains=awkArray,awkArrayElement,awkArrayArray,awkNumber,awkFloat 169 170" 10 should be enough. 171" (for the few instances where it would be more than "oneline") 172syn sync ccomment awkArray maxlines=10 173 174" define the default highlighting 175" For version 5.7 and earlier: only when not done already 176" For version 5.8 and later: only when an item doesn't have highlighting yet 177if version >= 508 || !exists("did_awk_syn_inits") 178 if version < 508 179 let did_awk_syn_inits = 1 180 command -nargs=+ HiLink hi link <args> 181 else 182 command -nargs=+ HiLink hi def link <args> 183 endif 184 185 HiLink awkConditional Conditional 186 HiLink awkFunction Function 187 HiLink awkRepeat Repeat 188 HiLink awkStatement Statement 189 190 HiLink awkString String 191 HiLink awkSpecialPrintf Special 192 HiLink awkSpecialCharacter Special 193 194 HiLink awkSearch String 195 HiLink awkBrackets awkRegExp 196 HiLink awkBrktRegExp awkNestRegExp 197 HiLink awkCharClass awkNestRegExp 198 HiLink awkNestRegExp Keyword 199 HiLink awkRegExp Special 200 201 HiLink awkNumber Number 202 HiLink awkFloat Float 203 204 HiLink awkFileIO Special 205 HiLink awkOperator Special 206 HiLink awkExpression Special 207 HiLink awkBoolLogic Special 208 209 HiLink awkPatterns Special 210 HiLink awkVariables Special 211 HiLink awkFieldVars Special 212 213 HiLink awkLineSkip Special 214 HiLink awkSemicolon Special 215 HiLink awkComma Special 216 "HiLink awkIdentifier Identifier 217 218 HiLink awkComment Comment 219 HiLink awkTodo Todo 220 221 " Change this if you want nested array names to be highlighted. 222 HiLink awkArrayArray awkArray 223 HiLink awkArrayElement Special 224 225 HiLink awkParenError awkError 226 HiLink awkInParen awkError 227 HiLink awkError Error 228 229 delcommand HiLink 230endif 231 232let b:current_syntax = "awk" 233 234let &cpo = s:cpo_save 235unlet s:cpo_save 236 237" vim: ts=8 238