[racket] Question about syntax splicing and formatting of the resulting code body

From: Danny Yoo (dyoo at hashcollision.org)
Date: Wed Jan 9 13:05:47 EST 2013

On Wed, Jan 9, 2013 at 6:36 AM, Grant Rettke <grettke at acm.org> wrote:

> Hi,
>
> I have the goal of inserting some Jess (http://herzberg.ca.sandia.gov/)
> code inside of a Scribble interaction environment (eventually I will have a
> language to evaluate this but I'm not there yet). Here is the relevant part
> of the code I have to do that:
>
> #lang racket
>
> (define (my-read-syntax src in)
>   (define path-ls (get-path in))
>   (define path (list->string path-ls))
>   (define fis (open-input-file #:mode 'text path))
>   (define datums (let loop ()
>                    (let* ((datum (read fis)))
>                      (cond ((eof-object? datum) empty)
>                            (else (cons datum (loop)))))))
>   (close-input-port fis)
>   (datum->syntax #f datums))
>
>

Hi Grant,

Can you use read-syntax instead of read?  The problem with read is that it
returns plain s-expressions with no location information, and as soon as
we've done that, we've lost.  read-syntax should preserve the locations
that you want.

By the way, there's a nice utility in the port->list function of the
racket/port library that does much of the inner loop in your code above.
 You can also use the port-next-location to see how much into the stream
we've read till we hit eof, just in case that sort of thing is useful.

Here's a revision of the code to demonstrate:

#lang racket/base
(require racket/port)

(define (my-read-syntax src in)
  (define path-ls (get-path in))
  (define path (list->string path-ls))
  (define fis (open-input-file #:mode 'text path))
  (port-count-lines! fis)
  (define-values (start-line start-column start-position)
(port-next-location fis))
  (define datums (port->list (lambda (port) (read-syntax path port)) fis))
  (define-values (end-line end-column end-position) (port-next-location
fis))
  (datum->syntax #f datums (list path start-line start-column
start-position
                                 (if (and (number? start-position)
                                          (number? end-position))
                                     (- end-position start-position)
                                     #f))))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130109/23d77a1b/attachment.html>

Posted on the users mailing list.