-
I'm attempting this year's advent of code with OCaml and Containers. The first challenge requires parsing a list of pairs from a text file formatted like this:
While I could get the bracketless pair parser working, the list parser won't cope with the lack of brackets. let parse_int_pair_list =
CCParse.U.(
list ~start:"" ~stop:"" ~sep:""
(pair ~start:"" ~stop:"" ~sep:"" int int)) So, is it possible to parse bracketless lists with CCParse? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Oh wait, I see why it returns the empty list immediately with those arguments... any hope of getting it to work without resorting to brackets? |
Beta Was this translation helpful? Give feedback.
-
I have this alternative if you like: # module P = CCParse
# let s = "1 2\n3 4\n5 6\n7 8";;
val s : string = "1 2\n3 4\n5 6\n7 8"
# P.parse_string P.(each_line (both (U.int <* many space) U.int)) s;;
- : ((int * int) list, string) result = Ok [(1, 2); (3, 4); (5, 6); (7, 8)] If there are empty lines it's a bit more annoying ( # let s = "1 2\n3 4\n5 6\n7 8\n";;
val s : string = "1 2\n3 4\n5 6\n7 8\n"
# P.parse_string P.(each_line (try_opt @@ both (U.int <* many space) U.int)) s;;
- : ((int * int) option list, string) result =
Ok [Some (1, 2); Some (3, 4); Some (5, 6); Some (7, 8); None] |
Beta Was this translation helpful? Give feedback.
I have this alternative if you like:
If there are empty lines it's a bit more annoying (
each_line
is somewhat rigid) but you can get options and filter later withCCList.keep_some
: