Using a special readtable for a library

Discussion of Common Lisp
Post Reply
dlweinreb
Posts: 41
Joined: Tue Jul 01, 2008 5:11 am
Location: Lexington, MA
Contact:

Using a special readtable for a library

Post by dlweinreb » Sat May 08, 2010 9:11 am

Suppose I want to write a Lisp library, that's in its own package, and uses its own readtable. I want to be able to load it into Lisp without breaking other packages. So I don't want to simply set *readtable*. Surely there mut be some kind of standard/idiomatic way to do this in Common Lisp? (I haven't used special readtables in decades!) Thanks!

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Using a special readtable for a library

Post by ramarren » Sat May 08, 2010 9:58 am

There is named-readtables. I'm not sure how common it is, but then using modified readtables is not exactly common itself.

Also remember that LOAD and COMPILE-FILE bind *readtable*, so changing it inside files will not affect global environment. This is used by, for example, cl-interpol. It will confuse Slime unfortunately.

Suroy
Posts: 46
Joined: Sat Dec 19, 2009 11:20 am

Re: Using a special readtable for a library

Post by Suroy » Sun May 09, 2010 10:28 am

But if you modify the readtable using named-readtables or whatever during development time, it will change every other librarie's readtable, which might not be a problem. Of course, you could always replace the reader mechanism with your own (i.e. a library out there which implements all the functionality of readtable and more, i know of at least one, go to dwim.hu and find hu.dwim.reader under 'project'). You would have to replace the common lisp reader functions, though.

Its actually quite simple: you can determine the package that your reader function is invoked by using the *package* global variable. You can then use a hash table to get a unique readtable structure per package and then get the required function. If no function exists, then we can simple read one character, treating the read character as being nonexistent for this package.

Then you can truly have control over the readtable mechanism, you could actually make readtables per package. :o Oh my :shock:

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Using a special readtable for a library

Post by ramarren » Sun May 09, 2010 10:42 am

Suroy wrote:But if you modify the readtable using named-readtables or whatever during development time, it will change every other librarie's readtable, which might not be a problem.
The point of named-readtables is that it will not. But it really only automates managing readtables. Remeber: there is no the readtable, readtables in Common Lisp are first class objects, so you can create a new one and modify that one without touching those used by other systems.

Suroy
Posts: 46
Joined: Sat Dec 19, 2009 11:20 am

Re: Using a special readtable for a library

Post by Suroy » Sun May 09, 2010 1:24 pm

If i remember playing with named-readtables, i believe that when you type (in-readtable readtable) in the REP, the global *readtable* is replaced. Its true that you can create readtables, but in order for it to be in effect you must set the global readtable.

This makes sense because there is no mechanism for package level readtables and there are no special 'hooks' into creating one. The platform specific nature of named-readtables is rather reading the readtable contents.

However, now that i think about it, i dont think it matters much. I remember running into trouble with evaluating functions in another package which was using another readtable from the package i was in, but i think i was just being stupid :D Too far back...

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Using a special readtable for a library

Post by ramarren » Sun May 09, 2010 1:35 pm

Well, obviously if you change the global environment then the global environment will be changed, much like it would after (in-package ...) in the REPL. You are supposed to use (in-readtable ...) at the beginning of a file, and since, as I have written before, compile/load binds *readtable*, it will not affect the global environment.

Post Reply