aboutsummaryrefslogtreecommitdiffstats
path: root/lib/scl/parser.ml
blob: 86c3f998d62bb8101c94a6b014674f60ab382a09 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module Error = struct
  open Condition
  let parse_error msg = condition "parse-error" ("message" |=| (of_string msg))
end

let validate = function
  | (Ast.Begin { limit = Some limit; _ } | Ast.Fetch { limit = Some limit; _ })
    when limit <= 0 ->
      Error (Error.parse_error "The provision of `limit` is expected to be positive.")
  | stmt -> Ok stmt

let of_sexp sexp =
  match Ast.statement_of_sexp sexp with
  | exception exn -> Error (Error.parse_error (Printexc.to_string exn))
  | stmt -> validate stmt

let of_string s =
  match Sexplib.Sexp.of_string s |> Ast.statement_of_sexp with
  | exception exn -> Error (Error.parse_error (Printexc.to_string exn))
  | stmt -> validate stmt

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