From a4ad9ec8f12bd13c272c5e57893908bed12d308a Mon Sep 17 00:00:00 2001 From: Valentin Gatien-Baron Date: Mon, 6 Jan 2025 17:56:00 +0100 Subject: [PATCH] add a Pattern constructor to Extended_ast.t So it's easier to print patterns, which seems like the main major syntactic category missing from the type. Granted, no standard source file contains a pattern, but the same argument applies to core_type. --- lib/Extended_ast.ml | 5 +++++ lib/Extended_ast.mli | 1 + lib/Fmt_ast.ml | 1 + lib/Std_ast.ml | 5 +++++ lib/Std_ast.mli | 1 + lib/Syntax.ml | 1 + lib/Syntax.mli | 1 + test/unit/test_translation_unit.ml | 6 ++++++ 8 files changed, 21 insertions(+) diff --git a/lib/Extended_ast.ml b/lib/Extended_ast.ml index a685f3142e..770845422c 100644 --- a/lib/Extended_ast.ml +++ b/lib/Extended_ast.ml @@ -25,6 +25,7 @@ type 'a t = | Core_type : core_type t | Module_type : module_type t | Expression : expression t + | Pattern : pattern t | Repl_file : repl_file t | Documentation : Ocamlformat_odoc_parser.Ast.t t @@ -37,6 +38,7 @@ let of_syntax = function | Core_type -> Any Core_type | Module_type -> Any Module_type | Expression -> Any Expression + | Pattern -> Any Pattern | Repl_file -> Any Repl_file | Documentation -> Any Documentation @@ -50,6 +52,7 @@ let map (type a) (x : a t) (m : Ast_mapper.mapper) : a -> a = | Core_type -> m.typ m | Module_type -> m.module_type m | Expression -> m.expr m + | Pattern -> m.pat m | Repl_file -> List.map ~f:(m.repl_phrase m) | Documentation -> Fn.id @@ -253,6 +256,7 @@ module Parse = struct | Core_type -> Parse.core_type ~ocaml_version lexbuf | Module_type -> Parse.module_type ~ocaml_version lexbuf | Expression -> Parse.expression ~ocaml_version lexbuf + | Pattern -> Parse.pattern ~ocaml_version lexbuf | Repl_file -> Toplevel_lexer.repl_file ~ocaml_version lexbuf | Documentation -> let pos = (Location.curr lexbuf).loc_start in @@ -274,6 +278,7 @@ module Printast = struct | Core_type -> core_type | Module_type -> module_type | Expression -> expression + | Pattern -> pattern | Repl_file -> repl_file | Documentation -> Docstring.dump end diff --git a/lib/Extended_ast.mli b/lib/Extended_ast.mli index 20495635c0..b6c52724f4 100644 --- a/lib/Extended_ast.mli +++ b/lib/Extended_ast.mli @@ -24,6 +24,7 @@ type 'a t = | Core_type : core_type t | Module_type : module_type t | Expression : expression t + | Pattern : pattern t | Repl_file : repl_file t | Documentation : Ocamlformat_odoc_parser.Ast.t t diff --git a/lib/Fmt_ast.ml b/lib/Fmt_ast.ml index 32d28a9688..1c5fb4e63f 100644 --- a/lib/Fmt_ast.ml +++ b/lib/Fmt_ast.ml @@ -4817,6 +4817,7 @@ let fmt_file (type a) ~ctx ~fmt_code ~debug (fragment : a Extended_ast.t) (fmt_module_type c (sub_mty ~ctx:(Mty mty) mty)) | Expression, e -> fmt_expression c (sub_exp ~ctx:(Str (Ast_helper.Str.eval e)) e) + | Pattern, p -> fmt_pattern c (sub_pat ~ctx:(Pld (PPat (p, None))) p) | Repl_file, l -> fmt_repl_file c ctx l | Documentation, d -> (* TODO: [source] and [cmts] should have never been computed when diff --git a/lib/Std_ast.ml b/lib/Std_ast.ml index ad53edfc48..c00c34ca0e 100644 --- a/lib/Std_ast.ml +++ b/lib/Std_ast.ml @@ -21,6 +21,7 @@ type 'a t = | Core_type : core_type t | Module_type : module_type t | Expression : expression t + | Pattern : pattern t (* not implemented *) | Repl_file : unit t | Documentation : unit t @@ -34,6 +35,7 @@ let of_syntax = function | Core_type -> Any Core_type | Module_type -> Any Module_type | Expression -> Any Expression + | Pattern -> Any Pattern | Repl_file -> Any Repl_file | Documentation -> Any Documentation @@ -52,6 +54,7 @@ let map (type a) (x : a t) (m : Ast_mapper.mapper) : a -> a = | Core_type -> m.typ m | Module_type -> m.module_type m | Expression -> m.expr m + | Pattern -> m.pat m | Repl_file -> Fn.id | Documentation -> Fn.id @@ -69,6 +72,7 @@ module Parse = struct | Core_type -> Parse.core_type ~ocaml_version lexbuf | Module_type -> Parse.module_type ~ocaml_version lexbuf | Expression -> Parse.expression ~ocaml_version lexbuf + | Pattern -> Parse.pattern ~ocaml_version lexbuf | Repl_file -> () | Documentation -> () end @@ -85,6 +89,7 @@ module Printast = struct | Core_type -> core_type 0 | Module_type -> module_type 0 | Expression -> expression 0 + | Pattern -> pattern 0 | Repl_file -> fun _ _ -> () | Documentation -> fun _ _ -> () end diff --git a/lib/Std_ast.mli b/lib/Std_ast.mli index 981b66e9ff..bc0646da38 100644 --- a/lib/Std_ast.mli +++ b/lib/Std_ast.mli @@ -24,6 +24,7 @@ type 'a t = | Core_type : core_type t | Module_type : module_type t | Expression : expression t + | Pattern : pattern t (* not implemented *) | Repl_file : unit t | Documentation : unit t diff --git a/lib/Syntax.ml b/lib/Syntax.ml index 26c7580f46..3f059153f7 100644 --- a/lib/Syntax.ml +++ b/lib/Syntax.ml @@ -16,6 +16,7 @@ type t = | Core_type | Module_type | Expression + | Pattern | Repl_file | Documentation diff --git a/lib/Syntax.mli b/lib/Syntax.mli index cafe0adf3b..1e201ebe85 100644 --- a/lib/Syntax.mli +++ b/lib/Syntax.mli @@ -16,6 +16,7 @@ type t = | Core_type | Module_type | Expression + | Pattern | Repl_file | Documentation diff --git a/test/unit/test_translation_unit.ml b/test/unit/test_translation_unit.ml index 8d0fc42a44..66a8023414 100644 --- a/test/unit/test_translation_unit.ml +++ b/test/unit/test_translation_unit.ml @@ -46,6 +46,11 @@ Error: Syntax error |} ) ] +let test_parse_and_format_pattern = + let make_test = test_parse_and_format "pattern" ~fg:Pattern in + [ make_test "A 1" ~input:"A 1" ~expected:(Ok "A 1\n") + ; make_test "A B C" ~input:"A B C" ~expected:(Ok "A (B C)\n") ] + let test_parse_and_format_module_type = let make_test = test_parse_and_format "module_type" ~fg:Module_type in [ make_test "sig end" ~input:"sig end" ~expected:(Ok "sig end\n") @@ -127,5 +132,6 @@ let tests = [ test_parse_and_format_core_type ; test_parse_and_format_expression ; test_parse_and_format_module_type + ; test_parse_and_format_pattern ; test_parse_and_format_signature ; test_parse_and_format_use_file ]