blob: c9696e6febb43569d63db61aa440a16ff8159737 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim syntax file
2" Language: awk, nawk, gawk, mawk
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +01003" Maintainer: Antonio Colombo <azc100@gmail.com>
Bram Moolenaar191acfd2020-03-27 20:42:43 +01004" Last Change: 2020 Mar 25
Bram Moolenaar071d4272004-06-13 20:20:40 +00005
Bram Moolenaar269f5952016-07-15 22:54:41 +02006" AWK ref. is: Alfred V. Aho, Brian W. Kernighan, Peter J. Weinberger
Bram Moolenaar071d4272004-06-13 20:20:40 +00007" The AWK Programming Language, Addison-Wesley, 1988
8
9" GAWK ref. is: Arnold D. Robbins
10" Effective AWK Programming, Third Edition, O'Reilly, 2001
Bram Moolenaar8a94d872015-01-25 13:02:57 +010011" Effective AWK Programming, Fourth Edition, O'Reilly, 2015
Bram Moolenaar191acfd2020-03-27 20:42:43 +010012" (up-to-date version available with the gawk source distribution)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
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
Bram Moolenaar269f5952016-07-15 22:54:41 +020022" Quit when a syntax file was already loaded
23if exists("b:current_syntax")
Bram Moolenaar071d4272004-06-13 20:20:40 +000024 finish
25endif
26
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +010027let s:cpo_save = &cpo
28set cpo&vim
29
Bram Moolenaar071d4272004-06-13 20:20:40 +000030" 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
Bram Moolenaar269f5952016-07-15 22:54:41 +020035" GAWK ref. Chapter 7-9
Bram Moolenaar191acfd2020-03-27 20:42:43 +010036syn keyword awkStatement case default switch nextfile
Bram Moolenaar269f5952016-07-15 22:54:41 +020037syn keyword awkStatement func
Bram Moolenaar191acfd2020-03-27 20:42:43 +010038" 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
Bram Moolenaar8a94d872015-01-25 13:02:57 +010043"
44" GAWK ref. Chapter 9, Functions
Bram Moolenaar8a94d872015-01-25 13:02:57 +010045" Numeric Functions
Bram Moolenaar191acfd2020-03-27 20:42:43 +010046syn keyword awkFunction atan2 cos exp int log rand sin sqrt srand
Bram Moolenaar8a94d872015-01-25 13:02:57 +010047" String Manipulation Functions
Bram Moolenaar191acfd2020-03-27 20:42:43 +010048syn keyword awkFunction asort asorti gensub gsub index length match
Bram Moolenaar8a94d872015-01-25 13:02:57 +010049syn keyword awkFunction patsplit split sprintf strtonum sub substr
Bram Moolenaar071d4272004-06-13 20:20:40 +000050syn keyword awkFunction tolower toupper
Bram Moolenaar8a94d872015-01-25 13:02:57 +010051" 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
Bram Moolenaar191acfd2020-03-27 20:42:43 +010057" Getting Type Information Functions
Bram Moolenaar269f5952016-07-15 22:54:41 +020058syn keyword awkFunction isarray typeof
Bram Moolenaar8a94d872015-01-25 13:02:57 +010059" String-Translation Functions
60syn keyword awkFunction bindtextdomain dcgettext dcngetext
Bram Moolenaar071d4272004-06-13 20:20:40 +000061
62syn keyword awkConditional if else
Bram Moolenaar269f5952016-07-15 22:54:41 +020063syn keyword awkRepeat while for do
Bram Moolenaar071d4272004-06-13 20:20:40 +000064
Bram Moolenaar269f5952016-07-15 22:54:41 +020065syn keyword awkTodo contained TODO
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
Bram Moolenaar269f5952016-07-15 22:54:41 +020067syn keyword awkPatterns BEGIN END BEGINFILE ENDFILE
Bram Moolenaar8a94d872015-01-25 13:02:57 +010068
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
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
Bram Moolenaar64d8e252016-09-06 22:12:34 +020079" Arithmetic operators: +, and - take care of ++, and --
80syn match awkOperator "+\|-\|\*\|/\|%\|="
81syn match awkOperator "+=\|-=\|\*=\|/=\|%="
82syn match awkOperator "\^\|\^="
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
84" Octal format character.
85syn match awkSpecialCharacter display contained "\\[0-7]\{1,3\}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000086" Hex format character.
87syn match awkSpecialCharacter display contained "\\x[0-9A-Fa-f]\+"
88
89syn match awkFieldVars "\$\d\+"
90
Bram Moolenaar269f5952016-07-15 22:54:41 +020091" catch errors caused by wrong parenthesis
92syn region awkParen transparent start="(" end=")" contains=ALLBUT,awkParenError,awkSpecialCharacter,awkArrayElement,awkArrayArray,awkTodo,awkRegExp,awkBrktRegExp,awkBrackets,awkCharClass,awkComment
Bram Moolenaar071d4272004-06-13 20:20:40 +000093syn match awkParenError display ")"
Bram Moolenaar269f5952016-07-15 22:54:41 +020094"syn match awkInParen display contained "[{}]"
Bram Moolenaar071d4272004-06-13 20:20:40 +000095
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
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +0200112syn region awkString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=@Spell,awkSpecialCharacter,awkSpecialPrintf
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113syn 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
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136" Comparison expressions.
Bram Moolenaar8a94d872015-01-25 13:02:57 +0100137syn match awkExpression "==\|>=\|=>\|<=\|=<\|\!="
138syn match awkExpression "\~\|\!\~"
139syn match awkExpression "?\|:"
140syn keyword awkExpression in
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141
142" Boolean Logic (OR, AND, NOT)
Bram Moolenaar269f5952016-07-15 22:54:41 +0200143syn match awkBoolLogic "||\|&&\|\!"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
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
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +0200149" keywords always have precedence over match & region.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150" 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
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +0200158syn match awkComment "#.*" contains=@Spell,awkTodo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159
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
Bram Moolenaar269f5952016-07-15 22:54:41 +0200173" Define the default highlighting.
Bram Moolenaar269f5952016-07-15 22:54:41 +0200174hi 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
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
209let b:current_syntax = "awk"
210
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +0100211let &cpo = s:cpo_save
212unlet s:cpo_save
213
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214" vim: ts=8