Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a Pattern constructor to Extended_ast.t #2644

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Items marked with an asterisk (\*) are changes that are likely to format
existing code differently from the previous release when using the default
profile. This started with version 0.26.0.

## unreleased

## 0.27.0

### Highlight
Expand Down
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
14 changes: 11 additions & 3 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 @@ -124,6 +129,9 @@ let test_parse_and_format_expression =

let tests =
List.concat
[ test_parse_and_format_signature @ test_parse_and_format_use_file
@ test_parse_and_format_core_type @ test_parse_and_format_module_type
@ test_parse_and_format_expression ]
[ 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 ]
Loading