Skip to content

Commit 6fb3b8c

Browse files
authored
Merge pull request #20 from NathanReb/dune-2
Upgrade to latest best practices
2 parents 1db98d8 + 1b97f2c commit 6fb3b8c

37 files changed

+410
-552
lines changed

.ocamlformat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
version=0.15.0

.ocamlformat-ignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test/rewriter/*
2+
test/rewriter/errors/*

dune-project

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
(lang dune 1.9)
1+
(lang dune 2.1)
2+
3+
(generate_opam_files true)
4+
25
(name ppx_yojson)
6+
(source (github NathanReb/ppx_yojson))
7+
(license BSD-2)
8+
(authors "Nathan Rebours <nathan.p.rebours@gmail.com>")
9+
(maintainers "Nathan Rebours <nathan.p.rebours@gmail.com>")
310

4-
(allow_approximate_merlin)
11+
(package
12+
(name ppx_yojson)
13+
(synopsis "PPX extension for Yojson literals and patterns")
14+
(depends
15+
(ocaml (>= 4.04.2))
16+
(alcotest :with-test)
17+
(ppxlib (and (>= 0.3.0) (< 0.18.0)))
18+
(yojson (and :with-test (>= 1.6.0)))))

lib/dune

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
(library
2-
(name ppx_yojson_lib)
3-
(public_name ppx_yojson._lib)
4-
(libraries
5-
ppxlib
6-
)
7-
(preprocess
8-
(pps
9-
ppxlib.metaquot
10-
)
11-
)
12-
)
2+
(name ppx_yojson_lib)
3+
(public_name ppx_yojson._lib)
4+
(libraries ppxlib)
5+
(preprocess
6+
(pps ppxlib.metaquot)))

lib/expression.ml

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,68 @@
11
open Ppxlib
22

3-
let expand_string ~loc s = [%expr `String [%e Ast_builder.Default.estring ~loc s]]
3+
let expand_string ~loc s =
4+
[%expr `String [%e Ast_builder.Default.estring ~loc s]]
45

5-
let expand_intlit ~loc s = [%expr `Intlit [%e Ast_builder.Default.estring ~loc s]]
6+
let expand_intlit ~loc s =
7+
[%expr `Intlit [%e Ast_builder.Default.estring ~loc s]]
68

79
let expand_int ~loc ~pexp_loc s =
810
match Ocaml_compat.int_of_string_opt s with
911
| Some i -> [%expr `Int [%e Ast_builder.Default.eint ~loc i]]
10-
| None when Integer_const.is_binary s -> Raise.unsupported_payload ~loc:pexp_loc
11-
| None when Integer_const.is_octal s -> Raise.unsupported_payload ~loc:pexp_loc
12-
| None when Integer_const.is_hexadecimal s -> Raise.unsupported_payload ~loc:pexp_loc
12+
| None when Integer_const.is_binary s ->
13+
Raise.unsupported_payload ~loc:pexp_loc
14+
| None when Integer_const.is_octal s ->
15+
Raise.unsupported_payload ~loc:pexp_loc
16+
| None when Integer_const.is_hexadecimal s ->
17+
Raise.unsupported_payload ~loc:pexp_loc
1318
| None -> expand_intlit ~loc s
1419

1520
let expand_float ~loc s = [%expr `Float [%e Ast_builder.Default.efloat ~loc s]]
1621

1722
let expand_anti_quotation ~pexp_loc = function
18-
| PStr [{pstr_desc = Pstr_eval (expr, _); _}] -> expr
19-
| PStr _
20-
| PSig _
21-
| PTyp _
22-
| PPat _ -> Raise.bad_expr_antiquotation_payload ~loc:pexp_loc
23+
| PStr [ { pstr_desc = Pstr_eval (expr, _); _ } ] -> expr
24+
| PStr _ | PSig _ | PTyp _ | PPat _ ->
25+
Raise.bad_expr_antiquotation_payload ~loc:pexp_loc
2326

2427
let rec expand ~loc ~path expr =
2528
match expr with
2629
| [%expr None] -> [%expr `Null]
27-
| [%expr true] -> [%expr (`Bool true)]
28-
| [%expr false] -> [%expr (`Bool false)]
29-
| {pexp_desc = Pexp_constant (Pconst_string (s, None)); _} -> expand_string ~loc s
30-
| {pexp_desc = Pexp_constant (Pconst_integer (s, None)); pexp_loc; _}
31-
->
32-
expand_int ~loc ~pexp_loc s
33-
| {pexp_desc = Pexp_constant (Pconst_integer (s, Some ('l' | 'L' | 'n'))); _}
34-
->
35-
expand_intlit ~loc s
36-
| {pexp_desc = Pexp_constant (Pconst_float (s, None)); _} -> expand_float ~loc s
30+
| [%expr true] -> [%expr `Bool true]
31+
| [%expr false] -> [%expr `Bool false]
32+
| { pexp_desc = Pexp_constant (Pconst_string (s, None)); _ } ->
33+
expand_string ~loc s
34+
| { pexp_desc = Pexp_constant (Pconst_integer (s, None)); pexp_loc; _ } ->
35+
expand_int ~loc ~pexp_loc s
36+
| {
37+
pexp_desc = Pexp_constant (Pconst_integer (s, Some ('l' | 'L' | 'n')));
38+
_;
39+
} ->
40+
expand_intlit ~loc s
41+
| { pexp_desc = Pexp_constant (Pconst_float (s, None)); _ } ->
42+
expand_float ~loc s
3743
| [%expr []] -> [%expr `List []]
38-
| [%expr [%e? _]::[%e? _]] -> [%expr `List [%e expand_list ~loc ~path expr]]
39-
| {pexp_desc = Pexp_record (l, None); _} -> [%expr `Assoc [%e expand_record ~loc ~path l]]
40-
| {pexp_desc = Pexp_extension ({txt = "y"; _}, p); pexp_loc; _}
41-
->
42-
expand_anti_quotation ~pexp_loc p
44+
| [%expr [%e? _] :: [%e? _]] -> [%expr `List [%e expand_list ~loc ~path expr]]
45+
| { pexp_desc = Pexp_record (l, None); _ } ->
46+
[%expr `Assoc [%e expand_record ~loc ~path l]]
47+
| { pexp_desc = Pexp_extension ({ txt = "y"; _ }, p); pexp_loc; _ } ->
48+
expand_anti_quotation ~pexp_loc p
4349
| _ -> Raise.unsupported_payload ~loc:expr.pexp_loc
50+
4451
and expand_list ~loc ~path = function
45-
| [%expr []]
46-
->
47-
[%expr []]
48-
| [%expr [%e? hd]::[%e? tl]]
49-
->
50-
let json_hd = expand ~loc ~path hd in
51-
let json_tl = expand_list ~loc ~path tl in
52-
[%expr [%e json_hd]::[%e json_tl]]
52+
| [%expr []] -> [%expr []]
53+
| [%expr [%e? hd] :: [%e? tl]] ->
54+
let json_hd = expand ~loc ~path hd in
55+
let json_tl = expand_list ~loc ~path tl in
56+
[%expr [%e json_hd] :: [%e json_tl]]
5357
| _ -> assert false
58+
5459
and expand_record ~loc ~path l =
5560
let field = function
56-
| {txt = Lident s; _} -> [%expr [%e Ast_builder.Default.estring ~loc s]]
57-
| {txt = _; loc} -> Raise.unsupported_record_field ~loc
61+
| { txt = Lident s; _ } -> [%expr [%e Ast_builder.Default.estring ~loc s]]
62+
| { txt = _; loc } -> Raise.unsupported_record_field ~loc
5863
in
5964
let expand_one (f, e) =
60-
[%expr ([%e field f], [%e expand ~loc:e.pexp_loc ~path e])]
65+
[%expr [%e field f], [%e expand ~loc:e.pexp_loc ~path e]]
6166
in
6267
let expr_list = List.map expand_one l in
6368
[%expr [%e Ast_builder.Default.elist ~loc expr_list]]

lib/expression.mli

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
(** Expression rewriting *)
22

33
val expand :
4-
loc: Ppxlib.Location.t ->
5-
path: string ->
6-
Ppxlib.expression ->
7-
Ppxlib.expression
4+
loc:Ppxlib.Location.t -> path:string -> Ppxlib.expression -> Ppxlib.expression

lib/integer_const.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ let is_special_encoding c s =
22
s.[0] = '0' && (s.[1] = c || s.[1] = Char.uppercase_ascii c)
33

44
let is_binary s = is_special_encoding 'b' s
5+
56
let is_octal s = is_special_encoding 'o' s
7+
68
let is_hexadecimal s = is_special_encoding 'x' s

lib/integer_const.mli

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
(** Helper functions to deal with AST's integer literals *)
1+
(** Helper functions to deal with AST's integer literals *)
22

3+
val is_binary : string -> bool
34
(** Return whether this integer string representation is in binary form.
45
i.e. if its in the form [0b100101].
56
*)
6-
val is_binary : string -> bool
77

8+
val is_octal : string -> bool
89
(** Return whether this integer string representation is in octal form.
910
i.e. if its in the form [0o1702].
1011
*)
11-
val is_octal : string -> bool
1212

13+
val is_hexadecimal : string -> bool
1314
(** Return whether this integer string representation is in hexadecimal form.
1415
i.e. if its in the form [0ox1a9f].
1516
*)
16-
val is_hexadecimal : string -> bool

lib/ocaml_compat.ml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
let int_of_string_opt s =
2-
try
3-
Some (int_of_string s)
4-
with Failure _ ->
5-
None
1+
let int_of_string_opt s = try Some (int_of_string s) with Failure _ -> None

lib/pattern.ml

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
open Ppxlib
22

3-
let expand_string ~loc s = [%pat? `String [%p Ast_builder.Default.pstring ~loc s]]
3+
let expand_string ~loc s =
4+
[%pat? `String [%p Ast_builder.Default.pstring ~loc s]]
45

5-
let expand_intlit ~loc s = [%pat? `Intlit [%p Ast_builder.Default.pstring ~loc s]]
6+
let expand_intlit ~loc s =
7+
[%pat? `Intlit [%p Ast_builder.Default.pstring ~loc s]]
68

79
let expand_int ~loc ~ppat_loc s =
810
match Ocaml_compat.int_of_string_opt s with
911
| Some i -> [%pat? `Int [%p Ast_builder.Default.pint ~loc i]]
10-
| None when Integer_const.is_binary s -> Raise.unsupported_payload ~loc:ppat_loc
11-
| None when Integer_const.is_octal s -> Raise.unsupported_payload ~loc:ppat_loc
12-
| None when Integer_const.is_hexadecimal s -> Raise.unsupported_payload ~loc:ppat_loc
12+
| None when Integer_const.is_binary s ->
13+
Raise.unsupported_payload ~loc:ppat_loc
14+
| None when Integer_const.is_octal s ->
15+
Raise.unsupported_payload ~loc:ppat_loc
16+
| None when Integer_const.is_hexadecimal s ->
17+
Raise.unsupported_payload ~loc:ppat_loc
1318
| None -> expand_intlit ~loc s
1419

1520
let expand_float ~loc s = [%pat? `Float [%p Ast_builder.Default.pfloat ~loc s]]
@@ -18,56 +23,57 @@ let expand_var ~loc var = Ast_builder.Default.ppat_var ~loc var
1823

1924
let expand_anti_quotation ~ppat_loc = function
2025
| PPat (ppat, _) -> ppat
21-
| PStr _
22-
| PSig _
23-
| PTyp _ -> Raise.bad_pat_antiquotation_payload ~loc:ppat_loc
26+
| PStr _ | PSig _ | PTyp _ ->
27+
Raise.bad_pat_antiquotation_payload ~loc:ppat_loc
2428

2529
let rec expand ~loc ~path pat =
2630
match pat with
2731
| [%pat? _] -> [%pat? _]
2832
| [%pat? None] -> [%pat? `Null]
2933
| [%pat? true] -> [%pat? `Bool true]
3034
| [%pat? false] -> [%pat? `Bool false]
31-
| {ppat_desc = Ppat_constant (Pconst_string (s, None)); _} -> expand_string ~loc s
32-
| {ppat_desc = Ppat_constant (Pconst_integer (s, None)); ppat_loc; _}
33-
->
34-
expand_int ~loc ~ppat_loc s
35-
| {ppat_desc = Ppat_constant (Pconst_integer (s, Some ('l' | 'L' | 'n'))); _}
36-
->
37-
expand_intlit ~loc s
38-
| {ppat_desc = Ppat_constant (Pconst_float (s, None)); _} -> expand_float ~loc s
39-
| {ppat_desc = Ppat_var v; _} -> expand_var ~loc v
40-
| {ppat_desc = Ppat_extension ({txt = "y"; _}, p); ppat_loc; _}
41-
->
42-
expand_anti_quotation ~ppat_loc p
43-
| [%pat? [%p? left] | [%p? right]]
44-
->
45-
([%pat? [%p expand ~loc ~path left] | [%p expand ~loc ~path right]])
46-
| {ppat_desc = Ppat_alias (pat, var); _}
47-
->
48-
let pat = expand ~loc ~path pat in
49-
Ast_builder.Default.ppat_alias ~loc pat var
35+
| { ppat_desc = Ppat_constant (Pconst_string (s, None)); _ } ->
36+
expand_string ~loc s
37+
| { ppat_desc = Ppat_constant (Pconst_integer (s, None)); ppat_loc; _ } ->
38+
expand_int ~loc ~ppat_loc s
39+
| {
40+
ppat_desc = Ppat_constant (Pconst_integer (s, Some ('l' | 'L' | 'n')));
41+
_;
42+
} ->
43+
expand_intlit ~loc s
44+
| { ppat_desc = Ppat_constant (Pconst_float (s, None)); _ } ->
45+
expand_float ~loc s
46+
| { ppat_desc = Ppat_var v; _ } -> expand_var ~loc v
47+
| { ppat_desc = Ppat_extension ({ txt = "y"; _ }, p); ppat_loc; _ } ->
48+
expand_anti_quotation ~ppat_loc p
49+
| [%pat? [%p? left] | [%p? right]] ->
50+
[%pat? [%p expand ~loc ~path left] | [%p expand ~loc ~path right]]
51+
| { ppat_desc = Ppat_alias (pat, var); _ } ->
52+
let pat = expand ~loc ~path pat in
53+
Ast_builder.Default.ppat_alias ~loc pat var
5054
| [%pat? []] -> [%pat? `List []]
51-
| [%pat? [%p? _]::[%p? _]] -> [%pat? `List [%p expand_list ~loc ~path pat]]
52-
| {ppat_desc = Ppat_record (l, Closed); ppat_loc; _} -> expand_record ~loc ~ppat_loc ~path l
53-
| {ppat_loc = loc; _} -> Raise.unsupported_payload ~loc
55+
| [%pat? [%p? _] :: [%p? _]] -> [%pat? `List [%p expand_list ~loc ~path pat]]
56+
| { ppat_desc = Ppat_record (l, Closed); ppat_loc; _ } ->
57+
expand_record ~loc ~ppat_loc ~path l
58+
| { ppat_loc = loc; _ } -> Raise.unsupported_payload ~loc
59+
5460
and expand_list ~loc ~path = function
5561
| [%pat? []] -> [%pat? []]
56-
| [%pat? [%p? hd]::[%p? tl]]
57-
->
58-
let json_hd = expand ~loc ~path hd in
59-
let json_tl = expand_list ~loc ~path tl in
60-
[%pat? [%p json_hd]::[%p json_tl]]
62+
| [%pat? [%p? hd] :: [%p? tl]] ->
63+
let json_hd = expand ~loc ~path hd in
64+
let json_tl = expand_list ~loc ~path tl in
65+
[%pat? [%p json_hd] :: [%p json_tl]]
6166
| _ -> assert false
67+
6268
and expand_record ~loc ~ppat_loc ~path l =
6369
let field = function
64-
| {txt = Lident s; _} -> [%pat? [%p Ast_builder.Default.pstring ~loc s]]
65-
| {txt = _; loc} -> Raise.unsupported_record_field ~loc
70+
| { txt = Lident s; _ } -> [%pat? [%p Ast_builder.Default.pstring ~loc s]]
71+
| { txt = _; loc } -> Raise.unsupported_record_field ~loc
6672
in
67-
let expand_one (f, p) =
68-
[%pat? ([%p field f], [%p expand ~loc ~path p])]
73+
let expand_one (f, p) = [%pat? [%p field f], [%p expand ~loc ~path p]] in
74+
let assoc_pattern pat_list =
75+
[%pat? `Assoc [%p Ast_builder.Default.plist ~loc pat_list]]
6976
in
70-
let assoc_pattern pat_list = [%pat? `Assoc [%p Ast_builder.Default.plist ~loc pat_list]] in
7177
if List.length l > 4 then
7278
Raise.too_many_fields_in_record_pattern ~loc:ppat_loc
7379
else
@@ -76,5 +82,6 @@ and expand_record ~loc ~ppat_loc ~path l =
7682
let assoc_patterns = List.map assoc_pattern permutations in
7783
match assoc_patterns with
7884
| [] -> assert false
79-
| [single] -> single
80-
| hd::tl -> List.fold_left (fun acc elm -> [%pat? [%p acc] | [%p elm]]) hd tl
85+
| [ single ] -> single
86+
| hd :: tl ->
87+
List.fold_left (fun acc elm -> [%pat? [%p acc] | [%p elm]]) hd tl

0 commit comments

Comments
 (0)