Skip to content

Instantly share code, notes, and snippets.

Last active December 12, 2015 07:38
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bizenn/4737619 to your computer and use it in GitHub Desktop.
Parser for Labeled TSV in Pure Scheme(R5RS).
(define (ltsv-reader in)
(define (rlist->string cl)
(list->string (reverse cl)))
(define (accum-elem cl elem)
(reverse (cons (rlist->string cl) elem)))
(define (make-record rec elem cl)
(reverse (cons (accum-elem cl elem) rec)))
(define (terminate rec elem cl c)
(if (and (null? rec) (null? elem) (null? cl))
c ;; eof
(make-record rec elem cl)))
(define (make-parser terminator cont)
(lambda (in rec elem)
(let loop ((cl '())
(c (read-char in)))
(cond ((eof-object? c) (terminate rec elem cl c))
((char=? c terminator) (cont in rec elem cl))
((char=? c #\newline) (make-record rec elem cl))
((char=? c #\return) (loop cl (read-char in)))
(else (loop (cons c cl) (read-char in)))))))
(define field-label
(make-parser #\: (lambda (in rec elem cl)
(field-value in rec (accum-elem cl elem)))))
(define field-value
(make-parser #\tab (lambda (in rec elem cl)
(field-label in (cons (accum-elem cl elem) rec) '()))))
(field-label in '() '()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment