1

I am wondering how one writes macros in Common Lisp that allow him to part with Lisp forms in calls to the former.

For instance, suppose I have the following macro:

(defmacro define-route ((app uri method) &body body) `(setf (ningle:route ,app ,uri :method ,method) ,@body)) 

A call to it would look something like this:

(define-route (*app* "/" :GET) (print "Welcome to ningle using GET!")) 

What if one wanted to write a macro that could be called like this:

@route(*app*, "/", :GET) 

or like this:

route: *app*, "/", :GET 

Is this possible? I have seen the @route syntax somewhere before but am not sure how to implement it, nor do I remember what it was called to look it up again.

1
  • 1
    Lisp macros can't do that. But if you want to develop a new programming language syntax, you can write a parser for it. If you want to do basic extensions to s-expressions, then google for 'reader macros'. Commented Apr 5, 2018 at 7:30

1 Answer 1

2

We encounter this decorator syntax (or annotations in CL) in the Caveman or Lucerne web frameworks:

@route GET "/" (defun index () (render #P"index.tmpl")) 

I doubt you can do route: *app*, "/", :GET.

cl-annot is a general annotation library for CL.

It is a reader macro, more examples here: http://lisp-lang.org/wiki/article/reader-macros

ps: Snooze, by the author of Sly (and yasnippet), is a web frameworks that treats routes as usual functions, thus route parameters as usual function arguments. It also has built-in reporting of errors (in browser, in the debugger, with a custom 404 page). I liked it better than the two mentioned. No big experience with either of them.

Sign up to request clarification or add additional context in comments.

2 Comments

@MadPhysicist did I already tell you about Snooze ? Just updated my answer. In my little experience it was easier and more consistant than caveman or ningle.
No, I have never heard of Snooze until now. Will check it out!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.