Skip to content

Commit

Permalink
Move Safe, Basic and Raw into their own modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonidas-from-XIV committed Jul 17, 2023
1 parent 95e8d0f commit 1ead566
Show file tree
Hide file tree
Showing 16 changed files with 333 additions and 254 deletions.
2 changes: 1 addition & 1 deletion .ocamlformat-ignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
lib/*.cppo.ml*
lib/monomorphic.ml
lib/write.ml*
lib/pretty.ml
lib/prettyprint.ml
lib/type.ml
lib/util.ml
19 changes: 19 additions & 0 deletions lib/basic.cppo.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#define INT
#define FLOAT
#define STRING
#include "type.ml"
#include "write.ml"
module Pretty =
struct
#include "prettyprint.ml"
end
#include "monomorphic.ml"
#include "write2.ml"
#include "read.ml"
module Util =
struct
#include "util.ml"
end
#undef INT
#undef FLOAT
#undef STRING
25 changes: 25 additions & 0 deletions lib/basic.cppo.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(**
This module supports standard JSON nodes only, i.e. no special syntax
for variants or tuples as supported by {!Yojson.Safe}.
Arbitrary integers are not supported as they must all fit within the
standard OCaml int type (31 or 63 bits depending on the platform).
The main advantage of this module is its simplicity.
*)

#define INT
#define FLOAT
#define STRING
#include "type.ml"
#include "write.mli"
#include "monomorphic.mli"
#include "write2.mli"
#include "read.mli"
(** This module provides combinators for extracting fields from JSON values. *)
module Util :
sig
#include "util.mli"
end
#undef INT
#undef FLOAT
#undef STRING
97 changes: 93 additions & 4 deletions lib/dune
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(ocamllex read)

(rule
(targets yojson.ml)
(targets basic.ml)
(deps
(:out yojson.cppo.ml)
(:out basic.cppo.ml)
monomorphic.ml
read.ml
write.ml
Expand All @@ -17,9 +17,98 @@
(run cppo %{out} -o %{targets})))

(rule
(targets yojson.mli)
(targets basic.mli)
(deps
(:out yojson.cppo.mli)
(:out basic.cppo.mli)
monomorphic.mli
write.mli
read.mli
safe_convert.mli
write2.mli
common.mli
util.mli
type.ml)
(action
(run cppo %{out} -o %{targets})))

(rule
(targets t.ml)
(deps
(:out t.cppo.ml)
type.ml
monomorphic.ml
prettyprint.ml
write.ml
write2.ml)
(action
(run cppo %{out} -o %{targets})))

(rule
(targets t.mli)
(deps
(:out t.cppo.mli)
monomorphic.mli
write.mli
read.mli
safe_convert.mli
write2.mli
common.mli
util.mli
type.ml)
(action
(run cppo %{out} -o %{targets})))

(rule
(targets safe.ml)
(deps
(:out safe.cppo.ml)
monomorphic.ml
read.ml
write.ml
safe_convert.ml
prettyprint.ml
write2.ml
common.ml
util.ml
type.ml)
(action
(run cppo %{out} -o %{targets})))

(rule
(targets safe.mli)
(deps
(:out safe.cppo.mli)
monomorphic.mli
write.mli
read.mli
safe_convert.mli
write2.mli
common.mli
util.mli
type.ml)
(action
(run cppo %{out} -o %{targets})))

(rule
(targets raw.ml)
(deps
(:out raw.cppo.ml)
monomorphic.ml
read.ml
write.ml
safe_convert.ml
prettyprint.ml
write2.ml
common.ml
util.ml
type.ml)
(action
(run cppo %{out} -o %{targets})))

(rule
(targets raw.mli)
(deps
(:out raw.cppo.mli)
monomorphic.mli
write.mli
read.mli
Expand Down
23 changes: 23 additions & 0 deletions lib/raw.cppo.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#define INTLIT
#define FLOATLIT
#define STRINGLIT
#define TUPLE
#define VARIANT
#include "type.ml"
#include "write.ml"
module Pretty =
struct
#include "prettyprint.ml"
end
#include "monomorphic.ml"
#include "write2.ml"
#include "read.ml"
module Util =
struct
#include "util.ml"
end
#undef INTLIT
#undef FLOATLIT
#undef STRINGLIT
#undef TUPLE
#undef VARIANT
27 changes: 27 additions & 0 deletions lib/raw.cppo.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(**
Ints, floats and strings literals are systematically preserved using
[`Intlit], [`Floatlit] and [`Stringlit].
This module also supports the specific syntax for variants and tuples
supported by {!Yojson.Safe}.
*)

#define INTLIT
#define FLOATLIT
#define STRINGLIT
#define TUPLE
#define VARIANT
#include "type.ml"
#include "monomorphic.mli"
#include "write.mli"
#include "write2.mli"
#include "read.mli"
(** This module provides combinators for extracting fields from JSON values. *)
module Util :
sig
#include "util.mli"
end
#undef INTLIT
#undef FLOATLIT
#undef STRINGLIT
#undef TUPLE
#undef VARIANT
26 changes: 26 additions & 0 deletions lib/safe.cppo.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#define INT
#define INTLIT
#define FLOAT
#define STRING
#define TUPLE
#define VARIANT
#include "type.ml"
#include "safe_convert.ml"
#include "write.ml"
module Pretty =
struct
#include "prettyprint.ml"
end
#include "monomorphic.ml"
#include "write2.ml"
#include "read.ml"
module Util =
struct
#include "util.ml"
end
#undef INT
#undef INTLIT
#undef FLOAT
#undef STRING
#undef TUPLE
#undef VARIANT
34 changes: 34 additions & 0 deletions lib/safe.cppo.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(**
This module supports a specific syntax for variants and tuples
in addition to the standard JSON nodes.
Arbitrary integers are supported and represented as a decimal string
using [`Intlit] when they cannot be represented using OCaml's int type
(31 or 63 bits depending on the platform).
This module is recommended for intensive use
or OCaml-friendly use of JSON.
*)

#define INT
#define INTLIT
#define FLOAT
#define STRING
#define TUPLE
#define VARIANT
#include "type.ml"
#include "monomorphic.mli"
#include "safe_convert.mli"
#include "write.mli"
#include "write2.mli"
#include "read.mli"
(** This module provides combinators for extracting fields from JSON values. *)
module Util :
sig
#include "util.mli"
end
#undef INT
#undef INTLIT
#undef FLOAT
#undef STRING
#undef TUPLE
#undef VARIANT
2 changes: 1 addition & 1 deletion lib/safe_convert.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let rec to_basic : t -> Basic.t = function
let rec to_basic = function
| (`Null | `Bool _ | `Int _ | `Float _ | `String _) as x -> x
| `Intlit s -> `String s
| `List l | `Tuple l -> `List (List.rev (List.rev_map to_basic l))
Expand Down
2 changes: 1 addition & 1 deletion lib/safe_convert.mli
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
val to_basic : t -> Basic.t
val to_basic : t -> _
(**
Tuples are converted to JSON arrays,
Variants are converted to JSON strings or arrays of a string (constructor)
Expand Down
25 changes: 25 additions & 0 deletions lib/t.cppo.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#define INT
#define INTLIT
#define FLOAT
#define FLOATLIT
#define STRING
#define STRINGLIT
#define TUPLE
#define VARIANT
#include "type.ml"
#include "write.ml"
#include "monomorphic.ml"
module Pretty =
struct
#include "prettyprint.ml"
end
#include "write2.ml"
#undef INT
#undef INTLIT
#undef FLOAT
#undef FLOATLIT
#undef STRING
#undef STRINGLIT
#undef TUPLE
#undef VARIANT

20 changes: 20 additions & 0 deletions lib/t.cppo.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#define INT
#define INTLIT
#define FLOAT
#define FLOATLIT
#define STRING
#define STRINGLIT
#define TUPLE
#define VARIANT
#include "type.ml"
#include "monomorphic.mli"
#include "write.mli"
#include "write2.mli"
#undef INT
#undef INTLIT
#undef FLOAT
#undef FLOATLIT
#undef STRING
#undef STRINGLIT
#undef TUPLE
#undef VARIANT
Loading

0 comments on commit 1ead566

Please sign in to comment.