xref: /vim-8.2.3635/runtime/syntax/ocaml.vim (revision 01a6c216)
1" Vim syntax file
2" Language:     OCaml
3" Filenames:    *.ml *.mli *.mll *.mly
4" Maintainers:  Markus Mottl      <[email protected]>
5"               Karl-Heinz Sylla  <[email protected]>
6"               Issac Trotts      <[email protected]>
7" URL:          http://www.ocaml.info/vim/syntax/ocaml.vim
8" Last Change:  2012 May 12 - Added Dominique Pellé's spell checking patch (MM)
9"               2012 Feb 01 - Improved module path highlighting (MM)
10"               2010 Oct 11 - Added highlighting of lnot (MM, thanks to Erick Matsen)
11
12" A minor patch was applied to the official version so that object/end
13" can be distinguished from begin/end, which is used for indentation,
14" and folding. (David Baelde)
15
16" quit when a syntax file was already loaded
17if exists("b:current_syntax") && b:current_syntax == "ocaml"
18  finish
19endif
20
21" OCaml is case sensitive.
22syn case match
23
24" Access to the method of an object
25syn match    ocamlMethod       "#"
26
27" Script headers highlighted like comments
28syn match    ocamlComment   "^#!.*" contains=@Spell
29
30" Scripting directives
31syn match    ocamlScript "^#\<\(quit\|labels\|warnings\|directory\|cd\|load\|use\|install_printer\|remove_printer\|require\|thread\|trace\|untrace\|untrace_all\|print_depth\|print_length\|camlp4o\)\>"
32
33" lowercase identifier - the standard way to match
34syn match    ocamlLCIdentifier /\<\(\l\|_\)\(\w\|'\)*\>/
35
36syn match    ocamlKeyChar    "|"
37
38" Errors
39syn match    ocamlBraceErr   "}"
40syn match    ocamlBrackErr   "\]"
41syn match    ocamlParenErr   ")"
42syn match    ocamlArrErr     "|]"
43
44syn match    ocamlCommentErr "\*)"
45
46syn match    ocamlCountErr   "\<downto\>"
47syn match    ocamlCountErr   "\<to\>"
48
49if !exists("ocaml_revised")
50  syn match    ocamlDoErr      "\<do\>"
51endif
52
53syn match    ocamlDoneErr    "\<done\>"
54syn match    ocamlThenErr    "\<then\>"
55
56" Error-highlighting of "end" without synchronization:
57" as keyword or as error (default)
58if exists("ocaml_noend_error")
59  syn match    ocamlKeyword    "\<end\>"
60else
61  syn match    ocamlEndErr     "\<end\>"
62endif
63
64" Some convenient clusters
65syn cluster  ocamlAllErrs contains=ocamlBraceErr,ocamlBrackErr,ocamlParenErr,ocamlCommentErr,ocamlCountErr,ocamlDoErr,ocamlDoneErr,ocamlEndErr,ocamlThenErr
66
67syn cluster  ocamlAENoParen contains=ocamlBraceErr,ocamlBrackErr,ocamlCommentErr,ocamlCountErr,ocamlDoErr,ocamlDoneErr,ocamlEndErr,ocamlThenErr
68
69syn cluster  ocamlContained contains=ocamlTodo,ocamlPreDef,ocamlModParam,ocamlModParam1,ocamlPreMPRestr,ocamlMPRestr,ocamlMPRestr1,ocamlMPRestr2,ocamlMPRestr3,ocamlModRHS,ocamlFuncWith,ocamlFuncStruct,ocamlModTypeRestr,ocamlModTRWith,ocamlWith,ocamlWithRest,ocamlModType,ocamlFullMod,ocamlVal
70
71
72" Enclosing delimiters
73syn region   ocamlEncl transparent matchgroup=ocamlKeyword start="(" matchgroup=ocamlKeyword end=")" contains=ALLBUT,@ocamlContained,ocamlParenErr
74syn region   ocamlEncl transparent matchgroup=ocamlKeyword start="{" matchgroup=ocamlKeyword end="}"  contains=ALLBUT,@ocamlContained,ocamlBraceErr
75syn region   ocamlEncl transparent matchgroup=ocamlKeyword start="\[" matchgroup=ocamlKeyword end="\]" contains=ALLBUT,@ocamlContained,ocamlBrackErr
76syn region   ocamlEncl transparent matchgroup=ocamlKeyword start="\[|" matchgroup=ocamlKeyword end="|\]" contains=ALLBUT,@ocamlContained,ocamlArrErr
77
78
79" Comments
80syn region   ocamlComment start="(\*" end="\*)" contains=@Spell,ocamlComment,ocamlTodo
81syn keyword  ocamlTodo contained TODO FIXME XXX NOTE
82
83
84" Objects
85syn region   ocamlEnd matchgroup=ocamlObject start="\<object\>" matchgroup=ocamlObject end="\<end\>" contains=ALLBUT,@ocamlContained,ocamlEndErr
86
87
88" Blocks
89if !exists("ocaml_revised")
90  syn region   ocamlEnd matchgroup=ocamlKeyword start="\<begin\>" matchgroup=ocamlKeyword end="\<end\>" contains=ALLBUT,@ocamlContained,ocamlEndErr
91endif
92
93
94" "for"
95syn region   ocamlNone matchgroup=ocamlKeyword start="\<for\>" matchgroup=ocamlKeyword end="\<\(to\|downto\)\>" contains=ALLBUT,@ocamlContained,ocamlCountErr
96
97
98" "do"
99if !exists("ocaml_revised")
100  syn region   ocamlDo matchgroup=ocamlKeyword start="\<do\>" matchgroup=ocamlKeyword end="\<done\>" contains=ALLBUT,@ocamlContained,ocamlDoneErr
101endif
102
103" "if"
104syn region   ocamlNone matchgroup=ocamlKeyword start="\<if\>" matchgroup=ocamlKeyword end="\<then\>" contains=ALLBUT,@ocamlContained,ocamlThenErr
105
106
107"" Modules
108
109" "sig"
110syn region   ocamlSig matchgroup=ocamlModule start="\<sig\>" matchgroup=ocamlModule end="\<end\>" contains=ALLBUT,@ocamlContained,ocamlEndErr,ocamlModule
111syn region   ocamlModSpec matchgroup=ocamlKeyword start="\<module\>" matchgroup=ocamlModule end="\<\u\(\w\|'\)*\>" contained contains=@ocamlAllErrs,ocamlComment skipwhite skipempty nextgroup=ocamlModTRWith,ocamlMPRestr
112
113" "open"
114syn region   ocamlNone matchgroup=ocamlKeyword start="\<open\>" matchgroup=ocamlModule end="\<\u\(\w\|'\)*\( *\. *\u\(\w\|'\)*\)*\>" contains=@ocamlAllErrs,ocamlComment
115
116" "include"
117syn match    ocamlKeyword "\<include\>" skipwhite skipempty nextgroup=ocamlModParam,ocamlFullMod
118
119" "module" - somewhat complicated stuff ;-)
120syn region   ocamlModule matchgroup=ocamlKeyword start="\<module\>" matchgroup=ocamlModule end="\<\u\(\w\|'\)*\>" contains=@ocamlAllErrs,ocamlComment skipwhite skipempty nextgroup=ocamlPreDef
121syn region   ocamlPreDef start="."me=e-1 matchgroup=ocamlKeyword end="\l\|=\|)"me=e-1 contained contains=@ocamlAllErrs,ocamlComment,ocamlModParam,ocamlModTypeRestr,ocamlModTRWith nextgroup=ocamlModPreRHS
122syn region   ocamlModParam start="([^*]" end=")" contained contains=@ocamlAENoParen,ocamlModParam1,ocamlVal
123syn match    ocamlModParam1 "\<\u\(\w\|'\)*\>" contained skipwhite skipempty nextgroup=ocamlPreMPRestr
124
125syn region   ocamlPreMPRestr start="."me=e-1 end=")"me=e-1 contained contains=@ocamlAllErrs,ocamlComment,ocamlMPRestr,ocamlModTypeRestr
126
127syn region   ocamlMPRestr start=":" end="."me=e-1 contained contains=@ocamlComment skipwhite skipempty nextgroup=ocamlMPRestr1,ocamlMPRestr2,ocamlMPRestr3
128syn region   ocamlMPRestr1 matchgroup=ocamlModule start="\ssig\s\=" matchgroup=ocamlModule end="\<end\>" contained contains=ALLBUT,@ocamlContained,ocamlEndErr,ocamlModule
129syn region   ocamlMPRestr2 start="\sfunctor\(\s\|(\)\="me=e-1 matchgroup=ocamlKeyword end="->" contained contains=@ocamlAllErrs,ocamlComment,ocamlModParam skipwhite skipempty nextgroup=ocamlFuncWith,ocamlMPRestr2
130syn match    ocamlMPRestr3 "\w\(\w\|'\)*\( *\. *\w\(\w\|'\)*\)*" contained
131syn match    ocamlModPreRHS "=" contained skipwhite skipempty nextgroup=ocamlModParam,ocamlFullMod
132syn keyword  ocamlKeyword val
133syn region   ocamlVal matchgroup=ocamlKeyword start="\<val\>" matchgroup=ocamlLCIdentifier end="\<\l\(\w\|'\)*\>" contains=@ocamlAllErrs,ocamlComment,ocamlFullMod skipwhite skipempty nextgroup=ocamlMPRestr
134syn region   ocamlModRHS start="." end=". *\w\|([^*]"me=e-2 contained contains=ocamlComment skipwhite skipempty nextgroup=ocamlModParam,ocamlFullMod
135syn match    ocamlFullMod "\<\u\(\w\|'\)*\( *\. *\u\(\w\|'\)*\)*" contained skipwhite skipempty nextgroup=ocamlFuncWith
136
137syn region   ocamlFuncWith start="([^*]"me=e-1 end=")" contained contains=ocamlComment,ocamlWith,ocamlFuncStruct skipwhite skipempty nextgroup=ocamlFuncWith
138syn region   ocamlFuncStruct matchgroup=ocamlModule start="[^a-zA-Z]struct\>"hs=s+1 matchgroup=ocamlModule end="\<end\>" contains=ALLBUT,@ocamlContained,ocamlEndErr
139
140syn match    ocamlModTypeRestr "\<\w\(\w\|'\)*\( *\. *\w\(\w\|'\)*\)*\>" contained
141syn region   ocamlModTRWith start=":\s*("hs=s+1 end=")" contained contains=@ocamlAENoParen,ocamlWith
142syn match    ocamlWith "\<\(\u\(\w\|'\)* *\. *\)*\w\(\w\|'\)*\>" contained skipwhite skipempty nextgroup=ocamlWithRest
143syn region   ocamlWithRest start="[^)]" end=")"me=e-1 contained contains=ALLBUT,@ocamlContained
144
145" "struct"
146syn region   ocamlStruct matchgroup=ocamlModule start="\<\(module\s\+\)\=struct\>" matchgroup=ocamlModule end="\<end\>" contains=ALLBUT,@ocamlContained,ocamlEndErr
147
148" "module type"
149syn region   ocamlKeyword start="\<module\>\s*\<type\>\(\s*\<of\>\)\=" matchgroup=ocamlModule end="\<\w\(\w\|'\)*\>" contains=ocamlComment skipwhite skipempty nextgroup=ocamlMTDef
150syn match    ocamlMTDef "=\s*\w\(\w\|'\)*\>"hs=s+1,me=s+1 skipwhite skipempty nextgroup=ocamlFullMod
151
152syn keyword  ocamlKeyword  and as assert class
153syn keyword  ocamlKeyword  constraint else
154syn keyword  ocamlKeyword  exception external fun
155
156syn keyword  ocamlKeyword  in inherit initializer
157syn keyword  ocamlKeyword  land lazy let match
158syn keyword  ocamlKeyword  method mutable new of
159syn keyword  ocamlKeyword  parser private raise rec
160syn keyword  ocamlKeyword  try type
161syn keyword  ocamlKeyword  virtual when while with
162
163if exists("ocaml_revised")
164  syn keyword  ocamlKeyword  do value
165  syn keyword  ocamlBoolean  True False
166else
167  syn keyword  ocamlKeyword  function
168  syn keyword  ocamlBoolean  true false
169  syn match    ocamlKeyChar  "!"
170endif
171
172syn keyword  ocamlType     array bool char exn float format format4
173syn keyword  ocamlType     int int32 int64 lazy_t list nativeint option
174syn keyword  ocamlType     string unit
175
176syn keyword  ocamlOperator asr lnot lor lsl lsr lxor mod not
177
178syn match    ocamlConstructor  "(\s*)"
179syn match    ocamlConstructor  "\[\s*\]"
180syn match    ocamlConstructor  "\[|\s*>|]"
181syn match    ocamlConstructor  "\[<\s*>\]"
182syn match    ocamlConstructor  "\u\(\w\|'\)*\>"
183
184" Polymorphic variants
185syn match    ocamlConstructor  "`\w\(\w\|'\)*\>"
186
187" Module prefix
188syn match    ocamlModPath      "\u\(\w\|'\)* *\."he=e-1
189
190syn match    ocamlCharacter    "'\\\d\d\d'\|'\\[\'ntbr]'\|'.'"
191syn match    ocamlCharacter    "'\\x\x\x'"
192syn match    ocamlCharErr      "'\\\d\d'\|'\\\d'"
193syn match    ocamlCharErr      "'\\[^\'ntbr]'"
194syn region   ocamlString       start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=@Spell
195
196syn match    ocamlFunDef       "->"
197syn match    ocamlRefAssign    ":="
198syn match    ocamlTopStop      ";;"
199syn match    ocamlOperator     "\^"
200syn match    ocamlOperator     "::"
201
202syn match    ocamlOperator     "&&"
203syn match    ocamlOperator     "<"
204syn match    ocamlOperator     ">"
205syn match    ocamlAnyVar       "\<_\>"
206syn match    ocamlKeyChar      "|[^\]]"me=e-1
207syn match    ocamlKeyChar      ";"
208syn match    ocamlKeyChar      "\~"
209syn match    ocamlKeyChar      "?"
210syn match    ocamlKeyChar      "\*"
211syn match    ocamlKeyChar      "="
212
213if exists("ocaml_revised")
214  syn match    ocamlErr        "<-"
215else
216  syn match    ocamlOperator   "<-"
217endif
218
219syn match    ocamlNumber        "\<-\=\d\(_\|\d\)*[l|L|n]\?\>"
220syn match    ocamlNumber        "\<-\=0[x|X]\(\x\|_\)\+[l|L|n]\?\>"
221syn match    ocamlNumber        "\<-\=0[o|O]\(\o\|_\)\+[l|L|n]\?\>"
222syn match    ocamlNumber        "\<-\=0[b|B]\([01]\|_\)\+[l|L|n]\?\>"
223syn match    ocamlFloat         "\<-\=\d\(_\|\d\)*\.\?\(_\|\d\)*\([eE][-+]\=\d\(_\|\d\)*\)\=\>"
224
225" Labels
226syn match    ocamlLabel        "\~\(\l\|_\)\(\w\|'\)*"lc=1
227syn match    ocamlLabel        "?\(\l\|_\)\(\w\|'\)*"lc=1
228syn region   ocamlLabel transparent matchgroup=ocamlLabel start="?(\(\l\|_\)\(\w\|'\)*"lc=2 end=")"me=e-1 contains=ALLBUT,@ocamlContained,ocamlParenErr
229
230
231" Synchronization
232syn sync minlines=50
233syn sync maxlines=500
234
235if !exists("ocaml_revised")
236  syn sync match ocamlDoSync      grouphere  ocamlDo      "\<do\>"
237  syn sync match ocamlDoSync      groupthere ocamlDo      "\<done\>"
238endif
239
240if exists("ocaml_revised")
241  syn sync match ocamlEndSync     grouphere  ocamlEnd     "\<\(object\)\>"
242else
243  syn sync match ocamlEndSync     grouphere  ocamlEnd     "\<\(begin\|object\)\>"
244endif
245
246syn sync match ocamlEndSync     groupthere ocamlEnd     "\<end\>"
247syn sync match ocamlStructSync  grouphere  ocamlStruct  "\<struct\>"
248syn sync match ocamlStructSync  groupthere ocamlStruct  "\<end\>"
249syn sync match ocamlSigSync     grouphere  ocamlSig     "\<sig\>"
250syn sync match ocamlSigSync     groupthere ocamlSig     "\<end\>"
251
252" Define the default highlighting.
253" Only when an item doesn't have highlighting yet
254
255hi def link ocamlBraceErr	   Error
256hi def link ocamlBrackErr	   Error
257hi def link ocamlParenErr	   Error
258hi def link ocamlArrErr	   Error
259
260hi def link ocamlCommentErr   Error
261
262hi def link ocamlCountErr	   Error
263hi def link ocamlDoErr	   Error
264hi def link ocamlDoneErr	   Error
265hi def link ocamlEndErr	   Error
266hi def link ocamlThenErr	   Error
267
268hi def link ocamlCharErr	   Error
269
270hi def link ocamlErr	   Error
271
272hi def link ocamlComment	   Comment
273
274hi def link ocamlModPath	   Include
275hi def link ocamlObject	   Include
276hi def link ocamlModule	   Include
277hi def link ocamlModParam1    Include
278hi def link ocamlModType	   Include
279hi def link ocamlMPRestr3	   Include
280hi def link ocamlFullMod	   Include
281hi def link ocamlModTypeRestr Include
282hi def link ocamlWith	   Include
283hi def link ocamlMTDef	   Include
284
285hi def link ocamlScript	   Include
286
287hi def link ocamlConstructor  Constant
288
289hi def link ocamlVal          Keyword
290hi def link ocamlModPreRHS    Keyword
291hi def link ocamlMPRestr2	   Keyword
292hi def link ocamlKeyword	   Keyword
293hi def link ocamlMethod	   Include
294hi def link ocamlFunDef	   Keyword
295hi def link ocamlRefAssign    Keyword
296hi def link ocamlKeyChar	   Keyword
297hi def link ocamlAnyVar	   Keyword
298hi def link ocamlTopStop	   Keyword
299hi def link ocamlOperator	   Keyword
300
301hi def link ocamlBoolean	   Boolean
302hi def link ocamlCharacter    Character
303hi def link ocamlNumber	   Number
304hi def link ocamlFloat	   Float
305hi def link ocamlString	   String
306
307hi def link ocamlLabel	   Identifier
308
309hi def link ocamlType	   Type
310
311hi def link ocamlTodo	   Todo
312
313hi def link ocamlEncl	   Keyword
314
315
316let b:current_syntax = "ocaml"
317
318" vim: ts=8
319