chickadee » csv » make-parser

make-parser CSV-INSTANCEprocedure

Once applied to an instance of the <CSV> typeclass, make-parser returns a constructor for the CSV parsing procedure. Optional argument DELIMITER specifies the field delimiter (comma by default). DELIMITER can be a character, or an SRFI-14 character set. The returned procedure takes in an input stream and returns a list of the form:

 ((<#csv-record (FIELD1 FIELD2 ...)>) (<#csv-record ... >))

where FIELD represents the field values in a record.

The following example illustrates the creation of an instance of <CSV> specialized for character lists.

(require-extension typeclass input-classes abnf csv)

(define char-list-<Input>
  (make-<Input> null? car cdr))

(define char-list-<Token>
  (Input->Token char-list-<Input>))

(define char-list-<CharLex>
  (Token->CharLex char-list-<Token>))

(define char-list-<CoreABNF>
  (CharLex->CoreABNF char-list-<CharLex>))

(define char-list-<CSV>
  (CoreABNF->CSV char-list-<CoreABNF> ))

(define parse-csv ((make-parser char-list-<CSV>) #\|))

(parse-csv (string->list "a|b|c"))

(map csv-record->list (parse-csv (string->list "a|b|c")))

 ==> (("a" "b" "c"))