4
votes

is there a way to quote an invocation of a reader macro? More specifically, I want to create a macro, that once evaluated, will generate a defclass statement and a respective XML file. Is this possible?

I thought about using #.( ... ) reader macros, but I assume the macro parameters aren't bound for use inside the reader macro. Is this correct?

Therefore, my second thought was to try to generate a statement that included the reader macros, but I'm unsure if there is a way to do that.

Any suggestions on generating XML files when expanding a macro invocation?

Thanks in advance for any ideas.

2

2 Answers

1
votes

Something along the lines of:

(defmacro def-wsdl-class (name (&rest supers)
                           (&rest slots)
                           &rest options)
  `(progn
     (eval-when (:compile-toplevel :execute)
       (with-open-file (xml-file (make-pathname :name (string-capitalize name)
                                                :type "wsdl"
                                                :defaults (or *compile-pathname*
                                                              *load-pathname*))
                                 :direction :output
                                 :if-exists ,(getf options :if-wsdl-exists :error))
         (when xml-file
           (spit-xml xml-file ',name ',supers ',slots ,@options))))
    `(defclass ,name (,@supers)
       (,@slots)
       ,@(chew options)))))

EDIT: To answer your original question, you can't generally (back)quote reader macros. They are executed right where the syntax is read, let's call it read-time. Reader macros don't participate in normal macro expansion, they act before macro expansion.

You could probably create a reader macro that knows it's being called inside a backquote reader macro to play with it, but it would require knowing or changing implementation dependent behaviour of the backquote reader macro.

However, you can return backquoted forms from reader macros.

4
votes

A "reader macro" and a "macro" are quite different beasts, used to do very different things.

A "reader macro" is usually a function, to start with. They're bound to one (or a specific sequence of two) characters and change how source code is read. They're not about code, but about object creation.

For a "reader macro", there's no obvious definition of what the "macro parameters" would be (apart, possibly, from the sequence of character(s) that caused the reader macro to be invoked in the first place, useful for, as an example, match a ( to a ) when you read a list).