xref: /vim-8.2.3635/runtime/syntax/awk.vim (revision bb76f24a)
1" Vim syntax file
2" Language:	awk, nawk, gawk, mawk
3" Maintainer:	Antonio Colombo <[email protected]>
4" Last Change:	2016 Sep 05
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" (also available and updated 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	switch nextfile
37syn keyword awkStatement	func
38"
39" GAWK ref. Chapter 9, Functions
40" Numeric Functions
41syn keyword awkFunction	atan2 cos exp int intdiv log rand sin sqrt srand
42" String Manipulation Functions
43syn keyword awkFunction	asort asort1 gensub gsub index length match
44syn keyword awkFunction	patsplit split sprintf strtonum sub substr
45syn keyword awkFunction	tolower toupper
46" 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
52" Getting Type Functions
53syn keyword awkFunction	isarray typeof
54" String-Translation Functions
55syn keyword awkFunction	bindtextdomain dcgettext dcngetext
56
57syn keyword awkConditional	if else
58syn keyword awkRepeat	while for do
59
60syn keyword awkTodo	contained TODO
61
62syn keyword awkPatterns	BEGIN END BEGINFILE ENDFILE
63
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
73
74" Arithmetic operators: +, and - take care of ++, and --
75syn match   awkOperator		"+\|-\|\*\|/\|%\|="
76syn match   awkOperator		"+=\|-=\|\*=\|/=\|%="
77syn match   awkOperator		"\^\|\^="
78
79" Octal format character.
80syn match   awkSpecialCharacter display contained "\\[0-7]\{1,3\}"
81" Hex   format character.
82syn match   awkSpecialCharacter display contained "\\x[0-9A-Fa-f]\+"
83
84syn match   awkFieldVars	"\$\d\+"
85
86" catch errors caused by wrong parenthesis
87syn region	awkParen	transparent start="(" end=")" contains=ALLBUT,awkParenError,awkSpecialCharacter,awkArrayElement,awkArrayArray,awkTodo,awkRegExp,awkBrktRegExp,awkBrackets,awkCharClass,awkComment
88syn match	awkParenError	display ")"
89"syn match	awkInParen	display contained "[{}]"
90
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
107syn region  awkString	start=+"+  skip=+\\\\\|\\"+  end=+"+  contains=@Spell,awkSpecialCharacter,awkSpecialPrintf
108syn 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
131" Comparison expressions.
132syn match   awkExpression	"==\|>=\|=>\|<=\|=<\|\!="
133syn match   awkExpression	"\~\|\!\~"
134syn match   awkExpression	"?\|:"
135syn keyword awkExpression	in
136
137" Boolean Logic (OR, AND, NOT)
138syn match  awkBoolLogic	"||\|&&\|\!"
139
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
144" keywords always have precedence over match & region.
145" 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
153syn match  awkComment	"#.*" contains=@Spell,awkTodo
154
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
168" Define the default highlighting.
169hi 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
203
204let b:current_syntax = "awk"
205
206let &cpo = s:cpo_save
207unlet s:cpo_save
208
209" vim: ts=8
210