Skip to content

Commit

Permalink
add a Pattern constructor to Extended_ast.t
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
v-gb committed Jan 6, 2025
1 parent 86d8e31 commit 4bb8bf6
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/Extended_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/Extended_ast.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions lib/Fmt_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/Std_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
1 change: 1 addition & 0 deletions lib/Std_ast.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/Syntax.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type t =
| Core_type
| Module_type
| Expression
| Pattern
| Repl_file
| Documentation

Expand Down
1 change: 1 addition & 0 deletions lib/Syntax.mli
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type t =
| Core_type
| Module_type
| Expression
| Pattern
| Repl_file
| Documentation

Expand Down
6 changes: 6 additions & 0 deletions test/unit/test_translation_unit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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 ]

0 comments on commit 4bb8bf6

Please sign in to comment.