<br><br><div class="gmail_quote">On Wed, Jan 9, 2013 at 6:36 AM, Grant Rettke <span dir="ltr"><<a href="mailto:grettke@acm.org" target="_blank">grettke@acm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi,<div><br></div><div>I have the goal of inserting some Jess (<a href="http://herzberg.ca.sandia.gov/" target="_blank">http://herzberg.ca.sandia.gov/</a>) 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:</div>
<div><br></div><div><div>#lang racket</div><div><br></div><div>(define (my-read-syntax src in)</div><div> (define path-ls (get-path in))</div><div> (define path (list->string path-ls))</div><div> (define fis (open-input-file #:mode 'text path))</div>
<div> (define datums (let loop ()</div><div> (let* ((datum (read fis)))</div><div> (cond ((eof-object? datum) empty)</div><div> (else (cons datum (loop)))))))</div>
<div> (close-input-port fis)</div><div> (datum->syntax #f datums))</div><div><br></div></div></blockquote><div><br></div><div><br></div><div>Hi Grant,</div><div><br></div><div>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.</div>
<div><br></div><div>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.</div>
<div><br></div><div>Here's a revision of the code to demonstrate:</div><div><br></div><div><div>#lang racket/base</div><div>(require racket/port)</div><div><br></div><div>(define (my-read-syntax src in)</div><div> (define path-ls (get-path in))</div>
<div> (define path (list->string path-ls))</div><div> (define fis (open-input-file #:mode 'text path))</div><div> (port-count-lines! fis)</div><div> (define-values (start-line start-column start-position) (port-next-location fis))</div>
<div> (define datums (port->list (lambda (port) (read-syntax path port)) fis))</div><div> (define-values (end-line end-column end-position) (port-next-location fis))</div><div> (datum->syntax #f datums (list path start-line start-column start-position </div>
<div> (if (and (number? start-position)</div><div> (number? end-position))</div><div> (- end-position start-position)</div>
<div> #f))))</div></div><div><br></div></div>