From 559946e83e001012fd50b416532987e50bfb8f0e Mon Sep 17 00:00:00 2001 From: Thomas Refis Date: Fri, 12 Feb 2016 17:01:20 +0000 Subject: [PATCH 1/4] allow docstring inside arrow types, attach them to parameters --- parsing/parser.mly | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/parsing/parser.mly b/parsing/parser.mly index fe7255f84..1b614cf97 100644 --- a/parsing/parser.mly +++ b/parsing/parser.mly @@ -347,6 +347,10 @@ let extra_csig pos items = extra_text Ctf.text pos items let extra_def pos items = extra_text (fun txt -> [Ptop_def (Str.text txt)]) pos items +let extra_rhs_core_type ct ~pos = + let docs = rhs_info pos in + { ct with ptyp_attributes = add_info_attrs docs ct.ptyp_attributes } + type let_binding = { lb_pattern: pattern; lb_expression: expression; @@ -2157,13 +2161,18 @@ core_type2: simple_core_type_or_tuple { $1 } | QUESTION LIDENT COLON core_type2 MINUSGREATER core_type2 - { mktyp(Ptyp_arrow(Optional $2 , $4, $6)) } + { let param = extra_rhs_core_type $4 ~pos:4 in + mktyp (Ptyp_arrow(Optional $2 , param, $6)) } | OPTLABEL core_type2 MINUSGREATER core_type2 - { mktyp(Ptyp_arrow(Optional $1 , $2, $4)) } + { let param = extra_rhs_core_type $2 ~pos:2 in + mktyp(Ptyp_arrow(Optional $1 , param, $4)) + } | LIDENT COLON core_type2 MINUSGREATER core_type2 - { mktyp(Ptyp_arrow(Labelled $1, $3, $5)) } + { let param = extra_rhs_core_type $3 ~pos:3 in + mktyp(Ptyp_arrow(Labelled $1, param, $5)) } | core_type2 MINUSGREATER core_type2 - { mktyp(Ptyp_arrow(Nolabel, $1, $3)) } + { let param = extra_rhs_core_type $1 ~pos:1 in + mktyp(Ptyp_arrow(Nolabel, param, $3)) } ; simple_core_type: From 78d4aa1ef14a2cae67e4e4a67fcf3220e248c759 Mon Sep 17 00:00:00 2001 From: Thomas Refis Date: Tue, 16 Feb 2016 10:58:19 +0000 Subject: [PATCH 2/4] allow docstring in object and polymorphic variant types. --- parsing/parser.mly | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/parsing/parser.mly b/parsing/parser.mly index 1b614cf97..bde8577e9 100644 --- a/parsing/parser.mly +++ b/parsing/parser.mly @@ -2239,9 +2239,9 @@ row_field: ; tag_field: name_tag OF opt_ampersand amper_type_list attributes - { Rtag ($1, $5, $3, List.rev $4) } + { Rtag ($1, add_info_attrs (symbol_info ()) $5, $3, List.rev $4) } | name_tag attributes - { Rtag ($1, $2, true, []) } + { Rtag ($1, add_info_attrs (symbol_info ()) $2, true, []) } ; opt_ampersand: AMPERSAND { true } @@ -2269,13 +2269,26 @@ core_type_list: | core_type_list STAR simple_core_type { $3 :: $1 } ; meth_list: - field SEMI meth_list { let (f, c) = $3 in ($1 :: f, c) } - | field opt_semi { [$1], Closed } + field_semi meth_list { let (f, c) = $2 in ($1 :: f, c) } + | field_semi { [$1], Closed } + | field { [$1], Closed } | DOTDOT { [], Open } ; field: - label COLON poly_type_no_attr attributes { ($1, $4, $3) } + label COLON poly_type_no_attr attributes + { ($1, add_info_attrs (symbol_info ()) $4, $3) } ; + +field_semi: + label COLON poly_type_no_attr attributes SEMI attributes + { let info = + match rhs_info 4 with + | Some _ as info_before_semi -> info_before_semi + | None -> symbol_info () + in + ($1, add_info_attrs info ($4 @ $6), $3) } +; + label: LIDENT { $1 } ; From 449e2331eb68c70330bd6519e942a26e575ce2bf Mon Sep 17 00:00:00 2001 From: Thomas Refis Date: Mon, 22 Feb 2016 11:33:52 +0000 Subject: [PATCH 3/4] Changes --- Changes | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Changes b/Changes index 17a9422b1..089abeb47 100644 --- a/Changes +++ b/Changes @@ -499,6 +499,9 @@ Bug fixes: (David Allsopp) - GPR#441: better type error location in presence of type constraints (Thomas Refis, report by Arseniy Alekseyev) +- GPR#477: reallow docstrings inside object types, and inside polymorphic + variant and arrow types + (Thomas Refis) Features wishes: - PR#4518, GPR#29: change location format for reporting errors in ocamldoc From 3fee64a34f141cb174927d0f4ce4df08ee0f3f1c Mon Sep 17 00:00:00 2001 From: Thomas Refis Date: Mon, 22 Feb 2016 11:40:40 +0000 Subject: [PATCH 4/4] docstrings: add a test file --- testsuite/tests/parsing/docstrings.ml | 16 ++ .../tests/parsing/docstrings.ml.reference | 146 ++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 testsuite/tests/parsing/docstrings.ml create mode 100644 testsuite/tests/parsing/docstrings.ml.reference diff --git a/testsuite/tests/parsing/docstrings.ml b/testsuite/tests/parsing/docstrings.ml new file mode 100644 index 000000000..ea8471133 --- /dev/null +++ b/testsuite/tests/parsing/docstrings.ml @@ -0,0 +1,16 @@ +type 'a with_default + = ?size:int (** default [42] *) + -> ?resizable:bool (** default [true] *) + -> 'a + +type obj = < + meth1 : int -> int; + (** method 1 *) + + meth2: unit -> float (** method 2 *); +> + +type var = [ + | `Foo (** foo *) + | `Bar of int * string (** bar *) +] diff --git a/testsuite/tests/parsing/docstrings.ml.reference b/testsuite/tests/parsing/docstrings.ml.reference new file mode 100644 index 000000000..da40ede71 --- /dev/null +++ b/testsuite/tests/parsing/docstrings.ml.reference @@ -0,0 +1,146 @@ +[ + structure_item (docstrings.ml[1,0+0]..[4,105+7]) + Pstr_type Rec + [ + type_declaration "with_default" (docstrings.ml[1,0+8]..[1,0+20]) (docstrings.ml[1,0+0]..[4,105+7]) + ptype_params = + [ + core_type (docstrings.ml[1,0+5]..[1,0+7]) + Ptyp_var a + ] + ptype_cstrs = + [] + ptype_kind = + Ptype_abstract + ptype_private = Public + ptype_manifest = + Some + core_type (docstrings.ml[2,21+5]..[4,105+7]) + Ptyp_arrow + Optional "size" + core_type (docstrings.ml[2,21+11]..[2,21+14]) + attribute "ocaml.doc" + [ + structure_item (docstrings.ml[2,21+21]..[2,21+40]) + Pstr_eval + expression (docstrings.ml[2,21+21]..[2,21+40]) + Pexp_constant PConst_string(" default [42] ",None) + ] + Ptyp_constr "int" (docstrings.ml[2,21+11]..[2,21+14]) + [] + core_type (docstrings.ml[3,62+5]..[4,105+7]) + Ptyp_arrow + Optional "resizable" + core_type (docstrings.ml[3,62+16]..[3,62+20]) + attribute "ocaml.doc" + [ + structure_item (docstrings.ml[3,62+21]..[3,62+42]) + Pstr_eval + expression (docstrings.ml[3,62+21]..[3,62+42]) + Pexp_constant PConst_string(" default [true] ",None) + ] + Ptyp_constr "bool" (docstrings.ml[3,62+16]..[3,62+20]) + [] + core_type (docstrings.ml[4,105+5]..[4,105+7]) + Ptyp_var a + ] + structure_item (docstrings.ml[6,114+0]..[11,208+1]) + Pstr_type Rec + [ + type_declaration "obj" (docstrings.ml[6,114+5]..[6,114+8]) (docstrings.ml[6,114+0]..[11,208+1]) + ptype_params = + [] + ptype_cstrs = + [] + ptype_kind = + Ptype_abstract + ptype_private = Public + ptype_manifest = + Some + core_type (docstrings.ml[6,114+11]..[11,208+1]) + Ptyp_object Closed + method meth1 + attribute "ocaml.doc" + [ + structure_item (docstrings.ml[8,149+2]..[8,149+17]) + Pstr_eval + expression (docstrings.ml[8,149+2]..[8,149+17]) + Pexp_constant PConst_string(" method 1 ",None) + ] + core_type (docstrings.ml[7,127+10]..[7,127+20]) + Ptyp_arrow + Nolabel + core_type (docstrings.ml[7,127+10]..[7,127+13]) + Ptyp_constr "int" (docstrings.ml[7,127+10]..[7,127+13]) + [] + core_type (docstrings.ml[7,127+17]..[7,127+20]) + Ptyp_constr "int" (docstrings.ml[7,127+17]..[7,127+20]) + [] + method meth2 + attribute "ocaml.doc" + [ + structure_item (docstrings.ml[10,168+23]..[10,168+38]) + Pstr_eval + expression (docstrings.ml[10,168+23]..[10,168+38]) + Pexp_constant PConst_string(" method 2 ",None) + ] + core_type (docstrings.ml[10,168+9]..[10,168+22]) + Ptyp_arrow + Nolabel + core_type (docstrings.ml[10,168+9]..[10,168+13]) + Ptyp_constr "unit" (docstrings.ml[10,168+9]..[10,168+13]) + [] + core_type (docstrings.ml[10,168+17]..[10,168+22]) + Ptyp_constr "float" (docstrings.ml[10,168+17]..[10,168+22]) + [] + ] + structure_item (docstrings.ml[13,211+0]..[16,280+1]) + Pstr_type Rec + [ + type_declaration "var" (docstrings.ml[13,211+5]..[13,211+8]) (docstrings.ml[13,211+0]..[16,280+1]) + ptype_params = + [] + ptype_cstrs = + [] + ptype_kind = + Ptype_abstract + ptype_private = Public + ptype_manifest = + Some + core_type (docstrings.ml[13,211+11]..[16,280+1]) + Ptyp_variant closed=Closed + [ + Rtag "Foo" true + attribute "ocaml.doc" + [ + structure_item (docstrings.ml[14,224+9]..[14,224+19]) + Pstr_eval + expression (docstrings.ml[14,224+9]..[14,224+19]) + Pexp_constant PConst_string(" foo ",None) + ] + [] + Rtag "Bar" false + attribute "ocaml.doc" + [ + structure_item (docstrings.ml[15,244+25]..[15,244+35]) + Pstr_eval + expression (docstrings.ml[15,244+25]..[15,244+35]) + Pexp_constant PConst_string(" bar ",None) + ] + [ + core_type (docstrings.ml[15,244+12]..[15,244+24]) + Ptyp_tuple + [ + core_type (docstrings.ml[15,244+12]..[15,244+15]) + Ptyp_constr "int" (docstrings.ml[15,244+12]..[15,244+15]) + [] + core_type (docstrings.ml[15,244+18]..[15,244+24]) + Ptyp_constr "string" (docstrings.ml[15,244+18]..[15,244+24]) + [] + ] + ] + ] + None + ] +] +