Skip to content

Instantly share code, notes, and snippets.

@Chubek
Last active June 28, 2024 16:43
Show Gist options
  • Save Chubek/b8318a2e68d9a54f2291d98cdb6bfd08 to your computer and use it in GitHub Desktop.
Save Chubek/b8318a2e68d9a54f2291d98cdb6bfd08 to your computer and use it in GitHub Desktop.
Schemeroni: Useful Scheme Macaronis

I will use this Gist to post useful Scheme macros.

These macros will be compliant with R7Rs and down.

All macros are partially tested (warning!). Don't use them in serious work before thorough test.

; `char-is?` is a hygenic macro that, given a character class and a character, it will tell you if the character belongs to that class.
; This of course only works with ASCII characters.
; Several characters like Tilde are unaccounted for.
; You can use this macro as a basis for your own macro to use in lexers and parsers.
(define (belongs-to? item set)
(not (eq? (member item set) #f)))
(define-syntax char-is?
(syntax-rules (newln
whitespace
blank
upper-case
lower-case
letter
numeric
delimiter
terminator
solidus
separator
apostrophe
arithmetic
quantifier)
[(_ newln ch)
(char=? ch #\newline)]
[(_ whitespace ch)
(belongs-to? ch '(#\space #\tab))]
[(_ blank ch)
(belongs-to? ch '(#\space #\tab #\newline))]
[(_ upper-case ch)
(and (char>=? #\A)
(char<=? #\Z))]
[(_ lower-case ch)
(and (char>=? #\a)
(char<? #\z))]
[(_ letter ch)
(or (and (char>? #\A) (char<? #\Z))
(and (char>? #\a) (char<? #\z)))]
[(_ numeric ch)
(and (char>=? #\0)
(char<=? #\9))]
[(_ delimiter ch)
(belongs-to? ch '(#\( #\) #\[ #\] #\{ #\} #\< #\>))]
[(_ terminator ch)
(belongs-to? ch '(#\! #\? #\.))]
[(_ solidus ch)
(belongs-to? ch '(#\\ #\/))]
[(_ separator ch)
(belongs-to? ch '(#\: #\; #\,))]
[(_ apostrophe ch)
(belongs-to? ch '(#\' #\"))]
[(_ arithmetic ch)
(belongs-to? ch '(#\+ #\- #\^ #\* #\&))]
[(_ quantifier ch)
(belongs-to? ch '(#\@ #\# #\$ #\%))]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment