From 1ead566a95c62b3e560537a591ae13d3cf917bc5 Mon Sep 17 00:00:00 2001 From: Marek Kubica Date: Mon, 17 Jul 2023 15:13:03 +0200 Subject: [PATCH] Move `Safe`, `Basic` and `Raw` into their own modules --- .ocamlformat-ignore | 2 +- lib/basic.cppo.ml | 19 ++++++ lib/basic.cppo.mli | 25 ++++++++ lib/dune | 97 +++++++++++++++++++++++++++-- lib/raw.cppo.ml | 23 +++++++ lib/raw.cppo.mli | 27 +++++++++ lib/safe.cppo.ml | 26 ++++++++ lib/safe.cppo.mli | 34 +++++++++++ lib/safe_convert.ml | 2 +- lib/safe_convert.mli | 2 +- lib/t.cppo.ml | 25 ++++++++ lib/t.cppo.mli | 20 ++++++ lib/yojson.cppo.ml | 106 -------------------------------- lib/yojson.cppo.mli | 141 ------------------------------------------- lib/yojson.ml | 7 +++ lib/yojson.mli | 31 ++++++++++ 16 files changed, 333 insertions(+), 254 deletions(-) create mode 100644 lib/basic.cppo.ml create mode 100644 lib/basic.cppo.mli create mode 100644 lib/raw.cppo.ml create mode 100644 lib/raw.cppo.mli create mode 100644 lib/safe.cppo.ml create mode 100644 lib/safe.cppo.mli create mode 100644 lib/t.cppo.ml create mode 100644 lib/t.cppo.mli delete mode 100644 lib/yojson.cppo.ml delete mode 100644 lib/yojson.cppo.mli create mode 100644 lib/yojson.ml create mode 100644 lib/yojson.mli diff --git a/.ocamlformat-ignore b/.ocamlformat-ignore index 8f26d613..64baf17b 100644 --- a/.ocamlformat-ignore +++ b/.ocamlformat-ignore @@ -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 diff --git a/lib/basic.cppo.ml b/lib/basic.cppo.ml new file mode 100644 index 00000000..4c67a662 --- /dev/null +++ b/lib/basic.cppo.ml @@ -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 diff --git a/lib/basic.cppo.mli b/lib/basic.cppo.mli new file mode 100644 index 00000000..d81b97d6 --- /dev/null +++ b/lib/basic.cppo.mli @@ -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 diff --git a/lib/dune b/lib/dune index 73abd919..f5413b2b 100644 --- a/lib/dune +++ b/lib/dune @@ -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 @@ -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 diff --git a/lib/raw.cppo.ml b/lib/raw.cppo.ml new file mode 100644 index 00000000..749c90e5 --- /dev/null +++ b/lib/raw.cppo.ml @@ -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 diff --git a/lib/raw.cppo.mli b/lib/raw.cppo.mli new file mode 100644 index 00000000..4edce088 --- /dev/null +++ b/lib/raw.cppo.mli @@ -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 diff --git a/lib/safe.cppo.ml b/lib/safe.cppo.ml new file mode 100644 index 00000000..9aef4dbd --- /dev/null +++ b/lib/safe.cppo.ml @@ -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 diff --git a/lib/safe.cppo.mli b/lib/safe.cppo.mli new file mode 100644 index 00000000..1b3df859 --- /dev/null +++ b/lib/safe.cppo.mli @@ -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 diff --git a/lib/safe_convert.ml b/lib/safe_convert.ml index dabcfefd..78186863 100644 --- a/lib/safe_convert.ml +++ b/lib/safe_convert.ml @@ -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)) diff --git a/lib/safe_convert.mli b/lib/safe_convert.mli index 3f06099a..723ac999 100644 --- a/lib/safe_convert.mli +++ b/lib/safe_convert.mli @@ -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) diff --git a/lib/t.cppo.ml b/lib/t.cppo.ml new file mode 100644 index 00000000..182a0f0b --- /dev/null +++ b/lib/t.cppo.ml @@ -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 + diff --git a/lib/t.cppo.mli b/lib/t.cppo.mli new file mode 100644 index 00000000..cfd4fc53 --- /dev/null +++ b/lib/t.cppo.mli @@ -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 diff --git a/lib/yojson.cppo.ml b/lib/yojson.cppo.ml deleted file mode 100644 index b38a2f11..00000000 --- a/lib/yojson.cppo.ml +++ /dev/null @@ -1,106 +0,0 @@ -#include "common.ml" - -#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 - -module Basic = -struct -#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 -end - -module Safe = -struct -#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 -end - -module Raw = -struct -#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 -end diff --git a/lib/yojson.cppo.mli b/lib/yojson.cppo.mli deleted file mode 100644 index 1f63152a..00000000 --- a/lib/yojson.cppo.mli +++ /dev/null @@ -1,141 +0,0 @@ -(** - The Yojson library provides several types for representing JSON values, with different use cases. - - - The {{!basic}Basic} JSON type, - - The {{!safe}Safe} JSON type, a superset of JSON with safer support for integers, - - The {{!raw}Raw} JSON type, a superset of JSON, safer but less integrated with OCaml types. - -Each of these different types have their own module. - -*) - -(** {1 Shared types and functions} *) - -#include "common.mli" - -(** {1:basic Basic JSON tree type} *) - -module Basic : -sig -(** - 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 -end - -(** {1:safe Multipurpose JSON tree type} *) - -module Safe : -sig -(** - 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 -end - -(** {1 JSON tree type with literal int/float/string leaves} *) - -module Raw : -sig -(** - 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 -end - -(** {1:raw Supertype of all JSON tree types} *) - -#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 diff --git a/lib/yojson.ml b/lib/yojson.ml new file mode 100644 index 00000000..8d7792d7 --- /dev/null +++ b/lib/yojson.ml @@ -0,0 +1,7 @@ +include Common + +include T + +module Basic = Basic +module Safe = Safe +module Raw = Raw diff --git a/lib/yojson.mli b/lib/yojson.mli new file mode 100644 index 00000000..338ec28d --- /dev/null +++ b/lib/yojson.mli @@ -0,0 +1,31 @@ +(** + The Yojson library provides several types for representing JSON values, with different use cases. + + - The {{!basic}Basic} JSON type, + - The {{!safe}Safe} JSON type, a superset of JSON with safer support for integers, + - The {{!raw}Raw} JSON type, a superset of JSON, safer but less integrated with OCaml types. + +Each of these different types have their own module. + +*) + +(** {1 Shared types and functions} *) + +(* include Common *) + +(* include T *) + +(** {1:basic Basic JSON tree type} *) + +module Basic = Basic + +(** {1:safe Multipurpose JSON tree type} *) + +module Safe = Safe + +(** {1 JSON tree type with literal int/float/string leaves} *) + +module Raw = Raw + +(** {1:raw Supertype of all JSON tree types} *) +