[racket] syntax-parse form at runtime
On 08/28/2013 04:59 PM, antoine wrote:
> Hello,
>
> I would like play with macros, and for keep it as simple as possible i
> decided
> to work with macro at runtime, i just pass the arguments encapsulated in a
> syntax object.
>
> But with the code under i get :
>
> test.rkt:8:16: syntax-parse: literal is unbound in phase 0 (phase 0
> relative to the enclosing module)
> at: abc
> in: (syntax-parse stx #:literals (abc) ((abc elem:id ...+) (syntax 1)))
>
> Could you help me to decypher it ?
syntax-parse (unlike syntax-rules and syntax-case) considers it an error
to specify a literal that has no binding. You can read my thoughts on
the subject here:
http://macrologist.blogspot.com/2011/09/macros-and-literals.html
http://macrologist.blogspot.com/2011/09/syntax-parse-and-literals.html
There are two ways to fix your code.
One way is to just add a definition of abc, any definition. If abc
doesn't have any meaning by itself, the usual idiom is a macro
definition that always raises an error:
(define-syntax abc
(lambda (stx)
(raise-syntax-error #f "not used as part of a parse-a form" stx)))
Then your macro/function will recognize identifiers that refer to that
binding of abc. Which means, for example, that if you want code in other
modules to use your macro, you need to provide abc.
The other way is to change your code to use #:datum-literal or ~datum to
recognize the identifier symbolically. But in that case, maybe a keyword
(eg #:abc) would be better.
Ryan
> I far as i understand it seems the '#:literals (abc)' put this into a
> binding
> 'literal' which is not available at runtime.
>
> Thank you.
>
> ;;----test.rkt----
> #lang racket
>
> (require
> syntax/parse)
>
> (define (parse-a stx)
> (syntax-parse stx
> #:literals (abc)
> [(abc elem:id ...+)
> #'1]))
>
> (parse-a #'(abc a b c))
> ;;----test.rkt----
>
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users