Move validation to a `print` function

Signed-off-by: Rudi Grinberg <rudi.grinberg@gmail.com>
This commit is contained in:
Rudi Grinberg 2018-06-20 15:31:18 +06:30
parent a1d714f9d4
commit 8a87b5b5bf
5 changed files with 10 additions and 9 deletions

View File

@ -296,13 +296,13 @@ module Of_sexp = struct
let string = plain_string (fun ~loc:_ x -> x)
let int =
basic "Integer" (fun s ->
match int_of_string (s Atom.Dune) with
match int_of_string s with
| x -> Ok x
| exception _ -> Result.Error ())
let float =
basic "Float" (fun s ->
match float_of_string (s Atom.Dune) with
match float_of_string s with
| x -> Ok x
| exception _ -> Result.Error ())

View File

@ -29,8 +29,9 @@ let is_valid_jbuild str =
not (loop (len - 1))
let of_string s = A s
let to_string (A s) = s
let to_string (A t) syntax =
let print (A t) syntax =
match syntax with
| Jbuild ->
if is_valid_jbuild t then

View File

@ -5,8 +5,9 @@ type syntax = Jbuild | Dune
val is_valid_dune : string -> bool
val of_string : string -> t
val to_string : t -> string
val to_string : t -> syntax -> string
val print : t -> syntax -> string
val of_int : int -> t
val of_float : float -> t

View File

@ -90,13 +90,13 @@ let quoted s =
Bytes.unsafe_to_string s'
let rec to_string = function
| Atom a -> Atom.to_string a Atom.Dune
| Atom a -> Atom.print a Atom.Dune
| Quoted_string s -> quoted s
| List l -> Printf.sprintf "(%s)" (List.map l ~f:to_string |> String.concat ~sep:" ")
let rec pp ppf = function
| Atom s ->
Format.pp_print_string ppf (Atom.to_string s Atom.Dune)
Format.pp_print_string ppf (Atom.print s Atom.Dune)
| Quoted_string s ->
Format.pp_print_string ppf (quoted s)
| List [] ->
@ -137,7 +137,7 @@ let pp_print_quoted_string ppf s =
Format.pp_print_string ppf (quoted s)
let rec pp_split_strings ppf = function
| Atom s -> Format.pp_print_string ppf (Atom.to_string s Atom.Dune)
| Atom s -> Format.pp_print_string ppf (Atom.print s Atom.Dune)
| Quoted_string s -> pp_print_quoted_string ppf s
| List [] ->
Format.pp_print_string ppf "()"

View File

@ -12,8 +12,7 @@ module Atom : sig
val is_valid_dune : string -> bool
val of_string : string -> t
val to_string : t -> syntax -> string
val to_string : t -> string
val of_int : int -> t
val of_float : float -> t