Skip to content
No description or website provided.
Clojure
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
src/reader_macros
test/reader_macros/test
.gitignore
README.org
TODO.org
project.clj

README.org

Motivation

Let’s see if we can’t inspire a few pitchforks with a reader-macro package for Clojure; thanks to Brian Carper, by the way, for doing fundamental work.

Also, may Zeus forgive us for opening this πίθος, &c.

Usage

We tried to stay close to the semantics of set-macro-character and set-dispatch-macro-character:

(set-macro-character character function)
Associates a reader-macro with character, where function has the signature (fn [stream character] ...).

Issuing e.g. (set-macro-character \[ f) will cause the reader to call (f stream \[) when it encounters [.

(set-dispatch-macro-character character function)
Associates a dispatch-macro with character, where function has the signature (fn [stream character] ...).

Dispatch-macros are distinguished from reader-macros in that they are activated by the dispatch prefix #; issuing e.g. (set-dispatch-macro-character \[ f), therefore, will cause the reader to call (f stream \[) when it encounters #[.

To define a reader macro, for instance, that reverses strings; try:

(use '[clojure.string :only (reverse)])

(set-macro-character \"
 (fn [reader quote]
   (reverse (macro-read-string reader quote))))

(println "hello, reader macros")
=> sorcam redaer ,olleh
  

We’ve also exposed a few utility functions from clojure.lang.LispReader to help extract things from readers:

  • read-arg
  • read-character
  • read-comment
  • read-deref
  • read-discard
  • read-dispatch
  • read-eval
  • read-fn
  • read-list
  • read-map
  • read-meta
  • read-quote
  • read-regex
  • read-set
  • read-string
  • read-syntax-quote
  • read-unmatched-delimiter
  • read-unquote
  • read-unreadable
  • read-vector
  • read-var

Installation

Add the following to your dependencies in project.clj:

[reader-macros "1.0.1"]

and use it followingly:

(use 'reader-macros.core)
Something went wrong with that request. Please try again.