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