98 lines
2.2 KiB
Plaintext
98 lines
2.2 KiB
Plaintext
(* -*- tuareg -*- *)
|
|
open Stdune;;
|
|
open Sexp.Of_sexp;;
|
|
|
|
let pp_sexp_ast ppf sexp =
|
|
Sexp.pp ppf (Sexp.Ast.remove_locs sexp)
|
|
;;
|
|
#install_printer pp_sexp_ast;;
|
|
[%%expect{|
|
|
val pp_sexp_ast : Format.formatter -> Stdune.Sexp.Ast.t -> unit = <fun>
|
|
|}]
|
|
|
|
Printexc.record_backtrace false;;
|
|
[%%expect{|
|
|
- : unit = ()
|
|
|}]
|
|
|
|
let sexp = Sexp.parse_string ~fname:"" ~mode:Single {|
|
|
((foo 1)
|
|
(foo 2))
|
|
|}
|
|
[%%expect{|
|
|
val sexp : Usexp.Ast.t = ((foo 1) (foo 2))
|
|
|}]
|
|
|
|
let of_sexp = record (field "foo" int)
|
|
let x = of_sexp sexp
|
|
[%%expect{|
|
|
val of_sexp : int Stdune.Sexp.Of_sexp.t = <fun>
|
|
Exception:
|
|
Stdune__Sexp.Of_sexp.Of_sexp (<abstr>,
|
|
"Field \"foo\" is present too many times", None).
|
|
|}]
|
|
|
|
let of_sexp = record (dup_field "foo" int)
|
|
let x = of_sexp sexp
|
|
[%%expect{|
|
|
val of_sexp : int list Stdune.Sexp.Of_sexp.t = <fun>
|
|
val x : int list = [1; 2]
|
|
|}]
|
|
|
|
type parse_result_diff =
|
|
{ jbuild : (Sexp.Ast.t list, string) result
|
|
; dune : (Sexp.Ast.t list, string) result
|
|
}
|
|
|
|
type parse_result =
|
|
| Same of (Sexp.Ast.t list, string) result
|
|
| Different of parse_result_diff
|
|
|
|
let parse s =
|
|
let f ~lexer =
|
|
try
|
|
Ok (Sexp.parse_string ~fname:"" ~mode:Many ~lexer s)
|
|
with Sexp.Parse_error e ->
|
|
Error (Sexp.Parse_error.message e)
|
|
in
|
|
let jbuild = f ~lexer:Sexp.Lexer.jbuild_token in
|
|
let dune = f ~lexer:Sexp.Lexer.token in
|
|
if jbuild <> dune then
|
|
Different { jbuild; dune }
|
|
else
|
|
Same jbuild
|
|
[%%expect{|
|
|
type parse_result_diff = {
|
|
jbuild : (Stdune.Sexp.Ast.t list, string) Stdune.result;
|
|
dune : (Stdune.Sexp.Ast.t list, string) Stdune.result;
|
|
}
|
|
type parse_result =
|
|
Same of (Stdune.Sexp.Ast.t list, string) Stdune.result
|
|
| Different of parse_result_diff
|
|
val parse : string -> parse_result = <fun>
|
|
|}]
|
|
|
|
parse {| # ## x##y x||y a#b|c#d copy# |}
|
|
[%%expect{|
|
|
- : parse_result = Same (Ok [#; ##; x##y; x||y; a#b|c#d; copy#])
|
|
|}]
|
|
|
|
|
|
parse {|x #| comment |# y|}
|
|
[%%expect{|
|
|
- : parse_result =
|
|
Different {jbuild = Ok [x; y]; dune = Ok [x; #|; comment; |#; y]}
|
|
|}]
|
|
|
|
parse {|x#|y|}
|
|
[%%expect{|
|
|
- : parse_result =
|
|
Different {jbuild = Error "jbuild_atoms cannot contain #|"; dune = Ok [x#|y]}
|
|
|}]
|
|
|
|
parse {|x|#y|}
|
|
[%%expect{|
|
|
- : parse_result =
|
|
Different {jbuild = Error "jbuild_atoms cannot contain |#"; dune = Ok [x|#y]}
|
|
|}]
|