Skip to content

Instantly share code, notes, and snippets.

@blippy
Created August 6, 2018 15:38
Show Gist options
  • Select an option

  • Save blippy/2137e9f14e38aca48db44d74b0df8941 to your computer and use it in GitHub Desktop.

Select an option

Save blippy/2137e9f14e38aca48db44d74b0df8941 to your computer and use it in GitHub Desktop.
Create tab-separated fields
#lang racket
(require (planet dyoo/while-loop:1:=1)) ; while / break / continue
(require data/queue)
(define-syntax-rule (defq var queuer)
(begin
(define var (make-queue))
(define (queuer lst) (enqueue! var lst))))
(define (build sp members)
(define token (open-output-string))
(while #t
(define c (read-char sp))
(when (eof-object? c) (break))
(when (member c members) (break))
(display c token))
(define res (get-output-string token))
(close-output-port token)
res)
(define (parse-line line)
(define sp (open-input-string line))
(defq fields f+)
(define (enq members) (f+ (build sp members)))
(while #t
(define c (peek-char sp))
(when (eof-object? c) (break))
(case c
[(#\space #\tab) (read-char sp)]
[(#\#) (break)]
[(#\") (read-char sp) (enq '(#\"))] ; string
[else (enq '(#\space #\tab #\#))])) ; word
(close-input-port sp)
(queue->list fields))
@blippy

blippy commented Aug 6, 2018

Copy link
Copy Markdown
Author

Behaves somewhat like my tsv utility.

Save the file as something like tsv.rkt .

Example usage:
(parse-line "foo \"hello world\" # ignored comment")
returns
'("foo" "hello world")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment