Commits

Sebastien Mondet committed 1cac43e

lib: add function `parse_string`

Comments (0)

Files changed (1)

sexp_parser_edsl.ml

     syntax_error:(location -> [> syntax_error] -> 'error) ->
     ('dsl, 'error) t -> Sexplib.Sexp.Annotated.t -> ('dsl, 'error) result
 
+  val parse_string:
+    syntax_error:(location -> [> syntax_error | `sexp of exn ] -> 'error) ->
+    ('dsl, 'error) t -> string -> ('dsl, 'error) result
+
 end
 
 (**
     in
     go grammar sexp
 
+  let parse_string ~syntax_error grammar sexp =
+    (
+      let open Pre_sexp in
+      try `Ok (Sexp.Annotated.of_string sexp)
+      with
+      | Parse_error {parse_state; _} as e ->
+        let pos =
+          match parse_state with
+          | `Annot s -> s.parse_pos
+          | `Sexp s -> s.parse_pos
+        in
+        let location =
+          location ()
+            ~from_line:pos.Parse_pos.text_line
+            ~from_character:pos.Parse_pos.text_char
+            ~to_line:pos.Parse_pos.text_line
+            ~to_character:pos.Parse_pos.text_char in
+        `Error (syntax_error location (`sexp e))
+      | e -> `Error (syntax_error (location ()) (`sexp e)))
+    >>= fun sexp ->
+    parse ~syntax_error grammar sexp
+
 end