Skip to content

Commit

Permalink
add support to library tools.reader
Browse files Browse the repository at this point in the history
  • Loading branch information
jingtaozf committed Jul 27, 2019
1 parent 831b5ef commit 70829a2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
5 changes: 3 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
(defproject literate-clojure "0.2.1"
(defproject literate-clojure "0.2.2"
:description "a literate programming tool to write clojure in org mode"
:url "http://github.com/jingtaozf/literate-clojure"
:license {:name "EPL-2.0"
:url "https://www.eclipse.org/legal/epl-2.0/"}
:dependencies [[org.clojure/clojure "1.9.0"]]
:dependencies [[org.clojure/clojure "1.9.0"]
[org.clojure/tools.reader "1.3.2"]]
:repositories [["snapshots" {:url "https://repo.clojars.org" :creds :gpg}]
["releases" {:url "https://repo.clojars.org" :creds :gpg}]
["alternate" {:url "https://repo.clojars.org" :creds :gpg}]]
Expand Down
19 changes: 18 additions & 1 deletion src/literate_clojure/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
(ns literate-clojure.core
(:require
[clojure.pprint :refer [cl-format]]
[clojure.string :refer [starts-with? lower-case trim split]])
[clojure.string :refer [starts-with? lower-case trim split]]
[clojure.tools.reader])
(:import (clojure.lang LispReader LispReader$WrappingReader)))

(defonce ^:dynamic debug-p nil)
Expand Down Expand Up @@ -38,6 +39,15 @@
(debug (cl-format nil "install dispatch reader macro for character '~a'" ch))
(aset dm (int ch) fun))))

(defn tools.reader.additional-dispatch-macros (ch)
)
(defn dispatch-tools.reader-macro [pairs]
(alter-var-root
(var clojure.tools.reader/dispatch-macros)
(fn [f]
(doseq [[ch fun] pairs]
))

(defn- load? [arguments]
(debug (cl-format nil "header arguments is: ~s" arguments))
(loop [left-arguments arguments]
Expand Down Expand Up @@ -85,6 +95,13 @@
(dispatch-reader-macro \space dispatch-sharp-space))
(install-org-dispatcher)

(defn tools.reader.additional-dispatch-macros [orig-fn]
#(or (orig-fn %)
(case %
\+ dispatch-sharp-plus
\space dispatch-sharp-space)))
(alter-var-root (var clojure.tools.reader/dispatch-macros) #'tools.reader.additional-dispatch-macros)

(def exception-id-of-end-of-stream "end-of-litereate-stream")
(defn tangle-file [org-file]
(with-open [reader (clojure.lang.LineNumberingPushbackReader. (clojure.java.io/reader org-file))]
Expand Down
27 changes: 26 additions & 1 deletion src/literate_clojure/core.org
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- [[#handle-org-syntax][handle org syntax]]
- [[#handle-end-of-source-code-block][handle end of source code block]]
- [[#install-new-dispatcher-functions][install new dispatcher functions]]
- [[#install-new-dispatcher-functions-to-toolsreader][install new dispatcher functions to tools.reader]]
- [[#tangle-org-file-to-clojure-file][tangle org file to clojure file]]
- [[#references][References]]

Expand Down Expand Up @@ -76,7 +77,8 @@ Let's create a new namespace for this library.
(ns literate-clojure.core
(:require
[clojure.pprint :refer [cl-format]]
[clojure.string :refer [starts-with? lower-case trim split]])
[clojure.string :refer [starts-with? lower-case trim split]]
[clojure.tools.reader])
(:import (clojure.lang LispReader LispReader$WrappingReader)))
#+END_SRC
*** debug function
Expand Down Expand Up @@ -132,6 +134,17 @@ Based on clojure's [[https://github.com/clojure/clojure/blob/master/src/jvm/cloj
(aset dm (int ch) fun))))
#+END_SRC

#+BEGIN_SRC clojure
(defn tools.reader.additional-dispatch-macros (ch)
)
(defn dispatch-tools.reader-macro [pairs]
(alter-var-root
(var clojure.tools.reader/dispatch-macros)
(fn [f]
(doseq [[ch fun] pairs]
))
#+END_SRC

** handle org syntax

There are a lot of different lisp codes occur in one org file, some for function implementation,
Expand Down Expand Up @@ -204,6 +217,18 @@ Let's define a new dispatch function for "#+" (sharp plus) to return back org sy
(dispatch-reader-macro \space dispatch-sharp-space))
(install-org-dispatcher)
#+END_SRC
** install new dispatcher functions to tools.reader
Sadly [[https://github.com/clojure/tools.reader][tools.reader]] use a private function to return dispatch functions(see function [[https://github.com/clojure/tools.reader/blob/master/src/main/clojure/clojure/tools/reader.clj][dispatch-macros]]).
So we have to advice this function to add new dispatch reader macro.
#+BEGIN_SRC clojure
(defn tools.reader.additional-dispatch-macros [orig-fn]
#(or (orig-fn %)
(case %
\+ dispatch-sharp-plus
\space dispatch-sharp-space)))
(alter-var-root (var clojure.tools.reader/dispatch-macros) #'tools.reader.additional-dispatch-macros)
#+END_SRC


** tangle org file to clojure file
To build clojure file from an org file, we implement a function ~tangle-file~.
Expand Down
Binary file modified src/literate_clojure/core.pdf
Binary file not shown.

0 comments on commit 70829a2

Please sign in to comment.