Skip to content

Commit 4bb8bf6

Browse files
committed
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.
1 parent 86d8e31 commit 4bb8bf6

8 files changed

+21
-0
lines changed

lib/Extended_ast.ml

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type 'a t =
2525
| Core_type : core_type t
2626
| Module_type : module_type t
2727
| Expression : expression t
28+
| Pattern : pattern t
2829
| Repl_file : repl_file t
2930
| Documentation : Ocamlformat_odoc_parser.Ast.t t
3031

@@ -37,6 +38,7 @@ let of_syntax = function
3738
| Core_type -> Any Core_type
3839
| Module_type -> Any Module_type
3940
| Expression -> Any Expression
41+
| Pattern -> Any Pattern
4042
| Repl_file -> Any Repl_file
4143
| Documentation -> Any Documentation
4244

@@ -50,6 +52,7 @@ let map (type a) (x : a t) (m : Ast_mapper.mapper) : a -> a =
5052
| Core_type -> m.typ m
5153
| Module_type -> m.module_type m
5254
| Expression -> m.expr m
55+
| Pattern -> m.pat m
5356
| Repl_file -> List.map ~f:(m.repl_phrase m)
5457
| Documentation -> Fn.id
5558

@@ -253,6 +256,7 @@ module Parse = struct
253256
| Core_type -> Parse.core_type ~ocaml_version lexbuf
254257
| Module_type -> Parse.module_type ~ocaml_version lexbuf
255258
| Expression -> Parse.expression ~ocaml_version lexbuf
259+
| Pattern -> Parse.pattern ~ocaml_version lexbuf
256260
| Repl_file -> Toplevel_lexer.repl_file ~ocaml_version lexbuf
257261
| Documentation ->
258262
let pos = (Location.curr lexbuf).loc_start in
@@ -274,6 +278,7 @@ module Printast = struct
274278
| Core_type -> core_type
275279
| Module_type -> module_type
276280
| Expression -> expression
281+
| Pattern -> pattern
277282
| Repl_file -> repl_file
278283
| Documentation -> Docstring.dump
279284
end

lib/Extended_ast.mli

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type 'a t =
2424
| Core_type : core_type t
2525
| Module_type : module_type t
2626
| Expression : expression t
27+
| Pattern : pattern t
2728
| Repl_file : repl_file t
2829
| Documentation : Ocamlformat_odoc_parser.Ast.t t
2930

lib/Fmt_ast.ml

+1
Original file line numberDiff line numberDiff line change
@@ -4817,6 +4817,7 @@ let fmt_file (type a) ~ctx ~fmt_code ~debug (fragment : a Extended_ast.t)
48174817
(fmt_module_type c (sub_mty ~ctx:(Mty mty) mty))
48184818
| Expression, e ->
48194819
fmt_expression c (sub_exp ~ctx:(Str (Ast_helper.Str.eval e)) e)
4820+
| Pattern, p -> fmt_pattern c (sub_pat ~ctx:(Pld (PPat (p, None))) p)
48204821
| Repl_file, l -> fmt_repl_file c ctx l
48214822
| Documentation, d ->
48224823
(* TODO: [source] and [cmts] should have never been computed when

lib/Std_ast.ml

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type 'a t =
2121
| Core_type : core_type t
2222
| Module_type : module_type t
2323
| Expression : expression t
24+
| Pattern : pattern t
2425
(* not implemented *)
2526
| Repl_file : unit t
2627
| Documentation : unit t
@@ -34,6 +35,7 @@ let of_syntax = function
3435
| Core_type -> Any Core_type
3536
| Module_type -> Any Module_type
3637
| Expression -> Any Expression
38+
| Pattern -> Any Pattern
3739
| Repl_file -> Any Repl_file
3840
| Documentation -> Any Documentation
3941

@@ -52,6 +54,7 @@ let map (type a) (x : a t) (m : Ast_mapper.mapper) : a -> a =
5254
| Core_type -> m.typ m
5355
| Module_type -> m.module_type m
5456
| Expression -> m.expr m
57+
| Pattern -> m.pat m
5558
| Repl_file -> Fn.id
5659
| Documentation -> Fn.id
5760

@@ -69,6 +72,7 @@ module Parse = struct
6972
| Core_type -> Parse.core_type ~ocaml_version lexbuf
7073
| Module_type -> Parse.module_type ~ocaml_version lexbuf
7174
| Expression -> Parse.expression ~ocaml_version lexbuf
75+
| Pattern -> Parse.pattern ~ocaml_version lexbuf
7276
| Repl_file -> ()
7377
| Documentation -> ()
7478
end
@@ -85,6 +89,7 @@ module Printast = struct
8589
| Core_type -> core_type 0
8690
| Module_type -> module_type 0
8791
| Expression -> expression 0
92+
| Pattern -> pattern 0
8893
| Repl_file -> fun _ _ -> ()
8994
| Documentation -> fun _ _ -> ()
9095
end

lib/Std_ast.mli

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type 'a t =
2424
| Core_type : core_type t
2525
| Module_type : module_type t
2626
| Expression : expression t
27+
| Pattern : pattern t
2728
(* not implemented *)
2829
| Repl_file : unit t
2930
| Documentation : unit t

lib/Syntax.ml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type t =
1616
| Core_type
1717
| Module_type
1818
| Expression
19+
| Pattern
1920
| Repl_file
2021
| Documentation
2122

lib/Syntax.mli

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type t =
1616
| Core_type
1717
| Module_type
1818
| Expression
19+
| Pattern
1920
| Repl_file
2021
| Documentation
2122

test/unit/test_translation_unit.ml

+6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ Error: Syntax error
4646
|}
4747
) ]
4848

49+
let test_parse_and_format_pattern =
50+
let make_test = test_parse_and_format "pattern" ~fg:Pattern in
51+
[ make_test "A 1" ~input:"A 1" ~expected:(Ok "A 1\n")
52+
; make_test "A B C" ~input:"A B C" ~expected:(Ok "A (B C)\n") ]
53+
4954
let test_parse_and_format_module_type =
5055
let make_test = test_parse_and_format "module_type" ~fg:Module_type in
5156
[ make_test "sig end" ~input:"sig end" ~expected:(Ok "sig end\n")
@@ -127,5 +132,6 @@ let tests =
127132
[ test_parse_and_format_core_type
128133
; test_parse_and_format_expression
129134
; test_parse_and_format_module_type
135+
; test_parse_and_format_pattern
130136
; test_parse_and_format_signature
131137
; test_parse_and_format_use_file ]

0 commit comments

Comments
 (0)