[racket] bad syntax; literal data is not allowed, because no #%datum syntax transformer is bound

From: Neil Van Dyke (neil at neilvandyke.org)
Date: Wed Aug 31 04:17:17 EDT 2011

Matthew Flatt wrote at 08/30/2011 07:13 PM:
> At Tue, 30 Aug 2011 19:03:24 -0400, Neil Van Dyke wrote:
>   
>> Thanks, Carl and Robby.  I need to set up the lexical context to be that 
>> of the macro, while preserving the source position info.  I will look 
>> into the pointers you gave me for how to do that.
>>     
>
> One more pointer: `replace-context' from `syntax/strip-context'.
>   

"replace-context" was exactly what I needed.  Thank you, PLTers.

Working code pasted below in case anyone Googles this in the future.

Aside... Once Carl and Robby pointed out what I was missing, my summary 
was a close match for the documentation of "replace-context".  Perhaps 
some grad student wants to see if there's anything that can be done with 
IR and NLP here, for finding library routines or even composing small 
expressions with them.  Use natural language, and possibly the context 
of code in a DrScheme window.  Something that does better than Google 
could, but short of handling "Computer, write me a program that..."


#lang racket/base

(require (for-syntax racket/base
                     syntax/strip-context))
 
(define-syntax (my-macro stx)
  (syntax-case stx ()
    ((_ NAME)
     (let ((my-read-stx (read-syntax "my-sourcename"
                                     (open-input-string "\"hello\""))))
       (quasisyntax/loc stx
         (string-append "Well, "
                        #,(replace-context stx my-read-stx)
                        ", "
                        NAME
                        "!"))))))

(my-macro "yourname")
;;==> "Well, hello, yourname!"


-- 
http://www.neilvandyke.org/


Posted on the users mailing list.