blob: 43455b864d59a1bd656c1ea819f28027adccf172 [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 Moolenaar207f0092020-08-30 17:20:20 +02004" Last Change: 2020 Aug 18
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 Moolenaar207f0092020-08-30 17:20:20 +020030syn iskeyword @,48-57,_,192-255,@-@
31
Bram Moolenaar071d4272004-06-13 20:20:40 +000032" A bunch of useful Awk keywords
33" AWK ref. p. 188
34syn keyword awkStatement break continue delete exit
35syn keyword awkStatement function getline next
36syn keyword awkStatement print printf return
Bram Moolenaar269f5952016-07-15 22:54:41 +020037" GAWK ref. Chapter 7-9
Bram Moolenaar191acfd2020-03-27 20:42:43 +010038syn keyword awkStatement case default switch nextfile
Bram Moolenaar269f5952016-07-15 22:54:41 +020039syn keyword awkStatement func
Bram Moolenaar191acfd2020-03-27 20:42:43 +010040" GAWK ref. Chapter 2.7, Including Other Files into Your Program
41" GAWK ref. Chapter 2.8, Loading Dynamic Extensions into Your Program
42" GAWK ref. Chapter 15, Namespaces
43" Directives
44syn keyword awkStatement @include @load @namespace
Bram Moolenaar8a94d872015-01-25 13:02:57 +010045"
46" GAWK ref. Chapter 9, Functions
Bram Moolenaar8a94d872015-01-25 13:02:57 +010047" Numeric Functions
Bram Moolenaar191acfd2020-03-27 20:42:43 +010048syn keyword awkFunction atan2 cos exp int log rand sin sqrt srand
Bram Moolenaar8a94d872015-01-25 13:02:57 +010049" String Manipulation Functions
Bram Moolenaar191acfd2020-03-27 20:42:43 +010050syn keyword awkFunction asort asorti gensub gsub index length match
Bram Moolenaar8a94d872015-01-25 13:02:57 +010051syn keyword awkFunction patsplit split sprintf strtonum sub substr
Bram Moolenaar071d4272004-06-13 20:20:40 +000052syn keyword awkFunction tolower toupper
Bram Moolenaar8a94d872015-01-25 13:02:57 +010053" Input Output Functions
54syn keyword awkFunction close fflush system
55" Time Functions
56syn keyword awkFunction mktime strftime systime
57" Bit Manipulation Functions
58syn keyword awkFunction and compl lshift or rshift xor
Bram Moolenaar191acfd2020-03-27 20:42:43 +010059" Getting Type Information Functions
Bram Moolenaar269f5952016-07-15 22:54:41 +020060syn keyword awkFunction isarray typeof
Bram Moolenaar8a94d872015-01-25 13:02:57 +010061" String-Translation Functions
62syn keyword awkFunction bindtextdomain dcgettext dcngetext
Bram Moolenaar071d4272004-06-13 20:20:40 +000063
64syn keyword awkConditional if else
Bram Moolenaar269f5952016-07-15 22:54:41 +020065syn keyword awkRepeat while for do
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
Bram Moolenaar269f5952016-07-15 22:54:41 +020067syn keyword awkTodo contained TODO
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaar269f5952016-07-15 22:54:41 +020069syn keyword awkPatterns BEGIN END BEGINFILE ENDFILE
Bram Moolenaar8a94d872015-01-25 13:02:57 +010070
71" GAWK ref. Chapter 7
72" Built-in Variables That Control awk
73syn keyword awkVariables BINMODE CONVFMT FIELDWIDTHS FPAT FS
74syn keyword awkVariables IGNORECASE LINT OFMT OFS ORS PREC
75syn keyword awkVariables ROUNDMODE RS SUBSEP TEXTDOMAIN
76" Built-in Variables That Convey Information
77syn keyword awkVariables ARGC ARGV ARGIND ENVIRON ERRNO FILENAME
78syn keyword awkVariables FNR NF FUNCTAB NR PROCINFO RLENGTH RSTART
79syn keyword awkVariables RT SYMTAB
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
Bram Moolenaar64d8e252016-09-06 22:12:34 +020081" Arithmetic operators: +, and - take care of ++, and --
82syn match awkOperator "+\|-\|\*\|/\|%\|="
83syn match awkOperator "+=\|-=\|\*=\|/=\|%="
84syn match awkOperator "\^\|\^="
Bram Moolenaar071d4272004-06-13 20:20:40 +000085
86" Octal format character.
87syn match awkSpecialCharacter display contained "\\[0-7]\{1,3\}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000088" Hex format character.
89syn match awkSpecialCharacter display contained "\\x[0-9A-Fa-f]\+"
90
91syn match awkFieldVars "\$\d\+"
92
Bram Moolenaar269f5952016-07-15 22:54:41 +020093" catch errors caused by wrong parenthesis
94syn region awkParen transparent start="(" end=")" contains=ALLBUT,awkParenError,awkSpecialCharacter,awkArrayElement,awkArrayArray,awkTodo,awkRegExp,awkBrktRegExp,awkBrackets,awkCharClass,awkComment
Bram Moolenaar071d4272004-06-13 20:20:40 +000095syn match awkParenError display ")"
Bram Moolenaar269f5952016-07-15 22:54:41 +020096"syn match awkInParen display contained "[{}]"
Bram Moolenaar071d4272004-06-13 20:20:40 +000097
98" 64 lines for complex &&'s, and ||'s in a big "if"
99syn sync ccomment awkParen maxlines=64
100
101" Search strings & Regular Expressions therein.
102syn region awkSearch oneline start="^[ \t]*/"ms=e start="\(,\|!\=\~\)[ \t]*/"ms=e skip="\\\\\|\\/" end="/" contains=awkBrackets,awkRegExp,awkSpecialCharacter
103syn region awkBrackets contained start="\[\^\]\="ms=s+2 start="\[[^\^]"ms=s+1 end="\]"me=e-1 contains=awkBrktRegExp,awkCharClass
104syn region awkSearch oneline start="[ \t]*/"hs=e skip="\\\\\|\\/" end="/" contains=awkBrackets,awkRegExp,awkSpecialCharacter
105
106syn match awkCharClass contained "\[:[^:\]]*:\]"
107syn match awkBrktRegExp contained "\\.\|.\-[^]]"
108syn match awkRegExp contained "/\^"ms=s+1
109syn match awkRegExp contained "\$/"me=e-1
110syn match awkRegExp contained "[?.*{}|+]"
111
112" String and Character constants
113" Highlight special characters (those which have a backslash) differently
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +0200114syn region awkString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=@Spell,awkSpecialCharacter,awkSpecialPrintf
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115syn match awkSpecialCharacter contained "\\."
116
117" Some of these combinations may seem weird, but they work.
118syn match awkSpecialPrintf contained "%[-+ #]*\d*\.\=\d*[cdefgiosuxEGX%]"
119
120" Numbers, allowing signs (both -, and +)
121" Integer number.
122syn match awkNumber display "[+-]\=\<\d\+\>"
123" Floating point number.
124syn match awkFloat display "[+-]\=\<\d\+\.\d+\>"
125" Floating point number, starting with a dot.
126syn match awkFloat display "[+-]\=\<.\d+\>"
127syn case ignore
128"floating point number, with dot, optional exponent
129syn match awkFloat display "\<\d\+\.\d*\(e[-+]\=\d\+\)\=\>"
130"floating point number, starting with a dot, optional exponent
131syn match awkFloat display "\.\d\+\(e[-+]\=\d\+\)\=\>"
132"floating point number, without dot, with exponent
133syn match awkFloat display "\<\d\+e[-+]\=\d\+\>"
134syn case match
135
136"syn match awkIdentifier "\<[a-zA-Z_][a-zA-Z0-9_]*\>"
137
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138" Comparison expressions.
Bram Moolenaar8a94d872015-01-25 13:02:57 +0100139syn match awkExpression "==\|>=\|=>\|<=\|=<\|\!="
140syn match awkExpression "\~\|\!\~"
141syn match awkExpression "?\|:"
142syn keyword awkExpression in
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143
144" Boolean Logic (OR, AND, NOT)
Bram Moolenaar269f5952016-07-15 22:54:41 +0200145syn match awkBoolLogic "||\|&&\|\!"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146
147" This is overridden by less-than & greater-than.
148" Put this above those to override them.
149" Put this in a 'match "\<printf\=\>.*;\="' to make it not override
150" less/greater than (most of the time), but it won't work yet because
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +0200151" keywords always have precedence over match & region.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152" File I/O: (print foo, bar > "filename") & for nawk (getline < "filename")
153"syn match awkFileIO contained ">"
154"syn match awkFileIO contained "<"
155
156" Expression separators: ';' and ','
157syn match awkSemicolon ";"
158syn match awkComma ","
159
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +0200160syn match awkComment "#.*" contains=@Spell,awkTodo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161
162syn match awkLineSkip "\\$"
163
164" Highlight array element's (recursive arrays allowed).
165" Keeps nested array names' separate from normal array elements.
166" Keeps numbers separate from normal array elements (variables).
167syn match awkArrayArray contained "[^][, \t]\+\["me=e-1
168syn match awkArrayElement contained "[^][, \t]\+"
169syn region awkArray transparent start="\[" end="\]" contains=awkArray,awkArrayElement,awkArrayArray,awkNumber,awkFloat
170
171" 10 should be enough.
172" (for the few instances where it would be more than "oneline")
173syn sync ccomment awkArray maxlines=10
174
Bram Moolenaar269f5952016-07-15 22:54:41 +0200175" Define the default highlighting.
Bram Moolenaar269f5952016-07-15 22:54:41 +0200176hi def link awkConditional Conditional
177hi def link awkFunction Function
178hi def link awkRepeat Repeat
179hi def link awkStatement Statement
180hi def link awkString String
181hi def link awkSpecialPrintf Special
182hi def link awkSpecialCharacter Special
183hi def link awkSearch String
184hi def link awkBrackets awkRegExp
185hi def link awkBrktRegExp awkNestRegExp
186hi def link awkCharClass awkNestRegExp
187hi def link awkNestRegExp Keyword
188hi def link awkRegExp Special
189hi def link awkNumber Number
190hi def link awkFloat Float
191hi def link awkFileIO Special
192hi def link awkOperator Special
193hi def link awkExpression Special
194hi def link awkBoolLogic Special
195hi def link awkPatterns Special
196hi def link awkVariables Special
197hi def link awkFieldVars Special
198hi def link awkLineSkip Special
199hi def link awkSemicolon Special
200hi def link awkComma Special
201hi def link awkIdentifier Identifier
202hi def link awkComment Comment
203hi def link awkTodo Todo
204" Change this if you want nested array names to be highlighted.
205hi def link awkArrayArray awkArray
206hi def link awkArrayElement Special
207hi def link awkParenError awkError
208hi def link awkInParen awkError
209hi def link awkError Error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
211let b:current_syntax = "awk"
212
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +0100213let &cpo = s:cpo_save
214unlet s:cpo_save
215
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216" vim: ts=8