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