[plt-scheme] syntax-parse literals

From: Ryan Culpepper (ryanc at ccs.neu.edu)
Date: Mon Oct 19 17:19:30 EDT 2009

On Oct 19, 2009, at 2:13 PM, Jon Rafkind wrote:

> The set of literals that can be used in syntax-parse have to be  
> defined which quickly leads me to a situation where I have duplicate  
> identifiers. Is there some way to tell syntax-parse an identifier is  
> a literal without defining it?

Not a literal per se, but you can use the following syntax class and  
pass it the identifier you want to match.

(define-syntax-class (literal-like lit)
   (pattern x:id #:when (free-identifier=? #'x lit)))

> In general it seems like a bad idea to have to define all literals  
> as it pollutes the namespace.

On the contrary, it's a bad idea not to. Literals generally indicate,  
or "tag", a special subform, like an 'else' branch of a conditional or  
a 'public' method declaration. These are entities in their own right.  
Giving them their own bindings lets them be renamable and  
documentable. The convention of defining these keywords as error- 
raising macros also effectively removes them from the domain of  
expressions, so your macros can retain uniform behavior on expressions.

If you want lightweight "tags", use keywords (#:blah) instead. Or use  
symbolic comparison (and the 'atom-in-list' syntax class, if you like).

Ryan

> This code gives a duplicate identifier for 'a' and 'b'.
>
> (define (test2)
> (define-syntax mymacro
>   (syntax-rules ()
>     [(_ (literal ...)
>         (name pattern ...)
>         template)
>      (begin
>        (define-syntax literal (lambda (stx) (error 'literal "dont  
> use this"))) ...
>        (define-syntax (name stx)
>          (syntax-parse stx
>                        #:literals (literal ...)
>                        [(_ pattern ...) #'template])))]))
> (mymacro (a b) (foo a b) 1)
> (mymacro (a b) (bar a b) 2)
> (foo a b))
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.