[plt-scheme] read-syntax/recursive in reader problem

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sun Dec 21 09:07:20 EST 2008

At Sun, 21 Dec 2008 14:56:15 +0100, "Jos Koot" wrote:
> Please help me with the following problem extracted from a larger piece of 
> code.
> I have in .../collects/problem/lang the following two files:
> 
> ;file reader.ss
> (module reader syntax/module-reader scheme
>  #:wrapper1
>  (lambda (t)
>   (parameterize ((current-readtable readtable))
>    (t)))
> 
>  (define read-braces
>   (case-lambda
>    ((ch port src line col pos)
>   #`(lambda #,@(read-syntax/recursive 'problem port #\{)))
>    ((ch port)
>     (cons 'lambda (read/recursive port #\{)))))
> 
>  (define readtable
>   (make-readtable #f #\{
>   'terminating-macro read-braces)))
> 
> and
> 
> ;file test.ss
> #lang problem
> ({x x} 1 2 3) ; to be read as ((lambda x x) 1 2 3)
> ; mark the two braces.
> 
> When I try to run test.ss it keeps eating memory in the compilation phase 
> until memory limits are exceeded (700Mb) DrScheme's run icon remains standing 
> still. I guess the reader is getting into an infinite regression, but I fail 
> to see how to avoid that problem.

You're right that parsing #\{ again just re-invokes your reader.

To say that you mean to parse #\{ as with the default reader, just add
an extra #f argument to your `read[-syntax]/recursive' calls:

 (define read-braces
  (case-lambda
   ((ch port src line col pos)
  #`(lambda #,@(read-syntax/recursive 'problem port #\{ #f)))
   ((ch port)
    (cons 'lambda (read/recursive port #\{ #f)))))

Also, the syntax case is't quite right. When you use `quasisyntax',
then the `lambda' ends up with lexical information, but a reader should
produce syntax objects with no lexical information. I recommend using
`datym->syntax', instead:

 (define read-braces
  (case-lambda
   ((ch port src line col pos)
    (datum->syntax #f `(lambda
                         . ,(read-syntax/recursive 'problem port #\{ #f)))
   ((ch port)
    (cons 'lambda (read/recursive port #\{ #f)))))




Posted on the users mailing list.