Skip to content

Instantly share code, notes, and snippets.

@mmontone
Created October 27, 2013 23:26
Show Gist options
  • Select an option

  • Save mmontone/7189175 to your computer and use it in GitHub Desktop.

Select an option

Save mmontone/7189175 to your computer and use it in GitHub Desktop.
Implementation of syntax for symbols to be able to use spaces instead of '-' in them. The default enclosing chars are '<' and '>'.
;; Implementation of syntax for symbols to be able to use spaces instead of '-' in them.
;; The default enclosing chars are '<' and '>'.
;; Example:
;; (defclass <My Class> (<Standard Object>)
;; ((<My Slot> :accessor <My Slot>
;; :initarg :my-slot)))
;;
;; (<with slots> (<My slot>)
;; (<make instance> '<My class> :my-slot "Value")
;; (print <My slot>))
(defpackage symbol-syntax
(:use cl))
(in-package symbol-syntax)
(defvar *original-readtable* nil)
(defvar *symbol-macro-open-char* #\<)
(defvar *symbol-macro-close-char* #\>)
(defmacro enable-symbol-reader-syntax ()
`(eval-when (:compile-toplevel :load-toplevel :execute)
(%enable-symbol-reader-syntax)))
(defun %enable-symbol-reader-syntax ()
(unless *original-readtable*
(setf *original-readtable* *readtable*
*readtable* (copy-readtable))
(set-macro-character *symbol-macro-open-char*
(lambda (str char)
(declare (ignore char)) ; Ignore me some warnings
(let ((keep-going t))
(set-macro-character *symbol-macro-close-char*
(lambda (stream char)
(declare (ignore char) (ignore stream))
(setf keep-going nil)))
(let ((symbol-parts
(loop for thing = (read str nil nil t)
while keep-going
collect thing)))
(intern (format nil "~{~a~^-~}" symbol-parts)))))))
(values))
(defmacro disable-symbol-reader-syntax ()
`(eval-when (:compile-toplevel :load-toplevel :execute)
(%disable-symbol-reader-syntax)))
(defun %disable-symbol-reader-syntax ()
(when *original-readtable*
(setf *readtable* *original-readtable*
*original-readtable* nil))
(values))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment