ocaml.vim 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. " Vim syntax file
  2. " Language: OCaml
  3. " Filenames: *.ml *.mli *.mll *.mly
  4. " Maintainers: Markus Mottl <markus.mottl@gmail.com>
  5. " Karl-Heinz Sylla <Karl-Heinz.Sylla@gmd.de>
  6. " Issac Trotts <ijtrotts@ucdavis.edu>
  7. " URL: https://github.com/ocaml/vim-ocaml
  8. " Last Change:
  9. " 2018 Nov 08 - Improved highlighting of operators (Maëlan)
  10. " 2018 Apr 22 - Improved support for PPX (Andrey Popp)
  11. " 2018 Mar 16 - Remove raise, lnot and not from keywords (Étienne Millon, "copy")
  12. " 2017 Apr 11 - Improved matching of negative numbers (MM)
  13. " 2016 Mar 11 - Improved support for quoted strings (Glen Mével)
  14. " 2015 Aug 13 - Allow apostrophes in identifiers (Jonathan Chan, Einar Lielmanis)
  15. " 2015 Jun 17 - Added new "nonrec" keyword (MM)
  16. " A minor patch was applied to the official version so that object/end
  17. " can be distinguished from begin/end, which is used for indentation,
  18. " and folding. (David Baelde)
  19. " Quit when a syntax file was already loaded
  20. if exists("b:current_syntax") && b:current_syntax == "ocaml"
  21. finish
  22. endif
  23. let s:keepcpo = &cpo
  24. set cpo&vim
  25. " ' can be used in OCaml identifiers
  26. setlocal iskeyword+='
  27. " ` is part of the name of polymorphic variants
  28. setlocal iskeyword+=`
  29. " OCaml is case sensitive.
  30. syn case match
  31. " Access to the method of an object
  32. syn match ocamlMethod "#"
  33. " Script headers highlighted like comments
  34. syn match ocamlComment "^#!.*" contains=@Spell
  35. " Scripting directives
  36. syn match ocamlScript "^#\<\(quit\|labels\|warnings\|warn_error\|directory\|remove_directory\|cd\|load\|load_rec\|use\|mod_use\|install_printer\|remove_printer\|require\|list\|ppx\|principal\|predicates\|rectypes\|thread\|trace\|untrace\|untrace_all\|print_depth\|print_length\|camlp4o\|camlp4r\|topfind_log\|topfind_verbose\)\>"
  37. " lowercase identifier - the standard way to match
  38. syn match ocamlLCIdentifier /\<\(\l\|_\)\(\w\|'\)*\>/
  39. syn match ocamlKeyChar "|"
  40. " Errors
  41. syn match ocamlBraceErr "}"
  42. syn match ocamlBrackErr "\]"
  43. syn match ocamlParenErr ")"
  44. syn match ocamlArrErr "|]"
  45. syn match ocamlCommentErr "\*)"
  46. syn match ocamlCountErr "\<downto\>"
  47. syn match ocamlCountErr "\<to\>"
  48. if !exists("ocaml_revised")
  49. syn match ocamlDoErr "\<do\>"
  50. endif
  51. syn match ocamlDoneErr "\<done\>"
  52. syn match ocamlThenErr "\<then\>"
  53. " Error-highlighting of "end" without synchronization:
  54. " as keyword or as error (default)
  55. if exists("ocaml_noend_error")
  56. syn match ocamlKeyword "\<end\>"
  57. else
  58. syn match ocamlEndErr "\<end\>"
  59. endif
  60. " Some convenient clusters
  61. syn cluster ocamlAllErrs contains=ocamlBraceErr,ocamlBrackErr,ocamlParenErr,ocamlCommentErr,ocamlCountErr,ocamlDoErr,ocamlDoneErr,ocamlEndErr,ocamlThenErr
  62. syn cluster ocamlAENoParen contains=ocamlBraceErr,ocamlBrackErr,ocamlCommentErr,ocamlCountErr,ocamlDoErr,ocamlDoneErr,ocamlEndErr,ocamlThenErr
  63. syn cluster ocamlContained contains=ocamlTodo,ocamlPreDef,ocamlModParam,ocamlModParam1,ocamlMPRestr,ocamlMPRestr1,ocamlMPRestr2,ocamlMPRestr3,ocamlModRHS,ocamlFuncWith,ocamlFuncStruct,ocamlModTypeRestr,ocamlModTRWith,ocamlWith,ocamlWithRest,ocamlModType,ocamlFullMod,ocamlVal
  64. " Enclosing delimiters
  65. syn region ocamlEncl transparent matchgroup=ocamlKeyword start="(" matchgroup=ocamlKeyword end=")" contains=ALLBUT,@ocamlContained,ocamlParenErr
  66. syn region ocamlEncl transparent matchgroup=ocamlKeyword start="{" matchgroup=ocamlKeyword end="}" contains=ALLBUT,@ocamlContained,ocamlBraceErr
  67. syn region ocamlEncl transparent matchgroup=ocamlKeyword start="\[" matchgroup=ocamlKeyword end="\]" contains=ALLBUT,@ocamlContained,ocamlBrackErr
  68. syn region ocamlEncl transparent matchgroup=ocamlKeyword start="\[|" matchgroup=ocamlKeyword end="|\]" contains=ALLBUT,@ocamlContained,ocamlArrErr
  69. " Comments
  70. syn region ocamlComment start="(\*" end="\*)" contains=@Spell,ocamlComment,ocamlTodo
  71. syn keyword ocamlTodo contained TODO FIXME XXX NOTE
  72. " Objects
  73. syn region ocamlEnd matchgroup=ocamlObject start="\<object\>" matchgroup=ocamlObject end="\<end\>" contains=ALLBUT,@ocamlContained,ocamlEndErr
  74. " Blocks
  75. if !exists("ocaml_revised")
  76. syn region ocamlEnd matchgroup=ocamlKeyword start="\<begin\>" matchgroup=ocamlKeyword end="\<end\>" contains=ALLBUT,@ocamlContained,ocamlEndErr
  77. endif
  78. " "for"
  79. syn region ocamlNone matchgroup=ocamlKeyword start="\<for\>" matchgroup=ocamlKeyword end="\<\(to\|downto\)\>" contains=ALLBUT,@ocamlContained,ocamlCountErr
  80. " "do"
  81. if !exists("ocaml_revised")
  82. syn region ocamlDo matchgroup=ocamlKeyword start="\<do\>" matchgroup=ocamlKeyword end="\<done\>" contains=ALLBUT,@ocamlContained,ocamlDoneErr
  83. endif
  84. " "if"
  85. syn region ocamlNone matchgroup=ocamlKeyword start="\<if\>" matchgroup=ocamlKeyword end="\<then\>" contains=ALLBUT,@ocamlContained,ocamlThenErr
  86. "" PPX nodes
  87. syn match ocamlPpxIdentifier /\(\[@\{1,3\}\)\@<=\w\+\(\.\w\+\)*/
  88. syn region ocamlPpx matchgroup=ocamlPpxEncl start="\[@\{1,3\}" contains=TOP end="\]"
  89. "" Modules
  90. " "sig"
  91. syn region ocamlSig matchgroup=ocamlSigEncl start="\<sig\>" matchgroup=ocamlSigEncl end="\<end\>" contains=ALLBUT,@ocamlContained,ocamlEndErr,ocamlModule
  92. syn region ocamlModSpec matchgroup=ocamlKeyword start="\<module\>" matchgroup=ocamlModule end="\<\u\(\w\|'\)*\>" contained contains=@ocamlAllErrs,ocamlComment skipwhite skipempty nextgroup=ocamlModTRWith,ocamlMPRestr
  93. " "open"
  94. syn match ocamlKeyword "\<open\>" skipwhite skipempty nextgroup=ocamlFullMod
  95. " "include"
  96. syn match ocamlKeyword "\<include\>" skipwhite skipempty nextgroup=ocamlModParam,ocamlFullMod
  97. " "module" - somewhat complicated stuff ;-)
  98. syn region ocamlModule matchgroup=ocamlKeyword start="\<module\>" matchgroup=ocamlModule end="\<\u\(\w\|'\)*\>" contains=@ocamlAllErrs,ocamlComment skipwhite skipempty nextgroup=ocamlPreDef
  99. syn region ocamlPreDef start="."me=e-1 matchgroup=ocamlKeyword end="\l\|=\|)"me=e-1 contained contains=@ocamlAllErrs,ocamlComment,ocamlModParam,ocamlGenMod,ocamlModTypeRestr,ocamlModTRWith nextgroup=ocamlModPreRHS
  100. syn region ocamlModParam start="([^*]" end=")" contained contains=ocamlGenMod,ocamlModParam1,ocamlSig,ocamlVal
  101. syn match ocamlModParam1 "\<\u\(\w\|'\)*\>" contained skipwhite skipempty
  102. syn match ocamlGenMod "()" contained skipwhite skipempty
  103. syn region ocamlMPRestr start=":" end="."me=e-1 contained contains=@ocamlComment skipwhite skipempty nextgroup=ocamlMPRestr1,ocamlMPRestr2,ocamlMPRestr3
  104. syn region ocamlMPRestr1 matchgroup=ocamlSigEncl start="\ssig\s\=" matchgroup=ocamlSigEncl end="\<end\>" contained contains=ALLBUT,@ocamlContained,ocamlEndErr,ocamlModule
  105. syn region ocamlMPRestr2 start="\sfunctor\(\s\|(\)\="me=e-1 matchgroup=ocamlKeyword end="->" contained contains=@ocamlAllErrs,ocamlComment,ocamlModParam,ocamlGenMod skipwhite skipempty nextgroup=ocamlFuncWith,ocamlMPRestr2
  106. syn match ocamlMPRestr3 "\w\(\w\|'\)*\( *\. *\w\(\w\|'\)*\)*" contained
  107. syn match ocamlModPreRHS "=" contained skipwhite skipempty nextgroup=ocamlModParam,ocamlFullMod
  108. syn keyword ocamlKeyword val
  109. syn region ocamlVal matchgroup=ocamlKeyword start="\<val\>" matchgroup=ocamlLCIdentifier end="\<\l\(\w\|'\)*\>" contains=@ocamlAllErrs,ocamlComment,ocamlFullMod skipwhite skipempty nextgroup=ocamlMPRestr
  110. syn region ocamlModRHS start="." end=". *\w\|([^*]"me=e-2 contained contains=ocamlComment skipwhite skipempty nextgroup=ocamlModParam,ocamlFullMod
  111. syn match ocamlFullMod "\<\u\(\w\|'\)*\( *\. *\u\(\w\|'\)*\)*" contained skipwhite skipempty nextgroup=ocamlFuncWith
  112. syn region ocamlFuncWith start="([^*)]"me=e-1 end=")" contained contains=ocamlComment,ocamlWith,ocamlFuncStruct skipwhite skipempty nextgroup=ocamlFuncWith
  113. syn region ocamlFuncStruct matchgroup=ocamlStructEncl start="[^a-zA-Z]struct\>"hs=s+1 matchgroup=ocamlStructEncl end="\<end\>" contains=ALLBUT,@ocamlContained,ocamlEndErr
  114. syn match ocamlModTypeRestr "\<\w\(\w\|'\)*\( *\. *\w\(\w\|'\)*\)*\>" contained
  115. syn region ocamlModTRWith start=":\s*("hs=s+1 end=")" contained contains=@ocamlAENoParen,ocamlWith
  116. syn match ocamlWith "\<\(\u\(\w\|'\)* *\. *\)*\w\(\w\|'\)*\>" contained skipwhite skipempty nextgroup=ocamlWithRest
  117. syn region ocamlWithRest start="[^)]" end=")"me=e-1 contained contains=ALLBUT,@ocamlContained
  118. " "struct"
  119. syn region ocamlStruct matchgroup=ocamlStructEncl start="\<\(module\s\+\)\=struct\>" matchgroup=ocamlStructEncl end="\<end\>" contains=ALLBUT,@ocamlContained,ocamlEndErr
  120. " "module type"
  121. syn region ocamlKeyword start="\<module\>\s*\<type\>\(\s*\<of\>\)\=" matchgroup=ocamlModule end="\<\w\(\w\|'\)*\>" contains=ocamlComment skipwhite skipempty nextgroup=ocamlMTDef
  122. syn match ocamlMTDef "=\s*\w\(\w\|'\)*\>"hs=s+1,me=s+1 skipwhite skipempty nextgroup=ocamlFullMod
  123. " Quoted strings
  124. syn region ocamlString matchgroup=ocamlQuotedStringDelim start="{\z\([a-z_]*\)|" end="|\z1}" contains=@Spell
  125. syn keyword ocamlKeyword and as assert class
  126. syn keyword ocamlKeyword constraint else
  127. syn keyword ocamlKeyword exception external fun
  128. syn keyword ocamlKeyword in inherit initializer
  129. syn keyword ocamlKeyword lazy let match
  130. syn keyword ocamlKeyword method mutable new nonrec of
  131. syn keyword ocamlKeyword parser private rec
  132. syn keyword ocamlKeyword try type
  133. syn keyword ocamlKeyword virtual when while with
  134. if exists("ocaml_revised")
  135. syn keyword ocamlKeyword do value
  136. syn keyword ocamlBoolean True False
  137. else
  138. syn keyword ocamlKeyword function
  139. syn keyword ocamlBoolean true false
  140. endif
  141. syn keyword ocamlType array bool char exn float format format4
  142. syn keyword ocamlType int int32 int64 lazy_t list nativeint option
  143. syn keyword ocamlType bytes string unit
  144. syn match ocamlConstructor "(\s*)"
  145. syn match ocamlConstructor "\[\s*\]"
  146. syn match ocamlConstructor "\[|\s*>|]"
  147. syn match ocamlConstructor "\[<\s*>\]"
  148. syn match ocamlConstructor "\u\(\w\|'\)*\>"
  149. " Polymorphic variants
  150. syn match ocamlConstructor "`\w\(\w\|'\)*\>"
  151. " Module prefix
  152. syn match ocamlModPath "\u\(\w\|'\)* *\."he=e-1
  153. syn match ocamlCharacter "'\\\d\d\d'\|'\\[\'ntbr]'\|'.'"
  154. syn match ocamlCharacter "'\\x\x\x'"
  155. syn match ocamlCharErr "'\\\d\d'\|'\\\d'"
  156. syn match ocamlCharErr "'\\[^\'ntbr]'"
  157. syn region ocamlString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=@Spell
  158. syn match ocamlTopStop ";;"
  159. syn match ocamlAnyVar "\<_\>"
  160. syn match ocamlKeyChar "|[^\]]"me=e-1
  161. syn match ocamlKeyChar ";"
  162. syn match ocamlKeyChar "\~"
  163. syn match ocamlKeyChar "?"
  164. "" Operators
  165. " The grammar of operators is found there:
  166. " https://caml.inria.fr/pub/docs/manual-ocaml/names.html#operator-name
  167. " https://caml.inria.fr/pub/docs/manual-ocaml/extn.html#s:ext-ops
  168. " https://caml.inria.fr/pub/docs/manual-ocaml/extn.html#s:index-operators
  169. " =, *, < and > are both operator names and keywords, we let the user choose how
  170. " to display them (has to be declared before regular infix operators):
  171. syn match ocamlEqual "="
  172. syn match ocamlStar "*"
  173. syn match ocamlAngle "<"
  174. syn match ocamlAngle ">"
  175. " Custom indexing operators:
  176. syn region ocamlIndexing matchgroup=ocamlIndexingOp
  177. \ start="\.[~?!:|&$%=>@^/*+-][~?!.:|&$%<=>@^*/+-]*\_s*("
  178. \ end=")\(\_s*<-\)\?"
  179. \ contains=ALLBUT,@ocamlContained,ocamlParenErr
  180. syn region ocamlIndexing matchgroup=ocamlIndexingOp
  181. \ start="\.[~?!:|&$%=>@^/*+-][~?!.:|&$%<=>@^*/+-]*\_s*\["
  182. \ end="]\(\_s*<-\)\?"
  183. \ contains=ALLBUT,@ocamlContained,ocamlBrackErr
  184. syn region ocamlIndexing matchgroup=ocamlIndexingOp
  185. \ start="\.[~?!:|&$%=>@^/*+-][~?!.:|&$%<=>@^*/+-]*\_s*{"
  186. \ end="}\(\_s*<-\)\?"
  187. \ contains=ALLBUT,@ocamlContained,ocamlBraceErr
  188. " Extension operators (has to be declared before regular infix operators):
  189. syn match ocamlExtensionOp "#[#~?!.:|&$%<=>@^*/+-]\+"
  190. " Infix and prefix operators:
  191. syn match ocamlPrefixOp "![~?!.:|&$%<=>@^*/+-]*"
  192. syn match ocamlPrefixOp "[~?][~?!.:|&$%<=>@^*/+-]\+"
  193. syn match ocamlInfixOp "[&$%@^/+-][~?!.:|&$%<=>@^*/+-]*"
  194. syn match ocamlInfixOp "[|<=>*][~?!.:|&$%<=>@^*/+-]\+"
  195. syn match ocamlInfixOp "#[~?!.:|&$%<=>@^*/+-]\+#\@!"
  196. syn match ocamlInfixOp "!=[~?!.:|&$%<=>@^*/+-]\@!"
  197. syn keyword ocamlInfixOpKeyword asr land lor lsl lsr lxor mod or
  198. " := is technically an infix operator, but we may want to show it as a keyword
  199. " (somewhat analogously to = for let‐bindings and <- for assignations):
  200. syn match ocamlRefAssign ":="
  201. " :: is technically not an operator, but we may want to show it as such:
  202. syn match ocamlCons "::"
  203. " -> and <- are keywords, not operators (but can appear in longer operators):
  204. syn match ocamlArrow "->[~?!.:|&$%<=>@^*/+-]\@!"
  205. if exists("ocaml_revised")
  206. syn match ocamlErr "<-[~?!.:|&$%<=>@^*/+-]\@!"
  207. else
  208. syn match ocamlKeyChar "<-[~?!.:|&$%<=>@^*/+-]\@!"
  209. endif
  210. syn match ocamlNumber "-\=\<\d\(_\|\d\)*[l|L|n]\?\>"
  211. syn match ocamlNumber "-\=\<0[x|X]\(\x\|_\)\+[l|L|n]\?\>"
  212. syn match ocamlNumber "-\=\<0[o|O]\(\o\|_\)\+[l|L|n]\?\>"
  213. syn match ocamlNumber "-\=\<0[b|B]\([01]\|_\)\+[l|L|n]\?\>"
  214. syn match ocamlFloat "-\=\<\d\(_\|\d\)*\.\?\(_\|\d\)*\([eE][-+]\=\d\(_\|\d\)*\)\=\>"
  215. " Labels
  216. syn match ocamlLabel "\~\(\l\|_\)\(\w\|'\)*"lc=1
  217. syn match ocamlLabel "?\(\l\|_\)\(\w\|'\)*"lc=1
  218. syn region ocamlLabel transparent matchgroup=ocamlLabel start="[~?](\(\l\|_\)\(\w\|'\)*"lc=2 end=")"me=e-1 contains=ALLBUT,@ocamlContained,ocamlParenErr
  219. " Synchronization
  220. syn sync minlines=50
  221. syn sync maxlines=500
  222. if !exists("ocaml_revised")
  223. syn sync match ocamlDoSync grouphere ocamlDo "\<do\>"
  224. syn sync match ocamlDoSync groupthere ocamlDo "\<done\>"
  225. endif
  226. if exists("ocaml_revised")
  227. syn sync match ocamlEndSync grouphere ocamlEnd "\<\(object\)\>"
  228. else
  229. syn sync match ocamlEndSync grouphere ocamlEnd "\<\(begin\|object\)\>"
  230. endif
  231. syn sync match ocamlEndSync groupthere ocamlEnd "\<end\>"
  232. syn sync match ocamlStructSync grouphere ocamlStruct "\<struct\>"
  233. syn sync match ocamlStructSync groupthere ocamlStruct "\<end\>"
  234. syn sync match ocamlSigSync grouphere ocamlSig "\<sig\>"
  235. syn sync match ocamlSigSync groupthere ocamlSig "\<end\>"
  236. " Define the default highlighting.
  237. hi def link ocamlBraceErr Error
  238. hi def link ocamlBrackErr Error
  239. hi def link ocamlParenErr Error
  240. hi def link ocamlArrErr Error
  241. hi def link ocamlCommentErr Error
  242. hi def link ocamlCountErr Error
  243. hi def link ocamlDoErr Error
  244. hi def link ocamlDoneErr Error
  245. hi def link ocamlEndErr Error
  246. hi def link ocamlThenErr Error
  247. hi def link ocamlCharErr Error
  248. hi def link ocamlErr Error
  249. hi def link ocamlComment Comment
  250. hi def link ocamlModPath Include
  251. hi def link ocamlObject Include
  252. hi def link ocamlModule Include
  253. hi def link ocamlModParam1 Include
  254. hi def link ocamlGenMod Include
  255. hi def link ocamlModType Include
  256. hi def link ocamlMPRestr3 Include
  257. hi def link ocamlFullMod Include
  258. hi def link ocamlFuncWith Include
  259. hi def link ocamlModParam Include
  260. hi def link ocamlModTypeRestr Include
  261. hi def link ocamlWith Include
  262. hi def link ocamlMTDef Include
  263. hi def link ocamlSigEncl ocamlModule
  264. hi def link ocamlStructEncl ocamlModule
  265. hi def link ocamlScript Include
  266. hi def link ocamlConstructor Constant
  267. hi def link ocamlVal Keyword
  268. hi def link ocamlModPreRHS Keyword
  269. hi def link ocamlMPRestr2 Keyword
  270. hi def link ocamlKeyword Keyword
  271. hi def link ocamlMethod Include
  272. hi def link ocamlArrow Keyword
  273. hi def link ocamlKeyChar Keyword
  274. hi def link ocamlAnyVar Keyword
  275. hi def link ocamlTopStop Keyword
  276. hi def link ocamlRefAssign ocamlKeyChar
  277. hi def link ocamlEqual ocamlKeyChar
  278. hi def link ocamlStar ocamlInfixOp
  279. hi def link ocamlAngle ocamlInfixOp
  280. hi def link ocamlCons ocamlInfixOp
  281. hi def link ocamlPrefixOp ocamlOperator
  282. hi def link ocamlInfixOp ocamlOperator
  283. hi def link ocamlExtensionOp ocamlOperator
  284. hi def link ocamlIndexingOp ocamlOperator
  285. if exists("ocaml_highlight_operators")
  286. hi def link ocamlInfixOpKeyword ocamlOperator
  287. hi def link ocamlOperator Operator
  288. else
  289. hi def link ocamlInfixOpKeyword Keyword
  290. endif
  291. hi def link ocamlBoolean Boolean
  292. hi def link ocamlCharacter Character
  293. hi def link ocamlNumber Number
  294. hi def link ocamlFloat Float
  295. hi def link ocamlString String
  296. hi def link ocamlQuotedStringDelim Identifier
  297. hi def link ocamlLabel Identifier
  298. hi def link ocamlType Type
  299. hi def link ocamlTodo Todo
  300. hi def link ocamlEncl Keyword
  301. hi def link ocamlPpxEncl ocamlEncl
  302. let b:current_syntax = "ocaml"
  303. let &cpo = s:keepcpo
  304. unlet s:keepcpo
  305. " vim: ts=8