Last active
December 12, 2015 07:38
-
-
Save bizenn/4737619 to your computer and use it in GitHub Desktop.
Parser for Labeled TSV in Pure Scheme(R5RS).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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