[plt-scheme] How to use syntax case?

From: John Clements (clements at brinckerhoff.org)
Date: Mon May 28 16:11:38 EDT 2007

On May 28, 2007, at 1:01 PM, Grant Rettke wrote:

> I'm starting with syntax case from zero. My end goal is to read a file
> and extract a definition by name, for example "define impl1" so that I
> can include it in another document.
> Reading about syntax objects and playing around with the results of
> read-syntax I can see what I want, because when printf it or call
> (syntax-object->datum impl1-stx) on it I see that function:
> (define impl1
>  (λ (max)
>    (let loop ((x 1) (y 2) (sum 0))
>      (let* ((new-x y) (new-y (+ x y)) (new-sum (if (even? y) (+ sum  
> y) sum)))
>        (cond ((> y max) sum) (else (loop new-x new-y new-sum)))))))
> It seems like I can use syntax-case to analyze each syntax object in
> the list read in and try to match on impl1. The only problem is that
> I've got absolutely no idea how to do this.
> Where do I start?

1) You almost certainly want to expand the result of read-syntax, to  
expand away uses of macros (that might turn out to be definitions).

2) If it's at all helpful, the no-brainer tool (cf. planet) uses  
syntax-case in the way you describe, to traverse an expanded  
expression.  It's looking for unused bindings and arity errors, but  
the traversal will be similar.

3) The traversal encapsulated by the no-brainer tool is a bit out-of- 
date; you'll probably want to look at the "Syntax" chapter of the  
MzScheme manual to see the most recent data definition for the  
results of 'expand'.

John Clements

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2223 bytes
Desc: not available
URL: <http://lists.racket-lang.org/users/archive/attachments/20070528/a7802b41/attachment.p7s>

Posted on the users mailing list.