> On Jun 11, 2016, at 10:26 PM, Matthew Butterick <m...@mbtype.com> wrote:
> 
> This seems like something that has been done before, but I'm not sure where 
> to look for it:
> 
> I need something like `syntax->datum` — call it `syntax->source` — that uses 
> the source locations and 'paren-shape property of the syntax to reconstitute 
> the source with its whitespace (I suppose comments would be unrecoverable, 
> but that's OK)

The syntax->string function from syntax/to-string formats a syntax object with 
its whitespace and indentation, but doesn't look at the paren-shapes:
http://docs.racket-lang.org/syntax/syntax-helpers.html#%28part._to-string%29

> (require syntax/to-string)
> (syntax->string
   #'((define (f x)
        (for/list ([i (in-range x)])
          (+ (f i) x)))))
"(define (f x)\n  (for/list ((i (in-range x)))\n    (+ (f i) x)))"
> (display
   (syntax->string
    #'((define (f x)
         (for/list ([i (in-range x)])
           (+ (f i) x))))))
(define (f x)
  (for/list ((i (in-range x)))
    (+ (f i) x)))
> (display
   (syntax->string
    #'((define (    f  x )
         (for/list (     [ i (in-range x) ]    )
                (   +
                   (f i)
        x))))))
(define (    f  x)
  (for/list (     ( i (in-range x)))
         (   +
            (f i)
 x)))

It doesn't seem to handle whitespace before closing parens, but that makes 
sense, since there isn't any closing paren source location for it to look at.

> `typeset-code` in Scribble is close, but takes strings as input, not syntax 
> objects.
> 
> `chunk` in `scribble/lp2` is close, but AFAICT demands cooperation from 
> `#lang scribble/lp2`. 
> 
> -- 
> 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.

-- 
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