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:
- 2020-05-22 A simple MCQ test program, in OCaml, compiled to JavaScript
- 2019-08-30 B-tree random write performance
- 2019-08-21 ML'19 Workshop at ICFP: A key-value store for OCaml
- 2018-07-09 Backup tools
- 2018-06-14 A typed DSL for parsing
- 2018-05-22 First Python program: an Earley parser!
- 2018-02-01 New OCaml library: path resolution
- 2017-11-14 New OCaml parsing algorithm: tjr_simple_earley
- 2017-09-17 Two new OCaml libraries: P0 and tjr-csv
- 2017-03-16 tjr-btree: a CoW B-tree library in OCaml
- 2016-11-17 OCaml string functions
- 2016-02-19 Tree-structured text
- 2016-02-09 Simple implementation of an Earley-like parsing algorithm
- 2015-06-26 P5 scala parsing library
- 2014-12-19 Parsing the IMAP protocol
- 2014-12-04 Parsing examples
- 2014-11-21 Talk on parsing at the University of Sussex
- 2014-09-26 P1 combinator parsing library for OCaml
- 2014-09-26 E3 earley parser library for OCaml
- 2014-09-18 SLE 2014 conference, and Parsing at SLE workshop, slides
- 2014-09-07 ICFP 2014, OCaml workshop, slides and video
- 2014-07-11 P3 paper accepted for SLE 2014
- 2014-04-15 New release of P3 code on github
- 2014-03-02 New release of P3 code on github
- 2013-12-16 New release of P3 code on github
- 2013-12-03 Implementing algorithms efficiently
- 2013-11-24 Experience of using Lem
- 2013-11-08 Talk on parsing and P3 given at Cambridge
- 2011-12-01 Verified parsing