Date: 2017-09-17
Categories: ocaml; software; parsing

Two new OCaml libraries: P0 and tjr-csv

I wrote these some time ago, but just realized it was maybe worth writing a blog post.

P0

A very simple monadic parsing library. https://github.com/tomjridge/p0

tjr-csv

Parse and pretty print CSV. Allows changing separator etc. Uses P0. https://github.com/tomjridge/tjr_csv

The entire contents of the library (not the executables) is as follows. I think this is a particularly nice specification of CSV.

open P0

(* csv -------------------------------------------------------------- *)

let dq = "\""
let comma = ","
let eol = "\n"
let rec inside sofar s = (
  upto_a dq -- a dq |>> fun (x,_) ->
  (opt (a dq) |>> function
    | None -> return (`Quoted (sofar^x))
    | Some _ -> inside (sofar^x^dq))) s
let quoted = (a dq -- inside "") |>> fun (_,x) -> return x
let unquoted_terminators = ("["^comma^dq^eol^"]")
(* NOTE the following will parse an empty line as an unquoted *)
let unquoted s = (
  upto_re unquoted_terminators |>> fun x -> return (`Unquoted x)) s
let field = quoted || unquoted
let row = plus ~sep:(a comma) field  (* see unquoted || (a"" |>> fun _ -> return []) *)
let rows = star ~sep:(a eol) row


Related posts: