[racket users] Detecting characters in symbols in macros

16 views
Skip to first unread message

Kevin Forchione

unread,
Dec 9, 2020, 12:29:59 PM12/9/20
to Racket-Users List
Hi guys,
Is there a way to detect a character in a symbol in a macro so that one branch of the syntax-parse would be chosen or discarded based on that?

Here’s roughly what I’m getting at….

#lang racket

(require (for-syntax syntax/parse))

(define-syntax (foo stx)
(syntax-parse stx
[(_ arg) #'(do-$ arg)]
[(_ arg) #'(do-other arg)]))

(foo $abc)
(foo abc)

I was thinking expo/c might come into play here, but maybe there’s another approach?

Thanks!

Kevin

Sam Tobin-Hochstadt

unread,
Dec 9, 2020, 12:33:27 PM12/9/20
to Kevin Forchione, Racket-Users List
Here's an example:

#lang racket

(require (for-syntax syntax/parse))

(define-syntax (foo stx)
(syntax-parse stx
[(_ arg:id)
#:when (regexp-match "[$]" (symbol->string (syntax-e #'arg)))
#'1]
[(_ arg) #'2]))

(foo $abc)
(foo abc)


That prints 1 followed by 2.

Sam
> --
> You received this message because you are subscribed to the Google Groups "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/FE7ED3FD-EAF0-4F03-8D3C-94D2092A9721%40gmail.com.
Reply all
Reply to author
Forward
0 new messages