Parsing alternative for more than one char with angstrom

With parsing library angstrom, this :

open Angstrom

let ab = char 'a' <|> char 'b'
parse_string ~consume:All ab "a";;
(*  Ok 'a' *)
parse_string ~consume:All ab "b";;
(*  Ok 'b' *)

works as I expect, but this :

open Angstrom

let ab = (many (char 'a')) <|> many (char 'b'))
parse_string ~consume:All ab "aa";;
(*  Ok ['a'; 'a'] *)
parse_string ~consume:All ab "bb";;
(*  Error "end of input: "*)

doesn’t. I suspect Angstrom cannot backtrack for more than one character, I am not very knowledgeable with parsers.
What would be the simplest solution to achieve what I want to do ?

Thanks in advance

many runs a parser zero or more times, whereas many1 runs a parser one or more times. You probably want the latter.

What happened is the (many (char 'a')) parser succeeded but consumed no input, and then parse_string failed to ~consume:All of its input, so you got an error.

1 Like

That’s it (and I found the solution to my actual problem, by the way, you put me on the right track).
Thanks !

1 Like