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