Lisp HUG Maillist Archive

Contextual reader macros

This is a CL question - not specific to LW - but, I find this group much better to interface with than c.l.l (although I know it is a subset of c.l.l), so I hope no one minds...

I'm wondering if it's possible to have a contextual reader macro?

Here's my specific use-case.

Over time, I've been adding more features to my LEXER package for LW (https://github.com/massung/lexer), and I have a code block I'd like to improve (if possible)... specifically the WITH-RE-MATCH macro found here: https://github.com/massung/lexer/blob/master/lexer.lisp#L182.

In particular, the macro assumes a finite number of sub-captures in the match. And while this is fine for most cases, what I'd prefer is a macro where the user could use any symbol like $24 if they wanted.

The only way I can think to do this is with a reader macro for #\$ that turns into the appropriate AREF call (note: currently the captures are in list form for destructuring-bind, but could easily be a vector if I can get this to work).

However, I would only want this reader macro to within the context of the WITH-RE-MATCH macro. But, I can't see that as being possible since the reader has already done its work before calling the macro...

...or is there a trick I'm missing?

Or perhaps there's a better way for me to do what I want that someone can shed some light on?

Thanks for any insights!

Jeff M.

Re: Contextual reader macros

Hi,

Maybe named readtables gives you what you need. Check http://common-lisp.net/project/editor-hints/darcs/named-readtables/doc/named-readtables.html - it's also in quicklisp, I believe.

Pascal

On 17 Feb 2013, at 15:13, Jeff Massung <massung@gmail.com> wrote:

This is a CL question - not specific to LW - but, I find this group much better to interface with than c.l.l (although I know it is a subset of c.l.l), so I hope no one minds...

I'm wondering if it's possible to have a contextual reader macro?

Here's my specific use-case.

Over time, I've been adding more features to my LEXER package for LW (https://github.com/massung/lexer), and I have a code block I'd like to improve (if possible)... specifically the WITH-RE-MATCH macro found here: https://github.com/massung/lexer/blob/master/lexer.lisp#L182.

In particular, the macro assumes a finite number of sub-captures in the match. And while this is fine for most cases, what I'd prefer is a macro where the user could use any symbol like $24 if they wanted.

The only way I can think to do this is with a reader macro for #\$ that turns into the appropriate AREF call (note: currently the captures are in list form for destructuring-bind, but could easily be a vector if I can get this to work).

However, I would only want this reader macro to within the context of the WITH-RE-MATCH macro. But, I can't see that as being possible since the reader has already done its work before calling the macro...

...or is there a trick I'm missing?

Or perhaps there's a better way for me to do what I want that someone can shed some light on?

Thanks for any insights!

Jeff M.

--
Pascal Costanza



Re: Contextual reader macros

Jeff Massung <massung@gmail.com> writes:

> ...or is there a trick I'm missing?

Read this: http://www.nhplace.com/kent/PS/Ambitious.html
and ask again.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.

_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html


Re: Contextual reader macros

I think list might be interested too:
http://code.google.com/p/def-symbol-readmacro/wiki/InstallationInEnglish

_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html


RE: Contextual reader macros

Would it be possible to let the user specify the symbols to use:

 

(defmacro with-re-match ((v match &rest symbols) &body body)…

 

 

If so, you could even dispense with v and the dollars do

(defmacro with-re-match (((&rest symbols) match) &body body) …

 

Using like

 

(with-re-match ((a b) (find-re #/{([^}]+)}/ "this {is a} test"))

            (print a)

            (print b))

"{is a}"

"is a"

"is a"

T

 

 

Eivind

 

 

From: owner-lisp-hug@lispworks.com [mailto:owner-lisp-hug@lispworks.com] On Behalf Of Jeff Massung
Sent: 17. februar 2013 15:14
To: Lisp Hug Lispworks
Subject: Contextual reader macros

 

This is a CL question - not specific to LW - but, I find this group much better to interface with than c.l.l (although I know it is a subset of c.l.l), so I hope no one minds...

 

I'm wondering if it's possible to have a contextual reader macro?

 

Here's my specific use-case.

 

Over time, I've been adding more features to my LEXER package for LW (https://github.com/massung/lexer), and I have a code block I'd like to improve (if possible)... specifically the WITH-RE-MATCH macro found here: https://github.com/massung/lexer/blob/master/lexer.lisp#L182.

 

In particular, the macro assumes a finite number of sub-captures in the match. And while this is fine for most cases, what I'd prefer is a macro where the user could use any symbol like $24 if they wanted.

 

The only way I can think to do this is with a reader macro for #\$ that turns into the appropriate AREF call (note: currently the captures are in list form for destructuring-bind, but could easily be a vector if I can get this to work).

 

However, I would only want this reader macro to within the context of the WITH-RE-MATCH macro. But, I can't see that as being possible since the reader has already done its work before calling the macro...

 

...or is there a trick I'm missing?

 

Or perhaps there's a better way for me to do what I want that someone can shed some light on?

 

Thanks for any insights!

 

Jeff M.

Re: Contextual reader macros

BTW, our lib have such feature as "custom-token-parser". Parsers (one
or many) are assigned to package. Any token which is about to be
considered as a symbol name from that package, is processed by all
custom token parsers. If it matches either parser, some value is
returned from the parser and inserted into source tree instead of
interning a symbol.

E.g. I use this for abbreviating structures access, see point 6 in my
Wiki, http://code.google.com/p/def-symbol-readmacro/wiki/InstallationInEnglish

_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html


Re: Contextual reader macros

I like this idea... I'll give it a go in a branch and see how it feels.

Thanks!

Jeff M.

Also, thanks to others for suggestions and reading material. ;-)


On Sun, Feb 17, 2013 at 12:25 PM, Eivind Midtgård <eivind@autosim.no> wrote:

Would it be possible to let the user specify the symbols to use:

 

(defmacro with-re-match ((v match &rest symbols) &body body)…

 

 

If so, you could even dispense with v and the dollars do

(defmacro with-re-match (((&rest symbols) match) &body body) …

 

Using like

 

(with-re-match ((a b) (find-re #/{([^}]+)}/ "this {is a} test"))

            (print a)

            (print b))

"{is a}"

"is a"

"is a"

T

 

 

Eivind

 

 

From: owner-lisp-hug@lispworks.com [mailto:owner-lisp-hug@lispworks.com] On Behalf Of Jeff Massung
Sent: 17. februar 2013 15:14
To: Lisp Hug Lispworks
Subject: Contextual reader macros

 

This is a CL question - not specific to LW - but, I find this group much better to interface with than c.l.l (although I know it is a subset of c.l.l), so I hope no one minds...

 

I'm wondering if it's possible to have a contextual reader macro?

 

Here's my specific use-case.

 

Over time, I've been adding more features to my LEXER package for LW (https://github.com/massung/lexer), and I have a code block I'd like to improve (if possible)... specifically the WITH-RE-MATCH macro found here: https://github.com/massung/lexer/blob/master/lexer.lisp#L182.

 

In particular, the macro assumes a finite number of sub-captures in the match. And while this is fine for most cases, what I'd prefer is a macro where the user could use any symbol like $24 if they wanted.

 

The only way I can think to do this is with a reader macro for #\$ that turns into the appropriate AREF call (note: currently the captures are in list form for destructuring-bind, but could easily be a vector if I can get this to work).

 

However, I would only want this reader macro to within the context of the WITH-RE-MATCH macro. But, I can't see that as being possible since the reader has already done its work before calling the macro...

 

...or is there a trick I'm missing?

 

Or perhaps there's a better way for me to do what I want that someone can shed some light on?

 

Thanks for any insights!

 

Jeff M.


Updated at: 2020-12-10 08:35 UTC