Thank you!
I am not 100% sure I understood all about the different phases, but I
seem to have semi-understood and am able to use my understanding
combined with a little trial and error.
I've now got it working as follows:
#+BEGIN_SRC racket
#lang racket
(require (for-syntax racket/string))
(define-syntax define-api-route
(lambda (stx)
(define (identifier-name->string id)
(symbol->string (syntax->datum id)))
(syntax-case stx (GET HEAD POST PUT DELETE CONNECT OPTIONS TRACE PATH)
[(_ route GET my-content-type)
(string-contains? (identifier-name->string (syntax route)) "abc")
(syntax (quote aaa))]
;; an else branch basically
[(_ route GET my-content-type) #t
(syntax (quote bbb))])))
#+END_SRC
However, the actual code is supposed to define some procedures. This
would be quite long all in one
macro. So I want to call another procedure in the cases of syntax-case.
In this other macro, `identifier-name->string` will not be available, so
I want to give the other macro already the route as string, which will
be used to send requests to an API.
#+BEGIN_SRC racket
#lang racket
(require (for-syntax racket/string))
(define-syntax define-api-route
(lambda (stx)
(define (identifier-name->string id)
(symbol->string (syntax->datum id)))
(define (identifier->symbol id)
(syntax->datum id))
(define another-macro
(lambda (route http-method my-content-type route-as-string)
(syntax (quote bbb))))
(syntax-case stx (GET HEAD POST PUT DELETE CONNECT OPTIONS TRACE PATH)
[(_ route GET my-content-type)
(string-contains? (identifier-name->string (syntax route)) "abc")
(syntax (quote aaa))]
;; an else branch basically
[(_ route GET my-content-type) #t
(another-macro (syntax route)
'GET
(syntax my-content-type)
(identifier-name->string (syntax route)))])))
#+END_SRC
This works, but I was hoping to be able to not define everything inside
one macro, but instead split it up into multiple parts. Just imagine how
big that one macro could become. For example something like:
#+BEGIN_SRC racket
#lang racket
(require (for-syntax racket/string))
(define define-simple-api-route
(lambda (route http-method my-content-type route-as-string)
(syntax (quote bbb))))
(define-syntax define-api-route
(lambda (stx)
(define (identifier-name->string id)
(symbol->string (syntax->datum id)))
(define (identifier->symbol id)
(syntax->datum id))
(syntax-case stx (GET HEAD POST PUT DELETE CONNECT OPTIONS TRACE PATH)
[(_ route GET my-content-type)
(string-contains? (identifier-name->string (syntax route)) "abc")
(syntax (quote aaa))]
;; an else branch basically
[(_ route GET my-content-type) #t
(define-simple-api-route (syntax route)
'GET
(syntax my-content-type)
(identifier-name->string (syntax
route)))])))
#+END_SRC
But then the procedure `define-simple-api-route` will not be defined for
use in the syntax-case.
You already mentioned the `begin-for-syntax`. However, it is Racket
specific and not available in for example Guile Scheme or other Schemes.
I guess I will have to put things in separate modules then. Or is there
any other way?