So a kind of pretty-printing for at-expressions?

I'm not aware of an existing procedure to do this.

It has some interesting wrinkles. Following is my "Greenspun's Tenth
Rule" version. (An ad hoc, informally-specified, bug-ridden, slow
implementation of half of what's probably needed.)

I've commented some deficiencies, I suspect there are many more.

#lang racket

;; IMPROVE-ME: Make something usable as a pretty-print-handler
(define (print/at-exp stx-or-sepxr)
  (match stx-or-sepxr
    [(? syntax? stx) (print/at-exp (syntax->datum stx))]
    [(list (? symbol?          f)
           (? (negate string?) xs) ...
           (? string?          ss) ...
                               ys  ...)
     (display #\@)
     (display f)
     (unless (empty? xs)
       (display #\[) (display (string-join (map ~a xs) " ")) (display #\]))
     (unless (and (empty? ss) (empty? ys))
       (display #\{)
       (for ([v (in-list (append ss ys))])
         (cond [(string? v) (display v)]
               ;; FIXME: Encode @ as @"@"
               ;; IMPROVE-ME: Surround in pipe chars only when necessary
               ;; IMPROVE-ME: Do line-wrapping at column N.
               [else (display "@|")
                     (display v)
                     (display "|")]))
       (display #\}))]
    [(list (? symbol? f) vs ...)
     (display #\@)
     (display f)
     (display #\[) (display (string-join (map ~v vs) " ")) (display #\])]
    [v (print v)]))

(print/at-exp #'(foo 1 2)) (newline)
(print/at-exp #'(foo 1 2 "a")) (newline)
(print/at-exp #'(foo 1 2 "a b")) (newline)
(print/at-exp #'(foo 1 2 "a b")) (newline)
(print/at-exp #'(foo "a")) (newline)
(print/at-exp #'(foo "a" "b")) (newline)
(print/at-exp #'(foo "a " "b")) (newline)
(print/at-exp #'(foo "a is " a " and b is " b)) (newline)
(print/at-exp #'(foo "a" "b" 1 2)) (newline)
(print/at-exp #'(foo "The " (code "x") " argument.")) (newline)


Output:

@foo[1 2]
@foo[1 2]{a}
@foo[1 2]{a b}
@foo[1 2]{a b}
@foo{a}
@foo{ab}
@foo{a b}
@foo{a is @|a| and b is @|b|}
@foo{ab@|1|@|2|}
@foo{The @|(code x)| argument.}

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to