From: Andy Chambers
Subject: How does one reverse the effect of set-macro-character?
Date: 
Message-ID: <52f178fd-ba21-417f-8a1a-0ff917566713@z9g2000yqi.googlegroups.com>
I'd like to write a macro

(defmacro with-!-syntax (&body body) ...)

that temporarily introduces the "!" macro character to convert symbols
into urls like in allegrograph.  How can I reverse the effects of
calling set-macro-character?

Cheers,
Andy

From: Barry Margolin
Subject: Re: How does one reverse the effect of set-macro-character?
Date: 
Message-ID: <barmar-F87674.21583030032009@mara100-84.onlink.net>
In article 
<····································@z9g2000yqi.googlegroups.com>,
 Andy Chambers <··············@googlemail.com> wrote:

> I'd like to write a macro
> 
> (defmacro with-!-syntax (&body body) ...)
> 
> that temporarily introduces the "!" macro character to convert symbols
> into urls like in allegrograph.  How can I reverse the effects of
> calling set-macro-character?

Copy the current readtable, call set-macro-character in the new 
readtable, and bind *READTABLE* to that readtable.

To avoid consing a new readtable every time you enter this context, you 
could define a global variable containing the modified readtable.  Your 
macro would simply bind *READTABLE* to this variable.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Pascal J. Bourguignon
Subject: Re: How does one reverse the effect of set-macro-character?
Date: 
Message-ID: <87skku9mc3.fsf@galatea.local>
Andy Chambers <··············@googlemail.com> writes:

> I'd like to write a macro
>
> (defmacro with-!-syntax (&body body) ...)
>
> that temporarily introduces the "!" macro character 

This is not possible.  
Any with-!-syntax form will have been READ long before it is MACROEXPANDED.

Have a look at:
http://www.nhplace.com/kent/PS/Ambitious.html


-- 
__Pascal Bourguignon__
From: Rob Warnock
Subject: Re: How does one reverse the effect of set-macro-character?
Date: 
Message-ID: <fvydnfacB59hUEzUnZ2dnUVZ_vWdnZ2d@speakeasy.net>
Pascal J. Bourguignon <···@informatimago.com> wrote:
+---------------
| Andy Chambers <··············@googlemail.com> writes:
| > I'd like to write a macro
| >  (defmacro with-!-syntax (&body body) ...)
| > that temporarily introduces the "!" macro character 
| 
| This is not possible.  
| Any with-!-syntax form will have been READ long before it is MACROEXPANDED.
+---------------

Darn! I was just about to say that! You beat me to it!!  ;-}

Though one thing he might do -- which doesn't actually involve
a readmacro at all -- is do a tree walk of the BODY and look for
symbols starting with "!" and rewrite *those*, something like the
thread we had a while back about extracting args like "$3" & "$*"
from a lambda body to build the arglist...

+---------------
| Have a look at: http://www.nhplace.com/kent/PS/Ambitious.html
+---------------

Good suggestion! That's definitely worth a read.


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Andy Chambers
Subject: Re: How does one reverse the effect of set-macro-character?
Date: 
Message-ID: <dfb55cfc-b0e2-4f1f-9bd1-2db33a929c9f@k2g2000yql.googlegroups.com>
On Mar 31, 9:02 am, ····@rpw3.org (Rob Warnock) wrote:
> Pascal J. Bourguignon <····@informatimago.com> wrote:
> +---------------
> | Andy Chambers <··············@googlemail.com> writes:
> | > I'd like to write a macro
> | >  (defmacro with-!-syntax (&body body) ...)
> | > that temporarily introduces the "!" macro character
> |
> | This is not possible.  
> | Any with-!-syntax form will have been READ long before it is MACROEXPANDED.
> +---------------
>
> Darn! I was just about to say that! You beat me to it!!  ;-}
>
> Though one thing he might do -- which doesn't actually involve
> a readmacro at all -- is do a tree walk of the BODY and look for
> symbols starting with "!" and rewrite *those*, something like the
> thread we had a while back about extracting args like "$3" & "$*"
> from a lambda body to build the arglist...

Thanks for all the pointers everyone.  The tree-rewrite sounds like
it'll work.  My day job involves a lot of XSLT so it feels like I'm
I'm re-writing trees all the time :-)

I'll need a little more time to digest Kent's paper.

--
Andy
From: Pascal J. Bourguignon
Subject: Re: How does one reverse the effect of set-macro-character?
Date: 
Message-ID: <7czlf2ow97.fsf@pbourguignon.anevia.com>
Andy Chambers <··············@googlemail.com> writes:

> On Mar 31, 9:02�am, ····@rpw3.org (Rob Warnock) wrote:
>> Pascal J. Bourguignon <····@informatimago.com> wrote:
>> +---------------
>> | Andy Chambers <··············@googlemail.com> writes:
>> | > I'd like to write a macro
>> | > �(defmacro with-!-syntax (&body body) ...)
>> | > that temporarily introduces the "!" macro character
>> |
>> | This is not possible. �
>> | Any with-!-syntax form will have been READ long before it is MACROEXPANDED.
>> +---------------
>>
>> Darn! I was just about to say that! You beat me to it!! �;-}
>>
>> Though one thing he might do -- which doesn't actually involve
>> a readmacro at all -- is do a tree walk of the BODY and look for
>> symbols starting with "!" and rewrite *those*, something like the
>> thread we had a while back about extracting args like "$3" & "$*"
>> from a lambda body to build the arglist...
>
> Thanks for all the pointers everyone.  The tree-rewrite sounds like
> it'll work.  My day job involves a lot of XSLT so it feels like I'm
> I'm re-writing trees all the time :-)
>
> I'll need a little more time to digest Kent's paper.

Basically, in CL, you have to distinguish five times:

- read time
- compilation time
- run time
- macro expansion time
- load time

Each kind of action is taken at a given time, and there may be some
overlap, but, and this is the delicate point, these overlaps depend on
the implementation and other factors.

For example, macro expansion may occur during compilation, or during
run time (depending on whether the implementation provides a compiler
or an interpreter).  Some run time may occur during read time
(eg. using #.).  Some compilation may occur during run time.  Etc.

But as mentionned in Kent's paper, in Common Lisp, a form is read
entirely before being passed to the compiler or to eval, so all the
elements needed to read it (readtable including reader macros, current
package,  etc)  must have been defined before starting to read that
form.

-- 
__Pascal Bourguignon__
From: ·····@franz.com
Subject: Re: How does one reverse the effect of set-macro-character?
Date: 
Message-ID: <ab775388-542b-470c-9805-e9a90ad2e17a@f1g2000prb.googlegroups.com>
On Mar 31, 12:35 am, ····@informatimago.com (Pascal J. Bourguignon)
wrote:
> Andy Chambers <··············@googlemail.com> writes:
> > I'd like to write a macro
>
> > (defmacro with-!-syntax (&body body) ...)
>
> > that temporarily introduces the "!" macro character
>
> This is not possible.  
> Any with-!-syntax form will have been READ long before it is MACROEXPANDED.

You're making a big assumption here, and that is that the read which
processed the with-!-syntax form must necessarily be the same read as
will be processing the syntax.  But it's not necessarily true;  for
example:

(with-!-syntax
  (load "foo.lisp"))

will not use the same invocation of read in the process of loading the
file, and the syntax will thus be established for that load operation.

Duane
From: Pascal J. Bourguignon
Subject: Re: How does one reverse the effect of set-macro-character?
Date: 
Message-ID: <874ox9a5vk.fsf@galatea.local>
·····@franz.com writes:

> On Mar 31, 12:35�am, ····@informatimago.com (Pascal J. Bourguignon)
> wrote:
>> Andy Chambers <··············@googlemail.com> writes:
>> > I'd like to write a macro
>>
>> > (defmacro with-!-syntax (&body body) ...)
>>
>> > that temporarily introduces the "!" macro character
>>
>> This is not possible. �
>> Any with-!-syntax form will have been READ long before it is MACROEXPANDED.
>
> You're making a big assumption here, and that is that the read which
> processed the with-!-syntax form must necessarily be the same read as
> will be processing the syntax.  But it's not necessarily true;  for
> example:
>
> (with-!-syntax
>   (load "foo.lisp"))
>
> will not use the same invocation of read in the process of loading the
> file, and the syntax will thus be established for that load operation.

Yes, but wouldn't it be better in this case to export a function named
LOAD-WITH-!-SYNTAX (and perhaps a few others) instead of a misleading
WITH-!-SYNTAX macro?  


-- 
__Pascal Bourguignon__
From: ·····@franz.com
Subject: Re: How does one reverse the effect of set-macro-character?
Date: 
Message-ID: <9b0f5635-b34a-4d8d-abd9-4a1fe1c35410@q30g2000prq.googlegroups.com>
On Mar 31, 11:45 am, ····@informatimago.com (Pascal J. Bourguignon)
wrote:
> ·····@franz.com writes:
> > On Mar 31, 12:35 am, ····@informatimago.com (Pascal J. Bourguignon)
> > wrote:
> >> Andy Chambers <··············@googlemail.com> writes:
> >> > I'd like to write a macro
>
> >> > (defmacro with-!-syntax (&body body) ...)
>
> >> > that temporarily introduces the "!" macro character
>
> >> This is not possible.  
> >> Any with-!-syntax form will have been READ long before it is MACROEXPANDED.
>
> > You're making a big assumption here, and that is that the read which
> > processed the with-!-syntax form must necessarily be the same read as
> > will be processing the syntax.  But it's not necessarily true;  for
> > example:
>
> > (with-!-syntax
> >   (load "foo.lisp"))
>
> > will not use the same invocation of read in the process of loading the
> > file, and the syntax will thus be established for that load operation.
>
> Yes, but wouldn't it be better in this case to export a function named
> LOAD-WITH-!-SYNTAX (and perhaps a few others) instead of a misleading
> WITH-!-SYNTAX macro?  

You miss my point.  Anything that starts a new call to read would be
sufficient.  So no, load-with-!-syntax wouldn't be a generic enough
name (unless the macro was specifically geared toward loading).  But
consider as another example this code

(with-!-syntax
  (break "go for it"))

This of course starts a new call to read, through the top-level's
repl, and would allow the new syntax to be processed.  But now
generalize it to _any_ new call to read, (including an explicit call
to read), and you'll understand my point.

Duane
From: Tobias C. Rittweiler
Subject: Re: How does one reverse the effect of set-macro-character?
Date: 
Message-ID: <87zlf1o6g3.fsf@freebits.de>
···@informatimago.com (Pascal J. Bourguignon) writes:

> ·····@franz.com writes:
>
>> On Mar 31, 12:35�am, ····@informatimago.com (Pascal J. Bourguignon)
>> wrote:
>>> Andy Chambers <··············@googlemail.com> writes:
>>> > I'd like to write a macro
>>>
>>> > (defmacro with-!-syntax (&body body) ...)
>>>
>>> > that temporarily introduces the "!" macro character
>>>
>>> This is not possible. �
>>> Any with-!-syntax form will have been READ long before it is MACROEXPANDED.
>>
>> You're making a big assumption here, and that is that the read which
>> processed the with-!-syntax form must necessarily be the same read as
>> will be processing the syntax.  But it's not necessarily true;  for
>> example:
>>
>> (with-!-syntax
>>   (load "foo.lisp"))
>>
>> will not use the same invocation of read in the process of loading the
>> file, and the syntax will thus be established for that load operation.
>
> Yes, but wouldn't it be better in this case to export a function named
> LOAD-WITH-!-SYNTAX (and perhaps a few others) instead of a misleading
> WITH-!-SYNTAX macro?  

No, other people can use ! for something else. All you need is a library
that "standardizes" what certain read-macros expand to. I haven't come
around publishing one.

So you can, for instance, have WITH-SQL-SYNTAX,
WITH-LIST-COMPREHENSION-SYNTAX, and WITH-ARRAY-SYNTAX; the latter
expanding [*array* 4 2] to (AREF *ARRAY* 4 2).

They would even nest probably, although that'd probably count as
obfuscation. :-)

  -T.

  
From: Tobias C. Rittweiler
Subject: Re: How does one reverse the effect of set-macro-character?
Date: 
Message-ID: <878wml6390.fsf@freebits.de>
Andy Chambers <··············@googlemail.com> writes:

> I'd like to write a macro
>
> (defmacro with-!-syntax (&body body) ...)
>
> that temporarily introduces the "!" macro character to convert symbols
> into urls like in allegrograph.  How can I reverse the effects of
> calling set-macro-character?

Write a global reader-macro which expands !foo to (bang foo), then write
a global macro for BANG which expands to a form signaling an appropriate
out-of-context error, then write WITH-!-SYNTAX to expand to a MACROLET
binding BANG appropriately.

  -T.