Skip to content

Commit

Permalink
Syntax: Fixes to type declarations
Browse files Browse the repository at this point in the history
Fix tupled type params, += and whitespaces around keywords.
Add examples to the test file.
  • Loading branch information
Julow committed Jun 8, 2023
1 parent 6dce805 commit 0f91255
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
11 changes: 6 additions & 5 deletions syntax/ocaml.vim
Original file line number Diff line number Diff line change
Expand Up @@ -475,13 +475,14 @@ hi link ocamlTypeSumAnnot ocamlTypeCatchAll
syn region ocamlTypeDefImpl
\ matchgroup=ocamlKeyword start="\<of\>"
\ matchgroup=ocamlKeyChar start=":="
\ matchgroup=ocamlKeyChar start="+="
\ matchgroup=ocamlKeyChar start=":"
\ matchgroup=ocamlKeyChar start="="
\ matchgroup=NONE end="\(\<type\>\|\<exception\>\|\<val\>\|\<module\>\|\<class\>\|\<method\>\|\<constraint\>\|\<inherit\>\|\<object\>\|\<struct\>\|\<open\>\|\<include\>\|\<let\>\|\<external\>\|\<in\>\|\<end\>\|)\|]\|}\|;\|;;\)\@="
\ matchgroup=NONE end="\(\<type\>\|\<exception\>\|\<val\>\|\<module\>\|\<class\>\|\<method\>\|\<constraint\>\|\<inherit\>\|\<object\>\|\<struct\>\|\<open\>\|\<include\>\|\<let\>\|\<external\>\|\<in\>\|\<end\>\|)\|]\|}\|;\|;;\|=\)\@="
\ matchgroup=NONE end="\(\<and\>\)\@="
\ contained skipwhite skipempty
\ contains=@ocamlTypeExpr,ocamlTypePrivate,ocamlTypeDefDots,ocamlTypeRecordDecl,ocamlTypeSumDecl,ocamlComment,ocamlPpx
\ nextgroup=ocamlTypeDefAnd
\ nextgroup=ocamlTypeDefImpl,ocamlTypeDefAnd
hi link ocamlTypeDefImpl ocamlTypeCatchAll

" Type context opened by “type” (type definition) and “constraint” (type
Expand All @@ -491,7 +492,7 @@ hi link ocamlTypeDefImpl ocamlTypeCatchAll
syn region ocamlTypeDef
\ matchgroup=ocamlKeyword start="\<type\>\(\_s\+\<nonrec\>\)\?\|\<constraint\>"
\ matchgroup=ocamlTypeIdentifier end="\<\l\(\w\|'\)*\>"
\ contains=@ocamlAllErrs,ocamlComment,ocamlTypeVariance,ocamlTypeVar,ocamlWhite
\ contains=@ocamlTypeExpr,@ocamlAllErrs,ocamlComment,ocamlTypeVariance,ocamlTypeVar,ocamlPpx,ocamlWhite
\ skipwhite skipempty
\ nextgroup=ocamlTypeDefImpl,ocamlTypeDefAnd

Expand All @@ -502,15 +503,15 @@ syn region ocamlTypeDef
syn region ocamlTypeDefAnd
\ matchgroup=ocamlKeyword start="\<and\>"
\ matchgroup=ocamlTypeIdentifier end="\<\l\(\w\|'\)*\>"
\ contains=@ocamlAllErrs,ocamlComment,ocamlTypeVariance,ocamlTypeVar,ocamlWhite
\ contains=@ocamlTypeExpr,@ocamlAllErrs,ocamlComment,ocamlTypeVariance,ocamlTypeVar,ocamlPpx,ocamlWhite
\ skipwhite skipempty
\ nextgroup=ocamlTypeDefImpl,ocamlTypeDefAnd

" Exception definitions. Like ocamlTypeDef, jump into ocamlTypeDefImpl.
syn region ocamlExceptionDef
\ matchgroup=ocamlKeyword start="\<exception\>"
\ matchgroup=ocamlConstructor end="\u\(\w\|'\)*\>"
\ contains=@ocamlAllErrs,ocamlComment,ocamlTypeVariance,ocamlTypeVar
\ contains=@ocamlAllErrs,ocamlComment,ocamlTypeVariance,ocamlTypeVar,ocamlPpx
\ skipwhite skipempty
\ nextgroup=ocamlTypeDefImpl

Expand Down
23 changes: 23 additions & 0 deletions type-linter-test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
type u = char
type v = string
type w = bytes
type abstract
type !+_ abstract'

(* type expressions with arrows, tuples, 0-ary type constructors *)
type t = t0 * t0 -> t0
Expand Down Expand Up @@ -166,6 +168,24 @@
(* definition of an empty type *)
type t = |

(* Constraints *)
type 'a foo := 'a bar

(* RECURSION *)

type foo = bar
and bar
and baz = foo

;;
let foo = 1
and bar = 2 in
()

(* FIXME: 'and' part not matched by module decl (maybe matched by types decl ?). *)
module rec Foo : sig end = struct end
and Bar : sig end = struct end

(* TYPE ANNOTATIONS *)

(* annotations on let binders *)
Expand Down Expand Up @@ -318,6 +338,9 @@
end
end

(* FIXME: ':=' not recognized and RHS highlighted as constructor. *)
module Foo := Bar

(* ATTRIBUTES AND COMMENTS *)

exception[@my.attr "payld"] (*c*) E [@my.attr "payld"] (*c*)
Expand Down

0 comments on commit 0f91255

Please sign in to comment.