diff --git a/bytecomp/translmod.ml b/bytecomp/translmod.ml index 0743acbbe..7692d80f9 100644 --- a/bytecomp/translmod.ml +++ b/bytecomp/translmod.ml @@ -286,7 +286,7 @@ and transl_structure fields cc rootpath = function match item.str_desc with | Tstr_eval expr -> Lsequence(transl_exp expr, transl_structure fields cc rootpath rem) - | Tstr_value(rec_flag, pat_expr_list) -> + | Tstr_value(rec_flag, pat_expr_list, _attrs) -> let ext_fields = rev_let_bound_idents pat_expr_list @ fields in transl_let rec_flag pat_expr_list (transl_structure ext_fields cc rootpath rem) @@ -365,7 +365,7 @@ let rec defined_idents = function | item :: rem -> match item.str_desc with | Tstr_eval expr -> defined_idents rem - | Tstr_value(rec_flag, pat_expr_list) -> + | Tstr_value(rec_flag, pat_expr_list, _attrs) -> let_bound_idents pat_expr_list @ defined_idents rem | Tstr_primitive desc -> defined_idents rem | Tstr_type decls -> defined_idents rem @@ -388,7 +388,7 @@ let rec more_idents = function | item :: rem -> match item.str_desc with | Tstr_eval expr -> more_idents rem - | Tstr_value(rec_flag, pat_expr_list) -> more_idents rem + | Tstr_value(rec_flag, pat_expr_list, _attrs) -> more_idents rem | Tstr_primitive _ -> more_idents rem | Tstr_type decls -> more_idents rem | Tstr_exception _ -> more_idents rem @@ -409,7 +409,7 @@ and all_idents = function | item :: rem -> match item.str_desc with | Tstr_eval expr -> all_idents rem - | Tstr_value(rec_flag, pat_expr_list) -> + | Tstr_value(rec_flag, pat_expr_list, _attrs) -> let_bound_idents pat_expr_list @ all_idents rem | Tstr_primitive _ -> all_idents rem | Tstr_type decls -> all_idents rem @@ -459,7 +459,7 @@ let transl_store_structure glob map prims str = | Tstr_eval expr -> Lsequence(subst_lambda subst (transl_exp expr), transl_store rootpath subst rem) - | Tstr_value(rec_flag, pat_expr_list) -> + | Tstr_value(rec_flag, pat_expr_list, _attrs) -> let ids = let_bound_idents pat_expr_list in let lam = transl_let rec_flag pat_expr_list (store_idents ids) in Lsequence(subst_lambda subst lam, @@ -672,7 +672,7 @@ let transl_toplevel_item item = match item.str_desc with Tstr_eval expr -> transl_exp expr - | Tstr_value(rec_flag, pat_expr_list) -> + | Tstr_value(rec_flag, pat_expr_list, _attrs) -> let idents = let_bound_idents pat_expr_list in transl_let rec_flag pat_expr_list (make_sequence toploop_setvalue_id idents) diff --git a/camlp4/Camlp4/Struct/Camlp4Ast2OCamlAst.ml b/camlp4/Camlp4/Struct/Camlp4Ast2OCamlAst.ml index e8231caff..23491af3b 100644 --- a/camlp4/Camlp4/Struct/Camlp4Ast2OCamlAst.ml +++ b/camlp4/Camlp4/Struct/Camlp4Ast2OCamlAst.ml @@ -1085,7 +1085,7 @@ value varify_constructors var_names = [mkstr loc (Pstr_open (long_uident id, [])) :: l] | StTyp loc tdl -> [mkstr loc (Pstr_type (mktype_decl tdl [])) :: l] | StVal loc rf bi -> - [mkstr loc (Pstr_value (mkrf rf) (binding bi [])) :: l] + [mkstr loc (Pstr_value (mkrf rf) (binding bi []) []) :: l] | <:str_item@loc< $anti:_$ >> -> error loc "antiquotation in str_item" ] and class_type = fun diff --git a/camlp4/boot/Camlp4.ml b/camlp4/boot/Camlp4.ml index dadd2d133..17a9c818f 100644 --- a/camlp4/boot/Camlp4.ml +++ b/camlp4/boot/Camlp4.ml @@ -15471,7 +15471,7 @@ module Struct = | StTyp (loc, tdl) -> (mkstr loc (Pstr_type (mktype_decl tdl []))) :: l | StVal (loc, rf, bi) -> - (mkstr loc (Pstr_value ((mkrf rf), (binding bi [])))) :: l + (mkstr loc (Pstr_value ((mkrf rf), (binding bi []), []))) :: l | Ast.StAnt (loc, _) -> error loc "antiquotation in str_item" and class_type = function diff --git a/ocamldoc/odoc_ast.ml b/ocamldoc/odoc_ast.ml index 4b63e8c45..45d913c23 100644 --- a/ocamldoc/odoc_ast.ml +++ b/ocamldoc/odoc_ast.ml @@ -98,7 +98,7 @@ module Typedtree_search = (CT (Name.from_ident id)) (Typedtree.Tstr_class_type [ci])) info_list - | Typedtree.Tstr_value (_, pat_exp_list) -> + | Typedtree.Tstr_value (_, pat_exp_list, _) -> List.iter (fun (pat,exp) -> match iter_val_pattern pat.Typedtree.pat_desc with @@ -1059,7 +1059,7 @@ module Analyser = | Parsetree.Pstr_attribute _ | Parsetree.Pstr_extension _ -> (0, env, []) - | Parsetree.Pstr_value (rec_flag, pat_exp_list) -> + | Parsetree.Pstr_value (rec_flag, pat_exp_list, _) -> (* of rec_flag * (pattern * expression) list *) (* For each value, look for the value name, then look in the typedtree for the corresponding information, diff --git a/otherlibs/labltk/browser/searchid.ml b/otherlibs/labltk/browser/searchid.ml index 9461d823a..936be064c 100644 --- a/otherlibs/labltk/browser/searchid.ml +++ b/otherlibs/labltk/browser/searchid.ml @@ -452,7 +452,7 @@ let search_structure str ~name ~kind ~prefix = List.iter (search_module str ~prefix) ~f: begin fun item -> if match item.pstr_desc with - Pstr_value (_, l) when kind = Pvalue -> + Pstr_value (_, l, _) when kind = Pvalue -> List.iter l ~f: begin fun (pat,_) -> if List.mem name (bound_variables pat) diff --git a/otherlibs/labltk/browser/searchpos.ml b/otherlibs/labltk/browser/searchpos.ml index 6ee451244..180277f70 100644 --- a/otherlibs/labltk/browser/searchpos.ml +++ b/otherlibs/labltk/browser/searchpos.ml @@ -660,7 +660,7 @@ let rec search_pos_structure ~pos str = List.iter str ~f: begin function str -> match str.str_desc with Tstr_eval exp -> search_pos_expr exp ~pos - | Tstr_value (rec_flag, l) -> + | Tstr_value (rec_flag, l, _) -> List.iter l ~f: begin fun (pat, exp) -> let env = diff --git a/parsing/ast_helper.ml b/parsing/ast_helper.ml index a3d6d50a8..3731b351d 100644 --- a/parsing/ast_helper.ml +++ b/parsing/ast_helper.ml @@ -158,7 +158,7 @@ module Str = struct let mk ?(loc = !default_loc) d = {pstr_desc = d; pstr_loc = loc} let eval ?loc a = mk ?loc (Pstr_eval a) - let value ?loc a b = mk ?loc (Pstr_value (a, b)) + let value ?loc ?(attrs = []) a b = mk ?loc (Pstr_value (a, b, attrs)) let primitive ?loc a = mk ?loc (Pstr_primitive a) let type_ ?loc a = mk ?loc (Pstr_type a) let exception_ ?loc a = mk ?loc (Pstr_exception a) diff --git a/parsing/ast_helper.mli b/parsing/ast_helper.mli index d8a78386c..1e64a5cc5 100644 --- a/parsing/ast_helper.mli +++ b/parsing/ast_helper.mli @@ -154,7 +154,7 @@ module Str: val mk: ?loc:loc -> structure_item_desc -> structure_item val eval: ?loc:loc -> expression -> structure_item - val value: ?loc:loc -> rec_flag -> (pattern * expression) list -> structure_item + val value: ?loc:loc -> ?attrs:attributes -> rec_flag -> (pattern * expression) list -> structure_item val primitive: ?loc:loc -> value_description -> structure_item val type_: ?loc:loc -> type_declaration list -> structure_item val exception_: ?loc:loc -> constructor_declaration -> structure_item diff --git a/parsing/ast_mapper.ml b/parsing/ast_mapper.ml index 8af7641d3..99593a153 100644 --- a/parsing/ast_mapper.ml +++ b/parsing/ast_mapper.ml @@ -166,7 +166,7 @@ module M = struct let loc = sub # location loc in match desc with | Pstr_eval x -> eval ~loc (sub # expr x) - | Pstr_value (r, pel) -> value ~loc r (List.map (map_tuple (sub # pat) (sub # expr)) pel) + | Pstr_value (r, pel, attrs) -> value ~loc ~attrs:(sub # attributes attrs) r (List.map (map_tuple (sub # pat) (sub # expr)) pel) | Pstr_primitive vd -> primitive ~loc (sub # value_description vd) | Pstr_type l -> type_ ~loc (List.map (sub # type_declaration) l) | Pstr_exception ed -> exception_ ~loc (sub # constructor_declaration ed) diff --git a/parsing/parser.mly b/parsing/parser.mly index d3b3330e7..dc10fdcd8 100644 --- a/parsing/parser.mly +++ b/parsing/parser.mly @@ -173,6 +173,9 @@ let unclosed opening_name opening_num closing_name closing_num = let expecting pos nonterm = raise Syntaxerr.(Error(Expecting(rhs_loc pos, nonterm))) +let not_expecting pos nonterm = + raise Syntaxerr.(Error(Not_expecting(rhs_loc pos, nonterm))) + let bigarray_function str name = ghloc (Ldot(Ldot(Lident "Bigarray", str), name)) @@ -597,11 +600,18 @@ str_attribute: post_item_attribute { mkstr(Pstr_attribute $1) } ; structure_item: - LET ext_attributes rec_flag let_bindings - { (* todo: keep attributes *) + LET ext_attributes rec_flag let_bindings post_item_attributes + { match $4 with [{ ppat_desc = Ppat_any; ppat_loc = _ }, exp] -> mkstr(Pstr_eval exp) - | l -> mkstr(Pstr_value($3, List.rev l)) } + (* todo: keep attributes, support extension *) + | l -> + begin match $2 with + | None, [] -> mkstr(Pstr_value($3, List.rev l, $5)) + | Some _, _ -> not_expecting 2 "extension" + | None, _ :: _ -> not_expecting 2 "attribute" + end + } | EXTERNAL val_ident COLON core_type EQUAL primitive_declaration post_item_attributes { mkstr (Pstr_primitive (Val.mk (mkrhs $2 2) $4 diff --git a/parsing/parsetree.mli b/parsing/parsetree.mli index 41ae9f5d1..3ce370166 100644 --- a/parsing/parsetree.mli +++ b/parsing/parsetree.mli @@ -337,7 +337,7 @@ and structure_item = and structure_item_desc = Pstr_eval of expression - | Pstr_value of rec_flag * (pattern * expression) list + | Pstr_value of rec_flag * (pattern * expression) list * attributes | Pstr_primitive of value_description | Pstr_type of type_declaration list | Pstr_exception of constructor_declaration diff --git a/parsing/pprintast.ml b/parsing/pprintast.ml index ba22bc441..08ad40345 100644 --- a/parsing/pprintast.ml +++ b/parsing/pprintast.ml @@ -1038,7 +1038,7 @@ class printer ()= object(self:'self) pp f "@[let@ _ =@ %a@]" self#expression e | Pstr_type [] -> assert false | Pstr_type l -> self#type_def_list f l - | Pstr_value (rf, l) -> (* pp f "@[let %a%a@]" self#rec_flag rf self#bindings l *) + | Pstr_value (rf, l, _attrs) -> (* pp f "@[let %a%a@]" self#rec_flag rf self#bindings l *) pp f "@[<2>%a@]" self#bindings (rf,l) | Pstr_exception ed -> self#exception_declaration f ed | Pstr_module x -> diff --git a/parsing/printast.ml b/parsing/printast.ml index eebab43ea..61fbff8df 100644 --- a/parsing/printast.ml +++ b/parsing/printast.ml @@ -677,8 +677,9 @@ and structure_item i ppf x = | Pstr_eval (e) -> line i ppf "Pstr_eval\n"; expression i ppf e; - | Pstr_value (rf, l) -> + | Pstr_value (rf, l, attrs) -> line i ppf "Pstr_value %a\n" fmt_rec_flag rf; + attributes i ppf attrs; list i pattern_x_expression_def ppf l; | Pstr_primitive vd -> line i ppf "Pstr_primitive\n"; diff --git a/parsing/syntaxerr.ml b/parsing/syntaxerr.ml index 5c17a99a3..b19a382d4 100644 --- a/parsing/syntaxerr.ml +++ b/parsing/syntaxerr.ml @@ -17,6 +17,7 @@ open Format type error = Unclosed of Location.t * string * Location.t * string | Expecting of Location.t * string + | Not_expecting of Location.t * string | Applicative_path of Location.t | Variable_in_scope of Location.t * string | Other of Location.t @@ -42,6 +43,10 @@ let report_error ppf = function fprintf ppf "%a@[Syntax error: %s expected.@]" Location.print_error loc nonterm + | Not_expecting (loc, nonterm) -> + fprintf ppf + "%a@[Syntax error: %s not expected.@]" + Location.print_error loc nonterm | Applicative_path loc -> fprintf ppf "%aSyntax error: applicative paths of the form F(X).t \ @@ -61,4 +66,5 @@ let location_of_error = function | Applicative_path l | Variable_in_scope(l,_) | Other l + | Not_expecting (l, _) | Expecting (l, _) -> l diff --git a/parsing/syntaxerr.mli b/parsing/syntaxerr.mli index 03cf532eb..0bacb0f95 100644 --- a/parsing/syntaxerr.mli +++ b/parsing/syntaxerr.mli @@ -17,6 +17,7 @@ open Format type error = Unclosed of Location.t * string * Location.t * string | Expecting of Location.t * string + | Not_expecting of Location.t * string | Applicative_path of Location.t | Variable_in_scope of Location.t * string | Other of Location.t diff --git a/tools/cmt2annot.ml b/tools/cmt2annot.ml index 7394fca06..ddf8f45a2 100644 --- a/tools/cmt2annot.ml +++ b/tools/cmt2annot.ml @@ -92,7 +92,7 @@ let iterator rebuild_env = method private structure_item_rem s rem = begin match s with - | {str_desc = Tstr_value (rec_flag, bindings); str_loc = loc} -> + | {str_desc = Tstr_value (rec_flag, bindings, _); str_loc = loc} -> let open Location in let doit loc_start = bind_bindings {scope with loc_start} bindings in begin match rec_flag, rem with diff --git a/tools/depend.ml b/tools/depend.ml index 9a851f950..cf7180ff5 100644 --- a/tools/depend.ml +++ b/tools/depend.ml @@ -262,7 +262,7 @@ and add_struct_item bv item = match item.pstr_desc with Pstr_eval e -> add_expr bv e; bv - | Pstr_value(rf, pel) -> + | Pstr_value(rf, pel, _attrs) -> let bv = add_bindings rf bv pel in bv | Pstr_primitive vd -> add_type bv vd.pval_type; bv diff --git a/tools/ocamlprof.ml b/tools/ocamlprof.ml index dfdb1b2a5..9e3977642 100644 --- a/tools/ocamlprof.ml +++ b/tools/ocamlprof.ml @@ -368,7 +368,7 @@ and rewrite_mod iflag smod = and rewrite_str_item iflag item = match item.pstr_desc with Pstr_eval exp -> rewrite_exp iflag exp - | Pstr_value(_, exps) + | Pstr_value(_, exps, _attrs) -> List.iter (function (_,exp) -> rewrite_exp iflag exp) exps | Pstr_module x -> rewrite_mod iflag x.pmb_expr (* todo: Pstr_recmodule?? *) diff --git a/tools/tast_iter.ml b/tools/tast_iter.ml index 04a7e093b..7682a5584 100644 --- a/tools/tast_iter.ml +++ b/tools/tast_iter.ml @@ -25,7 +25,7 @@ let constructor_decl sub cd = let structure_item sub x = match x.str_desc with | Tstr_eval exp -> sub # expression exp - | Tstr_value (rec_flag, list) -> sub # bindings (rec_flag, list) + | Tstr_value (rec_flag, list, _) -> sub # bindings (rec_flag, list) | Tstr_primitive v -> sub # value_description v | Tstr_type list -> List.iter (sub # type_declaration) list | Tstr_exception decl -> constructor_decl sub decl diff --git a/tools/untypeast.ml b/tools/untypeast.ml index e327af0f6..c4061c5ab 100644 --- a/tools/untypeast.ml +++ b/tools/untypeast.ml @@ -47,9 +47,9 @@ and untype_structure_item item = let desc = match item.str_desc with Tstr_eval exp -> Pstr_eval (untype_expression exp) - | Tstr_value (rec_flag, list) -> + | Tstr_value (rec_flag, list, attrs) -> Pstr_value (rec_flag, List.map (fun (pat, exp) -> - untype_pattern pat, untype_expression exp) list) + untype_pattern pat, untype_expression exp) list, attrs) | Tstr_primitive vd -> Pstr_primitive (untype_value_description vd) | Tstr_type list -> diff --git a/typing/printtyped.ml b/typing/printtyped.ml index eff21f9d7..afb1f8d26 100644 --- a/typing/printtyped.ml +++ b/typing/printtyped.ml @@ -683,8 +683,9 @@ and structure_item i ppf x = | Tstr_eval (e) -> line i ppf "Pstr_eval\n"; expression i ppf e; - | Tstr_value (rf, l) -> + | Tstr_value (rf, l, attrs) -> line i ppf "Pstr_value %a\n" fmt_rec_flag rf; + attributes i ppf attrs; list i pattern_x_expression_def ppf l; | Tstr_primitive vd -> line i ppf "Pstr_primitive\n"; diff --git a/typing/typecore.ml b/typing/typecore.ml index f4b831768..46a1a9818 100644 --- a/typing/typecore.ml +++ b/typing/typecore.ml @@ -167,7 +167,7 @@ let iter_expression f e = and structure_item str = match str.pstr_desc with | Pstr_eval e -> expr e - | Pstr_value (_, pel) -> List.iter (fun (_, e) -> expr e) pel + | Pstr_value (_, pel, _) -> List.iter (fun (_, e) -> expr e) pel | Pstr_primitive _ | Pstr_type _ | Pstr_exception _ @@ -1340,7 +1340,7 @@ and is_nonexpansive_mod mexp = (fun item -> match item.str_desc with | Tstr_eval _ | Tstr_primitive _ | Tstr_type _ | Tstr_modtype _ | Tstr_open _ | Tstr_class_type _ | Tstr_exn_rebind _ -> true - | Tstr_value (_, pat_exp_list) -> + | Tstr_value (_, pat_exp_list, _) -> List.for_all (fun (_, exp) -> is_nonexpansive exp) pat_exp_list | Tstr_module {mb_expr=m;_} | Tstr_include (m, _, _) -> is_nonexpansive_mod m | Tstr_recmodule id_mod_list -> diff --git a/typing/typedtree.ml b/typing/typedtree.ml index b1e3ff83a..2963b2bba 100644 --- a/typing/typedtree.ml +++ b/typing/typedtree.ml @@ -202,7 +202,7 @@ and structure_item = and structure_item_desc = Tstr_eval of expression - | Tstr_value of rec_flag * (pattern * expression) list + | Tstr_value of rec_flag * (pattern * expression) list * attributes | Tstr_primitive of value_description | Tstr_type of type_declaration list | Tstr_exception of constructor_declaration diff --git a/typing/typedtree.mli b/typing/typedtree.mli index e663ef354..85db5e8bb 100644 --- a/typing/typedtree.mli +++ b/typing/typedtree.mli @@ -201,7 +201,7 @@ and structure_item = and structure_item_desc = Tstr_eval of expression - | Tstr_value of rec_flag * (pattern * expression) list + | Tstr_value of rec_flag * (pattern * expression) list * attributes | Tstr_primitive of value_description | Tstr_type of type_declaration list | Tstr_exception of constructor_declaration diff --git a/typing/typedtreeIter.ml b/typing/typedtreeIter.ml index 4392eea82..55e589657 100644 --- a/typing/typedtreeIter.ml +++ b/typing/typedtreeIter.ml @@ -120,7 +120,7 @@ module MakeIterator(Iter : IteratorArgument) : sig begin match item.str_desc with Tstr_eval exp -> iter_expression exp - | Tstr_value (rec_flag, list) -> + | Tstr_value (rec_flag, list, _attrs) -> iter_bindings rec_flag list | Tstr_primitive vd -> iter_value_description vd | Tstr_type list -> List.iter iter_type_declaration list diff --git a/typing/typedtreeMap.ml b/typing/typedtreeMap.ml index f4d56d383..7b1ae5926 100644 --- a/typing/typedtreeMap.ml +++ b/typing/typedtreeMap.ml @@ -92,8 +92,8 @@ module MakeMap(Map : MapArgument) = struct let str_desc = match item.str_desc with Tstr_eval exp -> Tstr_eval (map_expression exp) - | Tstr_value (rec_flag, list) -> - Tstr_value (rec_flag, map_bindings rec_flag list) + | Tstr_value (rec_flag, list, attrs) -> + Tstr_value (rec_flag, map_bindings rec_flag list, attrs) | Tstr_primitive vd -> Tstr_primitive (map_value_description vd) | Tstr_type list -> diff --git a/typing/typemod.ml b/typing/typemod.ml index ce9a84e43..57881e72b 100644 --- a/typing/typemod.ml +++ b/typing/typemod.ml @@ -662,7 +662,7 @@ and closed_signature_item = function let check_nongen_scheme env str = match str.str_desc with - Tstr_value(rec_flag, pat_exp_list) -> + Tstr_value(rec_flag, pat_exp_list, _attrs) -> List.iter (fun (pat, exp) -> if not (Ctype.closed_schema exp.exp_type) then @@ -975,7 +975,7 @@ and type_structure ?(toplevel = false) funct_body anchor env sstr scope = let item = mk (Tstr_eval expr) in let (str_rem, sig_rem, final_env) = type_struct env srem in (item :: str_rem, sig_rem, final_env) - | Pstr_value(rec_flag, sdefs) -> + | Pstr_value(rec_flag, sdefs, attrs) -> let scope = match rec_flag with | Recursive -> Some (Annot.Idef {scope with @@ -988,7 +988,7 @@ and type_structure ?(toplevel = false) funct_body anchor env sstr scope = in let (defs, newenv) = Typecore.type_binding env rec_flag sdefs scope in - let item = mk (Tstr_value(rec_flag, defs)) in + let item = mk (Tstr_value(rec_flag, defs, attrs)) in let (str_rem, sig_rem, final_env) = type_struct newenv srem in let bound_idents = let_bound_idents defs in (* Note: Env.find_value does not trigger the value_used event. Values