1
0

ocaml.vim 13 KB

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