blob: 7c0682ce9f26cd9b64b66ce6cb56335cf9f3fec3 [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 Moolenaar64d8e252016-09-06 22:12:34 +02004" Last Change: 2016 Sep 05
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 Moolenaar269f5952016-07-15 22:54:41 +020012" (also available and updated 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
36syn keyword awkStatement switch nextfile
37syn keyword awkStatement func
Bram Moolenaar8a94d872015-01-25 13:02:57 +010038"
39" GAWK ref. Chapter 9, Functions
Bram Moolenaar8a94d872015-01-25 13:02:57 +010040" Numeric Functions
Bram Moolenaar269f5952016-07-15 22:54:41 +020041syn keyword awkFunction atan2 cos exp int intdiv log rand sin sqrt srand
Bram Moolenaar8a94d872015-01-25 13:02:57 +010042" String Manipulation Functions
43syn keyword awkFunction asort asort1 gensub gsub index length match
44syn keyword awkFunction patsplit split sprintf strtonum sub substr
Bram Moolenaar071d4272004-06-13 20:20:40 +000045syn keyword awkFunction tolower toupper
Bram Moolenaar8a94d872015-01-25 13:02:57 +010046" 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
Bram Moolenaar269f5952016-07-15 22:54:41 +020052" Getting Type Functions
53syn keyword awkFunction isarray typeof
Bram Moolenaar8a94d872015-01-25 13:02:57 +010054" String-Translation Functions
55syn keyword awkFunction bindtextdomain dcgettext dcngetext
Bram Moolenaar071d4272004-06-13 20:20:40 +000056
57syn keyword awkConditional if else
Bram Moolenaar269f5952016-07-15 22:54:41 +020058syn keyword awkRepeat while for do
Bram Moolenaar071d4272004-06-13 20:20:40 +000059
Bram Moolenaar269f5952016-07-15 22:54:41 +020060syn keyword awkTodo contained TODO
Bram Moolenaar071d4272004-06-13 20:20:40 +000061
Bram Moolenaar269f5952016-07-15 22:54:41 +020062syn keyword awkPatterns BEGIN END BEGINFILE ENDFILE
Bram Moolenaar8a94d872015-01-25 13:02:57 +010063
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
Bram Moolenaar071d4272004-06-13 20:20:40 +000073
Bram Moolenaar64d8e252016-09-06 22:12:34 +020074" Arithmetic operators: +, and - take care of ++, and --
75syn match awkOperator "+\|-\|\*\|/\|%\|="
76syn match awkOperator "+=\|-=\|\*=\|/=\|%="
77syn match awkOperator "\^\|\^="
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
79" Octal format character.
80syn match awkSpecialCharacter display contained "\\[0-7]\{1,3\}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000081" Hex format character.
82syn match awkSpecialCharacter display contained "\\x[0-9A-Fa-f]\+"
83
84syn match awkFieldVars "\$\d\+"
85
Bram Moolenaar269f5952016-07-15 22:54:41 +020086" catch errors caused by wrong parenthesis
87syn region awkParen transparent start="(" end=")" contains=ALLBUT,awkParenError,awkSpecialCharacter,awkArrayElement,awkArrayArray,awkTodo,awkRegExp,awkBrktRegExp,awkBrackets,awkCharClass,awkComment
Bram Moolenaar071d4272004-06-13 20:20:40 +000088syn match awkParenError display ")"
Bram Moolenaar269f5952016-07-15 22:54:41 +020089"syn match awkInParen display contained "[{}]"
Bram Moolenaar071d4272004-06-13 20:20:40 +000090
91" 64 lines for complex &&'s, and ||'s in a big "if"
92syn sync ccomment awkParen maxlines=64
93
94" Search strings & Regular Expressions therein.
95syn region awkSearch oneline start="^[ \t]*/"ms=e start="\(,\|!\=\~\)[ \t]*/"ms=e skip="\\\\\|\\/" end="/" contains=awkBrackets,awkRegExp,awkSpecialCharacter
96syn region awkBrackets contained start="\[\^\]\="ms=s+2 start="\[[^\^]"ms=s+1 end="\]"me=e-1 contains=awkBrktRegExp,awkCharClass
97syn region awkSearch oneline start="[ \t]*/"hs=e skip="\\\\\|\\/" end="/" contains=awkBrackets,awkRegExp,awkSpecialCharacter
98
99syn match awkCharClass contained "\[:[^:\]]*:\]"
100syn match awkBrktRegExp contained "\\.\|.\-[^]]"
101syn match awkRegExp contained "/\^"ms=s+1
102syn match awkRegExp contained "\$/"me=e-1
103syn match awkRegExp contained "[?.*{}|+]"
104
105" String and Character constants
106" Highlight special characters (those which have a backslash) differently
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +0200107syn region awkString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=@Spell,awkSpecialCharacter,awkSpecialPrintf
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108syn match awkSpecialCharacter contained "\\."
109
110" Some of these combinations may seem weird, but they work.
111syn match awkSpecialPrintf contained "%[-+ #]*\d*\.\=\d*[cdefgiosuxEGX%]"
112
113" Numbers, allowing signs (both -, and +)
114" Integer number.
115syn match awkNumber display "[+-]\=\<\d\+\>"
116" Floating point number.
117syn match awkFloat display "[+-]\=\<\d\+\.\d+\>"
118" Floating point number, starting with a dot.
119syn match awkFloat display "[+-]\=\<.\d+\>"
120syn case ignore
121"floating point number, with dot, optional exponent
122syn match awkFloat display "\<\d\+\.\d*\(e[-+]\=\d\+\)\=\>"
123"floating point number, starting with a dot, optional exponent
124syn match awkFloat display "\.\d\+\(e[-+]\=\d\+\)\=\>"
125"floating point number, without dot, with exponent
126syn match awkFloat display "\<\d\+e[-+]\=\d\+\>"
127syn case match
128
129"syn match awkIdentifier "\<[a-zA-Z_][a-zA-Z0-9_]*\>"
130
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131" Comparison expressions.
Bram Moolenaar8a94d872015-01-25 13:02:57 +0100132syn match awkExpression "==\|>=\|=>\|<=\|=<\|\!="
133syn match awkExpression "\~\|\!\~"
134syn match awkExpression "?\|:"
135syn keyword awkExpression in
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
137" Boolean Logic (OR, AND, NOT)
Bram Moolenaar269f5952016-07-15 22:54:41 +0200138syn match awkBoolLogic "||\|&&\|\!"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139
140" This is overridden by less-than & greater-than.
141" Put this above those to override them.
142" Put this in a 'match "\<printf\=\>.*;\="' to make it not override
143" less/greater than (most of the time), but it won't work yet because
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +0200144" keywords always have precedence over match & region.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145" File I/O: (print foo, bar > "filename") & for nawk (getline < "filename")
146"syn match awkFileIO contained ">"
147"syn match awkFileIO contained "<"
148
149" Expression separators: ';' and ','
150syn match awkSemicolon ";"
151syn match awkComma ","
152
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +0200153syn match awkComment "#.*" contains=@Spell,awkTodo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154
155syn match awkLineSkip "\\$"
156
157" Highlight array element's (recursive arrays allowed).
158" Keeps nested array names' separate from normal array elements.
159" Keeps numbers separate from normal array elements (variables).
160syn match awkArrayArray contained "[^][, \t]\+\["me=e-1
161syn match awkArrayElement contained "[^][, \t]\+"
162syn region awkArray transparent start="\[" end="\]" contains=awkArray,awkArrayElement,awkArrayArray,awkNumber,awkFloat
163
164" 10 should be enough.
165" (for the few instances where it would be more than "oneline")
166syn sync ccomment awkArray maxlines=10
167
Bram Moolenaar269f5952016-07-15 22:54:41 +0200168" Define the default highlighting.
Bram Moolenaar269f5952016-07-15 22:54:41 +0200169hi def link awkConditional Conditional
170hi def link awkFunction Function
171hi def link awkRepeat Repeat
172hi def link awkStatement Statement
173hi def link awkString String
174hi def link awkSpecialPrintf Special
175hi def link awkSpecialCharacter Special
176hi def link awkSearch String
177hi def link awkBrackets awkRegExp
178hi def link awkBrktRegExp awkNestRegExp
179hi def link awkCharClass awkNestRegExp
180hi def link awkNestRegExp Keyword
181hi def link awkRegExp Special
182hi def link awkNumber Number
183hi def link awkFloat Float
184hi def link awkFileIO Special
185hi def link awkOperator Special
186hi def link awkExpression Special
187hi def link awkBoolLogic Special
188hi def link awkPatterns Special
189hi def link awkVariables Special
190hi def link awkFieldVars Special
191hi def link awkLineSkip Special
192hi def link awkSemicolon Special
193hi def link awkComma Special
194hi def link awkIdentifier Identifier
195hi def link awkComment Comment
196hi def link awkTodo Todo
197" Change this if you want nested array names to be highlighted.
198hi def link awkArrayArray awkArray
199hi def link awkArrayElement Special
200hi def link awkParenError awkError
201hi def link awkInParen awkError
202hi def link awkError Error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204let b:current_syntax = "awk"
205
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +0100206let &cpo = s:cpo_save
207unlet s:cpo_save
208
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209" vim: ts=8