# Parsing with error-handling?

**URL:** <https://discuss.ocaml.org/t/parsing-with-error-handling/14775>\
**Category:** Learning\
**Tags:** angstrom\
**Created:** [June 11, 2024, 6:30pm UTC](https://discuss.ocaml.org/t/parsing-with-error-handling/14775 "2024-06-11T18:30:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![patrick-nicodemus](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/patrick-nicodemus/32/4704_2.png) [@patrick-nicodemus](https://discuss.ocaml.org/u/patrick-nicodemus)\
**Post date:** [June 11, 2024, 6:30pm UTC](https://discuss.ocaml.org/t/parsing-with-error-handling/14775/1 "2024-06-11T18:30:50Z")

</div>

I am using the Angstrom library to write a parser for a markdown language. How do I return an informative error message to the user about the location of a parsing error?

Edit: I want to clarify the problem a bit. A markdown format constitutes a few kinds of special syntactic elements that need to be just-so, properly formatted, because these give the document a tree structure. On the other hand a markdown is mostly just raw text.

There is a naive way of writing a parser here which is something like

```auto
type doc_element = 
| Structured_element of t1 * t2 * t3
| Timestamp of date * time
| Heading of int * string
| Unstructured_raw_paragraph of string 

```

Now I write the parser to look for strings that are of the correct form matching one of these structured elements, something like

```auto
let structured_elt_parser : doc_element Angstrom.t = (...);;
let heading_parser : doc_element Angstrom.t = (...);;
let timestamp_parser : doc_element Angstrom.t = (...);;
let unstructured_parser : doc_element Angstrom.t = (...);
let main_loop = Angstrom.choice [structured_elt_parser, heading_parser, timestamp_parser, unstructured_parser] 
 |> Angstrom.many

```

This design suffers from the flaw that a small syntax error in a structured element causes the parser to fail, then the unstructured parser will always succeed and classify the text as a raw unstructured paragraph.

I guess I am looking for a form of nonlocal control flow where the failure of a low-level parser can cause a higher level parser to fail on the grounds that the document is ill-formatted. I think just raising an exception would be good for my purposes, but I was wondering if there was a way to do something more idiomatic working inside the parsing library without just jumping out of the parser completely.
