[racket] A Macro for Omitting Parentheses
One other obstacle is that the macros are local-rewrites; the $ macro is
supposed to see things within its local vicinity. Let's say that we do
this:
(define-syntax ($ stx)
(syntax-case stx ()
(begin (printf "I see: ~s\n" stx) #'(void))))
This is a small test macro that let's us see what the macro can observe
when it has been activated.
We can run it on the example:
;; A usage of it:
(void ($ "foo" "bar"))
to see that it knows about $ and its immediate arguments foo and bar. But
if we try using it as just a bare identifier:
;; A usage of it:
(void $ "foo" "bar" $ "baz")
note that at most the macro use will only have the bare identifier $ to
work with. It won't have the syntax objects that surround it, as they are
outside of the macro usage's context.
You can work around this limitation by hooking up to the macro
infrastructure at a different point. One way is through procedure
application (#%app) instead.
Here's an example of this technique for a slightly funny example: infix
operators (yet again):
https://github.com/dyoo/infix-syntax-example
This keeps a compile-time table of operators that #%app knows about, so
that when it encounters them, #%app can rewrite the code appropriately.
Since it's #%app that's being activated, it knows about all the operators
and operands being passed to it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20121109/0511ab06/attachment-0001.html>