blob: 4e7c0d1e7214e1c89be67132cf88d9ea1e0c1a31 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim syntax file
Bram Moolenaar3132cdd2020-11-05 20:41:49 +01002" Language: awk, nawk, gawk, mawk
3" Maintainer: Doug Kearns <dougkearns@gmail.com>
4" Previous Maintainer: Antonio Colombo <azc100@gmail.com>
John M Devin1e2007e2024-10-28 21:46:09 +01005" Last Change: 2024 Oct 28
Bram Moolenaar071d4272004-06-13 20:20:40 +00006
Bram Moolenaar269f5952016-07-15 22:54:41 +02007" AWK ref. is: Alfred V. Aho, Brian W. Kernighan, Peter J. Weinberger
Bram Moolenaar071d4272004-06-13 20:20:40 +00008" The AWK Programming Language, Addison-Wesley, 1988
9
10" GAWK ref. is: Arnold D. Robbins
11" Effective AWK Programming, Third Edition, O'Reilly, 2001
Bram Moolenaar8a94d872015-01-25 13:02:57 +010012" Effective AWK Programming, Fourth Edition, O'Reilly, 2015
Bram Moolenaar191acfd2020-03-27 20:42:43 +010013" (up-to-date version available with the gawk source distribution)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15" MAWK is a "new awk" meaning it implements AWK ref.
16" mawk conforms to the Posix 1003.2 (draft 11.3)
17" definition of the AWK language which contains a few features
18" not described in the AWK book, and mawk provides a small number of extensions.
19
20" TODO:
21" Dig into the commented out syntax expressions below.
22
Bram Moolenaar269f5952016-07-15 22:54:41 +020023" Quit when a syntax file was already loaded
24if exists("b:current_syntax")
Bram Moolenaar071d4272004-06-13 20:20:40 +000025 finish
26endif
27
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +010028let s:cpo_save = &cpo
29set cpo&vim
30
Bram Moolenaar207f0092020-08-30 17:20:20 +020031syn iskeyword @,48-57,_,192-255,@-@
32
Bram Moolenaar071d4272004-06-13 20:20:40 +000033" 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
Bram Moolenaar269f5952016-07-15 22:54:41 +020038" GAWK ref. Chapter 7-9
Bram Moolenaar191acfd2020-03-27 20:42:43 +010039syn keyword awkStatement case default switch nextfile
Bram Moolenaar269f5952016-07-15 22:54:41 +020040syn keyword awkStatement func
Bram Moolenaar191acfd2020-03-27 20:42:43 +010041" GAWK ref. Chapter 2.7, Including Other Files into Your Program
42" GAWK ref. Chapter 2.8, Loading Dynamic Extensions into Your Program
43" GAWK ref. Chapter 15, Namespaces
44" Directives
45syn keyword awkStatement @include @load @namespace
Bram Moolenaar8a94d872015-01-25 13:02:57 +010046"
47" GAWK ref. Chapter 9, Functions
Bram Moolenaar8a94d872015-01-25 13:02:57 +010048" Numeric Functions
Bram Moolenaar191acfd2020-03-27 20:42:43 +010049syn keyword awkFunction atan2 cos exp int log rand sin sqrt srand
Bram Moolenaar8a94d872015-01-25 13:02:57 +010050" String Manipulation Functions
Bram Moolenaar191acfd2020-03-27 20:42:43 +010051syn keyword awkFunction asort asorti gensub gsub index length match
Bram Moolenaar8a94d872015-01-25 13:02:57 +010052syn keyword awkFunction patsplit split sprintf strtonum sub substr
Bram Moolenaar071d4272004-06-13 20:20:40 +000053syn keyword awkFunction tolower toupper
Bram Moolenaar8a94d872015-01-25 13:02:57 +010054" Input Output Functions
55syn keyword awkFunction close fflush system
56" Time Functions
57syn keyword awkFunction mktime strftime systime
58" Bit Manipulation Functions
59syn keyword awkFunction and compl lshift or rshift xor
Bram Moolenaar191acfd2020-03-27 20:42:43 +010060" Getting Type Information Functions
Bram Moolenaar269f5952016-07-15 22:54:41 +020061syn keyword awkFunction isarray typeof
Bram Moolenaar8a94d872015-01-25 13:02:57 +010062" String-Translation Functions
63syn keyword awkFunction bindtextdomain dcgettext dcngetext
Bram Moolenaar071d4272004-06-13 20:20:40 +000064
65syn keyword awkConditional if else
Bram Moolenaar269f5952016-07-15 22:54:41 +020066syn keyword awkRepeat while for do
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
Bram Moolenaar269f5952016-07-15 22:54:41 +020068syn keyword awkTodo contained TODO
Bram Moolenaar071d4272004-06-13 20:20:40 +000069
Bram Moolenaar269f5952016-07-15 22:54:41 +020070syn keyword awkPatterns BEGIN END BEGINFILE ENDFILE
Bram Moolenaar8a94d872015-01-25 13:02:57 +010071
72" GAWK ref. Chapter 7
73" Built-in Variables That Control awk
74syn keyword awkVariables BINMODE CONVFMT FIELDWIDTHS FPAT FS
75syn keyword awkVariables IGNORECASE LINT OFMT OFS ORS PREC
76syn keyword awkVariables ROUNDMODE RS SUBSEP TEXTDOMAIN
77" Built-in Variables That Convey Information
78syn keyword awkVariables ARGC ARGV ARGIND ENVIRON ERRNO FILENAME
79syn keyword awkVariables FNR NF FUNCTAB NR PROCINFO RLENGTH RSTART
80syn keyword awkVariables RT SYMTAB
Bram Moolenaar071d4272004-06-13 20:20:40 +000081
Bram Moolenaar64d8e252016-09-06 22:12:34 +020082" Arithmetic operators: +, and - take care of ++, and --
83syn match awkOperator "+\|-\|\*\|/\|%\|="
84syn match awkOperator "+=\|-=\|\*=\|/=\|%="
85syn match awkOperator "\^\|\^="
Bram Moolenaar071d4272004-06-13 20:20:40 +000086
87" Octal format character.
88syn match awkSpecialCharacter display contained "\\[0-7]\{1,3\}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000089" Hex format character.
90syn match awkSpecialCharacter display contained "\\x[0-9A-Fa-f]\+"
91
92syn match awkFieldVars "\$\d\+"
93
Bram Moolenaar269f5952016-07-15 22:54:41 +020094" catch errors caused by wrong parenthesis
John M Devin1e2007e2024-10-28 21:46:09 +010095syn region awkParen transparent start="(" end=")" contains=ALLBUT,awkParenError,awkSpecialCharacter,awkArrayElement,awkArrayArray,awkTodo,awkRegExp,awkBrktRegExp,awkBrackets,awkCharClass
Bram Moolenaar071d4272004-06-13 20:20:40 +000096syn match awkParenError display ")"
Bram Moolenaar269f5952016-07-15 22:54:41 +020097"syn match awkInParen display contained "[{}]"
Bram Moolenaar071d4272004-06-13 20:20:40 +000098
99" 64 lines for complex &&'s, and ||'s in a big "if"
100syn sync ccomment awkParen maxlines=64
101
102" Search strings & Regular Expressions therein.
103syn region awkSearch oneline start="^[ \t]*/"ms=e start="\(,\|!\=\~\)[ \t]*/"ms=e skip="\\\\\|\\/" end="/" contains=awkBrackets,awkRegExp,awkSpecialCharacter
104syn region awkBrackets contained start="\[\^\]\="ms=s+2 start="\[[^\^]"ms=s+1 end="\]"me=e-1 contains=awkBrktRegExp,awkCharClass
105syn region awkSearch oneline start="[ \t]*/"hs=e skip="\\\\\|\\/" end="/" contains=awkBrackets,awkRegExp,awkSpecialCharacter
106
107syn match awkCharClass contained "\[:[^:\]]*:\]"
108syn match awkBrktRegExp contained "\\.\|.\-[^]]"
109syn match awkRegExp contained "/\^"ms=s+1
110syn match awkRegExp contained "\$/"me=e-1
111syn match awkRegExp contained "[?.*{}|+]"
112
113" String and Character constants
114" Highlight special characters (those which have a backslash) differently
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +0200115syn region awkString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=@Spell,awkSpecialCharacter,awkSpecialPrintf
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116syn match awkSpecialCharacter contained "\\."
117
118" Some of these combinations may seem weird, but they work.
119syn match awkSpecialPrintf contained "%[-+ #]*\d*\.\=\d*[cdefgiosuxEGX%]"
120
121" Numbers, allowing signs (both -, and +)
122" Integer number.
123syn match awkNumber display "[+-]\=\<\d\+\>"
124" Floating point number.
125syn match awkFloat display "[+-]\=\<\d\+\.\d+\>"
126" Floating point number, starting with a dot.
127syn match awkFloat display "[+-]\=\<.\d+\>"
128syn case ignore
129"floating point number, with dot, optional exponent
130syn match awkFloat display "\<\d\+\.\d*\(e[-+]\=\d\+\)\=\>"
131"floating point number, starting with a dot, optional exponent
132syn match awkFloat display "\.\d\+\(e[-+]\=\d\+\)\=\>"
133"floating point number, without dot, with exponent
134syn match awkFloat display "\<\d\+e[-+]\=\d\+\>"
135syn case match
136
137"syn match awkIdentifier "\<[a-zA-Z_][a-zA-Z0-9_]*\>"
138
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139" Comparison expressions.
Bram Moolenaar8a94d872015-01-25 13:02:57 +0100140syn match awkExpression "==\|>=\|=>\|<=\|=<\|\!="
141syn match awkExpression "\~\|\!\~"
142syn match awkExpression "?\|:"
143syn keyword awkExpression in
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145" Boolean Logic (OR, AND, NOT)
Bram Moolenaar269f5952016-07-15 22:54:41 +0200146syn match awkBoolLogic "||\|&&\|\!"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147
148" This is overridden by less-than & greater-than.
149" Put this above those to override them.
150" Put this in a 'match "\<printf\=\>.*;\="' to make it not override
151" less/greater than (most of the time), but it won't work yet because
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +0200152" keywords always have precedence over match & region.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153" File I/O: (print foo, bar > "filename") & for nawk (getline < "filename")
154"syn match awkFileIO contained ">"
155"syn match awkFileIO contained "<"
156
157" Expression separators: ';' and ','
158syn match awkSemicolon ";"
159syn match awkComma ","
160
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +0200161syn match awkComment "#.*" contains=@Spell,awkTodo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162
163syn match awkLineSkip "\\$"
164
165" Highlight array element's (recursive arrays allowed).
166" Keeps nested array names' separate from normal array elements.
167" Keeps numbers separate from normal array elements (variables).
168syn match awkArrayArray contained "[^][, \t]\+\["me=e-1
169syn match awkArrayElement contained "[^][, \t]\+"
170syn region awkArray transparent start="\[" end="\]" contains=awkArray,awkArrayElement,awkArrayArray,awkNumber,awkFloat
171
172" 10 should be enough.
173" (for the few instances where it would be more than "oneline")
174syn sync ccomment awkArray maxlines=10
175
Bram Moolenaar269f5952016-07-15 22:54:41 +0200176" Define the default highlighting.
Bram Moolenaar269f5952016-07-15 22:54:41 +0200177hi def link awkConditional Conditional
178hi def link awkFunction Function
179hi def link awkRepeat Repeat
180hi def link awkStatement Statement
181hi def link awkString String
182hi def link awkSpecialPrintf Special
183hi def link awkSpecialCharacter Special
184hi def link awkSearch String
185hi def link awkBrackets awkRegExp
186hi def link awkBrktRegExp awkNestRegExp
187hi def link awkCharClass awkNestRegExp
188hi def link awkNestRegExp Keyword
189hi def link awkRegExp Special
190hi def link awkNumber Number
191hi def link awkFloat Float
192hi def link awkFileIO Special
193hi def link awkOperator Special
194hi def link awkExpression Special
195hi def link awkBoolLogic Special
196hi def link awkPatterns Special
197hi def link awkVariables Special
198hi def link awkFieldVars Special
199hi def link awkLineSkip Special
200hi def link awkSemicolon Special
201hi def link awkComma Special
202hi def link awkIdentifier Identifier
203hi def link awkComment Comment
204hi def link awkTodo Todo
205" Change this if you want nested array names to be highlighted.
206hi def link awkArrayArray awkArray
207hi def link awkArrayElement Special
208hi def link awkParenError awkError
209hi def link awkInParen awkError
210hi def link awkError Error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
212let b:current_syntax = "awk"
213
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +0100214let &cpo = s:cpo_save
215unlet s:cpo_save
216
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217" vim: ts=8