aboutsummaryrefslogtreecommitdiffstats
path: root/lib/dcl/parser.ml
blob: ef64924a8afdf0fa73d4f70120f8d3b5f18b9013 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
type error = ParseError of string

let of_sexp sexp =
  (* ppx_sexp_conv encodes no-argument variants as bare atoms, so
     unwrap a single-element list (e.g. (GetHead)) → atom (GetHead). *)
  let sexp = match sexp with
    | Sexplib.Sexp.List [atom] -> atom
    | other -> other
  in
  match Ast.statement_of_sexp sexp with
  | stmt          -> Ok stmt
  | exception exn -> Error (ParseError (Printexc.to_string exn))

let of_string s =
  of_sexp (Sexplib.Sexp.of_string s)

let to_string stmt =
  Ast.sexp_of_statement stmt |> Sexplib.Sexp.to_string_hum